Take advantage of the fact that b-tree cursors need not be closed while other cursors modify their tables to simplify trigger program generation code. (CVS 4654)

FossilOrigin-Name: f1966a8a47fca85f7862c0797a527ab01ac8b0c1
diff --git a/src/insert.c b/src/insert.c
index 6e3440d..26eb7a2 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle INSERT statements in SQLite.
 **
-** $Id: insert.c,v 1.199 2008/01/02 00:34:37 drh Exp $
+** $Id: insert.c,v 1.200 2008/01/02 11:50:51 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -625,8 +625,8 @@
     sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
   }
 
-  /* Open tables and indices if there are no row triggers */
-  if( !triggers_exist ){
+  /* If this is not a view, open the table and and all indices */
+  if( !isView ){
     base = pParse->nTab;
     sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
   }
@@ -713,14 +713,6 @@
     }
   }
 
-  /* If any triggers exists, the opening of tables and indices is deferred
-  ** until now.
-  */
-  if( triggers_exist && !isView ){
-    base = pParse->nTab;
-    sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
-  }
-
   /* Push the record number for the new entry onto the stack.  The
   ** record number is a randomly generate integer created by NewRowid
   ** except when the table has an INTEGER PRIMARY KEY column, in which
@@ -827,14 +819,6 @@
   }
 
   if( triggers_exist ){
-    /* Close all tables opened */
-    if( !isView ){
-      sqlite3VdbeAddOp(v, OP_Close, base, 0);
-      for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
-        sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
-      }
-    }
-
     /* Code AFTER triggers */
     if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
           newIdx, -1, onError, endOfLoop, 0, 0) ){
@@ -855,7 +839,7 @@
     sqlite3VdbeResolveLabel(v, iCleanup);
   }
 
-  if( !triggers_exist && !IsVirtual(pTab) ){
+  if( !IsVirtual(pTab) && !isView ){
     /* Close all tables opened */
     sqlite3VdbeAddOp(v, OP_Close, base, 0);
     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){