blob: 556ea01f41a008bdf28bd5a4e047ab6533d969e3 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001/* statement.c - the statement type
2 *
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00003 * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
Anthony Baxterc51ee692006-04-01 00:57:31 +00004 *
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 Baxter07f5b352006-04-01 08:36:27 +000025#include "cursor.h"
Anthony Baxterc51ee692006-04-01 00:57:31 +000026#include "connection.h"
Anthony Baxter07f5b352006-04-01 08:36:27 +000027#include "microprotocols.h"
28#include "prepare_protocol.h"
Anthony Baxter72289a62006-04-04 06:29:05 +000029#include "sqlitecompat.h"
Anthony Baxterc51ee692006-04-01 00:57:31 +000030
31/* prototypes */
Gerhard Häring0741a602007-01-14 01:43:50 +000032static int pysqlite_check_remaining_sql(const char* tail);
Anthony Baxterc51ee692006-04-01 00:57:31 +000033
34typedef 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äring1cc60ed2008-02-29 22:08:41 +000043typedef 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äring0741a602007-01-14 01:43:50 +000053int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql)
Anthony Baxterc51ee692006-04-01 00:57:31 +000054{
55 const char* tail;
56 int rc;
57 PyObject* sql_str;
58 char* sql_cstr;
59
60 self->st = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000061 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äring3e99c0a2006-04-23 15:24:26 +000077 self->in_weakreflist = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000078 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äring0741a602007-01-14 01:43:50 +000090 if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +000091 (void)sqlite3_finalize(self->st);
Neal Norwitz195e4e62006-04-16 03:37:19 +000092 self->st = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000093 rc = PYSQLITE_TOO_MUCH_SQL;
94 }
95
96 return rc;
97}
98
Gerhard Häring0741a602007-01-14 01:43:50 +000099int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
Anthony Baxter07f5b352006-04-01 08:36:27 +0000100{
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 Norwitz95f0e4c2006-04-01 09:08:06 +0000108 Py_ssize_t buflen;
Anthony Baxter07f5b352006-04-01 08:36:27 +0000109 PyObject* stringval;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000110 parameter_type paramtype;
Anthony Baxter07f5b352006-04-01 08:36:27 +0000111
112 if (parameter == Py_None) {
113 rc = sqlite3_bind_null(self->st, pos);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000114 goto final;
Anthony Baxter07f5b352006-04-01 08:36:27 +0000115 }
116
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000117 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
180final:
Anthony Baxter07f5b352006-04-01 08:36:27 +0000181 return rc;
182}
183
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000184/* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
185static 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äring0741a602007-01-14 01:43:50 +0000200void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
Anthony Baxter07f5b352006-04-01 08:36:27 +0000201{
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äring1cc60ed2008-02-29 22:08:41 +0000214 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 Baxter07f5b352006-04-01 08:36:27 +0000263 /* 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äring0741a602007-01-14 01:43:50 +0000269 PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000270 return;
271 }
272
273 binding_name++; /* skip first char (the colon) */
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000274 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 Baxter07f5b352006-04-01 08:36:27 +0000280 if (!current_param) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000281 PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000282 return;
283 }
284
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000285 if (!_need_adapt(current_param)) {
Anthony Baxter07f5b352006-04-01 08:36:27 +0000286 adapted = current_param;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000287 } 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 Baxter07f5b352006-04-01 08:36:27 +0000295 }
296
Gerhard Häring0741a602007-01-14 01:43:50 +0000297 rc = pysqlite_statement_bind_parameter(self, i, adapted);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000298 Py_DECREF(adapted);
299
300 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000301 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000302 return;
303 }
304 }
305 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000306 PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type");
Anthony Baxter07f5b352006-04-01 08:36:27 +0000307 }
308}
309
Gerhard Häring0741a602007-01-14 01:43:50 +0000310int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000311{
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 Baxter07f5b352006-04-01 08:36:27 +0000326 /* 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äring99b9df82007-12-11 21:07:40 +0000332 /* 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 Baxter07f5b352006-04-01 08:36:27 +0000337 #endif
338 #else
339 statement_bind_parameters(self, params);
340 #endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000341
342 (void)sqlite3_finalize(self->st);
343 self->st = new_st;
344 }
345
346 return rc;
347}
348
Gerhard Häring0741a602007-01-14 01:43:50 +0000349int pysqlite_statement_finalize(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000350{
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äring0741a602007-01-14 01:43:50 +0000366int pysqlite_statement_reset(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000367{
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äring0741a602007-01-14 01:43:50 +0000385void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000386{
387 self->in_use = 1;
388}
389
Gerhard Häring0741a602007-01-14 01:43:50 +0000390void pysqlite_statement_dealloc(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000391{
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äring3e99c0a2006-04-23 15:24:26 +0000404 if (self->in_weakreflist != NULL) {
405 PyObject_ClearWeakRefs((PyObject*)self);
406 }
407
Christian Heimese93237d2007-12-19 02:37:44 +0000408 Py_TYPE(self)->tp_free((PyObject*)self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000409}
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äring0741a602007-01-14 01:43:50 +0000419static int pysqlite_check_remaining_sql(const char* tail)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000420{
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äring0741a602007-01-14 01:43:50 +0000481PyTypeObject pysqlite_StatementType = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000482 PyVarObject_HEAD_INIT(NULL, 0)
Anthony Baxter8e7b4902006-04-05 18:25:33 +0000483 MODULE_NAME ".Statement", /* tp_name */
Gerhard Häring0741a602007-01-14 01:43:50 +0000484 sizeof(pysqlite_Statement), /* tp_basicsize */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000485 0, /* tp_itemsize */
Gerhard Häring0741a602007-01-14 01:43:50 +0000486 (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000487 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äring3e99c0a2006-04-23 15:24:26 +0000501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000502 0, /* tp_doc */
503 0, /* tp_traverse */
504 0, /* tp_clear */
505 0, /* tp_richcompare */
Gerhard Häring0741a602007-01-14 01:43:50 +0000506 offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000507 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äring0741a602007-01-14 01:43:50 +0000523extern int pysqlite_statement_setup_types(void)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000524{
Gerhard Häring0741a602007-01-14 01:43:50 +0000525 pysqlite_StatementType.tp_new = PyType_GenericNew;
526 return PyType_Ready(&pysqlite_StatementType);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000527}