blob: b80b955847ae63f3b1daebce7916582070d5eb6f [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);
Gerhard Häring6d214562007-08-10 18:15:11 +0000116 string = PyBytes_AsString(stringval);
117
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
119 Py_DECREF(stringval);
120 } else {
121 rc = -1;
122 }
123
124 return rc;
125}
126
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000127void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128{
129 PyObject* current_param;
130 PyObject* adapted;
131 const char* binding_name;
132 int i;
133 int rc;
134 int num_params_needed;
135 int num_params;
136
137 Py_BEGIN_ALLOW_THREADS
138 num_params_needed = sqlite3_bind_parameter_count(self->st);
139 Py_END_ALLOW_THREADS
140
141 if (PyDict_Check(parameters)) {
142 /* parameters passed as dictionary */
143 for (i = 1; i <= num_params_needed; i++) {
144 Py_BEGIN_ALLOW_THREADS
145 binding_name = sqlite3_bind_parameter_name(self->st, i);
146 Py_END_ALLOW_THREADS
147 if (!binding_name) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000148 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 +0000149 return;
150 }
151
152 binding_name++; /* skip first char (the colon) */
153 current_param = PyDict_GetItemString(parameters, binding_name);
154 if (!current_param) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000155 PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156 return;
157 }
158
159 Py_INCREF(current_param);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000160 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161 if (adapted) {
162 Py_DECREF(current_param);
163 } else {
164 PyErr_Clear();
165 adapted = current_param;
166 }
167
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000168 rc = pysqlite_statement_bind_parameter(self, i, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169 Py_DECREF(adapted);
170
171 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000172 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173 return;
174 }
175 }
176 } else {
177 /* parameters passed as sequence */
178 num_params = PySequence_Length(parameters);
179 if (num_params != num_params_needed) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180 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 +0000181 num_params_needed, num_params);
182 return;
183 }
184 for (i = 0; i < num_params; i++) {
185 current_param = PySequence_GetItem(parameters, i);
186 if (!current_param) {
187 return;
188 }
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000189 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190
191 if (adapted) {
192 Py_DECREF(current_param);
193 } else {
194 PyErr_Clear();
195 adapted = current_param;
196 }
197
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000198 rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000199 Py_DECREF(adapted);
200
201 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000202 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000203 return;
204 }
205 }
206 }
207}
208
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000209int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000210{
211 const char* tail;
212 int rc;
Guido van Rossum83857e32007-05-09 23:37:01 +0000213 const char* sql_cstr;
214 Py_ssize_t sql_len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 sqlite3_stmt* new_st;
216
Guido van Rossum83857e32007-05-09 23:37:01 +0000217 if (PyObject_AsCharBuffer(self->sql, &sql_cstr, &sql_len) < 0) {
218 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}