| 1 | n/a | /* statement.c - the statement type |
|---|
| 2 | n/a | * |
|---|
| 3 | n/a | * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> |
|---|
| 4 | n/a | * |
|---|
| 5 | n/a | * This file is part of pysqlite. |
|---|
| 6 | n/a | * |
|---|
| 7 | n/a | * This software is provided 'as-is', without any express or implied |
|---|
| 8 | n/a | * warranty. In no event will the authors be held liable for any damages |
|---|
| 9 | n/a | * arising from the use of this software. |
|---|
| 10 | n/a | * |
|---|
| 11 | n/a | * Permission is granted to anyone to use this software for any purpose, |
|---|
| 12 | n/a | * including commercial applications, and to alter it and redistribute it |
|---|
| 13 | n/a | * freely, subject to the following restrictions: |
|---|
| 14 | n/a | * |
|---|
| 15 | n/a | * 1. The origin of this software must not be misrepresented; you must not |
|---|
| 16 | n/a | * claim that you wrote the original software. If you use this software |
|---|
| 17 | n/a | * in a product, an acknowledgment in the product documentation would be |
|---|
| 18 | n/a | * appreciated but is not required. |
|---|
| 19 | n/a | * 2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 20 | n/a | * misrepresented as being the original software. |
|---|
| 21 | n/a | * 3. This notice may not be removed or altered from any source distribution. |
|---|
| 22 | n/a | */ |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | #include "statement.h" |
|---|
| 25 | n/a | #include "cursor.h" |
|---|
| 26 | n/a | #include "connection.h" |
|---|
| 27 | n/a | #include "microprotocols.h" |
|---|
| 28 | n/a | #include "prepare_protocol.h" |
|---|
| 29 | n/a | #include "util.h" |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | /* prototypes */ |
|---|
| 32 | n/a | static int pysqlite_check_remaining_sql(const char* tail); |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | typedef enum { |
|---|
| 35 | n/a | LINECOMMENT_1, |
|---|
| 36 | n/a | IN_LINECOMMENT, |
|---|
| 37 | n/a | COMMENTSTART_1, |
|---|
| 38 | n/a | IN_COMMENT, |
|---|
| 39 | n/a | COMMENTEND_1, |
|---|
| 40 | n/a | NORMAL |
|---|
| 41 | n/a | } parse_remaining_sql_state; |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | typedef enum { |
|---|
| 44 | n/a | TYPE_LONG, |
|---|
| 45 | n/a | TYPE_FLOAT, |
|---|
| 46 | n/a | TYPE_UNICODE, |
|---|
| 47 | n/a | TYPE_BUFFER, |
|---|
| 48 | n/a | TYPE_UNKNOWN |
|---|
| 49 | n/a | } parameter_type; |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql) |
|---|
| 52 | n/a | { |
|---|
| 53 | n/a | const char* tail; |
|---|
| 54 | n/a | int rc; |
|---|
| 55 | n/a | const char* sql_cstr; |
|---|
| 56 | n/a | Py_ssize_t sql_cstr_len; |
|---|
| 57 | n/a | const char* p; |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | self->st = NULL; |
|---|
| 60 | n/a | self->in_use = 0; |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len); |
|---|
| 63 | n/a | if (sql_cstr == NULL) { |
|---|
| 64 | n/a | rc = PYSQLITE_SQL_WRONG_TYPE; |
|---|
| 65 | n/a | return rc; |
|---|
| 66 | n/a | } |
|---|
| 67 | n/a | if (strlen(sql_cstr) != (size_t)sql_cstr_len) { |
|---|
| 68 | n/a | PyErr_SetString(PyExc_ValueError, "the query contains a null character"); |
|---|
| 69 | n/a | return PYSQLITE_SQL_WRONG_TYPE; |
|---|
| 70 | n/a | } |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | self->in_weakreflist = NULL; |
|---|
| 73 | n/a | Py_INCREF(sql); |
|---|
| 74 | n/a | self->sql = sql; |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | /* determine if the statement is a DDL statement */ |
|---|
| 77 | n/a | self->is_ddl = 0; |
|---|
| 78 | n/a | for (p = sql_cstr; *p != 0; p++) { |
|---|
| 79 | n/a | switch (*p) { |
|---|
| 80 | n/a | case ' ': |
|---|
| 81 | n/a | case '\r': |
|---|
| 82 | n/a | case '\n': |
|---|
| 83 | n/a | case '\t': |
|---|
| 84 | n/a | continue; |
|---|
| 85 | n/a | } |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | self->is_ddl = (PyOS_strnicmp(p, "create ", 7) == 0) |
|---|
| 88 | n/a | || (PyOS_strnicmp(p, "drop ", 5) == 0) |
|---|
| 89 | n/a | || (PyOS_strnicmp(p, "reindex ", 8) == 0); |
|---|
| 90 | n/a | break; |
|---|
| 91 | n/a | } |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 94 | n/a | rc = sqlite3_prepare(connection->db, |
|---|
| 95 | n/a | sql_cstr, |
|---|
| 96 | n/a | -1, |
|---|
| 97 | n/a | &self->st, |
|---|
| 98 | n/a | &tail); |
|---|
| 99 | n/a | Py_END_ALLOW_THREADS |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | self->db = connection->db; |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) { |
|---|
| 104 | n/a | (void)sqlite3_finalize(self->st); |
|---|
| 105 | n/a | self->st = NULL; |
|---|
| 106 | n/a | rc = PYSQLITE_TOO_MUCH_SQL; |
|---|
| 107 | n/a | } |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | return rc; |
|---|
| 110 | n/a | } |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter) |
|---|
| 113 | n/a | { |
|---|
| 114 | n/a | int rc = SQLITE_OK; |
|---|
| 115 | n/a | const char *string; |
|---|
| 116 | n/a | Py_ssize_t buflen; |
|---|
| 117 | n/a | parameter_type paramtype; |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | if (parameter == Py_None) { |
|---|
| 120 | n/a | rc = sqlite3_bind_null(self->st, pos); |
|---|
| 121 | n/a | goto final; |
|---|
| 122 | n/a | } |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | if (PyLong_CheckExact(parameter)) { |
|---|
| 125 | n/a | paramtype = TYPE_LONG; |
|---|
| 126 | n/a | } else if (PyFloat_CheckExact(parameter)) { |
|---|
| 127 | n/a | paramtype = TYPE_FLOAT; |
|---|
| 128 | n/a | } else if (PyUnicode_CheckExact(parameter)) { |
|---|
| 129 | n/a | paramtype = TYPE_UNICODE; |
|---|
| 130 | n/a | } else if (PyLong_Check(parameter)) { |
|---|
| 131 | n/a | paramtype = TYPE_LONG; |
|---|
| 132 | n/a | } else if (PyFloat_Check(parameter)) { |
|---|
| 133 | n/a | paramtype = TYPE_FLOAT; |
|---|
| 134 | n/a | } else if (PyUnicode_Check(parameter)) { |
|---|
| 135 | n/a | paramtype = TYPE_UNICODE; |
|---|
| 136 | n/a | } else if (PyObject_CheckBuffer(parameter)) { |
|---|
| 137 | n/a | paramtype = TYPE_BUFFER; |
|---|
| 138 | n/a | } else { |
|---|
| 139 | n/a | paramtype = TYPE_UNKNOWN; |
|---|
| 140 | n/a | } |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | switch (paramtype) { |
|---|
| 143 | n/a | case TYPE_LONG: { |
|---|
| 144 | n/a | sqlite_int64 value = _pysqlite_long_as_int64(parameter); |
|---|
| 145 | n/a | if (value == -1 && PyErr_Occurred()) |
|---|
| 146 | n/a | rc = -1; |
|---|
| 147 | n/a | else |
|---|
| 148 | n/a | rc = sqlite3_bind_int64(self->st, pos, value); |
|---|
| 149 | n/a | break; |
|---|
| 150 | n/a | } |
|---|
| 151 | n/a | case TYPE_FLOAT: |
|---|
| 152 | n/a | rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter)); |
|---|
| 153 | n/a | break; |
|---|
| 154 | n/a | case TYPE_UNICODE: |
|---|
| 155 | n/a | string = PyUnicode_AsUTF8AndSize(parameter, &buflen); |
|---|
| 156 | n/a | if (string == NULL) |
|---|
| 157 | n/a | return -1; |
|---|
| 158 | n/a | if (buflen > INT_MAX) { |
|---|
| 159 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 160 | n/a | "string longer than INT_MAX bytes"); |
|---|
| 161 | n/a | return -1; |
|---|
| 162 | n/a | } |
|---|
| 163 | n/a | rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT); |
|---|
| 164 | n/a | break; |
|---|
| 165 | n/a | case TYPE_BUFFER: { |
|---|
| 166 | n/a | Py_buffer view; |
|---|
| 167 | n/a | if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) { |
|---|
| 168 | n/a | PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); |
|---|
| 169 | n/a | return -1; |
|---|
| 170 | n/a | } |
|---|
| 171 | n/a | if (view.len > INT_MAX) { |
|---|
| 172 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 173 | n/a | "BLOB longer than INT_MAX bytes"); |
|---|
| 174 | n/a | PyBuffer_Release(&view); |
|---|
| 175 | n/a | return -1; |
|---|
| 176 | n/a | } |
|---|
| 177 | n/a | rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT); |
|---|
| 178 | n/a | PyBuffer_Release(&view); |
|---|
| 179 | n/a | break; |
|---|
| 180 | n/a | } |
|---|
| 181 | n/a | case TYPE_UNKNOWN: |
|---|
| 182 | n/a | rc = -1; |
|---|
| 183 | n/a | } |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | final: |
|---|
| 186 | n/a | return rc; |
|---|
| 187 | n/a | } |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | /* returns 0 if the object is one of Python's internal ones that don't need to be adapted */ |
|---|
| 190 | n/a | static int _need_adapt(PyObject* obj) |
|---|
| 191 | n/a | { |
|---|
| 192 | n/a | if (pysqlite_BaseTypeAdapted) { |
|---|
| 193 | n/a | return 1; |
|---|
| 194 | n/a | } |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj) |
|---|
| 197 | n/a | || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) { |
|---|
| 198 | n/a | return 0; |
|---|
| 199 | n/a | } else { |
|---|
| 200 | n/a | return 1; |
|---|
| 201 | n/a | } |
|---|
| 202 | n/a | } |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters) |
|---|
| 205 | n/a | { |
|---|
| 206 | n/a | PyObject* current_param; |
|---|
| 207 | n/a | PyObject* adapted; |
|---|
| 208 | n/a | const char* binding_name; |
|---|
| 209 | n/a | int i; |
|---|
| 210 | n/a | int rc; |
|---|
| 211 | n/a | int num_params_needed; |
|---|
| 212 | n/a | Py_ssize_t num_params; |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 215 | n/a | num_params_needed = sqlite3_bind_parameter_count(self->st); |
|---|
| 216 | n/a | Py_END_ALLOW_THREADS |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) { |
|---|
| 219 | n/a | /* parameters passed as sequence */ |
|---|
| 220 | n/a | if (PyTuple_CheckExact(parameters)) { |
|---|
| 221 | n/a | num_params = PyTuple_GET_SIZE(parameters); |
|---|
| 222 | n/a | } else if (PyList_CheckExact(parameters)) { |
|---|
| 223 | n/a | num_params = PyList_GET_SIZE(parameters); |
|---|
| 224 | n/a | } else { |
|---|
| 225 | n/a | num_params = PySequence_Size(parameters); |
|---|
| 226 | n/a | } |
|---|
| 227 | n/a | if (num_params != num_params_needed) { |
|---|
| 228 | n/a | PyErr_Format(pysqlite_ProgrammingError, |
|---|
| 229 | n/a | "Incorrect number of bindings supplied. The current " |
|---|
| 230 | n/a | "statement uses %d, and there are %zd supplied.", |
|---|
| 231 | n/a | num_params_needed, num_params); |
|---|
| 232 | n/a | return; |
|---|
| 233 | n/a | } |
|---|
| 234 | n/a | for (i = 0; i < num_params; i++) { |
|---|
| 235 | n/a | if (PyTuple_CheckExact(parameters)) { |
|---|
| 236 | n/a | current_param = PyTuple_GET_ITEM(parameters, i); |
|---|
| 237 | n/a | Py_XINCREF(current_param); |
|---|
| 238 | n/a | } else if (PyList_CheckExact(parameters)) { |
|---|
| 239 | n/a | current_param = PyList_GET_ITEM(parameters, i); |
|---|
| 240 | n/a | Py_XINCREF(current_param); |
|---|
| 241 | n/a | } else { |
|---|
| 242 | n/a | current_param = PySequence_GetItem(parameters, i); |
|---|
| 243 | n/a | } |
|---|
| 244 | n/a | if (!current_param) { |
|---|
| 245 | n/a | return; |
|---|
| 246 | n/a | } |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | if (!_need_adapt(current_param)) { |
|---|
| 249 | n/a | adapted = current_param; |
|---|
| 250 | n/a | } else { |
|---|
| 251 | n/a | adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL); |
|---|
| 252 | n/a | if (adapted) { |
|---|
| 253 | n/a | Py_DECREF(current_param); |
|---|
| 254 | n/a | } else { |
|---|
| 255 | n/a | PyErr_Clear(); |
|---|
| 256 | n/a | adapted = current_param; |
|---|
| 257 | n/a | } |
|---|
| 258 | n/a | } |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | rc = pysqlite_statement_bind_parameter(self, i + 1, adapted); |
|---|
| 261 | n/a | Py_DECREF(adapted); |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | if (rc != SQLITE_OK) { |
|---|
| 264 | n/a | if (!PyErr_Occurred()) { |
|---|
| 265 | n/a | PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i); |
|---|
| 266 | n/a | } |
|---|
| 267 | n/a | return; |
|---|
| 268 | n/a | } |
|---|
| 269 | n/a | } |
|---|
| 270 | n/a | } else if (PyDict_Check(parameters)) { |
|---|
| 271 | n/a | /* parameters passed as dictionary */ |
|---|
| 272 | n/a | for (i = 1; i <= num_params_needed; i++) { |
|---|
| 273 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 274 | n/a | binding_name = sqlite3_bind_parameter_name(self->st, i); |
|---|
| 275 | n/a | Py_END_ALLOW_THREADS |
|---|
| 276 | n/a | if (!binding_name) { |
|---|
| 277 | n/a | PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i); |
|---|
| 278 | n/a | return; |
|---|
| 279 | n/a | } |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | binding_name++; /* skip first char (the colon) */ |
|---|
| 282 | n/a | if (PyDict_CheckExact(parameters)) { |
|---|
| 283 | n/a | current_param = PyDict_GetItemString(parameters, binding_name); |
|---|
| 284 | n/a | Py_XINCREF(current_param); |
|---|
| 285 | n/a | } else { |
|---|
| 286 | n/a | current_param = PyMapping_GetItemString(parameters, binding_name); |
|---|
| 287 | n/a | } |
|---|
| 288 | n/a | if (!current_param) { |
|---|
| 289 | n/a | PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i); |
|---|
| 290 | n/a | return; |
|---|
| 291 | n/a | } |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | if (!_need_adapt(current_param)) { |
|---|
| 294 | n/a | adapted = current_param; |
|---|
| 295 | n/a | } else { |
|---|
| 296 | n/a | adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL); |
|---|
| 297 | n/a | if (adapted) { |
|---|
| 298 | n/a | Py_DECREF(current_param); |
|---|
| 299 | n/a | } else { |
|---|
| 300 | n/a | PyErr_Clear(); |
|---|
| 301 | n/a | adapted = current_param; |
|---|
| 302 | n/a | } |
|---|
| 303 | n/a | } |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | rc = pysqlite_statement_bind_parameter(self, i, adapted); |
|---|
| 306 | n/a | Py_DECREF(adapted); |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | if (rc != SQLITE_OK) { |
|---|
| 309 | n/a | if (!PyErr_Occurred()) { |
|---|
| 310 | n/a | PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name); |
|---|
| 311 | n/a | } |
|---|
| 312 | n/a | return; |
|---|
| 313 | n/a | } |
|---|
| 314 | n/a | } |
|---|
| 315 | n/a | } else { |
|---|
| 316 | n/a | PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type"); |
|---|
| 317 | n/a | } |
|---|
| 318 | n/a | } |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params) |
|---|
| 321 | n/a | { |
|---|
| 322 | n/a | const char* tail; |
|---|
| 323 | n/a | int rc; |
|---|
| 324 | n/a | const char* sql_cstr; |
|---|
| 325 | n/a | Py_ssize_t sql_len; |
|---|
| 326 | n/a | sqlite3_stmt* new_st; |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | sql_cstr = PyUnicode_AsUTF8AndSize(self->sql, &sql_len); |
|---|
| 329 | n/a | if (sql_cstr == NULL) { |
|---|
| 330 | n/a | rc = PYSQLITE_SQL_WRONG_TYPE; |
|---|
| 331 | n/a | return rc; |
|---|
| 332 | n/a | } |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 335 | n/a | rc = sqlite3_prepare(self->db, |
|---|
| 336 | n/a | sql_cstr, |
|---|
| 337 | n/a | -1, |
|---|
| 338 | n/a | &new_st, |
|---|
| 339 | n/a | &tail); |
|---|
| 340 | n/a | Py_END_ALLOW_THREADS |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | if (rc == SQLITE_OK) { |
|---|
| 343 | n/a | /* The efficient sqlite3_transfer_bindings is only available in SQLite |
|---|
| 344 | n/a | * version 3.2.2 or later. For older SQLite releases, that might not |
|---|
| 345 | n/a | * even define SQLITE_VERSION_NUMBER, we do it the manual way. |
|---|
| 346 | n/a | */ |
|---|
| 347 | n/a | #ifdef SQLITE_VERSION_NUMBER |
|---|
| 348 | n/a | #if SQLITE_VERSION_NUMBER >= 3002002 |
|---|
| 349 | n/a | /* The check for the number of parameters is necessary to not trigger a |
|---|
| 350 | n/a | * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */ |
|---|
| 351 | n/a | if (sqlite3_bind_parameter_count(self->st) > 0) { |
|---|
| 352 | n/a | (void)sqlite3_transfer_bindings(self->st, new_st); |
|---|
| 353 | n/a | } |
|---|
| 354 | n/a | #endif |
|---|
| 355 | n/a | #else |
|---|
| 356 | n/a | statement_bind_parameters(self, params); |
|---|
| 357 | n/a | #endif |
|---|
| 358 | n/a | |
|---|
| 359 | n/a | (void)sqlite3_finalize(self->st); |
|---|
| 360 | n/a | self->st = new_st; |
|---|
| 361 | n/a | } |
|---|
| 362 | n/a | |
|---|
| 363 | n/a | return rc; |
|---|
| 364 | n/a | } |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | int pysqlite_statement_finalize(pysqlite_Statement* self) |
|---|
| 367 | n/a | { |
|---|
| 368 | n/a | int rc; |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | rc = SQLITE_OK; |
|---|
| 371 | n/a | if (self->st) { |
|---|
| 372 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 373 | n/a | rc = sqlite3_finalize(self->st); |
|---|
| 374 | n/a | Py_END_ALLOW_THREADS |
|---|
| 375 | n/a | self->st = NULL; |
|---|
| 376 | n/a | } |
|---|
| 377 | n/a | |
|---|
| 378 | n/a | self->in_use = 0; |
|---|
| 379 | n/a | |
|---|
| 380 | n/a | return rc; |
|---|
| 381 | n/a | } |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | int pysqlite_statement_reset(pysqlite_Statement* self) |
|---|
| 384 | n/a | { |
|---|
| 385 | n/a | int rc; |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | rc = SQLITE_OK; |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | if (self->in_use && self->st) { |
|---|
| 390 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 391 | n/a | rc = sqlite3_reset(self->st); |
|---|
| 392 | n/a | Py_END_ALLOW_THREADS |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | if (rc == SQLITE_OK) { |
|---|
| 395 | n/a | self->in_use = 0; |
|---|
| 396 | n/a | } |
|---|
| 397 | n/a | } |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | return rc; |
|---|
| 400 | n/a | } |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | void pysqlite_statement_mark_dirty(pysqlite_Statement* self) |
|---|
| 403 | n/a | { |
|---|
| 404 | n/a | self->in_use = 1; |
|---|
| 405 | n/a | } |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | void pysqlite_statement_dealloc(pysqlite_Statement* self) |
|---|
| 408 | n/a | { |
|---|
| 409 | n/a | if (self->st) { |
|---|
| 410 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 411 | n/a | sqlite3_finalize(self->st); |
|---|
| 412 | n/a | Py_END_ALLOW_THREADS |
|---|
| 413 | n/a | } |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | self->st = NULL; |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | Py_XDECREF(self->sql); |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | if (self->in_weakreflist != NULL) { |
|---|
| 420 | n/a | PyObject_ClearWeakRefs((PyObject*)self); |
|---|
| 421 | n/a | } |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | Py_TYPE(self)->tp_free((PyObject*)self); |
|---|
| 424 | n/a | } |
|---|
| 425 | n/a | |
|---|
| 426 | n/a | /* |
|---|
| 427 | n/a | * Checks if there is anything left in an SQL string after SQLite compiled it. |
|---|
| 428 | n/a | * This is used to check if somebody tried to execute more than one SQL command |
|---|
| 429 | n/a | * with one execute()/executemany() command, which the DB-API and we don't |
|---|
| 430 | n/a | * allow. |
|---|
| 431 | n/a | * |
|---|
| 432 | n/a | * Returns 1 if there is more left than should be. 0 if ok. |
|---|
| 433 | n/a | */ |
|---|
| 434 | n/a | static int pysqlite_check_remaining_sql(const char* tail) |
|---|
| 435 | n/a | { |
|---|
| 436 | n/a | const char* pos = tail; |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | parse_remaining_sql_state state = NORMAL; |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | for (;;) { |
|---|
| 441 | n/a | switch (*pos) { |
|---|
| 442 | n/a | case 0: |
|---|
| 443 | n/a | return 0; |
|---|
| 444 | n/a | case '-': |
|---|
| 445 | n/a | if (state == NORMAL) { |
|---|
| 446 | n/a | state = LINECOMMENT_1; |
|---|
| 447 | n/a | } else if (state == LINECOMMENT_1) { |
|---|
| 448 | n/a | state = IN_LINECOMMENT; |
|---|
| 449 | n/a | } |
|---|
| 450 | n/a | break; |
|---|
| 451 | n/a | case ' ': |
|---|
| 452 | n/a | case '\t': |
|---|
| 453 | n/a | break; |
|---|
| 454 | n/a | case '\n': |
|---|
| 455 | n/a | case 13: |
|---|
| 456 | n/a | if (state == IN_LINECOMMENT) { |
|---|
| 457 | n/a | state = NORMAL; |
|---|
| 458 | n/a | } |
|---|
| 459 | n/a | break; |
|---|
| 460 | n/a | case '/': |
|---|
| 461 | n/a | if (state == NORMAL) { |
|---|
| 462 | n/a | state = COMMENTSTART_1; |
|---|
| 463 | n/a | } else if (state == COMMENTEND_1) { |
|---|
| 464 | n/a | state = NORMAL; |
|---|
| 465 | n/a | } else if (state == COMMENTSTART_1) { |
|---|
| 466 | n/a | return 1; |
|---|
| 467 | n/a | } |
|---|
| 468 | n/a | break; |
|---|
| 469 | n/a | case '*': |
|---|
| 470 | n/a | if (state == NORMAL) { |
|---|
| 471 | n/a | return 1; |
|---|
| 472 | n/a | } else if (state == LINECOMMENT_1) { |
|---|
| 473 | n/a | return 1; |
|---|
| 474 | n/a | } else if (state == COMMENTSTART_1) { |
|---|
| 475 | n/a | state = IN_COMMENT; |
|---|
| 476 | n/a | } else if (state == IN_COMMENT) { |
|---|
| 477 | n/a | state = COMMENTEND_1; |
|---|
| 478 | n/a | } |
|---|
| 479 | n/a | break; |
|---|
| 480 | n/a | default: |
|---|
| 481 | n/a | if (state == COMMENTEND_1) { |
|---|
| 482 | n/a | state = IN_COMMENT; |
|---|
| 483 | n/a | } else if (state == IN_LINECOMMENT) { |
|---|
| 484 | n/a | } else if (state == IN_COMMENT) { |
|---|
| 485 | n/a | } else { |
|---|
| 486 | n/a | return 1; |
|---|
| 487 | n/a | } |
|---|
| 488 | n/a | } |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | pos++; |
|---|
| 491 | n/a | } |
|---|
| 492 | n/a | |
|---|
| 493 | n/a | return 0; |
|---|
| 494 | n/a | } |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | PyTypeObject pysqlite_StatementType = { |
|---|
| 497 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 498 | n/a | MODULE_NAME ".Statement", /* tp_name */ |
|---|
| 499 | n/a | sizeof(pysqlite_Statement), /* tp_basicsize */ |
|---|
| 500 | n/a | 0, /* tp_itemsize */ |
|---|
| 501 | n/a | (destructor)pysqlite_statement_dealloc, /* tp_dealloc */ |
|---|
| 502 | n/a | 0, /* tp_print */ |
|---|
| 503 | n/a | 0, /* tp_getattr */ |
|---|
| 504 | n/a | 0, /* tp_setattr */ |
|---|
| 505 | n/a | 0, /* tp_reserved */ |
|---|
| 506 | n/a | 0, /* tp_repr */ |
|---|
| 507 | n/a | 0, /* tp_as_number */ |
|---|
| 508 | n/a | 0, /* tp_as_sequence */ |
|---|
| 509 | n/a | 0, /* tp_as_mapping */ |
|---|
| 510 | n/a | 0, /* tp_hash */ |
|---|
| 511 | n/a | 0, /* tp_call */ |
|---|
| 512 | n/a | 0, /* tp_str */ |
|---|
| 513 | n/a | 0, /* tp_getattro */ |
|---|
| 514 | n/a | 0, /* tp_setattro */ |
|---|
| 515 | n/a | 0, /* tp_as_buffer */ |
|---|
| 516 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|---|
| 517 | n/a | 0, /* tp_doc */ |
|---|
| 518 | n/a | 0, /* tp_traverse */ |
|---|
| 519 | n/a | 0, /* tp_clear */ |
|---|
| 520 | n/a | 0, /* tp_richcompare */ |
|---|
| 521 | n/a | offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */ |
|---|
| 522 | n/a | 0, /* tp_iter */ |
|---|
| 523 | n/a | 0, /* tp_iternext */ |
|---|
| 524 | n/a | 0, /* tp_methods */ |
|---|
| 525 | n/a | 0, /* tp_members */ |
|---|
| 526 | n/a | 0, /* tp_getset */ |
|---|
| 527 | n/a | 0, /* tp_base */ |
|---|
| 528 | n/a | 0, /* tp_dict */ |
|---|
| 529 | n/a | 0, /* tp_descr_get */ |
|---|
| 530 | n/a | 0, /* tp_descr_set */ |
|---|
| 531 | n/a | 0, /* tp_dictoffset */ |
|---|
| 532 | n/a | (initproc)0, /* tp_init */ |
|---|
| 533 | n/a | 0, /* tp_alloc */ |
|---|
| 534 | n/a | 0, /* tp_new */ |
|---|
| 535 | n/a | 0 /* tp_free */ |
|---|
| 536 | n/a | }; |
|---|
| 537 | n/a | |
|---|
| 538 | n/a | extern int pysqlite_statement_setup_types(void) |
|---|
| 539 | n/a | { |
|---|
| 540 | n/a | pysqlite_StatementType.tp_new = PyType_GenericNew; |
|---|
| 541 | n/a | return PyType_Ready(&pysqlite_StatementType); |
|---|
| 542 | n/a | } |
|---|