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