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; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | |
| 58 | self->st = NULL; |
| 59 | self->in_use = 0; |
| 60 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 61 | sql_cstr = _PyUnicode_AsStringAndSize(sql, &sql_cstr_len); |
Guido van Rossum | fa9a121 | 2007-08-29 03:34:29 +0000 | [diff] [blame] | 62 | if (sql_cstr == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 63 | rc = PYSQLITE_SQL_WRONG_TYPE; |
| 64 | return rc; |
| 65 | } |
Serhiy Storchaka | 42d67af | 2014-09-11 13:29:05 +0300 | [diff] [blame] | 66 | if (strlen(sql_cstr) != (size_t)sql_cstr_len) { |
| 67 | PyErr_SetString(PyExc_ValueError, "the query contains a null character"); |
| 68 | return PYSQLITE_SQL_WRONG_TYPE; |
| 69 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 70 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 71 | self->in_weakreflist = NULL; |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 72 | Py_INCREF(sql); |
| 73 | self->sql = sql; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 74 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 75 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 76 | rc = sqlite3_prepare(connection->db, |
| 77 | sql_cstr, |
| 78 | -1, |
| 79 | &self->st, |
| 80 | &tail); |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 81 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 82 | |
| 83 | self->db = connection->db; |
| 84 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 85 | if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | (void)sqlite3_finalize(self->st); |
| 87 | self->st = NULL; |
| 88 | rc = PYSQLITE_TOO_MUCH_SQL; |
| 89 | } |
| 90 | |
| 91 | return rc; |
| 92 | } |
| 93 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 94 | int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 95 | { |
| 96 | int rc = SQLITE_OK; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 97 | char* string; |
| 98 | Py_ssize_t buflen; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 99 | parameter_type paramtype; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 100 | |
| 101 | if (parameter == Py_None) { |
| 102 | rc = sqlite3_bind_null(self->st, pos); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 103 | goto final; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 106 | if (PyLong_CheckExact(parameter)) { |
| 107 | paramtype = TYPE_LONG; |
| 108 | } else if (PyFloat_CheckExact(parameter)) { |
| 109 | paramtype = TYPE_FLOAT; |
| 110 | } else if (PyUnicode_CheckExact(parameter)) { |
| 111 | paramtype = TYPE_UNICODE; |
| 112 | } else if (PyLong_Check(parameter)) { |
| 113 | paramtype = TYPE_LONG; |
| 114 | } else if (PyFloat_Check(parameter)) { |
| 115 | paramtype = TYPE_FLOAT; |
| 116 | } else if (PyUnicode_Check(parameter)) { |
Gerhard Häring | 6117f42 | 2008-09-22 06:04:51 +0000 | [diff] [blame] | 117 | paramtype = TYPE_UNICODE; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 118 | } else if (PyObject_CheckBuffer(parameter)) { |
| 119 | paramtype = TYPE_BUFFER; |
| 120 | } else { |
| 121 | paramtype = TYPE_UNKNOWN; |
| 122 | } |
| 123 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 124 | switch (paramtype) { |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 125 | case TYPE_LONG: { |
| 126 | sqlite_int64 value = _pysqlite_long_as_int64(parameter); |
| 127 | if (value == -1 && PyErr_Occurred()) |
| 128 | rc = -1; |
| 129 | else |
| 130 | rc = sqlite3_bind_int64(self->st, pos, value); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 131 | break; |
Serhiy Storchaka | 3cf96ac | 2013-02-07 17:01:47 +0200 | [diff] [blame] | 132 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 133 | case TYPE_FLOAT: |
| 134 | rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter)); |
| 135 | break; |
| 136 | case TYPE_UNICODE: |
Petri Lehtinen | 023fe33 | 2012-02-01 22:18:19 +0200 | [diff] [blame] | 137 | string = _PyUnicode_AsStringAndSize(parameter, &buflen); |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 138 | if (string == NULL) |
| 139 | return -1; |
| 140 | if (buflen > INT_MAX) { |
| 141 | PyErr_SetString(PyExc_OverflowError, |
| 142 | "string longer than INT_MAX bytes"); |
| 143 | return -1; |
| 144 | } |
| 145 | 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] | 146 | break; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 147 | case TYPE_BUFFER: { |
| 148 | Py_buffer view; |
| 149 | if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 150 | PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 151 | return -1; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 152 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 153 | if (view.len > INT_MAX) { |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 154 | PyErr_SetString(PyExc_OverflowError, |
| 155 | "BLOB longer than INT_MAX bytes"); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 156 | PyBuffer_Release(&view); |
Victor Stinner | 3f658be | 2013-11-18 01:36:29 +0100 | [diff] [blame] | 157 | return -1; |
| 158 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 159 | rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT); |
| 160 | PyBuffer_Release(&view); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 161 | break; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 162 | } |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 163 | case TYPE_UNKNOWN: |
| 164 | rc = -1; |
| 165 | } |
| 166 | |
| 167 | final: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 168 | return rc; |
| 169 | } |
| 170 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 171 | /* returns 0 if the object is one of Python's internal ones that don't need to be adapted */ |
| 172 | static int _need_adapt(PyObject* obj) |
| 173 | { |
| 174 | if (pysqlite_BaseTypeAdapted) { |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj) |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 179 | || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } else { |
| 182 | return 1; |
| 183 | } |
| 184 | } |
| 185 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 186 | void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 187 | { |
| 188 | PyObject* current_param; |
| 189 | PyObject* adapted; |
| 190 | const char* binding_name; |
| 191 | int i; |
| 192 | int rc; |
| 193 | int num_params_needed; |
Victor Stinner | afccb0a | 2013-11-18 02:07:29 +0100 | [diff] [blame] | 194 | Py_ssize_t num_params; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 195 | |
| 196 | Py_BEGIN_ALLOW_THREADS |
| 197 | num_params_needed = sqlite3_bind_parameter_count(self->st); |
| 198 | Py_END_ALLOW_THREADS |
| 199 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 200 | if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) { |
| 201 | /* parameters passed as sequence */ |
| 202 | if (PyTuple_CheckExact(parameters)) { |
| 203 | num_params = PyTuple_GET_SIZE(parameters); |
| 204 | } else if (PyList_CheckExact(parameters)) { |
| 205 | num_params = PyList_GET_SIZE(parameters); |
| 206 | } else { |
| 207 | num_params = PySequence_Size(parameters); |
| 208 | } |
| 209 | if (num_params != num_params_needed) { |
Victor Stinner | afccb0a | 2013-11-18 02:07:29 +0100 | [diff] [blame] | 210 | PyErr_Format(pysqlite_ProgrammingError, |
| 211 | "Incorrect number of bindings supplied. The current " |
| 212 | "statement uses %d, and there are %zd supplied.", |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 213 | num_params_needed, num_params); |
| 214 | return; |
| 215 | } |
| 216 | for (i = 0; i < num_params; i++) { |
| 217 | if (PyTuple_CheckExact(parameters)) { |
| 218 | current_param = PyTuple_GET_ITEM(parameters, i); |
| 219 | Py_XINCREF(current_param); |
| 220 | } else if (PyList_CheckExact(parameters)) { |
| 221 | current_param = PyList_GET_ITEM(parameters, i); |
| 222 | Py_XINCREF(current_param); |
| 223 | } else { |
| 224 | current_param = PySequence_GetItem(parameters, i); |
| 225 | } |
| 226 | if (!current_param) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | if (!_need_adapt(current_param)) { |
| 231 | adapted = current_param; |
| 232 | } else { |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 233 | adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 234 | if (adapted) { |
| 235 | Py_DECREF(current_param); |
| 236 | } else { |
| 237 | PyErr_Clear(); |
| 238 | adapted = current_param; |
| 239 | } |
| 240 | } |
| 241 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 242 | rc = pysqlite_statement_bind_parameter(self, i + 1, adapted); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 243 | Py_DECREF(adapted); |
| 244 | |
| 245 | if (rc != SQLITE_OK) { |
| 246 | if (!PyErr_Occurred()) { |
| 247 | PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i); |
| 248 | } |
| 249 | return; |
| 250 | } |
| 251 | } |
| 252 | } else if (PyDict_Check(parameters)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 253 | /* parameters passed as dictionary */ |
| 254 | for (i = 1; i <= num_params_needed; i++) { |
| 255 | Py_BEGIN_ALLOW_THREADS |
| 256 | binding_name = sqlite3_bind_parameter_name(self->st, i); |
| 257 | Py_END_ALLOW_THREADS |
| 258 | if (!binding_name) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 259 | 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] | 260 | return; |
| 261 | } |
| 262 | |
| 263 | binding_name++; /* skip first char (the colon) */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 264 | if (PyDict_CheckExact(parameters)) { |
| 265 | current_param = PyDict_GetItemString(parameters, binding_name); |
| 266 | Py_XINCREF(current_param); |
| 267 | } else { |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 268 | current_param = PyMapping_GetItemString(parameters, binding_name); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 269 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 270 | if (!current_param) { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 271 | PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 275 | if (!_need_adapt(current_param)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 276 | adapted = current_param; |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 277 | } else { |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 278 | adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL); |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 279 | if (adapted) { |
| 280 | Py_DECREF(current_param); |
| 281 | } else { |
| 282 | PyErr_Clear(); |
| 283 | adapted = current_param; |
| 284 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 287 | rc = pysqlite_statement_bind_parameter(self, i, adapted); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 288 | Py_DECREF(adapted); |
| 289 | |
| 290 | if (rc != SQLITE_OK) { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 291 | if (!PyErr_Occurred()) { |
| 292 | PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name); |
| 293 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | } |
| 297 | } else { |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 298 | PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 302 | int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 303 | { |
| 304 | const char* tail; |
| 305 | int rc; |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 306 | const char* sql_cstr; |
| 307 | Py_ssize_t sql_len; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 308 | sqlite3_stmt* new_st; |
| 309 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 310 | sql_cstr = _PyUnicode_AsStringAndSize(self->sql, &sql_len); |
Guido van Rossum | fa9a121 | 2007-08-29 03:34:29 +0000 | [diff] [blame] | 311 | if (sql_cstr == NULL) { |
Guido van Rossum | 83857e3 | 2007-05-09 23:37:01 +0000 | [diff] [blame] | 312 | rc = PYSQLITE_SQL_WRONG_TYPE; |
| 313 | return rc; |
| 314 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 315 | |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 316 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 317 | rc = sqlite3_prepare(self->db, |
| 318 | sql_cstr, |
| 319 | -1, |
| 320 | &new_st, |
| 321 | &tail); |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 322 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 323 | |
| 324 | if (rc == SQLITE_OK) { |
| 325 | /* The efficient sqlite3_transfer_bindings is only available in SQLite |
| 326 | * version 3.2.2 or later. For older SQLite releases, that might not |
| 327 | * even define SQLITE_VERSION_NUMBER, we do it the manual way. |
| 328 | */ |
| 329 | #ifdef SQLITE_VERSION_NUMBER |
| 330 | #if SQLITE_VERSION_NUMBER >= 3002002 |
Christian Heimes | 3805321 | 2007-12-14 01:24:44 +0000 | [diff] [blame] | 331 | /* The check for the number of parameters is necessary to not trigger a |
| 332 | * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */ |
| 333 | if (sqlite3_bind_parameter_count(self->st) > 0) { |
| 334 | (void)sqlite3_transfer_bindings(self->st, new_st); |
| 335 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 336 | #endif |
| 337 | #else |
| 338 | statement_bind_parameters(self, params); |
| 339 | #endif |
| 340 | |
| 341 | (void)sqlite3_finalize(self->st); |
| 342 | self->st = new_st; |
| 343 | } |
| 344 | |
| 345 | return rc; |
| 346 | } |
| 347 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 348 | int pysqlite_statement_finalize(pysqlite_Statement* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 349 | { |
| 350 | int rc; |
| 351 | |
| 352 | rc = SQLITE_OK; |
| 353 | if (self->st) { |
| 354 | Py_BEGIN_ALLOW_THREADS |
| 355 | rc = sqlite3_finalize(self->st); |
| 356 | Py_END_ALLOW_THREADS |
| 357 | self->st = NULL; |
| 358 | } |
| 359 | |
| 360 | self->in_use = 0; |
| 361 | |
| 362 | return rc; |
| 363 | } |
| 364 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 365 | int pysqlite_statement_reset(pysqlite_Statement* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 366 | { |
| 367 | int rc; |
| 368 | |
| 369 | rc = SQLITE_OK; |
| 370 | |
| 371 | if (self->in_use && self->st) { |
| 372 | Py_BEGIN_ALLOW_THREADS |
| 373 | rc = sqlite3_reset(self->st); |
| 374 | Py_END_ALLOW_THREADS |
| 375 | |
| 376 | if (rc == SQLITE_OK) { |
| 377 | self->in_use = 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return rc; |
| 382 | } |
| 383 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 384 | void pysqlite_statement_mark_dirty(pysqlite_Statement* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | { |
| 386 | self->in_use = 1; |
| 387 | } |
| 388 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 389 | void pysqlite_statement_dealloc(pysqlite_Statement* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 390 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 391 | if (self->st) { |
| 392 | Py_BEGIN_ALLOW_THREADS |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 393 | sqlite3_finalize(self->st); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 394 | Py_END_ALLOW_THREADS |
| 395 | } |
| 396 | |
| 397 | self->st = NULL; |
| 398 | |
| 399 | Py_XDECREF(self->sql); |
| 400 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 401 | if (self->in_weakreflist != NULL) { |
| 402 | PyObject_ClearWeakRefs((PyObject*)self); |
| 403 | } |
| 404 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 405 | Py_TYPE(self)->tp_free((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Checks if there is anything left in an SQL string after SQLite compiled it. |
| 410 | * This is used to check if somebody tried to execute more than one SQL command |
| 411 | * with one execute()/executemany() command, which the DB-API and we don't |
| 412 | * allow. |
| 413 | * |
| 414 | * Returns 1 if there is more left than should be. 0 if ok. |
| 415 | */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 416 | static int pysqlite_check_remaining_sql(const char* tail) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 417 | { |
| 418 | const char* pos = tail; |
| 419 | |
| 420 | parse_remaining_sql_state state = NORMAL; |
| 421 | |
| 422 | for (;;) { |
| 423 | switch (*pos) { |
| 424 | case 0: |
| 425 | return 0; |
| 426 | case '-': |
| 427 | if (state == NORMAL) { |
| 428 | state = LINECOMMENT_1; |
| 429 | } else if (state == LINECOMMENT_1) { |
| 430 | state = IN_LINECOMMENT; |
| 431 | } |
| 432 | break; |
| 433 | case ' ': |
| 434 | case '\t': |
| 435 | break; |
| 436 | case '\n': |
| 437 | case 13: |
| 438 | if (state == IN_LINECOMMENT) { |
| 439 | state = NORMAL; |
| 440 | } |
| 441 | break; |
| 442 | case '/': |
| 443 | if (state == NORMAL) { |
| 444 | state = COMMENTSTART_1; |
| 445 | } else if (state == COMMENTEND_1) { |
| 446 | state = NORMAL; |
| 447 | } else if (state == COMMENTSTART_1) { |
| 448 | return 1; |
| 449 | } |
| 450 | break; |
| 451 | case '*': |
| 452 | if (state == NORMAL) { |
| 453 | return 1; |
| 454 | } else if (state == LINECOMMENT_1) { |
| 455 | return 1; |
| 456 | } else if (state == COMMENTSTART_1) { |
| 457 | state = IN_COMMENT; |
| 458 | } else if (state == IN_COMMENT) { |
| 459 | state = COMMENTEND_1; |
| 460 | } |
| 461 | break; |
| 462 | default: |
| 463 | if (state == COMMENTEND_1) { |
| 464 | state = IN_COMMENT; |
| 465 | } else if (state == IN_LINECOMMENT) { |
| 466 | } else if (state == IN_COMMENT) { |
| 467 | } else { |
| 468 | return 1; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | pos++; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 478 | PyTypeObject pysqlite_StatementType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 479 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 480 | MODULE_NAME ".Statement", /* tp_name */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 481 | sizeof(pysqlite_Statement), /* tp_basicsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 482 | 0, /* tp_itemsize */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 483 | (destructor)pysqlite_statement_dealloc, /* tp_dealloc */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 484 | 0, /* tp_print */ |
| 485 | 0, /* tp_getattr */ |
| 486 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 487 | 0, /* tp_reserved */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 488 | 0, /* tp_repr */ |
| 489 | 0, /* tp_as_number */ |
| 490 | 0, /* tp_as_sequence */ |
| 491 | 0, /* tp_as_mapping */ |
| 492 | 0, /* tp_hash */ |
| 493 | 0, /* tp_call */ |
| 494 | 0, /* tp_str */ |
| 495 | 0, /* tp_getattro */ |
| 496 | 0, /* tp_setattro */ |
| 497 | 0, /* tp_as_buffer */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 498 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 499 | 0, /* tp_doc */ |
| 500 | 0, /* tp_traverse */ |
| 501 | 0, /* tp_clear */ |
| 502 | 0, /* tp_richcompare */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 503 | offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 504 | 0, /* tp_iter */ |
| 505 | 0, /* tp_iternext */ |
| 506 | 0, /* tp_methods */ |
| 507 | 0, /* tp_members */ |
| 508 | 0, /* tp_getset */ |
| 509 | 0, /* tp_base */ |
| 510 | 0, /* tp_dict */ |
| 511 | 0, /* tp_descr_get */ |
| 512 | 0, /* tp_descr_set */ |
| 513 | 0, /* tp_dictoffset */ |
| 514 | (initproc)0, /* tp_init */ |
| 515 | 0, /* tp_alloc */ |
| 516 | 0, /* tp_new */ |
| 517 | 0 /* tp_free */ |
| 518 | }; |
| 519 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 520 | extern int pysqlite_statement_setup_types(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 521 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 522 | pysqlite_StatementType.tp_new = PyType_GenericNew; |
| 523 | return PyType_Ready(&pysqlite_StatementType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 524 | } |