blob: d280a676834f4c00a005a9709aab23469e825112 [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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091
92 if (parameter == Py_None) {
93 rc = sqlite3_bind_null(self->st, pos);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094#ifdef HAVE_LONG_LONG
95 } else if (PyLong_Check(parameter)) {
96 longlongval = PyLong_AsLongLong(parameter);
97 /* in the overflow error case, longlongval is -1, and an exception is set */
98 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
Georg Brandle1a0d112007-10-23 19:24:22 +000099#else
100 } else if (PyLong_Check(parameter)) {
101 longval = PyLong_AsLong(parameter);
102 /* in the overflow error case, longval is -1, and an exception is set */
103 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104#endif
105 } else if (PyFloat_Check(parameter)) {
106 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107 } else if PyUnicode_Check(parameter) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000108 string = PyUnicode_AsString(parameter);
Gerhard Häring6d214562007-08-10 18:15:11 +0000109
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000111 } else if (PyObject_CheckBuffer(parameter)) {
112 if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
113 rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
114 } else {
115 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
116 rc = -1;
117 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 } else {
119 rc = -1;
120 }
121
122 return rc;
123}
124
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000125void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126{
127 PyObject* current_param;
128 PyObject* adapted;
129 const char* binding_name;
130 int i;
131 int rc;
132 int num_params_needed;
133 int num_params;
134
135 Py_BEGIN_ALLOW_THREADS
136 num_params_needed = sqlite3_bind_parameter_count(self->st);
137 Py_END_ALLOW_THREADS
138
139 if (PyDict_Check(parameters)) {
140 /* parameters passed as dictionary */
141 for (i = 1; i <= num_params_needed; i++) {
142 Py_BEGIN_ALLOW_THREADS
143 binding_name = sqlite3_bind_parameter_name(self->st, i);
144 Py_END_ALLOW_THREADS
145 if (!binding_name) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000146 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 +0000147 return;
148 }
149
150 binding_name++; /* skip first char (the colon) */
151 current_param = PyDict_GetItemString(parameters, binding_name);
152 if (!current_param) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000153 PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154 return;
155 }
156
157 Py_INCREF(current_param);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000158 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 if (adapted) {
160 Py_DECREF(current_param);
161 } else {
162 PyErr_Clear();
163 adapted = current_param;
164 }
165
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000166 rc = pysqlite_statement_bind_parameter(self, i, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167 Py_DECREF(adapted);
168
169 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000170 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 return;
172 }
173 }
174 } else {
175 /* parameters passed as sequence */
176 num_params = PySequence_Length(parameters);
177 if (num_params != num_params_needed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000178 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 +0000179 num_params_needed, num_params);
180 return;
181 }
182 for (i = 0; i < num_params; i++) {
183 current_param = PySequence_GetItem(parameters, i);
184 if (!current_param) {
185 return;
186 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000187 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188
189 if (adapted) {
190 Py_DECREF(current_param);
191 } else {
192 PyErr_Clear();
193 adapted = current_param;
194 }
195
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000196 rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197 Py_DECREF(adapted);
198
199 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000200 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201 return;
202 }
203 }
204 }
205}
206
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000207int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208{
209 const char* tail;
210 int rc;
Guido van Rossum83857e32007-05-09 23:37:01 +0000211 const char* sql_cstr;
212 Py_ssize_t sql_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213 sqlite3_stmt* new_st;
214
Guido van Rossum7d1df6c2007-08-29 13:53:23 +0000215 sql_cstr = PyUnicode_AsStringAndSize(self->sql, &sql_len);
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000216 if (sql_cstr == NULL) {
Guido van Rossum83857e32007-05-09 23:37:01 +0000217 rc = PYSQLITE_SQL_WRONG_TYPE;
218 return rc;
219 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220
221 rc = sqlite3_prepare(self->db,
222 sql_cstr,
223 -1,
224 &new_st,
225 &tail);
226
227 if (rc == SQLITE_OK) {
228 /* The efficient sqlite3_transfer_bindings is only available in SQLite
229 * version 3.2.2 or later. For older SQLite releases, that might not
230 * even define SQLITE_VERSION_NUMBER, we do it the manual way.
231 */
232 #ifdef SQLITE_VERSION_NUMBER
233 #if SQLITE_VERSION_NUMBER >= 3002002
Christian Heimes38053212007-12-14 01:24:44 +0000234 /* The check for the number of parameters is necessary to not trigger a
235 * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */
236 if (sqlite3_bind_parameter_count(self->st) > 0) {
237 (void)sqlite3_transfer_bindings(self->st, new_st);
238 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000239 #endif
240 #else
241 statement_bind_parameters(self, params);
242 #endif
243
244 (void)sqlite3_finalize(self->st);
245 self->st = new_st;
246 }
247
248 return rc;
249}
250
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000251int pysqlite_statement_finalize(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252{
253 int rc;
254
255 rc = SQLITE_OK;
256 if (self->st) {
257 Py_BEGIN_ALLOW_THREADS
258 rc = sqlite3_finalize(self->st);
259 Py_END_ALLOW_THREADS
260 self->st = NULL;
261 }
262
263 self->in_use = 0;
264
265 return rc;
266}
267
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000268int pysqlite_statement_reset(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269{
270 int rc;
271
272 rc = SQLITE_OK;
273
274 if (self->in_use && self->st) {
275 Py_BEGIN_ALLOW_THREADS
276 rc = sqlite3_reset(self->st);
277 Py_END_ALLOW_THREADS
278
279 if (rc == SQLITE_OK) {
280 self->in_use = 0;
281 }
282 }
283
284 return rc;
285}
286
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000287void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000288{
289 self->in_use = 1;
290}
291
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000292void pysqlite_statement_dealloc(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293{
294 int rc;
295
296 if (self->st) {
297 Py_BEGIN_ALLOW_THREADS
298 rc = sqlite3_finalize(self->st);
299 Py_END_ALLOW_THREADS
300 }
301
302 self->st = NULL;
303
304 Py_XDECREF(self->sql);
305
Thomas Wouters477c8d52006-05-27 19:21:47 +0000306 if (self->in_weakreflist != NULL) {
307 PyObject_ClearWeakRefs((PyObject*)self);
308 }
309
Christian Heimes90aa7642007-12-19 02:45:37 +0000310 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311}
312
313/*
314 * Checks if there is anything left in an SQL string after SQLite compiled it.
315 * This is used to check if somebody tried to execute more than one SQL command
316 * with one execute()/executemany() command, which the DB-API and we don't
317 * allow.
318 *
319 * Returns 1 if there is more left than should be. 0 if ok.
320 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000321static int pysqlite_check_remaining_sql(const char* tail)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000322{
323 const char* pos = tail;
324
325 parse_remaining_sql_state state = NORMAL;
326
327 for (;;) {
328 switch (*pos) {
329 case 0:
330 return 0;
331 case '-':
332 if (state == NORMAL) {
333 state = LINECOMMENT_1;
334 } else if (state == LINECOMMENT_1) {
335 state = IN_LINECOMMENT;
336 }
337 break;
338 case ' ':
339 case '\t':
340 break;
341 case '\n':
342 case 13:
343 if (state == IN_LINECOMMENT) {
344 state = NORMAL;
345 }
346 break;
347 case '/':
348 if (state == NORMAL) {
349 state = COMMENTSTART_1;
350 } else if (state == COMMENTEND_1) {
351 state = NORMAL;
352 } else if (state == COMMENTSTART_1) {
353 return 1;
354 }
355 break;
356 case '*':
357 if (state == NORMAL) {
358 return 1;
359 } else if (state == LINECOMMENT_1) {
360 return 1;
361 } else if (state == COMMENTSTART_1) {
362 state = IN_COMMENT;
363 } else if (state == IN_COMMENT) {
364 state = COMMENTEND_1;
365 }
366 break;
367 default:
368 if (state == COMMENTEND_1) {
369 state = IN_COMMENT;
370 } else if (state == IN_LINECOMMENT) {
371 } else if (state == IN_COMMENT) {
372 } else {
373 return 1;
374 }
375 }
376
377 pos++;
378 }
379
380 return 0;
381}
382
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000383PyTypeObject pysqlite_StatementType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000384 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 MODULE_NAME ".Statement", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000386 sizeof(pysqlite_Statement), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000387 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000388 (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000389 0, /* tp_print */
390 0, /* tp_getattr */
391 0, /* tp_setattr */
392 0, /* tp_compare */
393 0, /* tp_repr */
394 0, /* tp_as_number */
395 0, /* tp_as_sequence */
396 0, /* tp_as_mapping */
397 0, /* tp_hash */
398 0, /* tp_call */
399 0, /* tp_str */
400 0, /* tp_getattro */
401 0, /* tp_setattro */
402 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000403 Py_TPFLAGS_DEFAULT, /* tp_flags */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 0, /* tp_doc */
405 0, /* tp_traverse */
406 0, /* tp_clear */
407 0, /* tp_richcompare */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000408 offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 0, /* tp_iter */
410 0, /* tp_iternext */
411 0, /* tp_methods */
412 0, /* tp_members */
413 0, /* tp_getset */
414 0, /* tp_base */
415 0, /* tp_dict */
416 0, /* tp_descr_get */
417 0, /* tp_descr_set */
418 0, /* tp_dictoffset */
419 (initproc)0, /* tp_init */
420 0, /* tp_alloc */
421 0, /* tp_new */
422 0 /* tp_free */
423};
424
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000425extern int pysqlite_statement_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000427 pysqlite_StatementType.tp_new = PyType_GenericNew;
428 return PyType_Ready(&pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000429}