| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | /* cursor.c - the cursor type
 | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * Copyright (C) 2004-2006 Gerhard H<EFBFBD>ring <gh@ghaering.de> | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * This file is part of pysqlite. | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * This software is provided 'as-is', without any express or implied | 
					
						
							|  |  |  |  |  * warranty.  In no event will the authors be held liable for any damages | 
					
						
							|  |  |  |  |  * arising from the use of this software. | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * Permission is granted to anyone to use this software for any purpose, | 
					
						
							|  |  |  |  |  * including commercial applications, and to alter it and redistribute it | 
					
						
							|  |  |  |  |  * freely, subject to the following restrictions: | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * 1. The origin of this software must not be misrepresented; you must not | 
					
						
							|  |  |  |  |  *    claim that you wrote the original software. If you use this software | 
					
						
							|  |  |  |  |  *    in a product, an acknowledgment in the product documentation would be | 
					
						
							|  |  |  |  |  *    appreciated but is not required. | 
					
						
							|  |  |  |  |  * 2. Altered source versions must be plainly marked as such, and must not be | 
					
						
							|  |  |  |  |  *    misrepresented as being the original software. | 
					
						
							|  |  |  |  |  * 3. This notice may not be removed or altered from any source distribution. | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include "cursor.h"
 | 
					
						
							|  |  |  |  | #include "module.h"
 | 
					
						
							|  |  |  |  | #include "util.h"
 | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  | #include "sqlitecompat.h"
 | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | /* used to decide wether to call PyInt_FromLong or PyLong_FromLongLong */ | 
					
						
							| 
									
										
										
										
											2006-04-28 05:28:05 +00:00
										 |  |  |  | #ifndef INT32_MIN
 | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | #define INT32_MIN (-2147483647 - 1)
 | 
					
						
							| 
									
										
										
										
											2006-04-28 05:28:05 +00:00
										 |  |  |  | #endif
 | 
					
						
							|  |  |  |  | #ifndef INT32_MAX
 | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | #define INT32_MAX 2147483647
 | 
					
						
							| 
									
										
										
										
											2006-04-28 05:28:05 +00:00
										 |  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | static pysqlite_StatementKind detect_statement_type(char* statement) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     char buf[20]; | 
					
						
							|  |  |  |  |     char* src; | 
					
						
							|  |  |  |  |     char* dst; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     src = statement; | 
					
						
							|  |  |  |  |     /* skip over whitepace */ | 
					
						
							|  |  |  |  |     while (*src == '\r' || *src == '\n' || *src == ' ' || *src == '\t') { | 
					
						
							|  |  |  |  |         src++; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (*src == 0) | 
					
						
							|  |  |  |  |         return STATEMENT_INVALID; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     dst = buf; | 
					
						
							|  |  |  |  |     *dst = 0; | 
					
						
							|  |  |  |  |     while (isalpha(*src) && dst - buf < sizeof(buf) - 2) { | 
					
						
							|  |  |  |  |         *dst++ = tolower(*src++); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     *dst = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!strcmp(buf, "select")) { | 
					
						
							|  |  |  |  |         return STATEMENT_SELECT; | 
					
						
							|  |  |  |  |     } else if (!strcmp(buf, "insert")) { | 
					
						
							|  |  |  |  |         return STATEMENT_INSERT; | 
					
						
							|  |  |  |  |     } else if (!strcmp(buf, "update")) { | 
					
						
							|  |  |  |  |         return STATEMENT_UPDATE; | 
					
						
							|  |  |  |  |     } else if (!strcmp(buf, "delete")) { | 
					
						
							|  |  |  |  |         return STATEMENT_DELETE; | 
					
						
							|  |  |  |  |     } else if (!strcmp(buf, "replace")) { | 
					
						
							|  |  |  |  |         return STATEMENT_REPLACE; | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         return STATEMENT_OTHER; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject* kwargs) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     pysqlite_Connection* connection; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     if (!PyArg_ParseTuple(args, "O!", &pysqlite_ConnectionType, &connection)) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     { | 
					
						
							|  |  |  |  |         return -1;  | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_INCREF(connection); | 
					
						
							|  |  |  |  |     self->connection = connection; | 
					
						
							|  |  |  |  |     self->statement = NULL; | 
					
						
							|  |  |  |  |     self->next_row = NULL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     self->row_cast_map = PyList_New(0); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!self->row_cast_map) { | 
					
						
							|  |  |  |  |         return -1; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |     self->description = Py_None; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |     self->lastrowid= Py_None; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     self->arraysize = 1; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     self->rowcount = PyInt_FromLong(-1L); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!self->rowcount) { | 
					
						
							|  |  |  |  |         return -1; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |     self->row_factory = Py_None; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     if (!pysqlite_check_thread(self->connection)) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         return -1; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return 0; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | void pysqlite_cursor_dealloc(pysqlite_Cursor* self) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     int rc; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /* Reset the statement if the user has not closed the cursor */ | 
					
						
							|  |  |  |  |     if (self->statement) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         rc = pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         Py_DECREF(self->statement); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_XDECREF(self->connection); | 
					
						
							|  |  |  |  |     Py_XDECREF(self->row_cast_map); | 
					
						
							|  |  |  |  |     Py_XDECREF(self->description); | 
					
						
							|  |  |  |  |     Py_XDECREF(self->lastrowid); | 
					
						
							|  |  |  |  |     Py_XDECREF(self->rowcount); | 
					
						
							|  |  |  |  |     Py_XDECREF(self->row_factory); | 
					
						
							|  |  |  |  |     Py_XDECREF(self->next_row); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-19 02:37:44 +00:00
										 |  |  |  |     Py_TYPE(self)->tp_free((PyObject*)self); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* _pysqlite_get_converter(PyObject* key) | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     PyObject* upcase_key; | 
					
						
							|  |  |  |  |     PyObject* retval; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     upcase_key = PyObject_CallMethod(key, "upper", ""); | 
					
						
							|  |  |  |  |     if (!upcase_key) { | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     retval = PyDict_GetItem(converters, upcase_key); | 
					
						
							|  |  |  |  |     Py_DECREF(upcase_key); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return retval; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | int pysqlite_build_row_cast_map(pysqlite_Cursor* self) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     int i; | 
					
						
							|  |  |  |  |     const char* type_start = (const char*)-1; | 
					
						
							|  |  |  |  |     const char* pos; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     const char* colname; | 
					
						
							|  |  |  |  |     const char* decltype; | 
					
						
							|  |  |  |  |     PyObject* py_decltype; | 
					
						
							|  |  |  |  |     PyObject* converter; | 
					
						
							|  |  |  |  |     PyObject* key; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!self->connection->detect_types) { | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     Py_XDECREF(self->row_cast_map); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     self->row_cast_map = PyList_New(0); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     for (i = 0; i < sqlite3_column_count(self->statement->st); i++) { | 
					
						
							|  |  |  |  |         converter = NULL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         if (self->connection->detect_types & PARSE_COLNAMES) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             colname = sqlite3_column_name(self->statement->st, i); | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |             if (colname) { | 
					
						
							|  |  |  |  |                 for (pos = colname; *pos != 0; pos++) { | 
					
						
							|  |  |  |  |                     if (*pos == '[') { | 
					
						
							|  |  |  |  |                         type_start = pos + 1; | 
					
						
							|  |  |  |  |                     } else if (*pos == ']' && type_start != (const char*)-1) { | 
					
						
							|  |  |  |  |                         key = PyString_FromStringAndSize(type_start, pos - type_start); | 
					
						
							|  |  |  |  |                         if (!key) { | 
					
						
							|  |  |  |  |                             /* creating a string failed, but it is too complicated
 | 
					
						
							|  |  |  |  |                              * to propagate the error here, we just assume there is | 
					
						
							|  |  |  |  |                              * no converter and proceed */ | 
					
						
							|  |  |  |  |                             break; | 
					
						
							|  |  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                         converter = _pysqlite_get_converter(key); | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |                         Py_DECREF(key); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |                         break; | 
					
						
							|  |  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         if (!converter && self->connection->detect_types & PARSE_DECLTYPES) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             decltype = sqlite3_column_decltype(self->statement->st, i); | 
					
						
							|  |  |  |  |             if (decltype) { | 
					
						
							|  |  |  |  |                 for (pos = decltype;;pos++) { | 
					
						
							|  |  |  |  |                     if (*pos == ' ' || *pos == 0) { | 
					
						
							|  |  |  |  |                         py_decltype = PyString_FromStringAndSize(decltype, pos - decltype); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |                         if (!py_decltype) { | 
					
						
							|  |  |  |  |                             return -1; | 
					
						
							|  |  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                         break; | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                 converter = _pysqlite_get_converter(py_decltype); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 Py_DECREF(py_decltype); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |         if (!converter) { | 
					
						
							|  |  |  |  |             converter = Py_None; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (PyList_Append(self->row_cast_map, converter) != 0) { | 
					
						
							|  |  |  |  |             if (converter != Py_None) { | 
					
						
							|  |  |  |  |                 Py_DECREF(converter); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             Py_XDECREF(self->row_cast_map); | 
					
						
							|  |  |  |  |             self->row_cast_map = NULL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* _pysqlite_build_column_name(const char* colname) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     const char* pos; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!colname) { | 
					
						
							|  |  |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |         return Py_None; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     for (pos = colname;; pos++) { | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |         if (*pos == 0 || *pos == '[') { | 
					
						
							|  |  |  |  |             if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { | 
					
						
							|  |  |  |  |                 pos--; | 
					
						
							|  |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             return PyString_FromStringAndSize(colname, pos - colname); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     const char* check; | 
					
						
							|  |  |  |  |     int is_ascii = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (optimize) { | 
					
						
							|  |  |  |  |         is_ascii = 1; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         check = val_str; | 
					
						
							|  |  |  |  |         while (*check) { | 
					
						
							|  |  |  |  |             if (*check & 0x80) { | 
					
						
							|  |  |  |  |                 is_ascii = 0; | 
					
						
							|  |  |  |  |                 break; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             check++; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (is_ascii) { | 
					
						
							|  |  |  |  |         return PyString_FromString(val_str); | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         return PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Returns a row from the currently active SQLite statement | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * Precondidition: | 
					
						
							|  |  |  |  |  * - sqlite3_step() has been called before and it returned SQLITE_ROW. | 
					
						
							|  |  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     int i, numcols; | 
					
						
							|  |  |  |  |     PyObject* row; | 
					
						
							|  |  |  |  |     PyObject* item = NULL; | 
					
						
							|  |  |  |  |     int coltype; | 
					
						
							|  |  |  |  |     PY_LONG_LONG intval; | 
					
						
							|  |  |  |  |     PyObject* converter; | 
					
						
							|  |  |  |  |     PyObject* converted; | 
					
						
							| 
									
										
										
										
											2006-04-01 09:08:06 +00:00
										 |  |  |  |     Py_ssize_t nbytes; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     PyObject* buffer; | 
					
						
							|  |  |  |  |     void* raw_buffer; | 
					
						
							|  |  |  |  |     const char* val_str; | 
					
						
							|  |  |  |  |     char buf[200]; | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |     const char* colname; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |  |     numcols = sqlite3_data_count(self->statement->st); | 
					
						
							|  |  |  |  |     Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     row = PyTuple_New(numcols); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!row) { | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     for (i = 0; i < numcols; i++) { | 
					
						
							|  |  |  |  |         if (self->connection->detect_types) { | 
					
						
							|  |  |  |  |             converter = PyList_GetItem(self->row_cast_map, i); | 
					
						
							|  |  |  |  |             if (!converter) { | 
					
						
							|  |  |  |  |                 converter = Py_None; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             converter = Py_None; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (converter != Py_None) { | 
					
						
							| 
									
										
										
										
											2006-07-02 17:48:30 +00:00
										 |  |  |  |             nbytes = sqlite3_column_bytes(self->statement->st, i); | 
					
						
							|  |  |  |  |             val_str = (const char*)sqlite3_column_blob(self->statement->st, i); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             if (!val_str) { | 
					
						
							|  |  |  |  |                 Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |                 converted = Py_None; | 
					
						
							|  |  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2006-07-02 17:48:30 +00:00
										 |  |  |  |                 item = PyString_FromStringAndSize(val_str, nbytes); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |                 if (!item) { | 
					
						
							|  |  |  |  |                     return NULL; | 
					
						
							|  |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 converted = PyObject_CallFunction(converter, "O", item); | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |                 Py_DECREF(item); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 if (!converted) { | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |  |             coltype = sqlite3_column_type(self->statement->st, i); | 
					
						
							|  |  |  |  |             Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |  |             if (coltype == SQLITE_NULL) { | 
					
						
							|  |  |  |  |                 Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |                 converted = Py_None; | 
					
						
							|  |  |  |  |             } else if (coltype == SQLITE_INTEGER) { | 
					
						
							|  |  |  |  |                 intval = sqlite3_column_int64(self->statement->st, i); | 
					
						
							|  |  |  |  |                 if (intval < INT32_MIN || intval > INT32_MAX) { | 
					
						
							|  |  |  |  |                     converted = PyLong_FromLongLong(intval); | 
					
						
							|  |  |  |  |                 } else { | 
					
						
							|  |  |  |  |                     converted = PyInt_FromLong((long)intval); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } else if (coltype == SQLITE_FLOAT) { | 
					
						
							|  |  |  |  |                 converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i)); | 
					
						
							|  |  |  |  |             } else if (coltype == SQLITE_TEXT) { | 
					
						
							|  |  |  |  |                 val_str = (const char*)sqlite3_column_text(self->statement->st, i); | 
					
						
							|  |  |  |  |                 if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type) | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     || (self->connection->text_factory == pysqlite_OptimizedUnicode)) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     converted = pysqlite_unicode_from_string(val_str, | 
					
						
							|  |  |  |  |                         self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |                     if (!converted) { | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |                         colname = sqlite3_column_name(self->statement->st, i); | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |                         if (!colname) { | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |                             colname = "<unknown column name>"; | 
					
						
							|  |  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |                         PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'", | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |                                      colname , val_str); | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                         PyErr_SetString(pysqlite_OperationalError, buf); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                     } | 
					
						
							|  |  |  |  |                 } else if (self->connection->text_factory == (PyObject*)&PyString_Type) { | 
					
						
							|  |  |  |  |                     converted = PyString_FromString(val_str); | 
					
						
							|  |  |  |  |                 } else { | 
					
						
							|  |  |  |  |                     converted = PyObject_CallFunction(self->connection->text_factory, "s", val_str); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } else { | 
					
						
							|  |  |  |  |                 /* coltype == SQLITE_BLOB */ | 
					
						
							|  |  |  |  |                 nbytes = sqlite3_column_bytes(self->statement->st, i); | 
					
						
							|  |  |  |  |                 buffer = PyBuffer_New(nbytes); | 
					
						
							|  |  |  |  |                 if (!buffer) { | 
					
						
							|  |  |  |  |                     break; | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &nbytes)) { | 
					
						
							|  |  |  |  |                     break; | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 memcpy(raw_buffer, sqlite3_column_blob(self->statement->st, i), nbytes); | 
					
						
							|  |  |  |  |                 converted = buffer; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |         if (converted) { | 
					
						
							|  |  |  |  |             PyTuple_SetItem(row, i, converted); | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |             PyTuple_SetItem(row, i, Py_None); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (PyErr_Occurred()) { | 
					
						
							|  |  |  |  |         Py_DECREF(row); | 
					
						
							|  |  |  |  |         row = NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return row; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     PyObject* operation; | 
					
						
							|  |  |  |  |     PyObject* operation_bytestr = NULL; | 
					
						
							|  |  |  |  |     char* operation_cstr; | 
					
						
							|  |  |  |  |     PyObject* parameters_list = NULL; | 
					
						
							|  |  |  |  |     PyObject* parameters_iter = NULL; | 
					
						
							|  |  |  |  |     PyObject* parameters = NULL; | 
					
						
							|  |  |  |  |     int i; | 
					
						
							|  |  |  |  |     int rc; | 
					
						
							|  |  |  |  |     PyObject* func_args; | 
					
						
							|  |  |  |  |     PyObject* result; | 
					
						
							|  |  |  |  |     int numcols; | 
					
						
							|  |  |  |  |     PY_LONG_LONG lastrowid; | 
					
						
							|  |  |  |  |     int statement_type; | 
					
						
							|  |  |  |  |     PyObject* descriptor; | 
					
						
							|  |  |  |  |     PyObject* second_argument = NULL; | 
					
						
							|  |  |  |  |     long rowcount = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_XDECREF(self->next_row); | 
					
						
							|  |  |  |  |     self->next_row = NULL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (multiple) { | 
					
						
							|  |  |  |  |         /* executemany() */ | 
					
						
							|  |  |  |  |         if (!PyArg_ParseTuple(args, "OO", &operation, &second_argument)) { | 
					
						
							|  |  |  |  |             return NULL;  | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { | 
					
						
							|  |  |  |  |             PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); | 
					
						
							|  |  |  |  |             return NULL; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (PyIter_Check(second_argument)) { | 
					
						
							|  |  |  |  |             /* iterator */ | 
					
						
							|  |  |  |  |             Py_INCREF(second_argument); | 
					
						
							|  |  |  |  |             parameters_iter = second_argument; | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             /* sequence */ | 
					
						
							|  |  |  |  |             parameters_iter = PyObject_GetIter(second_argument); | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |             if (!parameters_iter) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 return NULL; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         /* execute() */ | 
					
						
							|  |  |  |  |         if (!PyArg_ParseTuple(args, "O|O", &operation, &second_argument)) { | 
					
						
							|  |  |  |  |             return NULL;  | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (!PyString_Check(operation) && !PyUnicode_Check(operation)) { | 
					
						
							|  |  |  |  |             PyErr_SetString(PyExc_ValueError, "operation parameter must be str or unicode"); | 
					
						
							|  |  |  |  |             return NULL; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         parameters_list = PyList_New(0); | 
					
						
							|  |  |  |  |         if (!parameters_list) { | 
					
						
							|  |  |  |  |             return NULL; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (second_argument == NULL) { | 
					
						
							|  |  |  |  |             second_argument = PyTuple_New(0); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |             if (!second_argument) { | 
					
						
							| 
									
										
										
										
											2006-04-04 07:25:25 +00:00
										 |  |  |  |                 goto error; | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         } else { | 
					
						
							|  |  |  |  |             Py_INCREF(second_argument); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |         if (PyList_Append(parameters_list, second_argument) != 0) { | 
					
						
							|  |  |  |  |             Py_DECREF(second_argument); | 
					
						
							| 
									
										
										
										
											2006-04-04 07:25:25 +00:00
										 |  |  |  |             goto error; | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         Py_DECREF(second_argument); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         parameters_iter = PyObject_GetIter(parameters_list); | 
					
						
							| 
									
										
										
										
											2006-04-09 04:07:39 +00:00
										 |  |  |  |         if (!parameters_iter) { | 
					
						
							|  |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (self->statement != NULL) { | 
					
						
							|  |  |  |  |         /* There is an active statement */ | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         rc = pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (PyString_Check(operation)) { | 
					
						
							|  |  |  |  |         operation_cstr = PyString_AsString(operation); | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         operation_bytestr = PyUnicode_AsUTF8String(operation); | 
					
						
							|  |  |  |  |         if (!operation_bytestr) { | 
					
						
							|  |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         operation_cstr = PyString_AsString(operation_bytestr); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /* reset description and rowcount */ | 
					
						
							|  |  |  |  |     Py_DECREF(self->description); | 
					
						
							|  |  |  |  |     Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |     self->description = Py_None; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_DECREF(self->rowcount); | 
					
						
							|  |  |  |  |     self->rowcount = PyInt_FromLong(-1L); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!self->rowcount) { | 
					
						
							|  |  |  |  |         goto error; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     statement_type = detect_statement_type(operation_cstr); | 
					
						
							|  |  |  |  |     if (self->connection->begin_statement) { | 
					
						
							|  |  |  |  |         switch (statement_type) { | 
					
						
							|  |  |  |  |             case STATEMENT_UPDATE: | 
					
						
							|  |  |  |  |             case STATEMENT_DELETE: | 
					
						
							|  |  |  |  |             case STATEMENT_INSERT: | 
					
						
							|  |  |  |  |             case STATEMENT_REPLACE: | 
					
						
							|  |  |  |  |                 if (!self->connection->inTransaction) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     result = _pysqlite_connection_begin(self->connection); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                     if (!result) { | 
					
						
							|  |  |  |  |                         goto error; | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                     Py_DECREF(result); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 break; | 
					
						
							|  |  |  |  |             case STATEMENT_OTHER: | 
					
						
							|  |  |  |  |                 /* it's a DDL statement or something similar
 | 
					
						
							|  |  |  |  |                    - we better COMMIT first so it works for all cases */ | 
					
						
							|  |  |  |  |                 if (self->connection->inTransaction) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     result = pysqlite_connection_commit(self->connection, NULL); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                     if (!result) { | 
					
						
							|  |  |  |  |                         goto error; | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                     Py_DECREF(result); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 break; | 
					
						
							|  |  |  |  |             case STATEMENT_SELECT: | 
					
						
							|  |  |  |  |                 if (multiple) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     PyErr_SetString(pysqlite_ProgrammingError, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                                 "You cannot execute SELECT statements in executemany()."); | 
					
						
							|  |  |  |  |                     goto error; | 
					
						
							|  |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     func_args = PyTuple_New(1); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!func_args) { | 
					
						
							|  |  |  |  |         goto error; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     Py_INCREF(operation); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (PyTuple_SetItem(func_args, 0, operation) != 0) { | 
					
						
							|  |  |  |  |         goto error; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (self->statement) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         (void)pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         Py_DECREF(self->statement); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     self->statement = (pysqlite_Statement*)pysqlite_cache_get(self->connection->statement_cache, func_args); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     Py_DECREF(func_args); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!self->statement) { | 
					
						
							|  |  |  |  |         goto error; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (self->statement->in_use) { | 
					
						
							|  |  |  |  |         Py_DECREF(self->statement); | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         self->statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |         if (!self->statement) { | 
					
						
							|  |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         rc = pysqlite_statement_create(self->statement, self->connection, operation); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         if (rc != SQLITE_OK) { | 
					
						
							|  |  |  |  |             self->statement = 0; | 
					
						
							|  |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     pysqlite_statement_reset(self->statement); | 
					
						
							|  |  |  |  |     pysqlite_statement_mark_dirty(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     while (1) { | 
					
						
							|  |  |  |  |         parameters = PyIter_Next(parameters_iter); | 
					
						
							|  |  |  |  |         if (!parameters) { | 
					
						
							|  |  |  |  |             break; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         pysqlite_statement_mark_dirty(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         pysqlite_statement_bind_parameters(self->statement, parameters); | 
					
						
							| 
									
										
										
										
											2006-04-01 08:36:27 +00:00
										 |  |  |  |         if (PyErr_Occurred()) { | 
					
						
							|  |  |  |  |             goto error; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         if (pysqlite_build_row_cast_map(self) != 0) { | 
					
						
							|  |  |  |  |             PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map"); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         /* Keep trying the SQL statement until the schema stops changing. */ | 
					
						
							|  |  |  |  |         while (1) { | 
					
						
							|  |  |  |  |             /* Actually execute the SQL statement. */ | 
					
						
							|  |  |  |  |             rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); | 
					
						
							|  |  |  |  |             if (rc == SQLITE_DONE ||  rc == SQLITE_ROW) { | 
					
						
							|  |  |  |  |                 /* If it worked, let's get out of the loop */ | 
					
						
							|  |  |  |  |                 break; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             /* Something went wrong.  Re-set the statement and try again. */ | 
					
						
							|  |  |  |  |             rc = pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             if (rc == SQLITE_SCHEMA) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                 /* If this was a result of the schema changing, let's try
 | 
					
						
							|  |  |  |  |                    again. */ | 
					
						
							|  |  |  |  |                 rc = pysqlite_statement_recompile(self->statement, parameters); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 if (rc == SQLITE_OK) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     continue; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     /* If the database gave us an error, promote it to Python. */ | 
					
						
							|  |  |  |  |                     _pysqlite_seterror(self->connection->db); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                     goto error; | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |                 if (PyErr_Occurred()) { | 
					
						
							| 
									
										
										
										
											2006-07-28 18:36:01 +00:00
										 |  |  |  |                     /* there was an error that occurred in a user-defined callback */ | 
					
						
							| 
									
										
										
										
											2006-06-13 22:24:47 +00:00
										 |  |  |  |                     if (_enable_callback_tracebacks) { | 
					
						
							|  |  |  |  |                         PyErr_Print(); | 
					
						
							|  |  |  |  |                     } else { | 
					
						
							|  |  |  |  |                         PyErr_Clear(); | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                 _pysqlite_seterror(self->connection->db); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 goto error; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { | 
					
						
							|  |  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |  |             numcols = sqlite3_column_count(self->statement->st); | 
					
						
							|  |  |  |  |             Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             if (self->description == Py_None) { | 
					
						
							|  |  |  |  |                 Py_DECREF(self->description); | 
					
						
							|  |  |  |  |                 self->description = PyTuple_New(numcols); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |                 if (!self->description) { | 
					
						
							|  |  |  |  |                     goto error; | 
					
						
							|  |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 for (i = 0; i < numcols; i++) { | 
					
						
							|  |  |  |  |                     descriptor = PyTuple_New(7); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |                     if (!descriptor) { | 
					
						
							|  |  |  |  |                         goto error; | 
					
						
							|  |  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                     PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i))); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); | 
					
						
							|  |  |  |  |                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); | 
					
						
							|  |  |  |  |                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); | 
					
						
							|  |  |  |  |                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None); | 
					
						
							|  |  |  |  |                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 5, Py_None); | 
					
						
							|  |  |  |  |                     Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 6, Py_None); | 
					
						
							|  |  |  |  |                     PyTuple_SetItem(self->description, i, descriptor); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (rc == SQLITE_ROW) { | 
					
						
							|  |  |  |  |             if (multiple) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |                 PyErr_SetString(pysqlite_ProgrammingError, "executemany() can only execute DML statements."); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |                 goto error; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |             self->next_row = _pysqlite_fetch_one_row(self); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         } else if (rc == SQLITE_DONE && !multiple) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |             pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             Py_DECREF(self->statement); | 
					
						
							|  |  |  |  |             self->statement = 0; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         switch (statement_type) { | 
					
						
							|  |  |  |  |             case STATEMENT_UPDATE: | 
					
						
							|  |  |  |  |             case STATEMENT_DELETE: | 
					
						
							|  |  |  |  |             case STATEMENT_INSERT: | 
					
						
							|  |  |  |  |             case STATEMENT_REPLACE: | 
					
						
							|  |  |  |  |                 Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |  |                 rowcount += (long)sqlite3_changes(self->connection->db); | 
					
						
							|  |  |  |  |                 Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |  |                 Py_DECREF(self->rowcount); | 
					
						
							|  |  |  |  |                 self->rowcount = PyInt_FromLong(rowcount); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         Py_DECREF(self->lastrowid); | 
					
						
							|  |  |  |  |         if (statement_type == STATEMENT_INSERT) { | 
					
						
							|  |  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |  |             lastrowid = sqlite3_last_insert_rowid(self->connection->db); | 
					
						
							|  |  |  |  |             Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |  |             self->lastrowid = PyInt_FromLong((long)lastrowid); | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |             self->lastrowid = Py_None; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (multiple) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |             rc = pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |         Py_XDECREF(parameters); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | error: | 
					
						
							|  |  |  |  |     Py_XDECREF(operation_bytestr); | 
					
						
							|  |  |  |  |     Py_XDECREF(parameters); | 
					
						
							| 
									
										
										
										
											2006-04-09 04:07:39 +00:00
										 |  |  |  |     Py_XDECREF(parameters_iter); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     Py_XDECREF(parameters_list); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (PyErr_Occurred()) { | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |         Py_INCREF(self); | 
					
						
							|  |  |  |  |         return (PyObject*)self; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     return _pysqlite_query_execute(self, 0, args); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_executemany(pysqlite_Cursor* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     return _pysqlite_query_execute(self, 1, args); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_executescript(pysqlite_Cursor* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     PyObject* script_obj; | 
					
						
							|  |  |  |  |     PyObject* script_str = NULL; | 
					
						
							|  |  |  |  |     const char* script_cstr; | 
					
						
							|  |  |  |  |     sqlite3_stmt* statement; | 
					
						
							|  |  |  |  |     int rc; | 
					
						
							|  |  |  |  |     PyObject* result; | 
					
						
							|  |  |  |  |     int statement_completed = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!PyArg_ParseTuple(args, "O", &script_obj)) { | 
					
						
							|  |  |  |  |         return NULL;  | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (PyString_Check(script_obj)) { | 
					
						
							|  |  |  |  |         script_cstr = PyString_AsString(script_obj); | 
					
						
							|  |  |  |  |     } else if (PyUnicode_Check(script_obj)) { | 
					
						
							|  |  |  |  |         script_str = PyUnicode_AsUTF8String(script_obj); | 
					
						
							| 
									
										
										
										
											2006-04-04 07:25:25 +00:00
										 |  |  |  |         if (!script_str) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             return NULL; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         script_cstr = PyString_AsString(script_str); | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         PyErr_SetString(PyExc_ValueError, "script argument must be unicode or string."); | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /* commit first */ | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     result = pysqlite_connection_commit(self->connection, NULL); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     if (!result) { | 
					
						
							|  |  |  |  |         goto error; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     while (1) { | 
					
						
							|  |  |  |  |         if (!sqlite3_complete(script_cstr)) { | 
					
						
							|  |  |  |  |             break; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         statement_completed = 1; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         rc = sqlite3_prepare(self->connection->db, | 
					
						
							|  |  |  |  |                              script_cstr, | 
					
						
							|  |  |  |  |                              -1, | 
					
						
							|  |  |  |  |                              &statement, | 
					
						
							|  |  |  |  |                              &script_cstr); | 
					
						
							|  |  |  |  |         if (rc != SQLITE_OK) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |             _pysqlite_seterror(self->connection->db); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         /* execute statement, and ignore results of SELECT statements */ | 
					
						
							|  |  |  |  |         rc = SQLITE_ROW; | 
					
						
							|  |  |  |  |         while (rc == SQLITE_ROW) { | 
					
						
							|  |  |  |  |             rc = _sqlite_step_with_busyhandler(statement, self->connection); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (rc != SQLITE_DONE) { | 
					
						
							|  |  |  |  |             (void)sqlite3_finalize(statement); | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |             _pysqlite_seterror(self->connection->db); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         rc = sqlite3_finalize(statement); | 
					
						
							|  |  |  |  |         if (rc != SQLITE_OK) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |             _pysqlite_seterror(self->connection->db); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             goto error; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | error: | 
					
						
							|  |  |  |  |     Py_XDECREF(script_str); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!statement_completed) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         PyErr_SetString(pysqlite_ProgrammingError, "you did not provide a complete SQL statement"); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (PyErr_Occurred()) { | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |         Py_INCREF(self); | 
					
						
							|  |  |  |  |         return (PyObject*)self; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_getiter(pysqlite_Cursor *self) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     Py_INCREF(self); | 
					
						
							|  |  |  |  |     return (PyObject*)self; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     PyObject* next_row_tuple; | 
					
						
							|  |  |  |  |     PyObject* next_row; | 
					
						
							|  |  |  |  |     int rc; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!self->next_row) { | 
					
						
							|  |  |  |  |          if (self->statement) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |             (void)pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |             Py_DECREF(self->statement); | 
					
						
							|  |  |  |  |             self->statement = NULL; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     next_row_tuple = self->next_row; | 
					
						
							|  |  |  |  |     self->next_row = NULL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (self->row_factory != Py_None) { | 
					
						
							|  |  |  |  |         next_row = PyObject_CallFunction(self->row_factory, "OO", self, next_row_tuple); | 
					
						
							|  |  |  |  |         Py_DECREF(next_row_tuple); | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         next_row = next_row_tuple; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     if (self->statement) { | 
					
						
							|  |  |  |  |         rc = _sqlite_step_with_busyhandler(self->statement->st, self->connection); | 
					
						
							|  |  |  |  |         if (rc != SQLITE_DONE && rc != SQLITE_ROW) { | 
					
						
							|  |  |  |  |             Py_DECREF(next_row); | 
					
						
							|  |  |  |  |             _pysqlite_seterror(self->connection->db); | 
					
						
							|  |  |  |  |             return NULL; | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         if (rc == SQLITE_ROW) { | 
					
						
							|  |  |  |  |             self->next_row = _pysqlite_fetch_one_row(self); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return next_row; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     PyObject* row; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     row = pysqlite_cursor_iternext(self); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     if (!row && !PyErr_Occurred()) { | 
					
						
							|  |  |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |         return Py_None; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return row; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_fetchmany(pysqlite_Cursor* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     PyObject* row; | 
					
						
							|  |  |  |  |     PyObject* list; | 
					
						
							|  |  |  |  |     int maxrows = self->arraysize; | 
					
						
							|  |  |  |  |     int counter = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!PyArg_ParseTuple(args, "|i", &maxrows)) { | 
					
						
							|  |  |  |  |         return NULL;  | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     list = PyList_New(0); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!list) { | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     /* just make sure we enter the loop */ | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     row = Py_None; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     while (row) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         row = pysqlite_cursor_iternext(self); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         if (row) { | 
					
						
							|  |  |  |  |             PyList_Append(list, row); | 
					
						
							|  |  |  |  |             Py_DECREF(row); | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             break; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (++counter == maxrows) { | 
					
						
							|  |  |  |  |             break; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (PyErr_Occurred()) { | 
					
						
							|  |  |  |  |         Py_DECREF(list); | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         return list; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     PyObject* row; | 
					
						
							|  |  |  |  |     PyObject* list; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     list = PyList_New(0); | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     if (!list) { | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     /* just make sure we enter the loop */ | 
					
						
							| 
									
										
										
										
											2006-04-04 06:29:05 +00:00
										 |  |  |  |     row = (PyObject*)Py_None; | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     while (row) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         row = pysqlite_cursor_iternext(self); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         if (row) { | 
					
						
							|  |  |  |  |             PyList_Append(list, row); | 
					
						
							|  |  |  |  |             Py_DECREF(row); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (PyErr_Occurred()) { | 
					
						
							|  |  |  |  |         Py_DECREF(list); | 
					
						
							|  |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         return list; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     /* don't care, return None */ | 
					
						
							|  |  |  |  |     Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |     return Py_None; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     if (!pysqlite_check_thread(self->connection) || !pysqlite_check_connection(self->connection)) { | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         return NULL; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (self->statement) { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         (void)pysqlite_statement_reset(self->statement); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         Py_DECREF(self->statement); | 
					
						
							|  |  |  |  |         self->statement = 0; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Py_INCREF(Py_None); | 
					
						
							|  |  |  |  |     return Py_None; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | static PyMethodDef cursor_methods[] = { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"execute", (PyCFunction)pysqlite_cursor_execute, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         PyDoc_STR("Executes a SQL statement.")}, | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"executemany", (PyCFunction)pysqlite_cursor_executemany, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         PyDoc_STR("Repeatedly executes a SQL statement.")}, | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"executescript", (PyCFunction)pysqlite_cursor_executescript, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"fetchone", (PyCFunction)pysqlite_cursor_fetchone, METH_NOARGS, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         PyDoc_STR("Fetches several rows from the resultset.")}, | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"fetchmany", (PyCFunction)pysqlite_cursor_fetchmany, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         PyDoc_STR("Fetches all rows from the resultset.")}, | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"fetchall", (PyCFunction)pysqlite_cursor_fetchall, METH_NOARGS, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         PyDoc_STR("Fetches one row from the resultset.")}, | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"close", (PyCFunction)pysqlite_cursor_close, METH_NOARGS, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         PyDoc_STR("Closes the cursor.")}, | 
					
						
							|  |  |  |  |     {"setinputsizes", (PyCFunction)pysqlite_noop, METH_VARARGS, | 
					
						
							|  |  |  |  |         PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, | 
					
						
							|  |  |  |  |     {"setoutputsize", (PyCFunction)pysqlite_noop, METH_VARARGS, | 
					
						
							|  |  |  |  |         PyDoc_STR("Required by DB-API. Does nothing in pysqlite.")}, | 
					
						
							|  |  |  |  |     {NULL, NULL} | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | static struct PyMemberDef cursor_members[] = | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     {"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), RO}, | 
					
						
							|  |  |  |  |     {"description", T_OBJECT, offsetof(pysqlite_Cursor, description), RO}, | 
					
						
							|  |  |  |  |     {"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0}, | 
					
						
							|  |  |  |  |     {"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), RO}, | 
					
						
							|  |  |  |  |     {"rowcount", T_OBJECT, offsetof(pysqlite_Cursor, rowcount), RO}, | 
					
						
							|  |  |  |  |     {"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0}, | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |     {NULL} | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  | static char cursor_doc[] = | 
					
						
							|  |  |  |  | PyDoc_STR("SQLite database cursor class."); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | PyTypeObject pysqlite_CursorType = { | 
					
						
							| 
									
										
										
										
											2007-07-21 06:55:02 +00:00
										 |  |  |  |         PyVarObject_HEAD_INIT(NULL, 0) | 
					
						
							| 
									
										
										
										
											2006-04-05 18:25:33 +00:00
										 |  |  |  |         MODULE_NAME ".Cursor",                          /* tp_name */ | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         sizeof(pysqlite_Cursor),                        /* tp_basicsize */ | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         0,                                              /* tp_itemsize */ | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         (destructor)pysqlite_cursor_dealloc,            /* tp_dealloc */ | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         0,                                              /* tp_print */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_getattr */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_setattr */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_compare */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_repr */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_as_number */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_as_sequence */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_as_mapping */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_hash */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_call */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_str */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_getattro */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_setattro */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_as_buffer */ | 
					
						
							|  |  |  |  |         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_ITER|Py_TPFLAGS_BASETYPE, /* tp_flags */ | 
					
						
							| 
									
										
										
										
											2006-04-23 15:24:26 +00:00
										 |  |  |  |         cursor_doc,                                     /* tp_doc */ | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         0,                                              /* tp_traverse */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_clear */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_richcompare */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_weaklistoffset */ | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         (getiterfunc)pysqlite_cursor_getiter,           /* tp_iter */ | 
					
						
							|  |  |  |  |         (iternextfunc)pysqlite_cursor_iternext,         /* tp_iternext */ | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         cursor_methods,                                 /* tp_methods */ | 
					
						
							|  |  |  |  |         cursor_members,                                 /* tp_members */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_getset */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_base */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_dict */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_descr_get */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_descr_set */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_dictoffset */ | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |         (initproc)pysqlite_cursor_init,                 /* tp_init */ | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  |         0,                                              /* tp_alloc */ | 
					
						
							|  |  |  |  |         0,                                              /* tp_new */ | 
					
						
							|  |  |  |  |         0                                               /* tp_free */ | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  | extern int pysqlite_cursor_setup_types(void) | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-01-14 01:43:50 +00:00
										 |  |  |  |     pysqlite_CursorType.tp_new = PyType_GenericNew; | 
					
						
							|  |  |  |  |     return PyType_Ready(&pysqlite_CursorType); | 
					
						
							| 
									
										
										
										
											2006-04-01 00:57:31 +00:00
										 |  |  |  | } |