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