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