@@ -37,44 +37,6 @@ PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
3737
3838static char * errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from." ;
3939
40- static pysqlite_StatementKind detect_statement_type (char * statement )
41- {
42- char buf [20 ];
43- char * src ;
44- char * dst ;
45-
46- src = statement ;
47- /* skip over whitepace */
48- while (* src == '\r' || * src == '\n' || * src == ' ' || * src == '\t' ) {
49- src ++ ;
50- }
51-
52- if (* src == 0 )
53- return STATEMENT_INVALID ;
54-
55- dst = buf ;
56- * dst = 0 ;
57- while (Py_ISALPHA (* src ) && dst - buf < sizeof (buf ) - 2 ) {
58- * dst ++ = Py_TOLOWER (* src ++ );
59- }
60-
61- * dst = 0 ;
62-
63- if (!strcmp (buf , "select" )) {
64- return STATEMENT_SELECT ;
65- } else if (!strcmp (buf , "insert" )) {
66- return STATEMENT_INSERT ;
67- } else if (!strcmp (buf , "update" )) {
68- return STATEMENT_UPDATE ;
69- } else if (!strcmp (buf , "delete" )) {
70- return STATEMENT_DELETE ;
71- } else if (!strcmp (buf , "replace" )) {
72- return STATEMENT_REPLACE ;
73- } else {
74- return STATEMENT_OTHER ;
75- }
76- }
77-
7840static int pysqlite_cursor_init (pysqlite_Cursor * self , PyObject * args , PyObject * kwargs )
7941{
8042 pysqlite_Connection * connection ;
@@ -454,7 +416,6 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
454416 PyObject * func_args ;
455417 PyObject * result ;
456418 int numcols ;
457- int statement_type ;
458419 PyObject * descriptor ;
459420 PyObject * second_argument = NULL ;
460421 int allow_8bit_chars ;
@@ -550,7 +511,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
550511 Py_DECREF (self -> description );
551512 Py_INCREF (Py_None );
552513 self -> description = Py_None ;
553- self -> rowcount = -1L ;
514+ self -> rowcount = 0L ;
554515
555516 func_args = PyTuple_New (1 );
556517 if (!func_args ) {
@@ -589,39 +550,13 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
589550 pysqlite_statement_reset (self -> statement );
590551 pysqlite_statement_mark_dirty (self -> statement );
591552
592- statement_type = detect_statement_type (operation_cstr );
593- if (self -> connection -> begin_statement ) {
594- switch (statement_type ) {
595- case STATEMENT_UPDATE :
596- case STATEMENT_DELETE :
597- case STATEMENT_INSERT :
598- case STATEMENT_REPLACE :
599- if (sqlite3_get_autocommit (self -> connection -> db )) {
600- result = _pysqlite_connection_begin (self -> connection );
601- if (!result ) {
602- goto error ;
603- }
604- Py_DECREF (result );
605- }
606- break ;
607- case STATEMENT_OTHER :
608- /* it's a DDL statement or something similar
609- - we better COMMIT first so it works for all cases */
610- if (!sqlite3_get_autocommit (self -> connection -> db )) {
611- result = pysqlite_connection_commit (self -> connection , NULL );
612- if (!result ) {
613- goto error ;
614- }
615- Py_DECREF (result );
616- }
617- break ;
618- case STATEMENT_SELECT :
619- if (multiple ) {
620- PyErr_SetString (pysqlite_ProgrammingError ,
621- "You cannot execute SELECT statements in executemany()." );
622- goto error ;
623- }
624- break ;
553+ if (self -> connection -> begin_statement && !sqlite3_stmt_readonly (self -> statement -> st )) {
554+ if (sqlite3_get_autocommit (self -> connection -> db )) {
555+ result = _pysqlite_connection_begin (self -> connection );
556+ if (!result ) {
557+ goto error ;
558+ }
559+ Py_DECREF (result );
625560 }
626561 }
627562
@@ -658,7 +593,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
658593 goto error ;
659594 }
660595
661- if (rc == SQLITE_ROW || ( rc == SQLITE_DONE && statement_type == STATEMENT_SELECT ) ) {
596+ if (rc == SQLITE_ROW || rc == SQLITE_DONE ) {
662597 if (self -> description == Py_None ) {
663598 Py_BEGIN_ALLOW_THREADS
664599 numcols = sqlite3_column_count (self -> statement -> st );
@@ -686,6 +621,21 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
686621 }
687622 }
688623
624+ if (!sqlite3_stmt_readonly (self -> statement -> st )) {
625+ self -> rowcount += (long )sqlite3_changes (self -> connection -> db );
626+ } else {
627+ self -> rowcount = -1 ;
628+ }
629+
630+ Py_DECREF (self -> lastrowid );
631+ if (!multiple ) {
632+ sqlite_int64 lastrowid ;
633+ Py_BEGIN_ALLOW_THREADS
634+ lastrowid = sqlite3_last_insert_rowid (self -> connection -> db );
635+ Py_END_ALLOW_THREADS
636+ self -> lastrowid = _pysqlite_long_from_int64 (lastrowid );
637+ }
638+
689639 if (rc == SQLITE_ROW ) {
690640 if (multiple ) {
691641 PyErr_SetString (pysqlite_ProgrammingError , "executemany() can only execute DML statements." );
@@ -698,29 +648,6 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
698648 Py_CLEAR (self -> statement );
699649 }
700650
701- switch (statement_type ) {
702- case STATEMENT_UPDATE :
703- case STATEMENT_DELETE :
704- case STATEMENT_INSERT :
705- case STATEMENT_REPLACE :
706- if (self -> rowcount == -1L ) {
707- self -> rowcount = 0L ;
708- }
709- self -> rowcount += (long )sqlite3_changes (self -> connection -> db );
710- }
711-
712- Py_DECREF (self -> lastrowid );
713- if (!multiple && statement_type == STATEMENT_INSERT ) {
714- sqlite_int64 lastrowid ;
715- Py_BEGIN_ALLOW_THREADS
716- lastrowid = sqlite3_last_insert_rowid (self -> connection -> db );
717- Py_END_ALLOW_THREADS
718- self -> lastrowid = _pysqlite_long_from_int64 (lastrowid );
719- } else {
720- Py_INCREF (Py_None );
721- self -> lastrowid = Py_None ;
722- }
723-
724651 if (multiple ) {
725652 rc = pysqlite_statement_reset (self -> statement );
726653 }
0 commit comments