blob: 4a957d6f4f1e6d3e6aa61a96b7efaf3a8644ef97 [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 Baxterc51ee692006-04-01 00:57:31 +000029
30/* prototypes */
31int check_remaining_sql(const char* tail);
32
33typedef enum {
34 LINECOMMENT_1,
35 IN_LINECOMMENT,
36 COMMENTSTART_1,
37 IN_COMMENT,
38 COMMENTEND_1,
39 NORMAL
40} parse_remaining_sql_state;
41
42int statement_create(Statement* self, Connection* connection, PyObject* sql)
43{
44 const char* tail;
45 int rc;
46 PyObject* sql_str;
47 char* sql_cstr;
48
49 self->st = NULL;
50
51 self->st = NULL;
52 self->in_use = 0;
53
54 if (PyString_Check(sql)) {
55 sql_str = sql;
56 Py_INCREF(sql_str);
57 } else if (PyUnicode_Check(sql)) {
58 sql_str = PyUnicode_AsUTF8String(sql);
59 if (!sql_str) {
60 rc = PYSQLITE_SQL_WRONG_TYPE;
61 return rc;
62 }
63 } else {
64 rc = PYSQLITE_SQL_WRONG_TYPE;
65 return rc;
66 }
67
68 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
80 if (rc == SQLITE_OK && check_remaining_sql(tail)) {
81 (void)sqlite3_finalize(self->st);
82 rc = PYSQLITE_TOO_MUCH_SQL;
83 }
84
85 return rc;
86}
87
Anthony Baxter07f5b352006-04-01 08:36:27 +000088int statement_bind_parameter(Statement* self, int pos, PyObject* parameter)
89{
90 int rc = SQLITE_OK;
91 long longval;
92#ifdef HAVE_LONG_LONG
93 PY_LONG_LONG longlongval;
94#endif
95 const char* buffer;
96 char* string;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +000097 Py_ssize_t buflen;
Anthony Baxter07f5b352006-04-01 08:36:27 +000098 PyObject* stringval;
99
100 if (parameter == Py_None) {
101 rc = sqlite3_bind_null(self->st, pos);
102 } else if (PyInt_Check(parameter)) {
103 longval = PyInt_AsLong(parameter);
104 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longval);
105#ifdef HAVE_LONG_LONG
106 } else if (PyLong_Check(parameter)) {
107 longlongval = PyLong_AsLongLong(parameter);
108 /* in the overflow error case, longlongval is -1, and an exception is set */
109 rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
110#endif
111 } else if (PyFloat_Check(parameter)) {
112 rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
113 } else if (PyBuffer_Check(parameter)) {
114 if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
115 rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
116 } else {
117 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
118 rc = -1;
119 }
120 } else if PyString_Check(parameter) {
121 string = PyString_AsString(parameter);
122 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
123 } else if PyUnicode_Check(parameter) {
124 stringval = PyUnicode_AsUTF8String(parameter);
125 string = PyString_AsString(stringval);
126 rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
127 Py_DECREF(stringval);
128 } else {
129 rc = -1;
130 }
131
132 return rc;
133}
134
135void statement_bind_parameters(Statement* self, PyObject* parameters)
136{
137 PyObject* current_param;
138 PyObject* adapted;
139 const char* binding_name;
140 int i;
141 int rc;
142 int num_params_needed;
143 int num_params;
144
145 Py_BEGIN_ALLOW_THREADS
146 num_params_needed = sqlite3_bind_parameter_count(self->st);
147 Py_END_ALLOW_THREADS
148
149 if (PyDict_Check(parameters)) {
150 /* parameters passed as dictionary */
151 for (i = 1; i <= num_params_needed; i++) {
152 Py_BEGIN_ALLOW_THREADS
153 binding_name = sqlite3_bind_parameter_name(self->st, i);
154 Py_END_ALLOW_THREADS
155 if (!binding_name) {
156 PyErr_Format(ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
157 return;
158 }
159
160 binding_name++; /* skip first char (the colon) */
161 current_param = PyDict_GetItemString(parameters, binding_name);
162 if (!current_param) {
163 PyErr_Format(ProgrammingError, "You did not supply a value for binding %d.", i);
164 return;
165 }
166
167 Py_INCREF(current_param);
168 adapted = microprotocols_adapt(current_param, (PyObject*)&SQLitePrepareProtocolType, NULL);
169 if (adapted) {
170 Py_DECREF(current_param);
171 } else {
172 PyErr_Clear();
173 adapted = current_param;
174 }
175
176 rc = statement_bind_parameter(self, i, adapted);
177 Py_DECREF(adapted);
178
179 if (rc != SQLITE_OK) {
180 PyErr_Format(InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
181 return;
182 }
183 }
184 } else {
185 /* parameters passed as sequence */
186 num_params = PySequence_Length(parameters);
187 if (num_params != num_params_needed) {
188 PyErr_Format(ProgrammingError, "Incorrect number of bindings supplied. The current statement uses %d, and there are %d supplied.",
189 num_params_needed, num_params);
190 return;
191 }
192 for (i = 0; i < num_params; i++) {
193 current_param = PySequence_GetItem(parameters, i);
194 if (!current_param) {
195 return;
196 }
197 adapted = microprotocols_adapt(current_param, (PyObject*)&SQLitePrepareProtocolType, NULL);
198
199 if (adapted) {
200 Py_DECREF(current_param);
201 } else {
202 PyErr_Clear();
203 adapted = current_param;
204 }
205
206 rc = statement_bind_parameter(self, i + 1, adapted);
207 Py_DECREF(adapted);
208
209 if (rc != SQLITE_OK) {
210 PyErr_Format(InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
211 return;
212 }
213 }
214 }
215}
216
217int statement_recompile(Statement* self, PyObject* params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000218{
219 const char* tail;
220 int rc;
221 char* sql_cstr;
222 sqlite3_stmt* new_st;
223
224 sql_cstr = PyString_AsString(self->sql);
225
226 rc = sqlite3_prepare(self->db,
227 sql_cstr,
228 -1,
229 &new_st,
230 &tail);
231
232 if (rc == SQLITE_OK) {
Anthony Baxter07f5b352006-04-01 08:36:27 +0000233 /* 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
Anthony Baxterc51ee692006-04-01 00:57:31 +0000239 (void)sqlite3_transfer_bindings(self->st, new_st);
Anthony Baxter07f5b352006-04-01 08:36:27 +0000240 #endif
241 #else
242 statement_bind_parameters(self, params);
243 #endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000244
245 (void)sqlite3_finalize(self->st);
246 self->st = new_st;
247 }
248
249 return rc;
250}
251
252int statement_finalize(Statement* self)
253{
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
269int statement_reset(Statement* self)
270{
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
288void statement_mark_dirty(Statement* self)
289{
290 self->in_use = 1;
291}
292
293void statement_dealloc(Statement* self)
294{
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
307 self->ob_type->tp_free((PyObject*)self);
308}
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 */
318int check_remaining_sql(const char* tail)
319{
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
380PyTypeObject StatementType = {
381 PyObject_HEAD_INIT(NULL)
382 0, /* ob_size */
383 "pysqlite2.dbapi2.Statement", /* tp_name */
384 sizeof(Statement), /* tp_basicsize */
385 0, /* tp_itemsize */
386 (destructor)statement_dealloc, /* tp_dealloc */
387 0, /* tp_print */
388 0, /* tp_getattr */
389 0, /* tp_setattr */
390 0, /* tp_compare */
391 0, /* tp_repr */
392 0, /* tp_as_number */
393 0, /* tp_as_sequence */
394 0, /* tp_as_mapping */
395 0, /* tp_hash */
396 0, /* tp_call */
397 0, /* tp_str */
398 0, /* tp_getattro */
399 0, /* tp_setattro */
400 0, /* tp_as_buffer */
401 Py_TPFLAGS_DEFAULT, /* tp_flags */
402 0, /* tp_doc */
403 0, /* tp_traverse */
404 0, /* tp_clear */
405 0, /* tp_richcompare */
406 0, /* tp_weaklistoffset */
407 0, /* tp_iter */
408 0, /* tp_iternext */
409 0, /* tp_methods */
410 0, /* tp_members */
411 0, /* tp_getset */
412 0, /* tp_base */
413 0, /* tp_dict */
414 0, /* tp_descr_get */
415 0, /* tp_descr_set */
416 0, /* tp_dictoffset */
417 (initproc)0, /* tp_init */
418 0, /* tp_alloc */
419 0, /* tp_new */
420 0 /* tp_free */
421};
422
423extern int statement_setup_types(void)
424{
425 StatementType.tp_new = PyType_GenericNew;
426 return PyType_Ready(&StatementType);
427}