]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Make sqlite3_count_changes() and total_changes() work with "DELETE FROM <table-name...
authordanielk1977 <danielk1977@noemail.net>
Mon, 27 Oct 2008 13:59:33 +0000 (13:59 +0000)
committerdanielk1977 <danielk1977@noemail.net>
Mon, 27 Oct 2008 13:59:33 +0000 (13:59 +0000)
FossilOrigin-Name: e68e4282adb9003aa297d033aeb5d9cadee215cd

manifest
manifest.uuid
src/btree.c
src/btree.h
src/delete.c
src/test3.c
src/vdbe.c
test/laststmtchanges.test

index 22f03526b0c427aee1c971bdfe6460a2c90a1021..1e706f68436e9e1d62adbf63c4d557b68e1508b2 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Remove\ssome\sif()\sconditions\sthat\sare\salways\strue\sfrom\sdelete.c.\s(CVS\s5843)
-D 2008-10-27T08:24:38
+C Make\ssqlite3_count_changes()\sand\stotal_changes()\swork\swith\s"DELETE\sFROM\s<table-name>".\s(CVS\s5844)
+D 2008-10-27T13:59:34
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 3fe17eccd87d385b5adc9766828716cfdd154d6b
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -99,14 +99,14 @@ F src/attach.c db3f4a60538733c1e4dcb9d0217a6e0d6ccd615b
 F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627
 F src/bitvec.c 95c86bd18d8fedf0533f5af196192546e10a7e7d
 F src/btmutex.c 3a90096c3080b9057dc570b8e16e46511e1c788a
-F src/btree.c 3e686f899659df8d79f2587d0f34f51fdf6a6276
-F src/btree.h 903682f2a88da37435f103da00cb915d63bc8015
+F src/btree.c 5d72b67ee20afba92e95827fc9fc599d917f7f68
+F src/btree.h 179c3ea813780df78a289a8f5130db18e6d4616e
 F src/btreeInt.h e38e9b2b285f40f5bc0a6664f630d4a141622f16
 F src/build.c d6d55f97abcb6f17ac3e9e7a1dd8c0db67db34fd
 F src/callback.c e970e5beddbdb23f89a6d05cb1a6419d9f755624
 F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c
 F src/date.c 6f4277fa56d8c1b8e70c0bde838c9e99609f5ec0
-F src/delete.c 8bb079e098c0d154f0469062ddbc8237d1cc0b55
+F src/delete.c b0b7c499ccf28c1857905c9cf42f4ddc49bf7331
 F src/expr.c 2b1945314fdc661fb04306cb86bd8516cfd12d4a
 F src/fault.c dc88c821842157460750d2d61a8a8b4197d047ff
 F src/func.c 8431b40a7843d1024145684d303c55b4ee087bbe
@@ -160,7 +160,7 @@ F src/table.c 22744786199c9195720c15a7a42cb97b2e2728d8
 F src/tclsqlite.c 96049bd454f1547abff0a57c45f0dfa57701e076
 F src/test1.c a7310348961945e656c096e5f91d4d87dee6b61d
 F src/test2.c 897528183edf2839c2a3c991d415905db56f1240
-F src/test3.c e85b7ce5c28c3ce7fbdbf7f98e1467b19786c62b
+F src/test3.c 88a246b56b824275300e6c899634fbac1dc94b14
 F src/test4.c f79ab52d27ff49b784b631a42e2ccd52cfd5c84c
 F src/test5.c 162a1cea2105a2c460a3f39fa6919617b562a288
 F src/test6.c 0a0304a69cfa4962a429d084c6d451ff9e4fb572
@@ -191,7 +191,7 @@ F src/update.c ed233298db6dd23d340e449e4b4a81b9306da6aa
 F src/utf.c c63e6f69082f85c19ab88d62dedaf91d71ac1a50
 F src/util.c afe659ccc05d1f8af9e8631dabfec3ee3a7144af
 F src/vacuum.c fd77433d0c26d3ff1eb96eab017a1787ac5aa642
-F src/vdbe.c b78adf571b9abac4092c641bec148832e8f26f34
+F src/vdbe.c be81532805ee6fc1fe7bd2e1c82cf4a09b7b3061
 F src/vdbe.h 41c99aaad9167c1b5431993db446de74b2f71fc3
 F src/vdbeInt.h a20f479ec3bf41c61b0c2bd5176a9bad6b2a07ef
 F src/vdbeapi.c 6798c70cf176d23594ec557db88190ca8c7fa860
