blob: 97d2e2a360b39b3f8fd18551c135d1486ce67032 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +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"
Anthony Baxter07f5b352006-04-01 08:36:27 +000025#include "cursor.h"
Anthony Baxterc51ee692006-04-01 00:57:31 +000026#include "connection.h"
Anthony Baxter07f5b352006-04-01 08:36:27 +000027#include "microprotocols.h"
28#include "prepare_protocol.h"
Anthony Baxter72289a62006-04-04 06:29:05 +000029#include "sqlitecompat.h"
Anthony Baxterc51ee692006-04-01 00:57:31 +000030
31/* prototypes */
Gerhard Häring0741a602007-01-14 01:43:50 +000032static int pysqlite_check_remaining_sql(const char* tail);
Anthony Baxterc51ee692006-04-01 00:57:31 +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
Gerhard Häring0741a602007-01-14 01:43:50 +000043int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql)
Anthony Baxterc51ee692006-04-01 00:57:31 +000044{
45 const char* tail;
46 int rc;
47 PyObject* sql_str;
48 char* sql_cstr;
49
50 self->st = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000051 self->in_use = 0;
52
53 if (PyString_Check(sql)) {
54 sql_str = sql;
55 Py_INCREF(sql_str);
56 } else if (PyUnicode_Check(sql)) {
57 sql_str = PyUnicode_AsUTF8String(sql);
58 if (!sql_str) {
59 rc = PYSQLITE_SQL_WRONG_TYPE;
60 return rc;
61 }
62 } else {
63 rc = PYSQLITE_SQL_WRONG_TYPE;
64 return rc;
65 }
66
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000067 self->in_weakreflist = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000068 self->sql = sql_str;
69
70 sql_cstr = PyString_AsString(sql_str);
71
72 rc = sqlite3_prepare(connection->db,
73 sql_cstr,
74 -1,
75 &self->st,
76 &tail);
77
78 self->db = connection->db;
79
Gerhard Häring0741a602007-01-14 01:43:50 +000080 if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +000081 (void)sqlite3_finalize(self->st);
Neal Norwitz195e4e62006-04-16 03:37:19 +000082 self->st = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000083 rc = PYSQLITE_TOO_MUCH_SQL;
84 }
85
86 return rc;
87}
88
Gerhard Häring0741a602007-01-14 01:43:50 +000089int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
Anthony Baxter07f5b352006-04-01 08:36:27 +000090{
91 int rc = SQLITE_OK;
92 long longval;
93#ifdef HAVE_LONG_LONG
94 PY_LONG_LONG longlongval;
95#endif
96 const char* buffer;
97 char* string;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +000098 Py_ssize_t buflen;
Anthony Baxter07f5b352006-04-01 08:36:27 +000099 PyObject* stringval;
100
101 if (parameter == Py_None) {
102 rc = sqlite3_bind_null(self->st, pos);
103 } else if (PyInt_Check(parameter)) {
104 longval = PyInt_AsLong(parameter);
105 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
106#ifdef HAVE_LONG_LONG
107 } else if (PyLong_Check(parameter)) {
108 longlongval = PyLong_AsLongLong(parameter);
109 /* in the overflow error case, longlongval is -1, and an exception is set */
110 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
111#endif
112 } else if (PyFloat_Check(parameter)) {
113 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
114 } else if (PyBuffer_Check(parameter)) {
115 if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
116 rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
117 } else {
118 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
119 rc = -1;
120 }
121 } else if PyString_Check(parameter) {
122 string = PyString_AsString(parameter);
123 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
124 } else if PyUnicode_Check(parameter) {
125 stringval = PyUnicode_AsUTF8String(parameter);
126 string = PyString_AsString(stringval);
127 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
128 Py_DECREF(stringval);
129 } else {
130 rc = -1;
131 }
132
133 return rc;
134}
135
Gerhard Häring0741a602007-01-14 01:43:50 +0000136void pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
Anthony Baxter07f5b352006-04-01 08:36:27 +0000137{
138 PyObject* current_param;
139 PyObject* adapted;
140 const char* binding_name;
141 int i;
142 int rc;
143 int num_params_needed;
144 int num_params;
145
146 Py_BEGIN_ALLOW_THREADS
147 num_params_needed = sqlite3_bind_parameter_count(self->st);
148 Py_END_ALLOW_THREADS
149
150 if (PyDict_Check(parameters)) {
151 /* parameters passed as dictionary */
152 for (i = 1; i <= num_params_needed; i++) {
153 Py_BEGIN_ALLOW_THREADS
154 binding_name = sqlite3_bind_parameter_name(self->st, i);
155 Py_END_ALLOW_THREADS
156 if (!binding_name) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000157 PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000158 return;
159 }
160
161 binding_name++; /* skip first char (the colon) */
162 current_param = PyDict_GetItemString(parameters, binding_name);
163 if (!current_param) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000164 PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000165 return;
166 }
167
168 Py_INCREF(current_param);
Gerhard Häring0741a602007-01-14 01:43:50 +0000169 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000170 if (adapted) {
171 Py_DECREF(current_param);
172 } else {
173 PyErr_Clear();
174 adapted = current_param;
175 }
176
Gerhard Häring0741a602007-01-14 01:43:50 +0000177 rc = pysqlite_statement_bind_parameter(self, i, adapted);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000178 Py_DECREF(adapted);
179
180 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000181 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000182 return;
183 }
184 }
185 } else {
186 /* parameters passed as sequence */
187 num_params = PySequence_Length(parameters);
188 if (num_params != num_params_needed) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000189 PyErr_Format(pysqlite_ProgrammingError, "Incorrect number of bindings supplied. The current statement uses %d, and there are %d supplied.",
Anthony Baxter07f5b352006-04-01 08:36:27 +0000190 num_params_needed, num_params);
191 return;
192 }
193 for (i = 0; i < num_params; i++) {
194 current_param = PySequence_GetItem(parameters, i);
195 if (!current_param) {
196 return;
197 }
Gerhard Häring0741a602007-01-14 01:43:50 +0000198 adapted = microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000199
200 if (adapted) {
201 Py_DECREF(current_param);
202 } else {
203 PyErr_Clear();
204 adapted = current_param;
205 }
206
Gerhard Häring0741a602007-01-14 01:43:50 +0000207 rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000208 Py_DECREF(adapted);
209
210 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000211 PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000212 return;
213 }
214 }
215 }
216}
217
Gerhard Häring0741a602007-01-14 01:43:50 +0000218int pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000219{
220 const char* tail;
221 int rc;
222 char* sql_cstr;
223 sqlite3_stmt* new_st;
224
225 sql_cstr = PyString_AsString(self->sql);
226
227 rc = sqlite3_prepare(self->db,
228 sql_cstr,
229 -1,
230 &new_st,
231 &tail);
232
233 if (rc == SQLITE_OK) {
Anthony Baxter07f5b352006-04-01 08:36:27 +0000234 /* The efficient sqlite3_transfer_bindings is only available in SQLite
235 * version 3.2.2 or later. For older SQLite releases, that might not
236 * even define SQLITE_VERSION_NUMBER, we do it the manual way.
237 */
238 #ifdef SQLITE_VERSION_NUMBER
239 #if SQLITE_VERSION_NUMBER >= 3002002
Gerhard Häring99b9df82007-12-11 21:07:40 +0000240 /* The check for the number of parameters is necessary to not trigger a
241 * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */
242 if (sqlite3_bind_parameter_count(self->st) > 0) {
243 (void)sqlite3_transfer_bindings(self->st, new_st);
244 }
Anthony Baxter07f5b352006-04-01 08:36:27 +0000245 #endif
246 #else
247 statement_bind_parameters(self, params);
248 #endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000249
250 (void)sqlite3_finalize(self->st);
251 self->st = new_st;
252 }
253
254 return rc;
255}
256
Gerhard Häring0741a602007-01-14 01:43:50 +0000257int pysqlite_statement_finalize(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000258{
259 int rc;
260
261 rc = SQLITE_OK;
262 if (self->st) {
263 Py_BEGIN_ALLOW_THREADS
264 rc = sqlite3_finalize(self->st);
265 Py_END_ALLOW_THREADS
266 self->st = NULL;
267 }
268
269 self->in_use = 0;
270
271 return rc;
272}
273
Gerhard Häring0741a602007-01-14 01:43:50 +0000274int pysqlite_statement_reset(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000275{
276 int rc;
277
278 rc = SQLITE_OK;
279
280 if (self->in_use && self->st) {
281 Py_BEGIN_ALLOW_THREADS
282 rc = sqlite3_reset(self->st);
283 Py_END_ALLOW_THREADS
284
285 if (rc == SQLITE_OK) {
286 self->in_use = 0;
287 }
288 }
289
290 return rc;
291}
292
Gerhard Häring0741a602007-01-14 01:43:50 +0000293void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000294{
295 self->in_use = 1;
296}
297
Gerhard Häring0741a602007-01-14 01:43:50 +0000298void pysqlite_statement_dealloc(pysqlite_Statement* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000299{
300 int rc;
301
302 if (self->st) {
303 Py_BEGIN_ALLOW_THREADS
304 rc = sqlite3_finalize(self->st);
305 Py_END_ALLOW_THREADS
306 }
307
308 self->st = NULL;
309
310 Py_XDECREF(self->sql);
311
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000312 if (self->in_weakreflist != NULL) {
313 PyObject_ClearWeakRefs((PyObject*)self);
314 }
315
Martin v. Löwis68192102007-07-21 06:55:02 +0000316 Py_Type(self)->tp_free((PyObject*)self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000317}
318
319/*
320 * Checks if there is anything left in an SQL string after SQLite compiled it.
321 * This is used to check if somebody tried to execute more than one SQL command
322 * with one execute()/executemany() command, which the DB-API and we don't
323 * allow.
324 *
325 * Returns 1 if there is more left than should be. 0 if ok.
326 */
Gerhard Häring0741a602007-01-14 01:43:50 +0000327static int pysqlite_check_remaining_sql(const char* tail)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000328{
329 const char* pos = tail;
330
331 parse_remaining_sql_state state = NORMAL;
332
333 for (;;) {
334 switch (*pos) {
335 case 0:
336 return 0;
337 case '-':
338 if (state == NORMAL) {
339 state = LINECOMMENT_1;
340 } else if (state == LINECOMMENT_1) {
341 state = IN_LINECOMMENT;
342 }
343 break;
344 case ' ':
345 case '\t':
346 break;
347 case '\n':
348 case 13:
349 if (state == IN_LINECOMMENT) {
350 state = NORMAL;
351 }
352 break;
353 case '/':
354 if (state == NORMAL) {
355 state = COMMENTSTART_1;
356 } else if (state == COMMENTEND_1) {
357 state = NORMAL;
358 } else if (state == COMMENTSTART_1) {
359 return 1;
360 }
361 break;
362 case '*':
363 if (state == NORMAL) {
364 return 1;
365 } else if (state == LINECOMMENT_1) {
366 return 1;
367 } else if (state == COMMENTSTART_1) {
368 state = IN_COMMENT;
369 } else if (state == IN_COMMENT) {
370 state = COMMENTEND_1;
371 }
372 break;
373 default:
374 if (state == COMMENTEND_1) {
375 state = IN_COMMENT;
376 } else if (state == IN_LINECOMMENT) {
377 } else if (state == IN_COMMENT) {
378 } else {
379 return 1;
380 }
381 }
382
383 pos++;
384 }
385
386 return 0;
387}
388
Gerhard Häring0741a602007-01-14 01:43:50 +0000389PyTypeObject pysqlite_StatementType = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000390 PyVarObject_HEAD_INIT(NULL, 0)
Anthony Baxter8e7b4902006-04-05 18:25:33 +0000391 MODULE_NAME ".Statement", /* tp_name */
Gerhard Häring0741a602007-01-14 01:43:50 +0000392 sizeof(pysqlite_Statement), /* tp_basicsize */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000393 0, /* tp_itemsize */
Gerhard Häring0741a602007-01-14 01:43:50 +0000394 (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000395 0, /* tp_print */
396 0, /* tp_getattr */
397 0, /* tp_setattr */
398 0, /* tp_compare */
399 0, /* tp_repr */
400 0, /* tp_as_number */
401 0, /* tp_as_sequence */
402 0, /* tp_as_mapping */
403 0, /* tp_hash */
404 0, /* tp_call */
405 0, /* tp_str */
406 0, /* tp_getattro */
407 0, /* tp_setattro */
408 0, /* tp_as_buffer */
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000409 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000410 0, /* tp_doc */
411 0, /* tp_traverse */
412 0, /* tp_clear */
413 0, /* tp_richcompare */
Gerhard Häring0741a602007-01-14 01:43:50 +0000414 offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
Anthony Baxterc51ee692006-04-01 00:57:31 +0000415 0, /* tp_iter */
416 0, /* tp_iternext */
417 0, /* tp_methods */
418 0, /* tp_members */
419 0, /* tp_getset */
420 0, /* tp_base */
421 0, /* tp_dict */
422 0, /* tp_descr_get */
423 0, /* tp_descr_set */
424 0, /* tp_dictoffset */
425 (initproc)0, /* tp_init */
426 0, /* tp_alloc */
427 0, /* tp_new */
428 0 /* tp_free */
429};
430
Gerhard Häring0741a602007-01-14 01:43:50 +0000431extern int pysqlite_statement_setup_types(void)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000432{
Gerhard Häring0741a602007-01-14 01:43:50 +0000433 pysqlite_StatementType.tp_new = PyType_GenericNew;
434 return PyType_Ready(&pysqlite_StatementType);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000435}