blob: 98cc68a853a531e9c079b38bd6f9317d2838d21e [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* statement.c - the statement type
2 *
3 * Copyright (C) 2005-2006 Gerhard Häring <gh@ghaering.de>
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"
25#include "cursor.h"
26#include "connection.h"
27#include "microprotocols.h"
28#include "prepare_protocol.h"
29#include "sqlitecompat.h"
30
31/* prototypes */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000032static int pysqlite_check_remaining_sql(const char* tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000043int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044{
45 const char* tail;
46 int rc;
Guido van Rossum83857e32007-05-09 23:37:01 +000047 const char* sql_cstr;
48 Py_ssize_t sql_cstr_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000049
50 self->st = NULL;
51 self->in_use = 0;
52
Guido van Rossum7d1df6c2007-08-29 13:53:23 +000053 sql_cstr = PyUnicode_AsStringAndSize(sql, &sql_cstr_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +000054 if (sql_cstr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055 rc = PYSQLITE_SQL_WRONG_TYPE;
56 return rc;
57 }
58
Thomas Wouters477c8d52006-05-27 19:21:47 +000059 self->in_weakreflist = NULL;
Guido van Rossum83857e32007-05-09 23:37:01 +000060 Py_INCREF(sql);
61 self->sql = sql;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062
63 rc = sqlite3_prepare(connection->db,
64 sql_cstr,
65 -1,
66 &self->st,
67 &tail);
68
69 self->db = connection->db;
70
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000071 if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072 (void)sqlite3_finalize(self->st);
73 self->st = NULL;
74 rc = PYSQLITE_TOO_MUCH_SQL;
75 }
76
77 return rc;
78}
79
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000080int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081{
82 int rc = SQLITE_OK;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000083#ifdef HAVE_LONG_LONG
84 PY_LONG_LONG longlongval;
Georg Brandle1a0d112007-10-23 19:24:22 +000085#else
86 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087#endif
88 const char* buffer;
89 char* string;
90 Py_ssize_t buflen;
91 PyObject* stringval;
92
93 if (parameter == Py_None) {
94 rc = sqlite3_bind_null(self->st, pos);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095#ifdef HAVE_LONG_LONG
96 } else if (PyLong_Check(parameter)) {
97 longlongval = PyLong_AsLongLong(parameter);
98 /* in the overflow error case, longlongval is -1, and an exception is set */
99 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
Georg Brandle1a0d112007-10-23 19:24:22 +0000100#else
101 } else if (PyLong_Check(parameter)) {
102 longval = PyLong_AsLong(parameter);
103 /* in the overflow error case, longval is -1, and an exception is set */
104 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105#endif
106 } else if (PyFloat_Check(parameter)) {
107 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 } else if PyUnicode_Check(parameter) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000109 string = PyUnicode_AsString(parameter);
Gerhard Häring6d214562007-08-10 18:15:11 +0000110
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000112 } else if (PyObject_CheckBuffer(parameter)) {
113 if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
114 rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
115 } else {
116 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
117 rc = -1;
118 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 } else {
120 rc = -1;
121 }
122
123 return rc;
124}
125
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000126void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127{
128 PyObject* current_param;
129 PyObject* adapted;
130 const char* binding_name;
131 int i;
132 int rc;
133 int num_params_needed;
134 int num_params;
135
136 Py_BEGIN_ALLOW_THREADS
137 num_params_needed = sqlite3_bind_parameter_count(self->st);
138 Py_END_ALLOW_THREADS
139
140 if (PyDict_Check(parameters)) {
141 /* parameters passed as dictionary */
142 for (i = 1; i <= num_params_needed; i++) {
143 Py_BEGIN_ALLOW_THREADS
144 binding_name = sqlite3_bind_parameter_name(self->st, i);
145 Py_END_ALLOW_THREADS
146 if (!binding_name) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000147 PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 return;
149 }
150
151 binding_name++; /* skip first char (the colon) */
152 current_param = PyDict_GetItemString(parameters, binding_name);
153 if (!current_param) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000154 PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155 return;
156 }
157
158 Py_INCREF(current_param);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000159 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 if (adapted) {
161 Py_DECREF(current_param);
162 } else {
163 PyErr_Clear();
164 adapted = current_param;
165 }
166
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000167 rc = pysqlite_statement_bind_parameter(self, i, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000168 Py_DECREF(adapted);
169
170 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000171 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172 return;
173 }
174 }
175 } else {
176 /* parameters passed as sequence */
177 num_params = PySequence_Length(parameters);
178 if (num_params != num_params_needed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000179 PyErr_Format(pysqlite_ProgrammingError, "Incorrect number of bindings supplied. The current statement uses %d, and there are %d supplied.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180 num_params_needed, num_params);
181 return;
182 }
183 for (i = 0; i < num_params; i++) {
184 current_param = PySequence_GetItem(parameters, i);
185 if (!current_param) {
186 return;
187 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000188 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189
190 if (adapted) {
191 Py_DECREF(current_param);
192 } else {
193 PyErr_Clear();
194 adapted = current_param;
195 }
196
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000197 rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 Py_DECREF(adapted);
199
200 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000201 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202 return;
203 }
204 }
205 }
206}
207
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000208int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209{
210 const char* tail;
211 int rc;
Guido van Rossum83857e32007-05-09 23:37:01 +0000212 const char* sql_cstr;
213 Py_ssize_t sql_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000214 sqlite3_stmt* new_st;
215
Guido van Rossum7d1df6c2007-08-29 13:53:23 +0000216 sql_cstr = PyUnicode_AsStringAndSize(self->sql, &sql_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000217 if (sql_cstr == NULL) {
Guido van Rossum83857e32007-05-09 23:37:01 +0000218 rc = PYSQLITE_SQL_WRONG_TYPE;
219 return rc;
220 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221
222 rc = sqlite3_prepare(self->db,
223 sql_cstr,
224 -1,
225 &new_st,
226 &tail);
227
228 if (rc == SQLITE_OK) {
229 /* The efficient sqlite3_transfer_bindings is only available in SQLite
230 * version 3.2.2 or later. For older SQLite releases, that might not
231 * even define SQLITE_VERSION_NUMBER, we do it the manual way.
232 */
233 #ifdef SQLITE_VERSION_NUMBER
234 #if SQLITE_VERSION_NUMBER >= 3002002
235 (void)sqlite3_transfer_bindings(self->st, new_st);
236 #endif
237 #else
238 statement_bind_parameters(self, params);
239 #endif
240
241 (void)sqlite3_finalize(self->st);
242 self->st = new_st;
243 }
244
245 return rc;
246}
247
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000248int pysqlite_statement_finalize(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000249{
250 int rc;
251
252 rc = SQLITE_OK;
253 if (self->st) {
254 Py_BEGIN_ALLOW_THREADS
255 rc = sqlite3_finalize(self->st);
256 Py_END_ALLOW_THREADS
257 self->st = NULL;
258 }
259
260 self->in_use = 0;
261
262 return rc;
263}
264
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000265int pysqlite_statement_reset(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266{
267 int rc;
268
269 rc = SQLITE_OK;
270
271 if (self->in_use && self->st) {
272 Py_BEGIN_ALLOW_THREADS
273 rc = sqlite3_reset(self->st);
274 Py_END_ALLOW_THREADS
275
276 if (rc == SQLITE_OK) {
277 self->in_use = 0;
278 }
279 }
280
281 return rc;
282}
283
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000284void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285{
286 self->in_use = 1;
287}
288
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000289void pysqlite_statement_dealloc(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000290{
291 int rc;
292
293 if (self->st) {
294 Py_BEGIN_ALLOW_THREADS
295 rc = sqlite3_finalize(self->st);
296 Py_END_ALLOW_THREADS
297 }
298
299 self->st = NULL;
300
301 Py_XDECREF(self->sql);
302
Thomas Wouters477c8d52006-05-27 19:21:47 +0000303 if (self->in_weakreflist != NULL) {
304 PyObject_ClearWeakRefs((PyObject*)self);
305 }
306
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000307 Py_Type(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308}
309
310/*
311 * Checks if there is anything left in an SQL string after SQLite compiled it.
312 * This is used to check if somebody tried to execute more than one SQL command
313 * with one execute()/executemany() command, which the DB-API and we don't
314 * allow.
315 *
316 * Returns 1 if there is more left than should be. 0 if ok.
317 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000318static int pysqlite_check_remaining_sql(const char* tail)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319{
320 const char* pos = tail;
321
322 parse_remaining_sql_state state = NORMAL;
323
324 for (;;) {
325 switch (*pos) {
326 case 0:
327 return 0;
328 case '-':
329 if (state == NORMAL) {
330 state = LINECOMMENT_1;
331 } else if (state == LINECOMMENT_1) {
332 state = IN_LINECOMMENT;
333 }
334 break;
335 case ' ':
336 case '\t':
337 break;
338 case '\n':
339 case 13:
340 if (state == IN_LINECOMMENT) {
341 state = NORMAL;
342 }
343 break;
344 case '/':
345 if (state == NORMAL) {
346 state = COMMENTSTART_1;
347 } else if (state == COMMENTEND_1) {
348 state = NORMAL;
349 } else if (state == COMMENTSTART_1) {
350 return 1;
351 }
352 break;
353 case '*':
354 if (state == NORMAL) {
355 return 1;
356 } else if (state == LINECOMMENT_1) {
357 return 1;
358 } else if (state == COMMENTSTART_1) {
359 state = IN_COMMENT;
360 } else if (state == IN_COMMENT) {
361 state = COMMENTEND_1;
362 }
363 break;
364 default:
365 if (state == COMMENTEND_1) {
366 state = IN_COMMENT;
367 } else if (state == IN_LINECOMMENT) {
368 } else if (state == IN_COMMENT) {
369 } else {
370 return 1;
371 }
372 }
373
374 pos++;
375 }
376
377 return 0;
378}
379
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000380PyTypeObject pysqlite_StatementType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000381 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 MODULE_NAME ".Statement", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000383 sizeof(pysqlite_Statement), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000385 (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 0, /* tp_print */
387 0, /* tp_getattr */
388 0, /* tp_setattr */
389 0, /* tp_compare */
390 0, /* tp_repr */
391 0, /* tp_as_number */
392 0, /* tp_as_sequence */
393 0, /* tp_as_mapping */
394 0, /* tp_hash */
395 0, /* tp_call */
396 0, /* tp_str */
397 0, /* tp_getattro */
398 0, /* tp_setattro */
399 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000400 Py_TPFLAGS_DEFAULT, /* tp_flags */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 0, /* tp_doc */
402 0, /* tp_traverse */
403 0, /* tp_clear */
404 0, /* tp_richcompare */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000405 offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 0, /* tp_iter */
407 0, /* tp_iternext */
408 0, /* tp_methods */
409 0, /* tp_members */
410 0, /* tp_getset */
411 0, /* tp_base */
412 0, /* tp_dict */
413 0, /* tp_descr_get */
414 0, /* tp_descr_set */
415 0, /* tp_dictoffset */
416 (initproc)0, /* tp_init */
417 0, /* tp_alloc */
418 0, /* tp_new */
419 0 /* tp_free */
420};
421
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000422extern int pysqlite_statement_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000424 pysqlite_StatementType.tp_new = PyType_GenericNew;
425 return PyType_Ready(&pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426}