1 /* cursor.c - the cursor type
3 * Copyright (C) 2004-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.
27 #include "sqlitecompat.h"
29 /* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */
31 #define INT32_MIN (-2147483647 - 1)
34 #define INT32_MAX 2147483647
37 PyObject
* pysqlite_cursor_iternext(pysqlite_Cursor
* self
);
39 static pysqlite_StatementKind
detect_statement_type(char* statement
)
46 /* skip over whitepace */
47 while (*src
== '\r' || *src
== '\n' || *src
== ' ' || *src
== '\t') {
52 return STATEMENT_INVALID
;
56 while (isalpha(*src
) && dst
- buf
< sizeof(buf
) - 2) {
57 *dst
++ = tolower(*src
++);
62 if (!strcmp(buf
, "select")) {
63 return STATEMENT_SELECT
;
64 } else if (!strcmp(buf
, "insert")) {
65 return STATEMENT_INSERT
;
66 } else if (!strcmp(buf
, "update")) {
67 return STATEMENT_UPDATE
;
68 } else if (!strcmp(buf
, "delete")) {
69 return STATEMENT_DELETE
;
70 } else if (!strcmp(buf
, "replace")) {
71 return STATEMENT_REPLACE
;
73 return STATEMENT_OTHER
;
77 int pysqlite_cursor_init(pysqlite_Cursor
* self
, PyObject
* args
, PyObject
* kwargs
)
79 pysqlite_Connection
* connection
;
81 if (!PyArg_ParseTuple(args
, "O!", &pysqlite_ConnectionType
, &connection
))
86 Py_INCREF(connection
);
87 self
->connection
= connection
;
88 self
->statement
= NULL
;
89 self
->next_row
= NULL
;
91 self
->row_cast_map
= PyList_New(0);
92 if (!self
->row_cast_map
) {
97 self
->description
= Py_None
;
100 self
->lastrowid
= Py_None
;
104 self
->rowcount
= -1L;
107 self
->row_factory
= Py_None
;
109 if (!pysqlite_check_thread(self
->connection
)) {
116 void pysqlite_cursor_dealloc(pysqlite_Cursor
* self
)
120 /* Reset the statement if the user has not closed the cursor */
121 if (self
->statement
) {
122 rc
= pysqlite_statement_reset(self
->statement
);
123 Py_DECREF(self
->statement
);
126 Py_XDECREF(self
->connection
);
127 Py_XDECREF(self
->row_cast_map
);
128 Py_XDECREF(self
->description
);
129 Py_XDECREF(self
->lastrowid
);
130 Py_XDECREF(self
->row_factory
);
131 Py_XDECREF(self
->next_row
);
133 Py_TYPE(self
)->tp_free((PyObject
*)self
);
136 PyObject
* _pysqlite_get_converter(PyObject
* key
)
138 PyObject
* upcase_key
;
141 upcase_key
= PyObject_CallMethod(key
, "upper", "");
146 retval
= PyDict_GetItem(converters
, upcase_key
);
147 Py_DECREF(upcase_key
);
152 int pysqlite_build_row_cast_map(pysqlite_Cursor
* self
)
155 const char* type_start
= (const char*)-1;
159 const char* decltype;
160 PyObject
* py_decltype
;
164 if (!self
->connection
->detect_types
) {
168 Py_XDECREF(self
->row_cast_map
);
169 self
->row_cast_map
= PyList_New(0);
171 for (i
= 0; i
< sqlite3_column_count(self
->statement
->st
); i
++) {
174 if (self
->connection
->detect_types
& PARSE_COLNAMES
) {
175 colname
= sqlite3_column_name(self
->statement
->st
, i
);
177 for (pos
= colname
; *pos
!= 0; pos
++) {
179 type_start
= pos
+ 1;
180 } else if (*pos
== ']' && type_start
!= (const char*)-1) {
181 key
= PyString_FromStringAndSize(type_start
, pos
- type_start
);
183 /* creating a string failed, but it is too complicated
184 * to propagate the error here, we just assume there is
185 * no converter and proceed */
189 converter
= _pysqlite_get_converter(key
);
197 if (!converter
&& self
->connection
->detect_types
& PARSE_DECLTYPES
) {
198 decltype = sqlite3_column_decltype(self
->statement
->st
, i
);
200 for (pos
= decltype;;pos
++) {
201 /* Converter names are split at '(' and blanks.
202 * This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
203 * 'NUMBER(10)' to be treated as 'NUMBER', for example.
204 * In other words, it will work as people expect it to work.*/
205 if (*pos
== ' ' || *pos
== '(' || *pos
== 0) {
206 py_decltype
= PyString_FromStringAndSize(decltype, pos
- decltype);
214 converter
= _pysqlite_get_converter(py_decltype
);
215 Py_DECREF(py_decltype
);
223 if (PyList_Append(self
->row_cast_map
, converter
) != 0) {
224 if (converter
!= Py_None
) {
225 Py_DECREF(converter
);
227 Py_XDECREF(self
->row_cast_map
);
228 self
->row_cast_map
= NULL
;
237 PyObject
* _pysqlite_build_column_name(const char* colname
)
246 for (pos
= colname
;; pos
++) {
247 if (*pos
== 0 || *pos
== '[') {
248 if ((*pos
== '[') && (pos
> colname
) && (*(pos
-1) == ' ')) {
251 return PyString_FromStringAndSize(colname
, pos
- colname
);
256 PyObject
* pysqlite_unicode_from_string(const char* val_str
, int optimize
)
276 return PyString_FromString(val_str
);
278 return PyUnicode_DecodeUTF8(val_str
, strlen(val_str
), NULL
);
283 * Returns a row from the currently active SQLite statement
286 * - sqlite3_step() has been called before and it returned SQLITE_ROW.
288 PyObject
* _pysqlite_fetch_one_row(pysqlite_Cursor
* self
)
292 PyObject
* item
= NULL
;
304 Py_BEGIN_ALLOW_THREADS
305 numcols
= sqlite3_data_count(self
->statement
->st
);
308 row
= PyTuple_New(numcols
);
313 for (i
= 0; i
< numcols
; i
++) {
314 if (self
->connection
->detect_types
) {
315 converter
= PyList_GetItem(self
->row_cast_map
, i
);
323 if (converter
!= Py_None
) {
324 nbytes
= sqlite3_column_bytes(self
->statement
->st
, i
);
325 val_str
= (const char*)sqlite3_column_blob(self
->statement
->st
, i
);
330 item
= PyString_FromStringAndSize(val_str
, nbytes
);
334 converted
= PyObject_CallFunction(converter
, "O", item
);
341 Py_BEGIN_ALLOW_THREADS
342 coltype
= sqlite3_column_type(self
->statement
->st
, i
);
344 if (coltype
== SQLITE_NULL
) {
347 } else if (coltype
== SQLITE_INTEGER
) {
348 intval
= sqlite3_column_int64(self
->statement
->st
, i
);
349 if (intval
< INT32_MIN
|| intval
> INT32_MAX
) {
350 converted
= PyLong_FromLongLong(intval
);
352 converted
= PyInt_FromLong((long)intval
);
354 } else if (coltype
== SQLITE_FLOAT
) {
355 converted
= PyFloat_FromDouble(sqlite3_column_double(self
->statement
->st
, i
));
356 } else if (coltype
== SQLITE_TEXT
) {
357 val_str
= (const char*)sqlite3_column_text(self
->statement
->st
, i
);
358 if ((self
->connection
->text_factory
== (PyObject
*)&PyUnicode_Type
)
359 || (self
->connection
->text_factory
== pysqlite_OptimizedUnicode
)) {
361 converted
= pysqlite_unicode_from_string(val_str
,
362 self
->connection
->text_factory
== pysqlite_OptimizedUnicode
? 1 : 0);
365 colname
= sqlite3_column_name(self
->statement
->st
, i
);
367 colname
= "<unknown column name>";
369 PyOS_snprintf(buf
, sizeof(buf
) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
371 PyErr_SetString(pysqlite_OperationalError
, buf
);
373 } else if (self
->connection
->text_factory
== (PyObject
*)&PyString_Type
) {
374 converted
= PyString_FromString(val_str
);
376 converted
= PyObject_CallFunction(self
->connection
->text_factory
, "s", val_str
);
379 /* coltype == SQLITE_BLOB */
380 nbytes
= sqlite3_column_bytes(self
->statement
->st
, i
);
381 buffer
= PyBuffer_New(nbytes
);
385 if (PyObject_AsWriteBuffer(buffer
, &raw_buffer
, &nbytes
)) {
388 memcpy(raw_buffer
, sqlite3_column_blob(self
->statement
->st
, i
), nbytes
);
394 PyTuple_SetItem(row
, i
, converted
);
397 PyTuple_SetItem(row
, i
, Py_None
);
401 if (PyErr_Occurred()) {
409 PyObject
* _pysqlite_query_execute(pysqlite_Cursor
* self
, int multiple
, PyObject
* args
)
412 PyObject
* operation_bytestr
= NULL
;
413 char* operation_cstr
;
414 PyObject
* parameters_list
= NULL
;
415 PyObject
* parameters_iter
= NULL
;
416 PyObject
* parameters
= NULL
;
422 PY_LONG_LONG lastrowid
;
424 PyObject
* descriptor
;
425 PyObject
* second_argument
= NULL
;
426 int allow_8bit_chars
;
428 if (!pysqlite_check_thread(self
->connection
) || !pysqlite_check_connection(self
->connection
)) {
432 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
433 allow_8bit_chars
= ((self
->connection
->text_factory
!= (PyObject
*)&PyUnicode_Type
) &&
434 (self
->connection
->text_factory
!= (PyObject
*)&PyUnicode_Type
&& pysqlite_OptimizedUnicode
));
436 Py_XDECREF(self
->next_row
);
437 self
->next_row
= NULL
;
441 if (!PyArg_ParseTuple(args
, "OO", &operation
, &second_argument
)) {
445 if (!PyString_Check(operation
) && !PyUnicode_Check(operation
)) {
446 PyErr_SetString(PyExc_ValueError
, "operation parameter must be str or unicode");
450 if (PyIter_Check(second_argument
)) {
452 Py_INCREF(second_argument
);
453 parameters_iter
= second_argument
;
456 parameters_iter
= PyObject_GetIter(second_argument
);
457 if (!parameters_iter
) {
463 if (!PyArg_ParseTuple(args
, "O|O", &operation
, &second_argument
)) {
467 if (!PyString_Check(operation
) && !PyUnicode_Check(operation
)) {
468 PyErr_SetString(PyExc_ValueError
, "operation parameter must be str or unicode");
472 parameters_list
= PyList_New(0);
473 if (!parameters_list
) {
477 if (second_argument
== NULL
) {
478 second_argument
= PyTuple_New(0);
479 if (!second_argument
) {
483 Py_INCREF(second_argument
);
485 if (PyList_Append(parameters_list
, second_argument
) != 0) {
486 Py_DECREF(second_argument
);
489 Py_DECREF(second_argument
);
491 parameters_iter
= PyObject_GetIter(parameters_list
);
492 if (!parameters_iter
) {
497 if (self
->statement
!= NULL
) {
498 /* There is an active statement */
499 rc
= pysqlite_statement_reset(self
->statement
);
502 if (PyString_Check(operation
)) {
503 operation_cstr
= PyString_AsString(operation
);
505 operation_bytestr
= PyUnicode_AsUTF8String(operation
);
506 if (!operation_bytestr
) {
510 operation_cstr
= PyString_AsString(operation_bytestr
);
513 /* reset description and rowcount */
514 Py_DECREF(self
->description
);
516 self
->description
= Py_None
;
517 self
->rowcount
= -1L;
519 func_args
= PyTuple_New(1);
523 Py_INCREF(operation
);
524 if (PyTuple_SetItem(func_args
, 0, operation
) != 0) {
528 if (self
->statement
) {
529 (void)pysqlite_statement_reset(self
->statement
);
530 Py_DECREF(self
->statement
);
533 self
->statement
= (pysqlite_Statement
*)pysqlite_cache_get(self
->connection
->statement_cache
, func_args
);
534 Py_DECREF(func_args
);
536 if (!self
->statement
) {
540 if (self
->statement
->in_use
) {
541 Py_DECREF(self
->statement
);
542 self
->statement
= PyObject_New(pysqlite_Statement
, &pysqlite_StatementType
);
543 if (!self
->statement
) {
546 rc
= pysqlite_statement_create(self
->statement
, self
->connection
, operation
);
547 if (rc
!= SQLITE_OK
) {
548 Py_CLEAR(self
->statement
);
553 pysqlite_statement_reset(self
->statement
);
554 pysqlite_statement_mark_dirty(self
->statement
);
556 statement_type
= detect_statement_type(operation_cstr
);
557 if (self
->connection
->begin_statement
) {
558 switch (statement_type
) {
559 case STATEMENT_UPDATE
:
560 case STATEMENT_DELETE
:
561 case STATEMENT_INSERT
:
562 case STATEMENT_REPLACE
:
563 if (!self
->connection
->inTransaction
) {
564 result
= _pysqlite_connection_begin(self
->connection
);
571 case STATEMENT_OTHER
:
572 /* it's a DDL statement or something similar
573 - we better COMMIT first so it works for all cases */
574 if (self
->connection
->inTransaction
) {
575 result
= pysqlite_connection_commit(self
->connection
, NULL
);
582 case STATEMENT_SELECT
:
584 PyErr_SetString(pysqlite_ProgrammingError
,
585 "You cannot execute SELECT statements in executemany().");
593 parameters
= PyIter_Next(parameters_iter
);
598 pysqlite_statement_mark_dirty(self
->statement
);
600 pysqlite_statement_bind_parameters(self
->statement
, parameters
, allow_8bit_chars
);
601 if (PyErr_Occurred()) {
605 /* Keep trying the SQL statement until the schema stops changing. */
607 /* Actually execute the SQL statement. */
608 rc
= pysqlite_step(self
->statement
->st
, self
->connection
);
609 if (rc
== SQLITE_DONE
|| rc
== SQLITE_ROW
) {
610 /* If it worked, let's get out of the loop */
613 /* Something went wrong. Re-set the statement and try again. */
614 rc
= pysqlite_statement_reset(self
->statement
);
615 if (rc
== SQLITE_SCHEMA
) {
616 /* If this was a result of the schema changing, let's try
618 rc
= pysqlite_statement_recompile(self
->statement
, parameters
);
619 if (rc
== SQLITE_OK
) {
622 /* If the database gave us an error, promote it to Python. */
623 (void)pysqlite_statement_reset(self
->statement
);
624 _pysqlite_seterror(self
->connection
->db
, NULL
);
628 if (PyErr_Occurred()) {
629 /* there was an error that occurred in a user-defined callback */
630 if (_enable_callback_tracebacks
) {
636 (void)pysqlite_statement_reset(self
->statement
);
637 _pysqlite_seterror(self
->connection
->db
, NULL
);
642 if (pysqlite_build_row_cast_map(self
) != 0) {
643 PyErr_SetString(pysqlite_OperationalError
, "Error while building row_cast_map");
647 if (rc
== SQLITE_ROW
|| (rc
== SQLITE_DONE
&& statement_type
== STATEMENT_SELECT
)) {
648 if (self
->description
== Py_None
) {
649 Py_BEGIN_ALLOW_THREADS
650 numcols
= sqlite3_column_count(self
->statement
->st
);
653 Py_DECREF(self
->description
);
654 self
->description
= PyTuple_New(numcols
);
655 if (!self
->description
) {
658 for (i
= 0; i
< numcols
; i
++) {
659 descriptor
= PyTuple_New(7);
663 PyTuple_SetItem(descriptor
, 0, _pysqlite_build_column_name(sqlite3_column_name(self
->statement
->st
, i
)));
664 Py_INCREF(Py_None
); PyTuple_SetItem(descriptor
, 1, Py_None
);
665 Py_INCREF(Py_None
); PyTuple_SetItem(descriptor
, 2, Py_None
);
666 Py_INCREF(Py_None
); PyTuple_SetItem(descriptor
, 3, Py_None
);
667 Py_INCREF(Py_None
); PyTuple_SetItem(descriptor
, 4, Py_None
);
668 Py_INCREF(Py_None
); PyTuple_SetItem(descriptor
, 5, Py_None
);
669 Py_INCREF(Py_None
); PyTuple_SetItem(descriptor
, 6, Py_None
);
670 PyTuple_SetItem(self
->description
, i
, descriptor
);
675 if (rc
== SQLITE_ROW
) {
677 PyErr_SetString(pysqlite_ProgrammingError
, "executemany() can only execute DML statements.");
681 self
->next_row
= _pysqlite_fetch_one_row(self
);
682 } else if (rc
== SQLITE_DONE
&& !multiple
) {
683 pysqlite_statement_reset(self
->statement
);
684 Py_CLEAR(self
->statement
);
687 switch (statement_type
) {
688 case STATEMENT_UPDATE
:
689 case STATEMENT_DELETE
:
690 case STATEMENT_INSERT
:
691 case STATEMENT_REPLACE
:
692 if (self
->rowcount
== -1L) {
695 self
->rowcount
+= (long)sqlite3_changes(self
->connection
->db
);
698 Py_DECREF(self
->lastrowid
);
699 if (!multiple
&& statement_type
== STATEMENT_INSERT
) {
700 Py_BEGIN_ALLOW_THREADS
701 lastrowid
= sqlite3_last_insert_rowid(self
->connection
->db
);
703 self
->lastrowid
= PyInt_FromLong((long)lastrowid
);
706 self
->lastrowid
= Py_None
;
710 rc
= pysqlite_statement_reset(self
->statement
);
712 Py_XDECREF(parameters
);
716 /* just to be sure (implicit ROLLBACKs with ON CONFLICT ROLLBACK/OR
717 * ROLLBACK could have happened */
718 #ifdef SQLITE_VERSION_NUMBER
719 #if SQLITE_VERSION_NUMBER >= 3002002
720 self
->connection
->inTransaction
= !sqlite3_get_autocommit(self
->connection
->db
);
724 Py_XDECREF(operation_bytestr
);
725 Py_XDECREF(parameters
);
726 Py_XDECREF(parameters_iter
);
727 Py_XDECREF(parameters_list
);
729 if (PyErr_Occurred()) {
730 self
->rowcount
= -1L;
734 return (PyObject
*)self
;
738 PyObject
* pysqlite_cursor_execute(pysqlite_Cursor
* self
, PyObject
* args
)
740 return _pysqlite_query_execute(self
, 0, args
);
743 PyObject
* pysqlite_cursor_executemany(pysqlite_Cursor
* self
, PyObject
* args
)
745 return _pysqlite_query_execute(self
, 1, args
);
748 PyObject
* pysqlite_cursor_executescript(pysqlite_Cursor
* self
, PyObject
* args
)
750 PyObject
* script_obj
;
751 PyObject
* script_str
= NULL
;
752 const char* script_cstr
;
753 sqlite3_stmt
* statement
;
756 int statement_completed
= 0;
758 if (!PyArg_ParseTuple(args
, "O", &script_obj
)) {
762 if (!pysqlite_check_thread(self
->connection
) || !pysqlite_check_connection(self
->connection
)) {
766 if (PyString_Check(script_obj
)) {
767 script_cstr
= PyString_AsString(script_obj
);
768 } else if (PyUnicode_Check(script_obj
)) {
769 script_str
= PyUnicode_AsUTF8String(script_obj
);
774 script_cstr
= PyString_AsString(script_str
);
776 PyErr_SetString(PyExc_ValueError
, "script argument must be unicode or string.");
781 result
= pysqlite_connection_commit(self
->connection
, NULL
);
788 if (!sqlite3_complete(script_cstr
)) {
791 statement_completed
= 1;
793 Py_BEGIN_ALLOW_THREADS
794 rc
= sqlite3_prepare(self
->connection
->db
,
800 if (rc
!= SQLITE_OK
) {
801 _pysqlite_seterror(self
->connection
->db
, NULL
);
805 /* execute statement, and ignore results of SELECT statements */
807 while (rc
== SQLITE_ROW
) {
808 rc
= pysqlite_step(statement
, self
->connection
);
809 /* TODO: we probably need more error handling here */
812 if (rc
!= SQLITE_DONE
) {
813 (void)sqlite3_finalize(statement
);
814 _pysqlite_seterror(self
->connection
->db
, NULL
);
818 rc
= sqlite3_finalize(statement
);
819 if (rc
!= SQLITE_OK
) {
820 _pysqlite_seterror(self
->connection
->db
, NULL
);
826 Py_XDECREF(script_str
);
828 if (!statement_completed
) {
829 PyErr_SetString(pysqlite_ProgrammingError
, "you did not provide a complete SQL statement");
832 if (PyErr_Occurred()) {
836 return (PyObject
*)self
;
840 PyObject
* pysqlite_cursor_getiter(pysqlite_Cursor
*self
)
843 return (PyObject
*)self
;
846 PyObject
* pysqlite_cursor_iternext(pysqlite_Cursor
*self
)
848 PyObject
* next_row_tuple
;
852 if (!pysqlite_check_thread(self
->connection
) || !pysqlite_check_connection(self
->connection
)) {
856 if (!self
->next_row
) {
857 if (self
->statement
) {
858 (void)pysqlite_statement_reset(self
->statement
);
859 Py_DECREF(self
->statement
);
860 self
->statement
= NULL
;
865 next_row_tuple
= self
->next_row
;
866 self
->next_row
= NULL
;
868 if (self
->row_factory
!= Py_None
) {
869 next_row
= PyObject_CallFunction(self
->row_factory
, "OO", self
, next_row_tuple
);
870 Py_DECREF(next_row_tuple
);
872 next_row
= next_row_tuple
;
875 if (self
->statement
) {
876 rc
= pysqlite_step(self
->statement
->st
, self
->connection
);
877 if (rc
!= SQLITE_DONE
&& rc
!= SQLITE_ROW
) {
878 (void)pysqlite_statement_reset(self
->statement
);
880 _pysqlite_seterror(self
->connection
->db
, NULL
);
884 if (rc
== SQLITE_ROW
) {
885 self
->next_row
= _pysqlite_fetch_one_row(self
);
892 PyObject
* pysqlite_cursor_fetchone(pysqlite_Cursor
* self
, PyObject
* args
)
896 row
= pysqlite_cursor_iternext(self
);
897 if (!row
&& !PyErr_Occurred()) {
905 PyObject
* pysqlite_cursor_fetchmany(pysqlite_Cursor
* self
, PyObject
* args
, PyObject
* kwargs
)
907 static char *kwlist
[] = {"size", NULL
, NULL
};
911 int maxrows
= self
->arraysize
;
914 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|i:fetchmany", kwlist
, &maxrows
)) {
918 list
= PyList_New(0);
923 /* just make sure we enter the loop */
927 row
= pysqlite_cursor_iternext(self
);
929 PyList_Append(list
, row
);
935 if (++counter
== maxrows
) {
940 if (PyErr_Occurred()) {
948 PyObject
* pysqlite_cursor_fetchall(pysqlite_Cursor
* self
, PyObject
* args
)
953 list
= PyList_New(0);
958 /* just make sure we enter the loop */
959 row
= (PyObject
*)Py_None
;
962 row
= pysqlite_cursor_iternext(self
);
964 PyList_Append(list
, row
);
969 if (PyErr_Occurred()) {
977 PyObject
* pysqlite_noop(pysqlite_Connection
* self
, PyObject
* args
)
979 /* don't care, return None */
984 PyObject
* pysqlite_cursor_close(pysqlite_Cursor
* self
, PyObject
* args
)
986 if (!pysqlite_check_thread(self
->connection
) || !pysqlite_check_connection(self
->connection
)) {
990 if (self
->statement
) {
991 (void)pysqlite_statement_reset(self
->statement
);
992 Py_CLEAR(self
->statement
);
999 static PyMethodDef cursor_methods
[] = {
1000 {"execute", (PyCFunction
)pysqlite_cursor_execute
, METH_VARARGS
,
1001 PyDoc_STR("Executes a SQL statement.")},
1002 {"executemany", (PyCFunction
)pysqlite_cursor_executemany
, METH_VARARGS
,
1003 PyDoc_STR("Repeatedly executes a SQL statement.")},
1004 {"executescript", (PyCFunction
)pysqlite_cursor_executescript
, METH_VARARGS
,
1005 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
1006 {"fetchone", (PyCFunction
)pysqlite_cursor_fetchone
, METH_NOARGS
,
1007 PyDoc_STR("Fetches one row from the resultset.")},
1008 {"fetchmany", (PyCFunction
)pysqlite_cursor_fetchmany
, METH_VARARGS
|METH_KEYWORDS
,
1009 PyDoc_STR("Fetches several rows from the resultset.")},
1010 {"fetchall", (PyCFunction
)pysqlite_cursor_fetchall
, METH_NOARGS
,
1011 PyDoc_STR("Fetches all rows from the resultset.")},
1012 {"close", (PyCFunction
)pysqlite_cursor_close
, METH_NOARGS
,
1013 PyDoc_STR("Closes the cursor.")},
1014 {"setinputsizes", (PyCFunction
)pysqlite_noop
, METH_VARARGS
,
1015 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1016 {"setoutputsize", (PyCFunction
)pysqlite_noop
, METH_VARARGS
,
1017 PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")},
1021 static struct PyMemberDef cursor_members
[] =
1023 {"connection", T_OBJECT
, offsetof(pysqlite_Cursor
, connection
), RO
},
1024 {"description", T_OBJECT
, offsetof(pysqlite_Cursor
, description
), RO
},
1025 {"arraysize", T_INT
, offsetof(pysqlite_Cursor
, arraysize
), 0},
1026 {"lastrowid", T_OBJECT
, offsetof(pysqlite_Cursor
, lastrowid
), RO
},
1027 {"rowcount", T_LONG
, offsetof(pysqlite_Cursor
, rowcount
), RO
},
1028 {"row_factory", T_OBJECT
, offsetof(pysqlite_Cursor
, row_factory
), 0},
1032 static char cursor_doc
[] =
1033 PyDoc_STR("SQLite database cursor class.");
1035 PyTypeObject pysqlite_CursorType
= {
1036 PyVarObject_HEAD_INIT(NULL
, 0)
1037 MODULE_NAME
".Cursor", /* tp_name */
1038 sizeof(pysqlite_Cursor
), /* tp_basicsize */
1039 0, /* tp_itemsize */
1040 (destructor
)pysqlite_cursor_dealloc
, /* tp_dealloc */
1046 0, /* tp_as_number */
1047 0, /* tp_as_sequence */
1048 0, /* tp_as_mapping */
1052 0, /* tp_getattro */
1053 0, /* tp_setattro */
1054 0, /* tp_as_buffer */
1055 Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_HAVE_ITER
|Py_TPFLAGS_BASETYPE
, /* tp_flags */
1056 cursor_doc
, /* tp_doc */
1057 0, /* tp_traverse */
1059 0, /* tp_richcompare */
1060 0, /* tp_weaklistoffset */
1061 (getiterfunc
)pysqlite_cursor_getiter
, /* tp_iter */
1062 (iternextfunc
)pysqlite_cursor_iternext
, /* tp_iternext */
1063 cursor_methods
, /* tp_methods */
1064 cursor_members
, /* tp_members */
1068 0, /* tp_descr_get */
1069 0, /* tp_descr_set */
1070 0, /* tp_dictoffset */
1071 (initproc
)pysqlite_cursor_init
, /* tp_init */
1077 extern int pysqlite_cursor_setup_types(void)
1079 pysqlite_CursorType
.tp_new
= PyType_GenericNew
;
1080 return PyType_Ready(&pysqlite_CursorType
);