»Core Development>Code coverage>Modules/_sqlite/statement.c

Python code coverage for Modules/_sqlite/statement.c

#countcontent
1n/a/* statement.c - the statement type
2n/a *
3n/a * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
4n/a *
5n/a * This file is part of pysqlite.
6n/a *
7n/a * This software is provided 'as-is', without any express or implied
8n/a * warranty. In no event will the authors be held liable for any damages
9n/a * arising from the use of this software.
10n/a *
11n/a * Permission is granted to anyone to use this software for any purpose,
12n/a * including commercial applications, and to alter it and redistribute it
13n/a * freely, subject to the following restrictions:
14n/a *
15n/a * 1. The origin of this software must not be misrepresented; you must not
16n/a * claim that you wrote the original software. If you use this software
17n/a * in a product, an acknowledgment in the product documentation would be
18n/a * appreciated but is not required.
19n/a * 2. Altered source versions must be plainly marked as such, and must not be
20n/a * misrepresented as being the original software.
21n/a * 3. This notice may not be removed or altered from any source distribution.
22n/a */
23n/a
24n/a#include "statement.h"
25n/a#include "cursor.h"
26n/a#include "connection.h"
27n/a#include "microprotocols.h"
28n/a#include "prepare_protocol.h"
29n/a#include "util.h"
30n/a
31n/a/* prototypes */
32n/astatic int pysqlite_check_remaining_sql(const char* tail);
33n/a
34n/atypedef enum {
35n/a LINECOMMENT_1,
36n/a IN_LINECOMMENT,
37n/a COMMENTSTART_1,
38n/a IN_COMMENT,
39n/a COMMENTEND_1,
40n/a NORMAL
41n/a} parse_remaining_sql_state;
42n/a
43n/atypedef enum {
44n/a TYPE_LONG,
45n/a TYPE_FLOAT,
46n/a TYPE_UNICODE,
47n/a TYPE_BUFFER,
48n/a TYPE_UNKNOWN
49n/a} parameter_type;
50n/a
51n/aint pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql)
52n/a{
53n/a const char* tail;
54n/a int rc;
55n/a const char* sql_cstr;
56n/a Py_ssize_t sql_cstr_len;
57n/a const char* p;
58n/a
59n/a self->st = NULL;
60n/a self->in_use = 0;
61n/a
62n/a sql_cstr = PyUnicode_AsUTF8AndSize(sql, &sql_cstr_len);
63n/a if (sql_cstr == NULL) {
64n/a rc = PYSQLITE_SQL_WRONG_TYPE;
65n/a return rc;
66n/a }
67n/a if (strlen(sql_cstr) != (size_t)sql_cstr_len) {
68n/a PyErr_SetString(PyExc_ValueError, "the query contains a null character");
69n/a return PYSQLITE_SQL_WRONG_TYPE;
70n/a }
71n/a
72n/a self->in_weakreflist = NULL;
73n/a Py_INCREF(sql);
74n/a self->sql = sql;
75n/a
76n/a /* determine if the statement is a DDL statement */
77n/a self->is_ddl = 0;
78n/a for (p = sql_cstr; *p != 0; p++) {
79n/a switch (*p) {
80n/a case ' ':
81n/a case '\r':
82n/a case '\n':
83n/a case '\t':
84n/a continue;
85n/a }
86n/a
87n/a self->is_ddl = (PyOS_strnicmp(p, "create ", 7) == 0)
88n/a || (PyOS_strnicmp(p, "drop ", 5) == 0)
89n/a || (PyOS_strnicmp(p, "reindex ", 8) == 0);
90n/a break;
91n/a }
92n/a
93n/a Py_BEGIN_ALLOW_THREADS
94n/a rc = sqlite3_prepare(connection->db,
95n/a sql_cstr,
96n/a -1,
97n/a &self->st,
98n/a &tail);
99n/a Py_END_ALLOW_THREADS
100n/a
101n/a self->db = connection->db;
102n/a
103n/a if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
104n/a (void)sqlite3_finalize(self->st);
105n/a self->st = NULL;
106n/a rc = PYSQLITE_TOO_MUCH_SQL;
107n/a }
108n/a
109n/a return rc;
110n/a}
111n/a
112n/aint pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObject* parameter)
113n/a{
114n/a int rc = SQLITE_OK;
115n/a const char *string;
116n/a Py_ssize_t buflen;
117n/a parameter_type paramtype;
118n/a
119n/a if (parameter == Py_None) {
120n/a rc = sqlite3_bind_null(self->st, pos);
121n/a goto final;
122n/a }
123n/a
124n/a if (PyLong_CheckExact(parameter)) {
125n/a paramtype = TYPE_LONG;
126n/a } else if (PyFloat_CheckExact(parameter)) {
127n/a paramtype = TYPE_FLOAT;
128n/a } else if (PyUnicode_CheckExact(parameter)) {
129n/a paramtype = TYPE_UNICODE;
130n/a } else if (PyLong_Check(parameter)) {
131n/a paramtype = TYPE_LONG;
132n/a } else if (PyFloat_Check(parameter)) {
133n/a paramtype = TYPE_FLOAT;
134n/a } else if (PyUnicode_Check(parameter)) {
135n/a paramtype = TYPE_UNICODE;
136n/a } else if (PyObject_CheckBuffer(parameter)) {
137n/a paramtype = TYPE_BUFFER;
138n/a } else {
139n/a paramtype = TYPE_UNKNOWN;
140n/a }
141n/a
142n/a switch (paramtype) {
143n/a case TYPE_LONG: {
144n/a sqlite_int64 value = _pysqlite_long_as_int64(parameter);
145n/a if (value == -1 && PyErr_Occurred())
146n/a rc = -1;
147n/a else
148n/a rc = sqlite3_bind_int64(self->st, pos, value);
149n/a break;
150n/a }
151n/a case TYPE_FLOAT:
152n/a rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
153n/a break;
154n/a case TYPE_UNICODE:
155n/a string = PyUnicode_AsUTF8AndSize(parameter, &buflen);
156n/a if (string == NULL)
157n/a return -1;
158n/a if (buflen > INT_MAX) {
159n/a PyErr_SetString(PyExc_OverflowError,
160n/a "string longer than INT_MAX bytes");
161n/a return -1;
162n/a }
163n/a rc = sqlite3_bind_text(self->st, pos, string, (int)buflen, SQLITE_TRANSIENT);
164n/a break;
165n/a case TYPE_BUFFER: {
166n/a Py_buffer view;
167n/a if (PyObject_GetBuffer(parameter, &view, PyBUF_SIMPLE) != 0) {
168n/a PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
169n/a return -1;
170n/a }
171n/a if (view.len > INT_MAX) {
172n/a PyErr_SetString(PyExc_OverflowError,
173n/a "BLOB longer than INT_MAX bytes");
174n/a PyBuffer_Release(&view);
175n/a return -1;
176n/a }
177n/a rc = sqlite3_bind_blob(self->st, pos, view.buf, (int)view.len, SQLITE_TRANSIENT);
178n/a PyBuffer_Release(&view);
179n/a break;
180n/a }
181n/a case TYPE_UNKNOWN:
182n/a rc = -1;
183n/a }
184n/a
185n/afinal:
186n/a return rc;
187n/a}
188n/a
189n/a/* returns 0 if the object is one of Python's internal ones that don't need to be adapted */
190n/astatic int _need_adapt(PyObject* obj)
191n/a{
192n/a if (pysqlite_BaseTypeAdapted) {
193n/a return 1;
194n/a }
195n/a
196n/a if (PyLong_CheckExact(obj) || PyFloat_CheckExact(obj)
197n/a || PyUnicode_CheckExact(obj) || PyByteArray_CheckExact(obj)) {
198n/a return 0;
199n/a } else {
200n/a return 1;
201n/a }
202n/a}
203n/a
204n/avoid pysqlite_statement_bind_parameters(pysqlite_Statement* self, PyObject* parameters)
205n/a{
206n/a PyObject* current_param;
207n/a PyObject* adapted;
208n/a const char* binding_name;
209n/a int i;
210n/a int rc;
211n/a int num_params_needed;
212n/a Py_ssize_t num_params;
213n/a
214n/a Py_BEGIN_ALLOW_THREADS
215n/a num_params_needed = sqlite3_bind_parameter_count(self->st);
216n/a Py_END_ALLOW_THREADS
217n/a
218n/a if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) {
219n/a /* parameters passed as sequence */
220n/a if (PyTuple_CheckExact(parameters)) {
221n/a num_params = PyTuple_GET_SIZE(parameters);
222n/a } else if (PyList_CheckExact(parameters)) {
223n/a num_params = PyList_GET_SIZE(parameters);
224n/a } else {
225n/a num_params = PySequence_Size(parameters);
226n/a }
227n/a if (num_params != num_params_needed) {
228n/a PyErr_Format(pysqlite_ProgrammingError,
229n/a "Incorrect number of bindings supplied. The current "
230n/a "statement uses %d, and there are %zd supplied.",
231n/a num_params_needed, num_params);
232n/a return;
233n/a }
234n/a for (i = 0; i < num_params; i++) {
235n/a if (PyTuple_CheckExact(parameters)) {
236n/a current_param = PyTuple_GET_ITEM(parameters, i);
237n/a Py_XINCREF(current_param);
238n/a } else if (PyList_CheckExact(parameters)) {
239n/a current_param = PyList_GET_ITEM(parameters, i);
240n/a Py_XINCREF(current_param);
241n/a } else {
242n/a current_param = PySequence_GetItem(parameters, i);
243n/a }
244n/a if (!current_param) {
245n/a return;
246n/a }
247n/a
248n/a if (!_need_adapt(current_param)) {
249n/a adapted = current_param;
250n/a } else {
251n/a adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
252n/a if (adapted) {
253n/a Py_DECREF(current_param);
254n/a } else {
255n/a PyErr_Clear();
256n/a adapted = current_param;
257n/a }
258n/a }
259n/a
260n/a rc = pysqlite_statement_bind_parameter(self, i + 1, adapted);
261n/a Py_DECREF(adapted);
262n/a
263n/a if (rc != SQLITE_OK) {
264n/a if (!PyErr_Occurred()) {
265n/a PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
266n/a }
267n/a return;
268n/a }
269n/a }
270n/a } else if (PyDict_Check(parameters)) {
271n/a /* parameters passed as dictionary */
272n/a for (i = 1; i <= num_params_needed; i++) {
273n/a Py_BEGIN_ALLOW_THREADS
274n/a binding_name = sqlite3_bind_parameter_name(self->st, i);
275n/a Py_END_ALLOW_THREADS
276n/a if (!binding_name) {
277n/a PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
278n/a return;
279n/a }
280n/a
281n/a binding_name++; /* skip first char (the colon) */
282n/a if (PyDict_CheckExact(parameters)) {
283n/a current_param = PyDict_GetItemString(parameters, binding_name);
284n/a Py_XINCREF(current_param);
285n/a } else {
286n/a current_param = PyMapping_GetItemString(parameters, binding_name);
287n/a }
288n/a if (!current_param) {
289n/a PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
290n/a return;
291n/a }
292n/a
293n/a if (!_need_adapt(current_param)) {
294n/a adapted = current_param;
295n/a } else {
296n/a adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
297n/a if (adapted) {
298n/a Py_DECREF(current_param);
299n/a } else {
300n/a PyErr_Clear();
301n/a adapted = current_param;
302n/a }
303n/a }
304n/a
305n/a rc = pysqlite_statement_bind_parameter(self, i, adapted);
306n/a Py_DECREF(adapted);
307n/a
308n/a if (rc != SQLITE_OK) {
309n/a if (!PyErr_Occurred()) {
310n/a PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
311n/a }
312n/a return;
313n/a }
314n/a }
315n/a } else {
316n/a PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type");
317n/a }
318n/a}
319n/a
320n/aint pysqlite_statement_recompile(pysqlite_Statement* self, PyObject* params)
321n/a{
322n/a const char* tail;
323n/a int rc;
324n/a const char* sql_cstr;
325n/a Py_ssize_t sql_len;
326n/a sqlite3_stmt* new_st;
327n/a
328n/a sql_cstr = PyUnicode_AsUTF8AndSize(self->sql, &sql_len);
329n/a if (sql_cstr == NULL) {
330n/a rc = PYSQLITE_SQL_WRONG_TYPE;
331n/a return rc;
332n/a }
333n/a
334n/a Py_BEGIN_ALLOW_THREADS
335n/a rc = sqlite3_prepare(self->db,
336n/a sql_cstr,
337n/a -1,
338n/a &new_st,
339n/a &tail);
340n/a Py_END_ALLOW_THREADS
341n/a
342n/a if (rc == SQLITE_OK) {
343n/a /* The efficient sqlite3_transfer_bindings is only available in SQLite
344n/a * version 3.2.2 or later. For older SQLite releases, that might not
345n/a * even define SQLITE_VERSION_NUMBER, we do it the manual way.
346n/a */
347n/a #ifdef SQLITE_VERSION_NUMBER
348n/a #if SQLITE_VERSION_NUMBER >= 3002002
349n/a /* The check for the number of parameters is necessary to not trigger a
350n/a * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */
351n/a if (sqlite3_bind_parameter_count(self->st) > 0) {
352n/a (void)sqlite3_transfer_bindings(self->st, new_st);
353n/a }
354n/a #endif
355n/a #else
356n/a statement_bind_parameters(self, params);
357n/a #endif
358n/a
359n/a (void)sqlite3_finalize(self->st);
360n/a self->st = new_st;
361n/a }
362n/a
363n/a return rc;
364n/a}
365n/a
366n/aint pysqlite_statement_finalize(pysqlite_Statement* self)
367n/a{
368n/a int rc;
369n/a
370n/a rc = SQLITE_OK;
371n/a if (self->st) {
372n/a Py_BEGIN_ALLOW_THREADS
373n/a rc = sqlite3_finalize(self->st);
374n/a Py_END_ALLOW_THREADS
375n/a self->st = NULL;
376n/a }
377n/a
378n/a self->in_use = 0;
379n/a
380n/a return rc;
381n/a}
382n/a
383n/aint pysqlite_statement_reset(pysqlite_Statement* self)
384n/a{
385n/a int rc;
386n/a
387n/a rc = SQLITE_OK;
388n/a
389n/a if (self->in_use && self->st) {
390n/a Py_BEGIN_ALLOW_THREADS
391n/a rc = sqlite3_reset(self->st);
392n/a Py_END_ALLOW_THREADS
393n/a
394n/a if (rc == SQLITE_OK) {
395n/a self->in_use = 0;
396n/a }
397n/a }
398n/a
399n/a return rc;
400n/a}
401n/a
402n/avoid pysqlite_statement_mark_dirty(pysqlite_Statement* self)
403n/a{
404n/a self->in_use = 1;
405n/a}
406n/a
407n/avoid pysqlite_statement_dealloc(pysqlite_Statement* self)
408n/a{
409n/a if (self->st) {
410n/a Py_BEGIN_ALLOW_THREADS
411n/a sqlite3_finalize(self->st);
412n/a Py_END_ALLOW_THREADS
413n/a }
414n/a
415n/a self->st = NULL;
416n/a
417n/a Py_XDECREF(self->sql);
418n/a
419n/a if (self->in_weakreflist != NULL) {
420n/a PyObject_ClearWeakRefs((PyObject*)self);
421n/a }
422n/a
423n/a Py_TYPE(self)->tp_free((PyObject*)self);
424n/a}
425n/a
426n/a/*
427n/a * Checks if there is anything left in an SQL string after SQLite compiled it.
428n/a * This is used to check if somebody tried to execute more than one SQL command
429n/a * with one execute()/executemany() command, which the DB-API and we don't
430n/a * allow.
431n/a *
432n/a * Returns 1 if there is more left than should be. 0 if ok.
433n/a */
434n/astatic int pysqlite_check_remaining_sql(const char* tail)
435n/a{
436n/a const char* pos = tail;
437n/a
438n/a parse_remaining_sql_state state = NORMAL;
439n/a
440n/a for (;;) {
441n/a switch (*pos) {
442n/a case 0:
443n/a return 0;
444n/a case '-':
445n/a if (state == NORMAL) {
446n/a state = LINECOMMENT_1;
447n/a } else if (state == LINECOMMENT_1) {
448n/a state = IN_LINECOMMENT;
449n/a }
450n/a break;
451n/a case ' ':
452n/a case '\t':
453n/a break;
454n/a case '\n':
455n/a case 13:
456n/a if (state == IN_LINECOMMENT) {
457n/a state = NORMAL;
458n/a }
459n/a break;
460n/a case '/':
461n/a if (state == NORMAL) {
462n/a state = COMMENTSTART_1;
463n/a } else if (state == COMMENTEND_1) {
464n/a state = NORMAL;
465n/a } else if (state == COMMENTSTART_1) {
466n/a return 1;
467n/a }
468n/a break;
469n/a case '*':
470n/a if (state == NORMAL) {
471n/a return 1;
472n/a } else if (state == LINECOMMENT_1) {
473n/a return 1;
474n/a } else if (state == COMMENTSTART_1) {
475n/a state = IN_COMMENT;
476n/a } else if (state == IN_COMMENT) {
477n/a state = COMMENTEND_1;
478n/a }
479n/a break;
480n/a default:
481n/a if (state == COMMENTEND_1) {
482n/a state = IN_COMMENT;
483n/a } else if (state == IN_LINECOMMENT) {
484n/a } else if (state == IN_COMMENT) {
485n/a } else {
486n/a return 1;
487n/a }
488n/a }
489n/a
490n/a pos++;
491n/a }
492n/a
493n/a return 0;
494n/a}
495n/a
496n/aPyTypeObject pysqlite_StatementType = {
497n/a PyVarObject_HEAD_INIT(NULL, 0)
498n/a MODULE_NAME ".Statement", /* tp_name */
499n/a sizeof(pysqlite_Statement), /* tp_basicsize */
500n/a 0, /* tp_itemsize */
501n/a (destructor)pysqlite_statement_dealloc, /* tp_dealloc */
502n/a 0, /* tp_print */
503n/a 0, /* tp_getattr */
504n/a 0, /* tp_setattr */
505n/a 0, /* tp_reserved */
506n/a 0, /* tp_repr */
507n/a 0, /* tp_as_number */
508n/a 0, /* tp_as_sequence */
509n/a 0, /* tp_as_mapping */
510n/a 0, /* tp_hash */
511n/a 0, /* tp_call */
512n/a 0, /* tp_str */
513n/a 0, /* tp_getattro */
514n/a 0, /* tp_setattro */
515n/a 0, /* tp_as_buffer */
516n/a Py_TPFLAGS_DEFAULT, /* tp_flags */
517n/a 0, /* tp_doc */
518n/a 0, /* tp_traverse */
519n/a 0, /* tp_clear */
520n/a 0, /* tp_richcompare */
521n/a offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
522n/a 0, /* tp_iter */
523n/a 0, /* tp_iternext */
524n/a 0, /* tp_methods */
525n/a 0, /* tp_members */
526n/a 0, /* tp_getset */
527n/a 0, /* tp_base */
528n/a 0, /* tp_dict */
529n/a 0, /* tp_descr_get */
530n/a 0, /* tp_descr_set */
531n/a 0, /* tp_dictoffset */
532n/a (initproc)0, /* tp_init */
533n/a 0, /* tp_alloc */
534n/a 0, /* tp_new */
535n/a 0 /* tp_free */
536n/a};
537n/a
538n/aextern int pysqlite_statement_setup_types(void)
539n/a{
540n/a pysqlite_StatementType.tp_new = PyType_GenericNew;
541n/a return PyType_Ready(&pysqlite_StatementType);
542n/a}