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