@@ -396,7 +396,7 @@ F test/join5.test 86675fc2919269aa923c84dd00ee4249b97990fe
 F test/journal1.test 36f2d1bb9bf03f790f43fbdb439e44c0657fab19
 F test/jrnlmode.test c2e8212a6f6d32f8fd8f869b93553154eaf2244c
 F test/lastinsert.test 474d519c68cb79d07ecae56a763aa7f322c72f51
-F test/laststmtchanges.test 18ead86c8a87ade949a1d5658f6dc4bb111d1b02
+F test/laststmtchanges.test ae613f53819206b3222771828d024154d51db200
 F test/like.test fef924922828d5a2a5bff80b9bdd9ff57a1ca500
 F test/like2.test 3b2ee13149ba4a8a60b59756f4e5d345573852da
 F test/limit.test 2db7b3b34fb925b8e847d583d2eb67531d0ce67e
@@ -651,7 +651,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P 3224ea59812d0f3b5685bd92751054b81e3b681e
-R ba54cbfc3be9ecc00ba76b8e1609779a
+P 297ad90dd3a69002e6e4175e9e3938b3a627574d
+R a6b93c46cddb9e42feecabc035375dbb
 U danielk1977
-Z ccda3a5856c2017497ad6a21acb2893e
+Z 98ec3e2e89a662c96092f7315cee2195
index 958e7b8d26b416ffb6600932903b279d591a3f8d..10af58814539725788407759c221c514f5c2dcda 100644 (file)
@@ -1 +1 @@
-297ad90dd3a69002e6e4175e9e3938b3a627574d
\ No newline at end of file
+e68e4282adb9003aa297d033aeb5d9cadee215cd
\ No newline at end of file
index 538a706d6b8c6919338d7e5087e522b84c12dc15..d94d9857a112d2d797556be1ac6b06c24d65e2de 100644 (file)
@@ -9,7 +9,7 @@
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** $Id: btree.c,v 1.525 2008/10/08 17:58:49 danielk1977 Exp $
+** $Id: btree.c,v 1.526 2008/10/27 13:59:34 danielk1977 Exp $
 **
 ** This file implements a external (disk-based) database using BTrees.
 ** See the header comment on "btreeInt.h" for additional information.
@@ -6187,7 +6187,8 @@ static int clearDatabasePage(
   BtShared *pBt,           /* The BTree that contains the table */
   Pgno pgno,            /* Page number to clear */
   MemPage *pParent,     /* Parent page.  NULL for the root */
-  int freePageFlag      /* Deallocate page if true */
+  int freePageFlag,     /* Deallocate page if true */
+  int *pnChange
 ){
   MemPage *pPage = 0;
   int rc;
@@ -6204,15 +6205,18 @@ static int clearDatabasePage(
   for(i=0; i<pPage->nCell; i++){
     pCell = findCell(pPage, i);
     if( !pPage->leaf ){
-      rc = clearDatabasePage(pBt, get4byte(pCell), pPage, 1);
+      rc = clearDatabasePage(pBt, get4byte(pCell), pPage, 1, pnChange);
       if( rc ) goto cleardatabasepage_out;
     }
     rc = clearCell(pPage, pCell);
     if( rc ) goto cleardatabasepage_out;
   }
   if( !pPage->leaf ){
-    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage, 1);
+    rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage, 1, pnChange);
     if( rc ) goto cleardatabasepage_out;
+  }else if( pnChange ){
+    assert( pPage->intKey );
+    *pnChange += pPage->nCell;
   }
   if( freePageFlag ){
     rc = freePage(pPage);
@@ -6233,8 +6237,12 @@ cleardatabasepage_out:
 ** This routine will fail with SQLITE_LOCKED if there are any open
 ** read cursors on the table.  Open write cursors are moved to the
 ** root of the table.
+**
+** If pnChange is not NULL, then table iTable must be an intkey table. The
+** integer value pointed to by pnChange is incremented by the number of
+** entries in the table.
 */
-int sqlite3BtreeClearTable(Btree *p, int iTable){
+int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
   int rc;
   BtShared *pBt = p->pBt;
   sqlite3BtreeEnter(p);
@@ -6246,7 +6254,7 @@ int sqlite3BtreeClearTable(Btree *p, int iTable){
   }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
     /* nothing to do */
   }else{
-    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
+    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0, pnChange);
   }
   sqlite3BtreeLeave(p);
   return rc;
