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