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