@@ -6294,7 +6302,7 @@ static int btreeDropTable(Btree *p, int iTable, int *piMoved){
 
   rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
   if( rc ) return rc;
-  rc = sqlite3BtreeClearTable(p, iTable);
+  rc = sqlite3BtreeClearTable(p, iTable, 0);
   if( rc ){
     releasePage(pPage);
     return rc;
index 3f8c4ea4bae37f068e34fdf46ebc3ba33288e5fd..ba16730863ee07033cc17a500ad1968050fb5b12 100644 (file)
@@ -13,7 +13,7 @@
 ** subsystem.  See comments in the source code for a detailed description
 ** of what each interface routine does.
 **
-** @(#) $Id: btree.h,v 1.104 2008/10/08 17:58:49 danielk1977 Exp $
+** @(#) $Id: btree.h,v 1.105 2008/10/27 13:59:34 danielk1977 Exp $
 */
 #ifndef _BTREE_H_
 #define _BTREE_H_
@@ -117,7 +117,7 @@ int sqlite3BtreeIncrVacuum(Btree *);
 #define BTREE_LEAFDATA   4    /* Data stored in leaves only.  Implies INTKEY */
 
 int sqlite3BtreeDropTable(Btree*, int, int*);
-int sqlite3BtreeClearTable(Btree*, int);
+int sqlite3BtreeClearTable(Btree*, int, int*);
 int sqlite3BtreeGetMeta(Btree*, int idx, u32 *pValue);
 int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
 void sqlite3BtreeTripAllCursors(Btree*, int);
index ef036cba921eba9f3b3d2cb3a8ad4500b0fd1105..382f1a67f0a5cfb05cc6d3b1da285cf228adfaf1 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** in order to generate code for DELETE FROM statements.
 **
-** $Id: delete.c,v 1.183 2008/10/27 08:24:38 danielk1977 Exp $
+** $Id: delete.c,v 1.184 2008/10/27 13:59:34 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -233,7 +233,7 @@ void sqlite3DeleteFrom(
   int oldIdx = -1;       /* Cursor for the OLD table of AFTER triggers */
   NameContext sNC;       /* Name context to resolve expressions in */
   int iDb;               /* Database number */
-  int memCnt = 0;        /* Memory cell used for change counting */
+  int memCnt = -1;       /* Memory cell used for change counting */
 
 #ifndef SQLITE_OMIT_TRIGGER
   int isView;                  /* True if attempting to delete from a view */
@@ -372,17 +372,7 @@ void sqlite3DeleteFrom(
   */
   if( pWhere==0 && !triggers_exist && !IsVirtual(pTab) ){
     assert( !isView );
-    if( db->flags & SQLITE_CountRows ){
-      /* If counting rows deleted, just count the total number of
-      ** entries in the table. */
-      int addr2;
-      sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
-      sqlite3VdbeAddOp2(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2);
-      addr2 = sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
-      sqlite3VdbeAddOp2(v, OP_Next, iCur, addr2);
-      sqlite3VdbeAddOp1(v, OP_Close, iCur);
-    }
-    sqlite3VdbeAddOp2(v, OP_Clear, pTab->tnum, iDb);
+    sqlite3VdbeAddOp3(v, OP_Clear, pTab->tnum, iDb, memCnt);
     if( !pParse->nested ){
       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
     }
index 4b75690fffe97cd9c6037c3c462ee3d25a79f569..0d915f3cb56ed5f340d2fd94f8d8300bb4f64d99 100644 (file)
@@ -13,7 +13,7 @@
 ** is not included in the SQLite library.  It is used for automated
 ** testing of the SQLite library.
 **
-** $Id: test3.c,v 1.101 2008/08/13 19:11:48 drh Exp $
+** $Id: test3.c,v 1.102 2008/10/27 13:59:34 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include "btreeInt.h"
@@ -389,7 +389,7 @@ static int btree_clear_table(
   pBt = sqlite3TestTextToPtr(argv[1]);
   if( Tcl_GetInt(interp, argv[2], &iTable) ) return TCL_ERROR;
   sqlite3BtreeEnter(pBt);
-  rc = sqlite3BtreeClearTable(pBt, iTable);
+  rc = sqlite3BtreeClearTable(pBt, iTable, 0);
   sqlite3BtreeLeave(pBt);
   if( rc!=SQLITE_OK ){
     Tcl_AppendResult(interp, errorName(rc), 0);
index 6ee22c1b6e76e356a97b4ff36e04e5851a65710f..a4a62b8bd1961c0fd5f58b81daa23f857a7e85db 100644 (file)
@@ -43,7 +43,7 @@
 ** in this file for details.  If in doubt, do not deviate from existing
 ** commenting and indentation practices when changing or adding code.
 **
-** $Id: vdbe.c,v 1.782 2008/10/08 17:58:49 danielk1977 Exp $
+** $Id: vdbe.c,v 1.783 2008/10/27 13:59:34 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -4042,7 +4042,7 @@ case OP_Destroy: {     /* out2-prerelease */
   break;
 }
 
-/* Opcode: Clear P1 P2 *
+/* Opcode: Clear P1 P2 P3
 **
 ** Delete all contents of the database table or index whose root page
 ** in the database file is given by P1.  But, unlike Destroy, do not
@@ -4052,11 +4052,26 @@ case OP_Destroy: {     /* out2-prerelease */
 ** P2==1 then the table to be clear is in the auxiliary database file
 ** that is used to store tables create using CREATE TEMPORARY TABLE.
 **
+** If the P3 value is non-zero, then the table refered to must be an
+** intkey table (an SQL table, not an index). In this case the row change 
+** count is incremented by the number of rows in the table being cleared. 
+** If P3 is greater than zero, then the value stored in register P3 is
+** also incremented by the number of rows in the table being cleared.
+**
 ** See also: Destroy
 */
 case OP_Clear: {
+  int nChange = 0;
   assert( (p->btreeMask & (1<<pOp->p2))!=0 );
-  rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
+  rc = sqlite3BtreeClearTable(
+      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
+  );
+  if( pOp->p3 ){
+    p->nChange += nChange;
+    if( pOp->p3>0 ){
+      p->aMem[pOp->p3].u.i += nChange;
+    }
+  }
   break;
 }
 
index 13bb5facfdc9663a8835f740d771fcff4b26405e..ea4c3892b75d257cf51a3f71864c537b93a4ba67 100644 (file)
@@ -1,3 +1,4 @@
+#
 # The author disclaims copyright to this source code.  In place of
 # a legal notice, here is a blessing:
 #
@@ -19,6 +20,7 @@
 # Note 3: changes() is not changed by a change to a view (since everything
 #         is done within instead of trigger context).
 #
+# $Id: laststmtchanges.test,v 1.7 2008/10/27 13:59:34 danielk1977 Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -278,4 +280,52 @@ do_test laststmtchanges-5.5 {
 
 } ;# ifcapable view
 
+
+# ----------------------------------------------------------------------------
+# 6.x - Test "DELETE FROM <table>" in the absence of triggers
+#
+do_test laststmtchanges-6.1 {
+  execsql {
+    CREATE TABLE t3(a, b, c);
+    INSERT INTO t3 VALUES(1, 2, 3);
+    INSERT INTO t3 VALUES(4, 5, 6);
+  }
+} {}
+do_test laststmtchanges-6.2 {
+  execsql {
+    BEGIN;
+    DELETE FROM t3;
+    SELECT changes();
+  }
+} {2}
+do_test laststmtchanges-6.3 {
+  execsql {
+    ROLLBACK;
+    BEGIN;
+    DELETE FROM t3 WHERE a IS NOT NULL;
+    SELECT changes();
+  }
+} {2}
+do_test laststmtchanges-6.4 {
+  execsql {
+    ROLLBACK;
+    CREATE INDEX t3_i1 ON t3(a);
+    BEGIN;
+    DELETE FROM t3;
+    SELECT changes();
+  }
+} {2}
+do_test laststmtchanges-6.5 {
+  execsql { ROLLBACK }
+  set nTotalChange [execsql {SELECT total_changes()}]
+  expr 0
+} {0}
+do_test laststmtchanges-6.6 {
+  execsql {
+    SELECT total_changes();
+    DELETE FROM t3;
+    SELECT total_changes();
+  }
+} [list $nTotalChange [expr $nTotalChange+2]]
+
 finish_test