blob: 5987b2a68eabd5412544ee723888ce3efb332b14 [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 Rossum83857e32007-05-09 23:37:01 +000053 if (PyObject_AsCharBuffer(sql, &sql_cstr, &sql_cstr_len) < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054 rc = PYSQLITE_SQL_WRONG_TYPE;
55 return rc;
56 }
57
Thomas Wouters477c8d52006-05-27 19:21:47 +000058 self->in_weakreflist = NULL;
Guido van Rossum83857e32007-05-09 23:37:01 +000059 Py_INCREF(sql);
60 self->sql = sql;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061
62 rc = sqlite3_prepare(connection->db,
63 sql_cstr,
64 -1,
65 &self->st,
66 &tail);
67
68 self->db = connection->db;
69
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000070 if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071 (void)sqlite3_finalize(self->st);
72 self->st = NULL;
73 rc = PYSQLITE_TOO_MUCH_SQL;
74 }
75
76 return rc;
77}
78
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000079int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080{
81 int rc = SQLITE_OK;
82 long longval;
83#ifdef HAVE_LONG_LONG
84 PY_LONG_LONG longlongval;
85#endif
86 const char* buffer;
87 char* string;
88 Py_ssize_t buflen;
89 PyObject* stringval;
90
91 if (parameter == Py_None) {
92 rc = sqlite3_bind_null(self->st, pos);
Guido van Rossum523d4f92007-01-15 00:31:49 +000093 } else if (PyInt_CheckExact(parameter)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 longval = PyInt_AsLong(parameter);
95 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
96#ifdef HAVE_LONG_LONG
97 } else if (PyLong_Check(parameter)) {
98 longlongval = PyLong_AsLongLong(parameter);
99 /* in the overflow error case, longlongval is -1, and an exception is set */
100 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
101#endif
102 } else if (PyFloat_Check(parameter)) {
103 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
104 } else if (PyBuffer_Check(parameter)) {
105 if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
106 rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
107 } else {
108 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
109 rc = -1;
110 }
111 } else if PyString_Check(parameter) {
112 string = PyString_AsString(parameter);
113 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
114 } else if PyUnicode_Check(parameter) {
115 stringval = PyUnicode_AsUTF8String(parameter);
116 string = PyString_AsString(stringval);
117 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
118 Py_DECREF(stringval);
119 } 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 Rossum83857e32007-05-09 23:37:01 +0000216 if (PyObject_AsCharBuffer(self->sql, &sql_cstr, &sql_len) < 0) {
217 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
234 (void)sqlite3_transfer_bindings(self->st, new_st);
235 #endif
236 #else
237 statement_bind_parameters(self, params);
238 #endif
239
240 (void)sqlite3_finalize(self->st);
241 self->st = new_st;
242 }
243
244 return rc;
245}
246
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000247int pysqlite_statement_finalize(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248{
249 int rc;
250
251 rc = SQLITE_OK;
252 if (self->st) {
253 Py_BEGIN_ALLOW_THREADS
254 rc = sqlite3_finalize(self->st);
255 Py_END_ALLOW_THREADS
256 self->st = NULL;
257 }
258
259 self->in_use = 0;
260
261 return rc;
262}
263
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000264int pysqlite_statement_reset(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000265{
266 int rc;
267
268 rc = SQLITE_OK;
269
270 if (self->in_use && self->st) {
271 Py_BEGIN_ALLOW_THREADS
272 rc = sqlite3_reset(self->st);
273 Py_END_ALLOW_THREADS
274
275 if (rc == SQLITE_OK) {
276 self->in_use = 0;
277 }
278 }
279
280 return rc;
281}
282
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000283void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000284{
285 self->in_use = 1;
286}
287
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000288void pysqlite_statement_dealloc(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289{
290 int rc;
291
292 if (self->st) {
293 Py_BEGIN_ALLOW_THREADS
294 rc = sqlite3_finalize(self->st);
295 Py_END_ALLOW_THREADS
296 }
297
298 self->st = NULL;
299
300 Py_XDECREF(self->sql);
301
Thomas Wouters477c8d52006-05-27 19:21:47 +0000302 if (self->in_weakreflist != NULL) {
303 PyObject_ClearWeakRefs((PyObject*)self);
304 }
305
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000306 Py_Type(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307}
308
309/*
310 * Checks if there is anything left in an SQL string after SQLite compiled it.
311 * This is used to check if somebody tried to execute more than one SQL command
312 * with one execute()/executemany() command, which the DB-API and we don't
313 * allow.
314 *
315 * Returns 1 if there is more left than should be. 0 if ok.
316 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317static int pysqlite_check_remaining_sql(const char* tail)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318{
319 const char* pos = tail;
320
321 parse_remaining_sql_state state = NORMAL;
322
323 for (;;) {
324 switch (*pos) {
325 case 0:
326 return 0;
327 case '-':
328 if (state == NORMAL) {
329 state = LINECOMMENT_1;
330 } else if (state == LINECOMMENT_1) {
331 state = IN_LINECOMMENT;
332 }
333 break;
334 case ' ':
335 case '\t':
336 break;
337 case '\n':
338 case 13:
339 if (state == IN_LINECOMMENT) {
340 state = NORMAL;
341 }
342 break;
343 case '/':
344 if (state == NORMAL) {
345 state = COMMENTSTART_1;
346 } else if (state == COMMENTEND_1) {
347 state = NORMAL;
348 } else if (state == COMMENTSTART_1) {
349 return 1;
350 }
351 break;
352 case '*':
353 if (state == NORMAL) {
354 return 1;
355 } else if (state == LINECOMMENT_1) {
356 return 1;
357 } else if (state == COMMENTSTART_1) {
358 state = IN_COMMENT;
359 } else if (state == IN_COMMENT) {
360 state = COMMENTEND_1;
361 }
362 break;
363 default:
364 if (state == COMMENTEND_1) {
365 state = IN_COMMENT;
366 } else if (state == IN_LINECOMMENT) {
367 } else if (state == IN_COMMENT) {
368 } else {
369 return 1;
370 }
371 }
372
373 pos++;
374 }
375
376 return 0;
377}
378
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000379PyTypeObject pysqlite_StatementType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000380 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381 MODULE_NAME ".Statement", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000382 sizeof(pysqlite_Statement), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000383 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000384 (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385 0, /* tp_print */
386 0, /* tp_getattr */
387 0, /* tp_setattr */
388 0, /* tp_compare */
389 0, /* tp_repr */
390 0, /* tp_as_number */
391 0, /* tp_as_sequence */
392 0, /* tp_as_mapping */
393 0, /* tp_hash */
394 0, /* tp_call */
395 0, /* tp_str */
396 0, /* tp_getattro */
397 0, /* tp_setattro */
398 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000399 Py_TPFLAGS_DEFAULT, /* tp_flags */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000400 0, /* tp_doc */
401 0, /* tp_traverse */
402 0, /* tp_clear */
403 0, /* tp_richcompare */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000404 offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405 0, /* tp_iter */
406 0, /* tp_iternext */
407 0, /* tp_methods */
408 0, /* tp_members */
409 0, /* tp_getset */
410 0, /* tp_base */
411 0, /* tp_dict */
412 0, /* tp_descr_get */
413 0, /* tp_descr_set */
414 0, /* tp_dictoffset */
415 (initproc)0, /* tp_init */
416 0, /* tp_alloc */
417 0, /* tp_new */
418 0 /* tp_free */
419};
420
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000421extern int pysqlite_statement_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000423 pysqlite_StatementType.tp_new = PyType_GenericNew;
424 return PyType_Ready(&pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425}