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