1 /* statement.c - the statement type
3 * Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
5 * This file is part of pysqlite.
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.
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:
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.
24 #include "statement.h"
26 #include "connection.h"
27 #include "microprotocols.h"
28 #include "prepare_protocol.h"
29 #include "sqlitecompat.h"
32 static int pysqlite_check_remaining_sql(const char* tail
);
41 } parse_remaining_sql_state
;
53 int pysqlite_statement_create(pysqlite_Statement
* self
, pysqlite_Connection
* connection
, PyObject
* sql
)
63 if (PyString_Check(sql
)) {
66 } else if (PyUnicode_Check(sql
)) {
67 sql_str
= PyUnicode_AsUTF8String(sql
);
69 rc
= PYSQLITE_SQL_WRONG_TYPE
;
73 rc
= PYSQLITE_SQL_WRONG_TYPE
;
77 self
->in_weakreflist
= NULL
;
80 sql_cstr
= PyString_AsString(sql_str
);
82 Py_BEGIN_ALLOW_THREADS
83 rc
= sqlite3_prepare(connection
->db
,
90 self
->db
= connection
->db
;
92 if (rc
== SQLITE_OK
&& pysqlite_check_remaining_sql(tail
)) {
93 (void)sqlite3_finalize(self
->st
);
95 rc
= PYSQLITE_TOO_MUCH_SQL
;
101 int pysqlite_statement_bind_parameter(pysqlite_Statement
* self
, int pos
, PyObject
* parameter
, int allow_8bit_chars
)
105 PY_LONG_LONG longlongval
;
110 parameter_type paramtype
;
113 if (parameter
== Py_None
) {
114 rc
= sqlite3_bind_null(self
->st
, pos
);
118 if (PyInt_CheckExact(parameter
)) {
119 paramtype
= TYPE_INT
;
120 } else if (PyLong_CheckExact(parameter
)) {
121 paramtype
= TYPE_LONG
;
122 } else if (PyFloat_CheckExact(parameter
)) {
123 paramtype
= TYPE_FLOAT
;
124 } else if (PyString_CheckExact(parameter
)) {
125 paramtype
= TYPE_STRING
;
126 } else if (PyUnicode_CheckExact(parameter
)) {
127 paramtype
= TYPE_UNICODE
;
128 } else if (PyBuffer_Check(parameter
)) {
129 paramtype
= TYPE_BUFFER
;
130 } else if (PyInt_Check(parameter
)) {
131 paramtype
= TYPE_INT
;
132 } else if (PyLong_Check(parameter
)) {
133 paramtype
= TYPE_LONG
;
134 } else if (PyFloat_Check(parameter
)) {
135 paramtype
= TYPE_FLOAT
;
136 } else if (PyString_Check(parameter
)) {
137 paramtype
= TYPE_STRING
;
138 } else if (PyUnicode_Check(parameter
)) {
139 paramtype
= TYPE_UNICODE
;
141 paramtype
= TYPE_UNKNOWN
;
144 if (paramtype
== TYPE_STRING
&& !allow_8bit_chars
) {
145 string
= PyString_AS_STRING(parameter
);
146 for (c
= string
; *c
!= 0; c
++) {
148 PyErr_SetString(pysqlite_ProgrammingError
, "You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.");
157 longval
= PyInt_AsLong(parameter
);
158 rc
= sqlite3_bind_int64(self
->st
, pos
, (sqlite_int64
)longval
);
161 longlongval
= PyLong_AsLongLong(parameter
);
162 /* in the overflow error case, longlongval is -1, and an exception is set */
163 rc
= sqlite3_bind_int64(self
->st
, pos
, (sqlite_int64
)longlongval
);
166 rc
= sqlite3_bind_double(self
->st
, pos
, PyFloat_AsDouble(parameter
));
169 string
= PyString_AS_STRING(parameter
);
170 rc
= sqlite3_bind_text(self
->st
, pos
, string
, -1, SQLITE_TRANSIENT
);
173 stringval
= PyUnicode_AsUTF8String(parameter
);
174 string
= PyString_AsString(stringval
);
175 rc
= sqlite3_bind_text(self
->st
, pos
, string
, -1, SQLITE_TRANSIENT
);
176 Py_DECREF(stringval
);
179 if (PyObject_AsCharBuffer(parameter
, &buffer
, &buflen
) == 0) {
180 rc
= sqlite3_bind_blob(self
->st
, pos
, buffer
, buflen
, SQLITE_TRANSIENT
);
182 PyErr_SetString(PyExc_ValueError
, "could not convert BLOB to buffer");
194 /* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
195 static int _need_adapt(PyObject
* obj
)
197 if (pysqlite_BaseTypeAdapted
) {
201 if (PyInt_CheckExact(obj
) || PyLong_CheckExact(obj
)
202 || PyFloat_CheckExact(obj
) || PyString_CheckExact(obj
)
203 || PyUnicode_CheckExact(obj
) || PyBuffer_Check(obj
)) {
210 void pysqlite_statement_bind_parameters(pysqlite_Statement
* self
, PyObject
* parameters
, int allow_8bit_chars
)
212 PyObject
* current_param
;
214 const char* binding_name
;
217 int num_params_needed
;
220 Py_BEGIN_ALLOW_THREADS
221 num_params_needed
= sqlite3_bind_parameter_count(self
->st
);
224 if (PyTuple_CheckExact(parameters
) || PyList_CheckExact(parameters
) || (!PyDict_Check(parameters
) && PySequence_Check(parameters
))) {
225 /* parameters passed as sequence */
226 if (PyTuple_CheckExact(parameters
)) {
227 num_params
= PyTuple_GET_SIZE(parameters
);
228 } else if (PyList_CheckExact(parameters
)) {
229 num_params
= PyList_GET_SIZE(parameters
);
231 num_params
= PySequence_Size(parameters
);
233 if (num_params
!= num_params_needed
) {
234 PyErr_Format(pysqlite_ProgrammingError
, "Incorrect number of bindings supplied. The current statement uses %d, and there are %d supplied.",
235 num_params_needed
, num_params
);
238 for (i
= 0; i
< num_params
; i
++) {
239 if (PyTuple_CheckExact(parameters
)) {
240 current_param
= PyTuple_GET_ITEM(parameters
, i
);
241 Py_XINCREF(current_param
);
242 } else if (PyList_CheckExact(parameters
)) {
243 current_param
= PyList_GET_ITEM(parameters
, i
);
244 Py_XINCREF(current_param
);
246 current_param
= PySequence_GetItem(parameters
, i
);
248 if (!current_param
) {
252 if (!_need_adapt(current_param
)) {
253 adapted
= current_param
;
255 adapted
= pysqlite_microprotocols_adapt(current_param
, (PyObject
*)&pysqlite_PrepareProtocolType
, NULL
);
257 Py_DECREF(current_param
);
260 adapted
= current_param
;
264 rc
= pysqlite_statement_bind_parameter(self
, i
+ 1, adapted
, allow_8bit_chars
);
267 if (rc
!= SQLITE_OK
) {
268 if (!PyErr_Occurred()) {
269 PyErr_Format(pysqlite_InterfaceError
, "Error binding parameter %d - probably unsupported type.", i
);
274 } else if (PyDict_Check(parameters
)) {
275 /* parameters passed as dictionary */
276 for (i
= 1; i
<= num_params_needed
; i
++) {
277 Py_BEGIN_ALLOW_THREADS
278 binding_name
= sqlite3_bind_parameter_name(self
->st
, i
);
281 PyErr_Format(pysqlite_ProgrammingError
, "Binding %d has no name, but you supplied a dictionary (which has only names).", i
);
285 binding_name
++; /* skip first char (the colon) */
286 if (PyDict_CheckExact(parameters
)) {
287 current_param
= PyDict_GetItemString(parameters
, binding_name
);
288 Py_XINCREF(current_param
);
290 current_param
= PyMapping_GetItemString(parameters
, (char*)binding_name
);
292 if (!current_param
) {
293 PyErr_Format(pysqlite_ProgrammingError
, "You did not supply a value for binding %d.", i
);
297 if (!_need_adapt(current_param
)) {
298 adapted
= current_param
;
300 adapted
= pysqlite_microprotocols_adapt(current_param
, (PyObject
*)&pysqlite_PrepareProtocolType
, NULL
);
302 Py_DECREF(current_param
);
305 adapted
= current_param
;
309 rc
= pysqlite_statement_bind_parameter(self
, i
, adapted
, allow_8bit_chars
);
312 if (rc
!= SQLITE_OK
) {
313 if (!PyErr_Occurred()) {
314 PyErr_Format(pysqlite_InterfaceError
, "Error binding parameter :%s - probably unsupported type.", binding_name
);
320 PyErr_SetString(PyExc_ValueError
, "parameters are of unsupported type");
324 int pysqlite_statement_recompile(pysqlite_Statement
* self
, PyObject
* params
)
329 sqlite3_stmt
* new_st
;
331 sql_cstr
= PyString_AsString(self
->sql
);
333 Py_BEGIN_ALLOW_THREADS
334 rc
= sqlite3_prepare(self
->db
,
341 if (rc
== SQLITE_OK
) {
342 /* The efficient sqlite3_transfer_bindings is only available in SQLite
343 * version 3.2.2 or later. For older SQLite releases, that might not
344 * even define SQLITE_VERSION_NUMBER, we do it the manual way.
346 #ifdef SQLITE_VERSION_NUMBER
347 #if SQLITE_VERSION_NUMBER >= 3002002
348 /* The check for the number of parameters is necessary to not trigger a
349 * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */
350 if (sqlite3_bind_parameter_count(self
->st
) > 0) {
351 (void)sqlite3_transfer_bindings(self
->st
, new_st
);
355 statement_bind_parameters(self
, params
);
358 (void)sqlite3_finalize(self
->st
);
365 int pysqlite_statement_finalize(pysqlite_Statement
* self
)
371 Py_BEGIN_ALLOW_THREADS
372 rc
= sqlite3_finalize(self
->st
);
382 int pysqlite_statement_reset(pysqlite_Statement
* self
)
388 if (self
->in_use
&& self
->st
) {
389 Py_BEGIN_ALLOW_THREADS
390 rc
= sqlite3_reset(self
->st
);
393 if (rc
== SQLITE_OK
) {
401 void pysqlite_statement_mark_dirty(pysqlite_Statement
* self
)
406 void pysqlite_statement_dealloc(pysqlite_Statement
* self
)
411 Py_BEGIN_ALLOW_THREADS
412 rc
= sqlite3_finalize(self
->st
);
418 Py_XDECREF(self
->sql
);
420 if (self
->in_weakreflist
!= NULL
) {
421 PyObject_ClearWeakRefs((PyObject
*)self
);
424 Py_TYPE(self
)->tp_free((PyObject
*)self
);
428 * Checks if there is anything left in an SQL string after SQLite compiled it.
429 * This is used to check if somebody tried to execute more than one SQL command
430 * with one execute()/executemany() command, which the DB-API and we don't
433 * Returns 1 if there is more left than should be. 0 if ok.
435 static int pysqlite_check_remaining_sql(const char* tail
)
437 const char* pos
= tail
;
439 parse_remaining_sql_state state
= NORMAL
;
446 if (state
== NORMAL
) {
447 state
= LINECOMMENT_1
;
448 } else if (state
== LINECOMMENT_1
) {
449 state
= IN_LINECOMMENT
;
457 if (state
== IN_LINECOMMENT
) {
462 if (state
== NORMAL
) {
463 state
= COMMENTSTART_1
;
464 } else if (state
== COMMENTEND_1
) {
466 } else if (state
== COMMENTSTART_1
) {
471 if (state
== NORMAL
) {
473 } else if (state
== LINECOMMENT_1
) {
475 } else if (state
== COMMENTSTART_1
) {
477 } else if (state
== IN_COMMENT
) {
478 state
= COMMENTEND_1
;
482 if (state
== COMMENTEND_1
) {
484 } else if (state
== IN_LINECOMMENT
) {
485 } else if (state
== IN_COMMENT
) {
497 PyTypeObject pysqlite_StatementType
= {
498 PyVarObject_HEAD_INIT(NULL
, 0)
499 MODULE_NAME
".Statement", /* tp_name */
500 sizeof(pysqlite_Statement
), /* tp_basicsize */
502 (destructor
)pysqlite_statement_dealloc
, /* tp_dealloc */
508 0, /* tp_as_number */
509 0, /* tp_as_sequence */
510 0, /* tp_as_mapping */
516 0, /* tp_as_buffer */
517 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_WEAKREFS
, /* tp_flags */
521 0, /* tp_richcompare */
522 offsetof(pysqlite_Statement
, in_weakreflist
), /* tp_weaklistoffset */
530 0, /* tp_descr_get */
531 0, /* tp_descr_set */
532 0, /* tp_dictoffset */
533 (initproc
)0, /* tp_init */
539 extern int pysqlite_statement_setup_types(void)
541 pysqlite_StatementType
.tp_new
= PyType_GenericNew
;
542 return PyType_Ready(&pysqlite_StatementType
);