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