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

Python code coverage for Modules/_sqlite/cursor.c

#countcontent
1n/a/* cursor.c - the cursor type
2n/a *
3n/a * Copyright (C) 2004-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 "cursor.h"
25n/a#include "module.h"
26n/a#include "util.h"
27n/a
28n/aPyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
29n/a
30n/astatic const char errmsg_fetch_across_rollback[] = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
31n/a
32n/astatic int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
33n/a{
34n/a pysqlite_Connection* connection;
35n/a
36n/a if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection))
37n/a {
38n/a return -1;
39n/a }
40n/a
41n/a Py_INCREF(connection);
42n/a self->connection = connection;
43n/a self->statement = NULL;
44n/a self->next_row = NULL;
45n/a self->in_weakreflist = NULL;
46n/a
47n/a self->row_cast_map = PyList_New(0);
48n/a if (!self->row_cast_map) {
49n/a return -1;
50n/a }
51n/a
52n/a Py_INCREF(Py_None);
53n/a self->description = Py_None;
54n/a
55n/a Py_INCREF(Py_None);
56n/a self->lastrowid= Py_None;
57n/a
58n/a self->arraysize = 1;
59n/a self->closed = 0;
60n/a self->reset = 0;
61n/a
62n/a self->rowcount = -1L;
63n/a
64n/a Py_INCREF(Py_None);
65n/a self->row_factory = Py_None;
66n/a
67n/a if (!pysqlite_check_thread(self->connection)) {
68n/a return -1;
69n/a }
70n/a
71n/a if (!pysqlite_connection_register_cursor(connection, (PyObject*)self)) {
72n/a return -1;
73n/a }
74n/a
75n/a self->initialized = 1;
76n/a
77n/a return 0;
78n/a}
79n/a
80n/astatic void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
81n/a{
82n/a /* Reset the statement if the user has not closed the cursor */
83n/a if (self->statement) {
84n/a pysqlite_statement_reset(self->statement);
85n/a Py_DECREF(self->statement);
86n/a }
87n/a
88n/a Py_XDECREF(self->connection);
89n/a Py_XDECREF(self->row_cast_map);
90n/a Py_XDECREF(self->description);
91n/a Py_XDECREF(self->lastrowid);
92n/a Py_XDECREF(self->row_factory);
93n/a Py_XDECREF(self->next_row);
94n/a
95n/a if (self->in_weakreflist != NULL) {
96n/a PyObject_ClearWeakRefs((PyObject*)self);
97n/a }
98n/a
99n/a Py_TYPE(self)->tp_free((PyObject*)self);
100n/a}
101n/a
102n/aPyObject* _pysqlite_get_converter(PyObject* key)
103n/a{
104n/a PyObject* upcase_key;
105n/a PyObject* retval;
106n/a _Py_IDENTIFIER(upper);
107n/a
108n/a upcase_key = _PyObject_CallMethodId(key, &PyId_upper, NULL);
109n/a if (!upcase_key) {
110n/a return NULL;
111n/a }
112n/a
113n/a retval = PyDict_GetItem(converters, upcase_key);
114n/a Py_DECREF(upcase_key);
115n/a
116n/a return retval;
117n/a}
118n/a
119n/aint pysqlite_build_row_cast_map(pysqlite_Cursor* self)
120n/a{
121n/a int i;
122n/a const char* type_start = (const char*)-1;
123n/a const char* pos;
124n/a
125n/a const char* colname;
126n/a const char* decltype;
127n/a PyObject* py_decltype;
128n/a PyObject* converter;
129n/a PyObject* key;
130n/a
131n/a if (!self->connection->detect_types) {
132n/a return 0;
133n/a }
134n/a
135n/a Py_XSETREF(self->row_cast_map, PyList_New(0));
136n/a
137n/a for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
138n/a converter = NULL;
139n/a
140n/a if (self->connection->detect_types & PARSE_COLNAMES) {
141n/a colname = sqlite3_column_name(self->statement->st, i);
142n/a if (colname) {
143n/a for (pos = colname; *pos != 0; pos++) {
144n/a if (*pos == '[') {
145n/a type_start = pos + 1;
146n/a } else if (*pos == ']' && type_start != (const char*)-1) {
147n/a key = PyUnicode_FromStringAndSize(type_start, pos - type_start);
148n/a if (!key) {
149n/a /* creating a string failed, but it is too complicated
150n/a * to propagate the error here, we just assume there is
151n/a * no converter and proceed */
152n/a break;
153n/a }
154n/a
155n/a converter = _pysqlite_get_converter(key);
156n/a Py_DECREF(key);
157n/a break;
158n/a }
159n/a }
160n/a }
161n/a }
162n/a
163n/a if (!converter && self->connection->detect_types & PARSE_DECLTYPES) {
164n/a decltype = sqlite3_column_decltype(self->statement->st, i);
165n/a if (decltype) {
166n/a for (pos = decltype;;pos++) {
167n/a /* Converter names are split at '(' and blanks.
168n/a * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
169n/a * 'NUMBER(10)' to be treated as 'NUMBER', for example.
170n/a * In other words, it will work as people expect it to work.*/
171n/a if (*pos == ' ' || *pos == '(' || *pos == 0) {
172n/a py_decltype = PyUnicode_FromStringAndSize(decltype, pos - decltype);
173n/a if (!py_decltype) {
174n/a return -1;
175n/a }
176n/a break;
177n/a }
178n/a }
179n/a
180n/a converter = _pysqlite_get_converter(py_decltype);
181n/a Py_DECREF(py_decltype);
182n/a }
183n/a }
184n/a
185n/a if (!converter) {
186n/a converter = Py_None;
187n/a }
188n/a
189n/a if (PyList_Append(self->row_cast_map, converter) != 0) {
190n/a if (converter != Py_None) {
191n/a Py_DECREF(converter);
192n/a }
193n/a Py_CLEAR(self->row_cast_map);
194n/a
195n/a return -1;
196n/a }
197n/a }
198n/a
199n/a return 0;
200n/a}
201n/a
202n/aPyObject* _pysqlite_build_column_name(const char* colname)
203n/a{
204n/a const char* pos;
205n/a
206n/a if (!colname) {
207n/a Py_RETURN_NONE;
208n/a }
209n/a
210n/a for (pos = colname;; pos++) {
211n/a if (*pos == 0 || *pos == '[') {
212n/a if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) {
213n/a pos--;
214n/a }
215n/a return PyUnicode_FromStringAndSize(colname, pos - colname);
216n/a }
217n/a }
218n/a}
219n/a
220n/a/*
221n/a * Returns a row from the currently active SQLite statement
222n/a *
223n/a * Precondidition:
224n/a * - sqlite3_step() has been called before and it returned SQLITE_ROW.
225n/a */
226n/aPyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
227n/a{
228n/a int i, numcols;
229n/a PyObject* row;
230n/a PyObject* item = NULL;
231n/a int coltype;
232n/a PyObject* converter;
233n/a PyObject* converted;
234n/a Py_ssize_t nbytes;
235n/a PyObject* buffer;
236n/a const char* val_str;
237n/a char buf[200];
238n/a const char* colname;
239n/a PyObject* buf_bytes;
240n/a PyObject* error_obj;
241n/a
242n/a if (self->reset) {
243n/a PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
244n/a return NULL;
245n/a }
246n/a
247n/a Py_BEGIN_ALLOW_THREADS
248n/a numcols = sqlite3_data_count(self->statement->st);
249n/a Py_END_ALLOW_THREADS
250n/a
251n/a row = PyTuple_New(numcols);
252n/a if (!row)
253n/a return NULL;
254n/a
255n/a for (i = 0; i < numcols; i++) {
256n/a if (self->connection->detect_types) {
257n/a converter = PyList_GetItem(self->row_cast_map, i);
258n/a if (!converter) {
259n/a converter = Py_None;
260n/a }
261n/a } else {
262n/a converter = Py_None;
263n/a }
264n/a
265n/a if (converter != Py_None) {
266n/a nbytes = sqlite3_column_bytes(self->statement->st, i);
267n/a val_str = (const char*)sqlite3_column_blob(self->statement->st, i);
268n/a if (!val_str) {
269n/a Py_INCREF(Py_None);
270n/a converted = Py_None;
271n/a } else {
272n/a item = PyBytes_FromStringAndSize(val_str, nbytes);
273n/a if (!item)
274n/a goto error;
275n/a converted = PyObject_CallFunction(converter, "O", item);
276n/a Py_DECREF(item);
277n/a if (!converted)
278n/a break;
279n/a }
280n/a } else {
281n/a Py_BEGIN_ALLOW_THREADS
282n/a coltype = sqlite3_column_type(self->statement->st, i);
283n/a Py_END_ALLOW_THREADS
284n/a if (coltype == SQLITE_NULL) {
285n/a Py_INCREF(Py_None);
286n/a converted = Py_None;
287n/a } else if (coltype == SQLITE_INTEGER) {
288n/a converted = _pysqlite_long_from_int64(sqlite3_column_int64(self->statement->st, i));
289n/a } else if (coltype == SQLITE_FLOAT) {
290n/a converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
291n/a } else if (coltype == SQLITE_TEXT) {
292n/a val_str = (const char*)sqlite3_column_text(self->statement->st, i);
293n/a nbytes = sqlite3_column_bytes(self->statement->st, i);
294n/a if (self->connection->text_factory == (PyObject*)&PyUnicode_Type) {
295n/a converted = PyUnicode_FromStringAndSize(val_str, nbytes);
296n/a if (!converted) {
297n/a PyErr_Clear();
298n/a colname = sqlite3_column_name(self->statement->st, i);
299n/a if (!colname) {
300n/a colname = "<unknown column name>";
301n/a }
302n/a PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
303n/a colname , val_str);
304n/a buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
305n/a if (!buf_bytes) {
306n/a PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
307n/a } else {
308n/a error_obj = PyUnicode_FromEncodedObject(buf_bytes, "ascii", "replace");
309n/a if (!error_obj) {
310n/a PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
311n/a } else {
312n/a PyErr_SetObject(pysqlite_OperationalError, error_obj);
313n/a Py_DECREF(error_obj);
314n/a }
315n/a Py_DECREF(buf_bytes);
316n/a }
317n/a }
318n/a } else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
319n/a converted = PyBytes_FromStringAndSize(val_str, nbytes);
320n/a } else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
321n/a converted = PyByteArray_FromStringAndSize(val_str, nbytes);
322n/a } else {
323n/a converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes);
324n/a }
325n/a } else {
326n/a /* coltype == SQLITE_BLOB */
327n/a nbytes = sqlite3_column_bytes(self->statement->st, i);
328n/a buffer = PyBytes_FromStringAndSize(
329n/a sqlite3_column_blob(self->statement->st, i), nbytes);
330n/a if (!buffer)
331n/a break;
332n/a converted = buffer;
333n/a }
334n/a }
335n/a
336n/a if (converted) {
337n/a PyTuple_SetItem(row, i, converted);
338n/a } else {
339n/a Py_INCREF(Py_None);
340n/a PyTuple_SetItem(row, i, Py_None);
341n/a }
342n/a }
343n/a
344n/a if (PyErr_Occurred())
345n/a goto error;
346n/a
347n/a return row;
348n/a
349n/aerror:
350n/a Py_DECREF(row);
351n/a return NULL;
352n/a}
353n/a
354n/a/*
355n/a * Checks if a cursor object is usable.
356n/a *
357n/a * 0 => error; 1 => ok
358n/a */
359n/astatic int check_cursor(pysqlite_Cursor* cur)
360n/a{
361n/a if (!cur->initialized) {
362n/a PyErr_SetString(pysqlite_ProgrammingError, "Base Cursor.__init__ not called.");
363n/a return 0;
364n/a }
365n/a
366n/a if (cur->closed) {
367n/a PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed cursor.");
368n/a return 0;
369n/a }
370n/a
371n/a if (cur->locked) {
372n/a PyErr_SetString(pysqlite_ProgrammingError, "Recursive use of cursors not allowed.");
373n/a return 0;
374n/a }
375n/a
376n/a return pysqlite_check_thread(cur->connection) && pysqlite_check_connection(cur->connection);
377n/a}
378n/a
379n/aPyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
380n/a{
381n/a PyObject* operation;
382n/a const char* operation_cstr;
383n/a Py_ssize_t operation_len;
384n/a PyObject* parameters_list = NULL;
385n/a PyObject* parameters_iter = NULL;
386n/a PyObject* parameters = NULL;
387n/a int i;
388n/a int rc;
389n/a PyObject* func_args;
390n/a PyObject* result;
391n/a int numcols;
392n/a PyObject* descriptor;
393n/a PyObject* second_argument = NULL;
394n/a sqlite_int64 lastrowid;
395n/a
396n/a if (!check_cursor(self)) {
397n/a goto error;
398n/a }
399n/a
400n/a self->locked = 1;
401n/a self->reset = 0;
402n/a
403n/a Py_CLEAR(self->next_row);
404n/a
405n/a if (multiple) {
406n/a /* executemany() */
407n/a if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) {
408n/a goto error;
409n/a }
410n/a
411n/a if (!PyUnicode_Check(operation)) {
412n/a PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
413n/a goto error;
414n/a }
415n/a
416n/a if (PyIter_Check(second_argument)) {
417n/a /* iterator */
418n/a Py_INCREF(second_argument);
419n/a parameters_iter = second_argument;
420n/a } else {
421n/a /* sequence */
422n/a parameters_iter = PyObject_GetIter(second_argument);
423n/a if (!parameters_iter) {
424n/a goto error;
425n/a }
426n/a }
427n/a } else {
428n/a /* execute() */
429n/a if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) {
430n/a goto error;
431n/a }
432n/a
433n/a if (!PyUnicode_Check(operation)) {
434n/a PyErr_SetString(PyExc_ValueError, "operation parameter must be str");
435n/a goto error;
436n/a }
437n/a
438n/a parameters_list = PyList_New(0);
439n/a if (!parameters_list) {
440n/a goto error;
441n/a }
442n/a
443n/a if (second_argument == NULL) {
444n/a second_argument = PyTuple_New(0);
445n/a if (!second_argument) {
446n/a goto error;
447n/a }
448n/a } else {
449n/a Py_INCREF(second_argument);
450n/a }
451n/a if (PyList_Append(parameters_list, second_argument) != 0) {
452n/a Py_DECREF(second_argument);
453n/a goto error;
454n/a }
455n/a Py_DECREF(second_argument);
456n/a
457n/a parameters_iter = PyObject_GetIter(parameters_list);
458n/a if (!parameters_iter) {
459n/a goto error;
460n/a }
461n/a }
462n/a
463n/a if (self->statement != NULL) {
464n/a /* There is an active statement */
465n/a pysqlite_statement_reset(self->statement);
466n/a }
467n/a
468n/a operation_cstr = PyUnicode_AsUTF8AndSize(operation, &operation_len);
469n/a if (operation_cstr == NULL)
470n/a goto error;
471n/a
472n/a /* reset description and rowcount */
473n/a Py_INCREF(Py_None);
474n/a Py_SETREF(self->description, Py_None);
475n/a self->rowcount = 0L;
476n/a
477n/a func_args = PyTuple_New(1);
478n/a if (!func_args) {
479n/a goto error;
480n/a }
481n/a Py_INCREF(operation);
482n/a if (PyTuple_SetItem(func_args, 0, operation) != 0) {
483n/a goto error;
484n/a }
485n/a
486n/a if (self->statement) {
487n/a (void)pysqlite_statement_reset(self->statement);
488n/a }
489n/a
490n/a Py_XSETREF(self->statement,
491n/a (pysqlite_Statement *)pysqlite_cache_get(self->connection->statement_cache, func_args));
492n/a Py_DECREF(func_args);
493n/a
494n/a if (!self->statement) {
495n/a goto error;
496n/a }
497n/a
498n/a if (self->statement->in_use) {
499n/a Py_SETREF(self->statement,
500n/a PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
501n/a if (!self->statement) {
502n/a goto error;
503n/a }
504n/a rc = pysqlite_statement_create(self->statement, self->connection, operation);
505n/a if (rc != SQLITE_OK) {
506n/a Py_CLEAR(self->statement);
507n/a goto error;
508n/a }
509n/a }
510n/a
511n/a pysqlite_statement_reset(self->statement);
512n/a pysqlite_statement_mark_dirty(self->statement);
513n/a
514n/a /* For backwards compatibility reasons, do not start a transaction if a
515n/a DDL statement is encountered. If anybody wants transactional DDL,
516n/a they can issue a BEGIN statement manually. */
517n/a if (self->connection->begin_statement && !sqlite3_stmt_readonly(self->statement->st) && !self->statement->is_ddl) {
518n/a if (sqlite3_get_autocommit(self->connection->db)) {
519n/a result = _pysqlite_connection_begin(self->connection);
520n/a if (!result) {
521n/a goto error;
522n/a }
523n/a Py_DECREF(result);
524n/a }
525n/a }
526n/a
527n/a while (1) {
528n/a parameters = PyIter_Next(parameters_iter);
529n/a if (!parameters) {
530n/a break;
531n/a }
532n/a
533n/a pysqlite_statement_mark_dirty(self->statement);
534n/a
535n/a pysqlite_statement_bind_parameters(self->statement, parameters);
536n/a if (PyErr_Occurred()) {
537n/a goto error;
538n/a }
539n/a
540n/a /* Keep trying the SQL statement until the schema stops changing. */
541n/a while (1) {
542n/a /* Actually execute the SQL statement. */
543n/a rc = pysqlite_step(self->statement->st, self->connection);
544n/a if (PyErr_Occurred()) {
545n/a (void)pysqlite_statement_reset(self->statement);
546n/a goto error;
547n/a }
548n/a if (rc == SQLITE_DONE || rc == SQLITE_ROW) {
549n/a /* If it worked, let's get out of the loop */
550n/a break;
551n/a }
552n/a /* Something went wrong. Re-set the statement and try again. */
553n/a rc = pysqlite_statement_reset(self->statement);
554n/a if (rc == SQLITE_SCHEMA) {
555n/a /* If this was a result of the schema changing, let's try
556n/a again. */
557n/a rc = pysqlite_statement_recompile(self->statement, parameters);
558n/a if (rc == SQLITE_OK) {
559n/a continue;
560n/a } else {
561n/a /* If the database gave us an error, promote it to Python. */
562n/a (void)pysqlite_statement_reset(self->statement);
563n/a _pysqlite_seterror(self->connection->db, NULL);
564n/a goto error;
565n/a }
566n/a } else {
567n/a if (PyErr_Occurred()) {
568n/a /* there was an error that occurred in a user-defined callback */
569n/a if (_enable_callback_tracebacks) {
570n/a PyErr_Print();
571n/a } else {
572n/a PyErr_Clear();
573n/a }
574n/a }
575n/a (void)pysqlite_statement_reset(self->statement);
576n/a _pysqlite_seterror(self->connection->db, NULL);
577n/a goto error;
578n/a }
579n/a }
580n/a
581n/a if (pysqlite_build_row_cast_map(self) != 0) {
582n/a PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
583n/a goto error;
584n/a }
585n/a
586n/a if (rc == SQLITE_ROW || rc == SQLITE_DONE) {
587n/a Py_BEGIN_ALLOW_THREADS
588n/a numcols = sqlite3_column_count(self->statement->st);
589n/a Py_END_ALLOW_THREADS
590n/a if (self->description == Py_None && numcols > 0) {
591n/a Py_SETREF(self->description, PyTuple_New(numcols));
592n/a if (!self->description) {
593n/a goto error;
594n/a }
595n/a for (i = 0; i < numcols; i++) {
596n/a descriptor = PyTuple_New(7);
597n/a if (!descriptor) {
598n/a goto error;
599n/a }
600n/a PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
601n/a Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
602n/a Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
603n/a Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
604n/a Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
605n/a Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None);
606n/a Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None);
607n/a PyTuple_SetItem(self->description, i, descriptor);
608n/a }
609n/a }
610n/a }
611n/a
612n/a if (!sqlite3_stmt_readonly(self->statement->st)) {
613n/a self->rowcount += (long)sqlite3_changes(self->connection->db);
614n/a } else {
615n/a self->rowcount= -1L;
616n/a }
617n/a
618n/a if (!multiple) {
619n/a Py_DECREF(self->lastrowid);
620n/a Py_BEGIN_ALLOW_THREADS
621n/a lastrowid = sqlite3_last_insert_rowid(self->connection->db);
622n/a Py_END_ALLOW_THREADS
623n/a self->lastrowid = _pysqlite_long_from_int64(lastrowid);
624n/a }
625n/a
626n/a if (rc == SQLITE_ROW) {
627n/a if (multiple) {
628n/a PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements.");
629n/a goto error;
630n/a }
631n/a
632n/a self->next_row = _pysqlite_fetch_one_row(self);
633n/a if (self->next_row == NULL)
634n/a goto error;
635n/a } else if (rc == SQLITE_DONE && !multiple) {
636n/a pysqlite_statement_reset(self->statement);
637n/a Py_CLEAR(self->statement);
638n/a }
639n/a
640n/a if (multiple) {
641n/a pysqlite_statement_reset(self->statement);
642n/a }
643n/a Py_XDECREF(parameters);
644n/a }
645n/a
646n/aerror:
647n/a Py_XDECREF(parameters);
648n/a Py_XDECREF(parameters_iter);
649n/a Py_XDECREF(parameters_list);
650n/a
651n/a self->locked = 0;
652n/a
653n/a if (PyErr_Occurred()) {
654n/a self->rowcount = -1L;
655n/a return NULL;
656n/a } else {
657n/a Py_INCREF(self);
658n/a return (PyObject*)self;
659n/a }
660n/a}
661n/a
662n/aPyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
663n/a{
664n/a return _pysqlite_query_execute(self, 0, args);
665n/a}
666n/a
667n/aPyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args)
668n/a{
669n/a return _pysqlite_query_execute(self, 1, args);
670n/a}
671n/a
672n/aPyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args)
673n/a{
674n/a PyObject* script_obj;
675n/a PyObject* script_str = NULL;
676n/a const char* script_cstr;
677n/a sqlite3_stmt* statement;
678n/a int rc;
679n/a PyObject* result;
680n/a
681n/a if (!PyArg_ParseTuple(args, "O", &script_obj)) {
682n/a return NULL;
683n/a }
684n/a
685n/a if (!check_cursor(self)) {
686n/a return NULL;
687n/a }
688n/a
689n/a self->reset = 0;
690n/a
691n/a if (PyUnicode_Check(script_obj)) {
692n/a script_cstr = PyUnicode_AsUTF8(script_obj);
693n/a if (!script_cstr) {
694n/a return NULL;
695n/a }
696n/a } else {
697n/a PyErr_SetString(PyExc_ValueError, "script argument must be unicode.");
698n/a return NULL;
699n/a }
700n/a
701n/a /* commit first */
702n/a result = pysqlite_connection_commit(self->connection, NULL);
703n/a if (!result) {
704n/a goto error;
705n/a }
706n/a Py_DECREF(result);
707n/a
708n/a while (1) {
709n/a Py_BEGIN_ALLOW_THREADS
710n/a rc = sqlite3_prepare(self->connection->db,
711n/a script_cstr,
712n/a -1,
713n/a &statement,
714n/a &script_cstr);
715n/a Py_END_ALLOW_THREADS
716n/a if (rc != SQLITE_OK) {
717n/a _pysqlite_seterror(self->connection->db, NULL);
718n/a goto error;
719n/a }
720n/a
721n/a /* execute statement, and ignore results of SELECT statements */
722n/a rc = SQLITE_ROW;
723n/a while (rc == SQLITE_ROW) {
724n/a rc = pysqlite_step(statement, self->connection);
725n/a if (PyErr_Occurred()) {
726n/a (void)sqlite3_finalize(statement);
727n/a goto error;
728n/a }
729n/a }
730n/a
731n/a if (rc != SQLITE_DONE) {
732n/a (void)sqlite3_finalize(statement);
733n/a _pysqlite_seterror(self->connection->db, NULL);
734n/a goto error;
735n/a }
736n/a
737n/a rc = sqlite3_finalize(statement);
738n/a if (rc != SQLITE_OK) {
739n/a _pysqlite_seterror(self->connection->db, NULL);
740n/a goto error;
741n/a }
742n/a
743n/a if (*script_cstr == (char)0) {
744n/a break;
745n/a }
746n/a }
747n/a
748n/aerror:
749n/a Py_XDECREF(script_str);
750n/a
751n/a if (PyErr_Occurred()) {
752n/a return NULL;
753n/a } else {
754n/a Py_INCREF(self);
755n/a return (PyObject*)self;
756n/a }
757n/a}
758n/a
759n/aPyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self)
760n/a{
761n/a Py_INCREF(self);
762n/a return (PyObject*)self;
763n/a}
764n/a
765n/aPyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
766n/a{
767n/a PyObject* next_row_tuple;
768n/a PyObject* next_row;
769n/a int rc;
770n/a
771n/a if (!check_cursor(self)) {
772n/a return NULL;
773n/a }
774n/a
775n/a if (self->reset) {
776n/a PyErr_SetString(pysqlite_InterfaceError, errmsg_fetch_across_rollback);
777n/a return NULL;
778n/a }
779n/a
780n/a if (!self->next_row) {
781n/a if (self->statement) {
782n/a (void)pysqlite_statement_reset(self->statement);
783n/a Py_CLEAR(self->statement);
784n/a }
785n/a return NULL;
786n/a }
787n/a
788n/a next_row_tuple = self->next_row;
789n/a assert(next_row_tuple != NULL);
790n/a self->next_row = NULL;
791n/a
792n/a if (self->row_factory != Py_None) {
793n/a next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple);
794n/a if (next_row == NULL) {
795n/a self->next_row = next_row_tuple;
796n/a return NULL;
797n/a }
798n/a Py_DECREF(next_row_tuple);
799n/a } else {
800n/a next_row = next_row_tuple;
801n/a }
802n/a
803n/a if (self->statement) {
804n/a rc = pysqlite_step(self->statement->st, self->connection);
805n/a if (PyErr_Occurred()) {
806n/a (void)pysqlite_statement_reset(self->statement);
807n/a Py_DECREF(next_row);
808n/a return NULL;
809n/a }
810n/a if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
811n/a (void)pysqlite_statement_reset(self->statement);
812n/a Py_DECREF(next_row);
813n/a _pysqlite_seterror(self->connection->db, NULL);
814n/a return NULL;
815n/a }
816n/a
817n/a if (rc == SQLITE_ROW) {
818n/a self->next_row = _pysqlite_fetch_one_row(self);
819n/a if (self->next_row == NULL) {
820n/a (void)pysqlite_statement_reset(self->statement);
821n/a return NULL;
822n/a }
823n/a }
824n/a }
825n/a
826n/a return next_row;
827n/a}
828n/a
829n/aPyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
830n/a{
831n/a PyObject* row;
832n/a
833n/a row = pysqlite_cursor_iternext(self);
834n/a if (!row && !PyErr_Occurred()) {
835n/a Py_RETURN_NONE;
836n/a }
837n/a
838n/a return row;
839n/a}
840n/a
841n/aPyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs)
842n/a{
843n/a static char *kwlist[] = {"size", NULL, NULL};
844n/a
845n/a PyObject* row;
846n/a PyObject* list;
847n/a int maxrows = self->arraysize;
848n/a int counter = 0;
849n/a
850n/a if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:fetchmany", kwlist, &maxrows)) {
851n/a return NULL;
852n/a }
853n/a
854n/a list = PyList_New(0);
855n/a if (!list) {
856n/a return NULL;
857n/a }
858n/a
859n/a /* just make sure we enter the loop */
860n/a row = Py_None;
861n/a
862n/a while (row) {
863n/a row = pysqlite_cursor_iternext(self);
864n/a if (row) {
865n/a PyList_Append(list, row);
866n/a Py_DECREF(row);
867n/a } else {
868n/a break;
869n/a }
870n/a
871n/a if (++counter == maxrows) {
872n/a break;
873n/a }
874n/a }
875n/a
876n/a if (PyErr_Occurred()) {
877n/a Py_DECREF(list);
878n/a return NULL;
879n/a } else {
880n/a return list;
881n/a }
882n/a}
883n/a
884n/aPyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
885n/a{
886n/a PyObject* row;
887n/a PyObject* list;
888n/a
889n/a list = PyList_New(0);
890n/a if (!list) {
891n/a return NULL;
892n/a }
893n/a
894n/a /* just make sure we enter the loop */
895n/a row = (PyObject*)Py_None;
896n/a
897n/a while (row) {
898n/a row = pysqlite_cursor_iternext(self);
899n/a if (row) {
900n/a PyList_Append(list, row);
901n/a Py_DECREF(row);
902n/a }
903n/a }
904n/a
905n/a if (PyErr_Occurred()) {
906n/a Py_DECREF(list);
907n/a return NULL;
908n/a } else {
909n/a return list;
910n/a }
911n/a}
912n/a
913n/aPyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
914n/a{
915n/a /* don't care, return None */
916n/a Py_RETURN_NONE;
917n/a}
918n/a
919n/aPyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
920n/a{
921n/a if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) {
922n/a return NULL;
923n/a }
924n/a
925n/a if (self->statement) {
926n/a (void)pysqlite_statement_reset(self->statement);
927n/a Py_CLEAR(self->statement);
928n/a }
929n/a
930n/a self->closed = 1;
931n/a
932n/a Py_RETURN_NONE;
933n/a}
934n/a
935n/astatic PyMethodDef cursor_methods[] = {
936n/a {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS,
937n/a PyDoc_STR("Executes a SQL statement.")},
938n/a {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS,
939n/a PyDoc_STR("Repeatedly executes a SQL statement.")},
940n/a {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS,
941n/a PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
942n/a {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS,
943n/a PyDoc_STR("Fetches one row from the resultset.")},
944n/a {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS|METH_KEYWORDS,
945n/a PyDoc_STR("Fetches several rows from the resultset.")},
946n/a {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS,
947n/a PyDoc_STR("Fetches all rows from the resultset.")},
948n/a {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS,
949n/a PyDoc_STR("Closes the cursor.")},
950n/a {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS,
951n/a PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
952n/a {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS,
953n/a PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
954n/a {NULL, NULL}
955n/a};
956n/a
957n/astatic struct PyMemberDef cursor_members[] =
958n/a{
959n/a {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
960n/a {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
961n/a {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
962n/a {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
963n/a {"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
964n/a {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
965n/a {NULL}
966n/a};
967n/a
968n/astatic const char cursor_doc[] =
969n/aPyDoc_STR("SQLite database cursor class.");
970n/a
971n/aPyTypeObject pysqlite_CursorType = {
972n/a PyVarObject_HEAD_INIT(NULL, 0)
973n/a MODULE_NAME ".Cursor", /* tp_name */
974n/a sizeof(pysqlite_Cursor), /* tp_basicsize */
975n/a 0, /* tp_itemsize */
976n/a (destructor)pysqlite_cursor_dealloc, /* tp_dealloc */
977n/a 0, /* tp_print */
978n/a 0, /* tp_getattr */
979n/a 0, /* tp_setattr */
980n/a 0, /* tp_reserved */
981n/a 0, /* tp_repr */
982n/a 0, /* tp_as_number */
983n/a 0, /* tp_as_sequence */
984n/a 0, /* tp_as_mapping */
985n/a 0, /* tp_hash */
986n/a 0, /* tp_call */
987n/a 0, /* tp_str */
988n/a 0, /* tp_getattro */
989n/a 0, /* tp_setattro */
990n/a 0, /* tp_as_buffer */
991n/a Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
992n/a cursor_doc, /* tp_doc */
993n/a 0, /* tp_traverse */
994n/a 0, /* tp_clear */
995n/a 0, /* tp_richcompare */
996n/a offsetof(pysqlite_Cursor, in_weakreflist), /* tp_weaklistoffset */
997n/a (getiterfunc)pysqlite_cursor_getiter, /* tp_iter */
998n/a (iternextfunc)pysqlite_cursor_iternext, /* tp_iternext */
999n/a cursor_methods, /* tp_methods */
1000n/a cursor_members, /* tp_members */
1001n/a 0, /* tp_getset */
1002n/a 0, /* tp_base */
1003n/a 0, /* tp_dict */
1004n/a 0, /* tp_descr_get */
1005n/a 0, /* tp_descr_set */
1006n/a 0, /* tp_dictoffset */
1007n/a (initproc)pysqlite_cursor_init, /* tp_init */
1008n/a 0, /* tp_alloc */
1009n/a 0, /* tp_new */
1010n/a 0 /* tp_free */
1011n/a};
1012n/a
1013n/aextern int pysqlite_cursor_setup_types(void)
1014n/a{
1015n/a pysqlite_CursorType.tp_new = PyType_GenericNew;
1016n/a return PyType_Ready(&pysqlite_CursorType);
1017n/a}