blob: a5801d69282aafa0e29e1840a7bd5f50c1f22a13 [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 Rossumfa9a1212007-08-29 03:34:29 +000053 sql_cstr = PyUnicode_AsString(sql);
54 if (sql_cstr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055 rc = PYSQLITE_SQL_WRONG_TYPE;
56 return rc;
57 }
Guido van Rossumfa9a1212007-08-29 03:34:29 +000058 sql_cstr_len = strlen(sql_cstr); /* XXX */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060 self->in_weakreflist = NULL;
Guido van Rossum83857e32007-05-09 23:37:01 +000061 Py_INCREF(sql);
62 self->sql = sql;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063
64 rc = sqlite3_prepare(connection->db,
65 sql_cstr,
66 -1,
67 &self->st,
68 &tail);
69
70 self->db = connection->db;
71
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000072 if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073 (void)sqlite3_finalize(self->st);
74 self->st = NULL;
75 rc = PYSQLITE_TOO_MUCH_SQL;
76 }
77
78 return rc;
79}
80
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000081int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082{
83 int rc = SQLITE_OK;
84 long longval;
85#ifdef HAVE_LONG_LONG
86 PY_LONG_LONG longlongval;
87#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);
Guido van Rossum523d4f92007-01-15 00:31:49 +000095 } else if (PyInt_CheckExact(parameter)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096 longval = PyInt_AsLong(parameter);
97 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
98#ifdef HAVE_LONG_LONG
99 } else if (PyLong_Check(parameter)) {
100 longlongval = PyLong_AsLongLong(parameter);
101 /* in the overflow error case, longlongval is -1, and an exception is set */
102 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
103#endif
104 } else if (PyFloat_Check(parameter)) {
105 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
106 } else if (PyBuffer_Check(parameter)) {
107 if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
108 rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
109 } else {
110 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
111 rc = -1;
112 }
113 } else if PyString_Check(parameter) {
114 string = PyString_AsString(parameter);
115 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
116 } else if PyUnicode_Check(parameter) {
117 stringval = PyUnicode_AsUTF8String(parameter);
Gerhard Häring6d214562007-08-10 18:15:11 +0000118 string = PyBytes_AsString(stringval);
119
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
121 Py_DECREF(stringval);
122 } else {
123 rc = -1;
124 }
125
126 return rc;
127}
128
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000129void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130{
131 PyObject* current_param;
132 PyObject* adapted;
133 const char* binding_name;
134 int i;
135 int rc;
136 int num_params_needed;
137 int num_params;
138
139 Py_BEGIN_ALLOW_THREADS
140 num_params_needed = sqlite3_bind_parameter_count(self->st);
141 Py_END_ALLOW_THREADS
142
143 if (PyDict_Check(parameters)) {
144 /* parameters passed as dictionary */
145 for (i = 1; i <= num_params_needed; i++) {
146 Py_BEGIN_ALLOW_THREADS
147 binding_name = sqlite3_bind_parameter_name(self->st, i);
148 Py_END_ALLOW_THREADS
149 if (!binding_name) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000150 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 +0000151 return;
152 }
153
154 binding_name++; /* skip first char (the colon) */
155 current_param = PyDict_GetItemString(parameters, binding_name);
156 if (!current_param) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157 PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158 return;
159 }
160
161 Py_INCREF(current_param);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000162 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163 if (adapted) {
164 Py_DECREF(current_param);
165 } else {
166 PyErr_Clear();
167 adapted = current_param;
168 }
169
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000170 rc = pysqlite_statement_bind_parameter(self, i, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 Py_DECREF(adapted);
172
173 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000174 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175 return;
176 }
177 }
178 } else {
179 /* parameters passed as sequence */
180 num_params = PySequence_Length(parameters);
181 if (num_params != num_params_needed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000182 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 +0000183 num_params_needed, num_params);
184 return;
185 }
186 for (i = 0; i < num_params; i++) {
187 current_param = PySequence_GetItem(parameters, i);
188 if (!current_param) {
189 return;
190 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000191 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000192
193 if (adapted) {
194 Py_DECREF(current_param);
195 } else {
196 PyErr_Clear();
197 adapted = current_param;
198 }
199
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000200 rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201 Py_DECREF(adapted);
202
203 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000204 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205 return;
206 }
207 }
208 }
209}
210
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000211int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212{
213 const char* tail;
214 int rc;
Guido van Rossum83857e32007-05-09 23:37:01 +0000215 const char* sql_cstr;
216 Py_ssize_t sql_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000217 sqlite3_stmt* new_st;
218
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000219 sql_cstr = PyUnicode_AsString(self->sql);
220 if (sql_cstr == NULL) {
Guido van Rossum83857e32007-05-09 23:37:01 +0000221 rc = PYSQLITE_SQL_WRONG_TYPE;
222 return rc;
223 }
Guido van Rossumfa9a1212007-08-29 03:34:29 +0000224 sql_len = strlen(sql_cstr); /* XXXX */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225
226 rc = sqlite3_prepare(self->db,
227 sql_cstr,
228 -1,
229 &new_st,
230 &tail);
231
232 if (rc == SQLITE_OK) {
233 /* The efficient sqlite3_transfer_bindings is only available in SQLite
234 * version 3.2.2 or later. For older SQLite releases, that might not
235 * even define SQLITE_VERSION_NUMBER, we do it the manual way.
236 */
237 #ifdef SQLITE_VERSION_NUMBER
238 #if SQLITE_VERSION_NUMBER >= 3002002
239 (void)sqlite3_transfer_bindings(self->st, new_st);
240 #endif
241 #else
242 statement_bind_parameters(self, params);
243 #endif
244
245 (void)sqlite3_finalize(self->st);
246 self->st = new_st;
247 }
248
249 return rc;
250}
251
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000252int pysqlite_statement_finalize(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253{
254 int rc;
255
256 rc = SQLITE_OK;
257 if (self->st) {
258 Py_BEGIN_ALLOW_THREADS
259 rc = sqlite3_finalize(self->st);
260 Py_END_ALLOW_THREADS
261 self->st = NULL;
262 }
263
264 self->in_use = 0;
265
266 return rc;
267}
268
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000269int pysqlite_statement_reset(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000270{
271 int rc;
272
273 rc = SQLITE_OK;
274
275 if (self->in_use && self->st) {
276 Py_BEGIN_ALLOW_THREADS
277 rc = sqlite3_reset(self->st);
278 Py_END_ALLOW_THREADS
279
280 if (rc == SQLITE_OK) {
281 self->in_use = 0;
282 }
283 }
284
285 return rc;
286}
287
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000288void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289{
290 self->in_use = 1;
291}
292
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000293void pysqlite_statement_dealloc(pysqlite_Statement* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294{
295 int rc;
296
297 if (self->st) {
298 Py_BEGIN_ALLOW_THREADS
299 rc = sqlite3_finalize(self->st);
300 Py_END_ALLOW_THREADS
301 }
302
303 self->st = NULL;
304
305 Py_XDECREF(self->sql);
306
Thomas Wouters477c8d52006-05-27 19:21:47 +0000307 if (self->in_weakreflist != NULL) {
308 PyObject_ClearWeakRefs((PyObject*)self);
309 }
310
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000311 Py_Type(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312}
313
314/*
315 * Checks if there is anything left in an SQL string after SQLite compiled it.
316 * This is used to check if somebody tried to execute more than one SQL command
317 * with one execute()/executemany() command, which the DB-API and we don't
318 * allow.
319 *
320 * Returns 1 if there is more left than should be. 0 if ok.
321 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000322static int pysqlite_check_remaining_sql(const char* tail)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323{
324 const char* pos = tail;
325
326 parse_remaining_sql_state state = NORMAL;
327
328 for (;;) {
329 switch (*pos) {
330 case 0:
331 return 0;
332 case '-':
333 if (state == NORMAL) {
334 state = LINECOMMENT_1;
335 } else if (state == LINECOMMENT_1) {
336 state = IN_LINECOMMENT;
337 }
338 break;
339 case ' ':
340 case '\t':
341 break;
342 case '\n':
343 case 13:
344 if (state == IN_LINECOMMENT) {
345 state = NORMAL;
346 }
347 break;
348 case '/':
349 if (state == NORMAL) {
350 state = COMMENTSTART_1;
351 } else if (state == COMMENTEND_1) {
352 state = NORMAL;
353 } else if (state == COMMENTSTART_1) {
354 return 1;
355 }
356 break;
357 case '*':
358 if (state == NORMAL) {
359 return 1;
360 } else if (state == LINECOMMENT_1) {
361 return 1;
362 } else if (state == COMMENTSTART_1) {
363 state = IN_COMMENT;
364 } else if (state == IN_COMMENT) {
365 state = COMMENTEND_1;
366 }
367 break;
368 default:
369 if (state == COMMENTEND_1) {
370 state = IN_COMMENT;
371 } else if (state == IN_LINECOMMENT) {
372 } else if (state == IN_COMMENT) {
373 } else {
374 return 1;
375 }
376 }
377
378 pos++;
379 }
380
381 return 0;
382}
383
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000384PyTypeObject pysqlite_StatementType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000385 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 MODULE_NAME ".Statement", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000387 sizeof(pysqlite_Statement), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000389 (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 0, /* tp_print */
391 0, /* tp_getattr */
392 0, /* tp_setattr */
393 0, /* tp_compare */
394 0, /* tp_repr */
395 0, /* tp_as_number */
396 0, /* tp_as_sequence */
397 0, /* tp_as_mapping */
398 0, /* tp_hash */
399 0, /* tp_call */
400 0, /* tp_str */
401 0, /* tp_getattro */
402 0, /* tp_setattro */
403 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000404 Py_TPFLAGS_DEFAULT, /* tp_flags */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405 0, /* tp_doc */
406 0, /* tp_traverse */
407 0, /* tp_clear */
408 0, /* tp_richcompare */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000409 offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410 0, /* tp_iter */
411 0, /* tp_iternext */
412 0, /* tp_methods */
413 0, /* tp_members */
414 0, /* tp_getset */
415 0, /* tp_base */
416 0, /* tp_dict */
417 0, /* tp_descr_get */
418 0, /* tp_descr_set */
419 0, /* tp_dictoffset */
420 (initproc)0, /* tp_init */
421 0, /* tp_alloc */
422 0, /* tp_new */
423 0 /* tp_free */
424};
425
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000426extern int pysqlite_statement_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000427{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000428 pysqlite_StatementType.tp_new = PyType_GenericNew;
429 return PyType_Ready(&pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430}