From c6b52df32d43900ad48cb231efa0ff3a9569066a Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 4 Jan 2002 03:09:29 +0000 Subject: [PATCH] An attempt to delete a single row using a WHERE clause that specifies the rowid would result in an error if the rowid did not exist. This problem has been resolved. (CVS 338) FossilOrigin-Name: 011be9a9d2632d261489005d97c69b0a0bc5a108 --- VERSION | 2 +- manifest | 30 +++++++++++++++--------------- manifest.uuid | 2 +- src/btree.c | 37 ++++++++++++++++++++++++++++++++++--- src/btree.h | 3 ++- src/expr.c | 4 ++-- src/insert.c | 4 ++-- src/select.c | 10 +++++----- src/vdbe.c | 45 +++++++++++++++++++++++++++++++++------------ src/where.c | 3 ++- test/rowid.test | 23 ++++++++++++++++++++++- www/changes.tcl | 5 +++++ 12 files changed, 124 insertions(+), 44 deletions(-) diff --git a/VERSION b/VERSION index ccbccc3dc6..c043eea776 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.0 +2.2.1 diff --git a/manifest b/manifest index 28143ca21e..6085832f53 100644 --- a/manifest +++ b/manifest @@ -1,9 +1,9 @@ -C Comment\schanges.\s\sChange\sthe\suse\sof\sBTree\sso\sthat\seither\sthe\skey\sis\nan\sinteger\sor\sthe\sdata\sis\sempty.\s(CVS\s337) -D 2001-12-31T02:48:51 +C An\sattempt\sto\sdelete\sa\ssingle\srow\susing\sa\sWHERE\sclause\sthat\sspecifies\nthe\srowid\swould\sresult\sin\san\serror\sif\sthe\srowid\sdid\snot\sexist.\s\sThis\nproblem\shas\sbeen\sresolved.\s(CVS\s338) +D 2002-01-04T03:09:29 F Makefile.in 352fed589f09dd94347e0bb391d047118ebd6105 F Makefile.template c88ffcb9c339e718f434d0c7f045bcd7eea125af F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0 -F VERSION 353ee68ca2468dce6464d331044643d350fd256f +F VERSION 6565c509ed61af074681258bd346dca53ad38128 F aclocal.m4 11faa843caa38fd451bc6aeb43e248d1723a269d F config.guess f38b1e93d1e0fa6f5a6913e9e7b12774b9232588 F config.log 6a73d03433669b10a3f0c221198c3f26b9413914 @@ -19,14 +19,14 @@ F libtool c56e618713c9510a103bda6b95f3ea3900dcacd6 F ltmain.sh e9ed72eb1d690f447c13945eaf69e28af531eda1 F publish.sh cb0f8f7bcb65b8360d0f6668a216a9ac9d5da892 F src/TODO af7f3cab0228e34149cf98e073aa83d45878e7e6 -F src/btree.c c3c36b3b5f07c3efdabf76df9ea423086b1ce142 -F src/btree.h 8767bd4ecf841c4999b7aee6876906bd607546e7 +F src/btree.c c796e387da340cb628dc1e41f684fc20253f561e +F src/btree.h 9ead7f54c270d8a554e59352ca7318fdaf411390 F src/build.c 6c01002e98204ad2b993d0d043ee56c8c7dc8692 F src/delete.c f7690efc09ad6a2f1f3f0490e1b0cbb676bb95cf -F src/expr.c ef1c365c5d558fa691878830501d3c36ed7edb25 +F src/expr.c fcdb7416e502cb7e26b933ccd44b0baff5b78420 F src/hash.c 6f1a7712ae3aac8351662969aec5693740a2fbf7 F src/hash.h a5f5b3ce2d086a172c5879b0b06a27a82eac9fac -F src/insert.c 74e249cf3c0e7da634eb115edeef77b42ec1d947 +F src/insert.c 7b36ce2e0b555d9d12bbbdbcd579728ad518ec80 F src/main.c 00a9f5603e130fc0b1a05f731731c9c99ebdc2dc F src/md5.c 52f677bfc590e09f71d07d7e327bd59da738d07c F src/os.c 07882cde5c61f26751b8ee76fd84726c1f7e453c @@ -36,7 +36,7 @@ F src/pager.h f78d064c780855ff70beacbeba0e2324471b26fe F src/parse.y f050644e7a2586227686e8c1709aa2662b9bcf9c F src/printf.c 300a90554345751f26e1fc0c0333b90a66110a1d F src/random.c 2a9cc2c9716d14815fd4c2accf89d87a1143e46b -F src/select.c 79120c805dc61513bc5902baa39991896718c5c4 +F src/select.c bddd8b5d07ffdae0d798c10b20dc7167469a3904 F src/shell.c 407095aaeeae78f42deb3e846b1ad77f8ed3b4ef F src/shell.tcl 27ecbd63dd88396ad16d81ab44f73e6c0ea9d20e F src/sqlite.h.in a4c11d38d62b1bfbd50a5804edee8ca54c1adc9b @@ -49,9 +49,9 @@ F src/test3.c d6775f95fd91f5b3cf0e2382a28e5aaeb68f745b F src/tokenize.c 830e9ef684334070a26583d94770bb869e2727bf F src/update.c f9f48e78c13c79e32a55840414bea84479f9848d F src/util.c 8e9ca72d8288cae39c57c6f397abd14a56b14a38 -F src/vdbe.c b00fbd59e4a3264a2eec4dc7103c2cf52e9e49b0 +F src/vdbe.c 90a561f7b9bd8711dd01dbdc491b8eff134af476 F src/vdbe.h e5cc6fb13d1905a4339db4d6dba4ab393c0765fa -F src/where.c 178a908a40cc6d72150a747db69638a97bd86487 +F src/where.c d673265cad96f790923210707b45fdcaec9408f1 F test/all.test 2a51e5395ac7c2c539689b123b9782a05e3837fe F test/bigrow.test 8ab252dba108f12ad64e337b0f2ff31a807ac578 F test/btree.test 6ab4dc5f595905a276ef588fad3c9236dc07a47b @@ -76,7 +76,7 @@ F test/pager.test 59bbc4e3d489529ed33db6e15595789e51056077 F test/printf.test 3cb415073754cb8ff076f26173143c3cd293a9da F test/quick.test 6f023c7a73fc413e6d65b7a1879c79764038dc05 F test/quote.test 286db944717afa9a9bf829dd85e59185c65d5435 -F test/rowid.test ba56df896cb913b70d06661d960869e310fe426e +F test/rowid.test cb023f2df36886e1fc4cdfd32eaba05cf7db4331 F test/select1.test 7423a9dbb81b6d282b13cbe10be64d1216dec155 F test/select2.test ed2c1882857106b85478f54f67000e14966be4c4 F test/select3.test 5e1fe8e5a4e63fb2827ab3b89527e0fd4ae35259 @@ -105,7 +105,7 @@ F www/arch.fig d5f9752a4dbf242e9cfffffd3f5762b6c63b3bcf F www/arch.png 82ef36db1143828a7abc88b1e308a5f55d4336f4 F www/arch.tcl 72a0c80e9054cc7025a50928d28d9c75c02c2b8b F www/c_interface.tcl 9123810452845783fac8e3184929463d9e70d609 -F www/changes.tcl a57ebb01b6bc941b5494a7b169576881c524cc53 +F www/changes.tcl 3a2c158047b9a899dbaa7ff11979b246f568cb6a F www/crosscompile.tcl 3622ebbe518927a3854a12de51344673eb2dd060 F www/download.tcl 1ea61f9d89a2a5a9b2cee36b0d5cf97321bdefe0 F www/dynload.tcl 02eb8273aa78cfa9070dd4501dca937fb22b466c @@ -119,7 +119,7 @@ F www/speed.tcl 83457b2bf6bb430900bd48ca3dd98264d9a916a5 F www/sqlite.tcl 8b5884354cb615049aed83039f8dfe1552a44279 F www/tclsqlite.tcl 880ef67cb4f2797b95bf1368fc4e0d8ca0fda956 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218 -P 6bb62d8fab621b79b2fd1601669b1d4c8a547774 -R c0cff954d526606a2c04afff4a5fc733 +P 18e606f7486eb3a4ab128504d88a44f53d39e5b2 +R 0880ebdecbc9a3b9787a721d544d5701 U drh -Z 124639b155da23bf7001c10245c441bd +Z cf2abd860be64f6e25eafc52151b0f16 diff --git a/manifest.uuid b/manifest.uuid index 5a7e7ede15..9f1edc4895 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -18e606f7486eb3a4ab128504d88a44f53d39e5b2 \ No newline at end of file +011be9a9d2632d261489005d97c69b0a0bc5a108 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 289282b55f..e4b18d53f5 100644 --- a/src/btree.c +++ b/src/btree.c @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.45 2001/12/15 14:22:19 drh Exp $ +** $Id: btree.c,v 1.46 2002/01/04 03:09:29 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to @@ -728,6 +728,7 @@ static int newDatabase(Btree *pBt){ ** unless a transaction is started first: ** ** sqliteBtreeCreateTable() +** sqliteBtreeCreateIndex() ** sqliteBtreeClearTable() ** sqliteBtreeDropTable() ** sqliteBtreeInsert() @@ -2227,8 +2228,13 @@ int sqliteBtreeDelete(BtCursor *pCur){ } /* -** Create a new BTree in the same file. Write into *piTable the index -** of the root page of the new table. +** Create a new BTree table. Write into *piTable the page +** number for the root page of the new table. +** +** In the current implementation, BTree tables and BTree indices are the +** the same. But in the future, we may change this so that BTree tables +** are restricted to having a 4-byte integer key and arbitrary data and +** BTree indices are restricted to having an arbitrary key and no data. */ int sqliteBtreeCreateTable(Btree *pBt, int *piTable){ MemPage *pRoot; @@ -2246,6 +2252,31 @@ int sqliteBtreeCreateTable(Btree *pBt, int *piTable){ return SQLITE_OK; } +/* +** Create a new BTree index. Write into *piTable the page +** number for the root page of the new index. +** +** In the current implementation, BTree tables and BTree indices are the +** the same. But in the future, we may change this so that BTree tables +** are restricted to having a 4-byte integer key and arbitrary data and +** BTree indices are restricted to having an arbitrary key and no data. +*/ +int sqliteBtreeCreateIndex(Btree *pBt, int *piIndex){ + MemPage *pRoot; + Pgno pgnoRoot; + int rc; + if( !pBt->inTrans ){ + return SQLITE_ERROR; /* Must start a transaction first */ + } + rc = allocatePage(pBt, &pRoot, &pgnoRoot); + if( rc ) return rc; + assert( sqlitepager_iswriteable(pRoot) ); + zeroPage(pRoot); + sqlitepager_unref(pRoot); + *piIndex = (int)pgnoRoot; + return SQLITE_OK; +} + /* ** Erase the given database page and all its children. Return ** the page to the freelist. diff --git a/src/btree.h b/src/btree.h index bce1a84d97..a86966136b 100644 --- a/src/btree.h +++ b/src/btree.h @@ -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.18 2001/12/15 14:22:19 drh Exp $ +** @(#) $Id: btree.h,v 1.19 2002/01/04 03:09:30 drh Exp $ */ #ifndef _BTREE_H_ #define _BTREE_H_ @@ -30,6 +30,7 @@ int sqliteBtreeCommit(Btree*); int sqliteBtreeRollback(Btree*); int sqliteBtreeCreateTable(Btree*, int*); +int sqliteBtreeCreateIndex(Btree*, int*); int sqliteBtreeDropTable(Btree*, int); int sqliteBtreeClearTable(Btree*, int); diff --git a/src/expr.c b/src/expr.c index 37f4d28e1b..526ad66220 100644 --- a/src/expr.c +++ b/src/expr.c @@ -12,7 +12,7 @@ ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** -** $Id: expr.c,v 1.35 2001/12/21 14:30:43 drh Exp $ +** $Id: expr.c,v 1.36 2002/01/04 03:09:30 drh Exp $ */ #include "sqliteInt.h" @@ -244,7 +244,7 @@ int sqliteExprResolveIds(Parse *pParse, IdList *pTabList, Expr *pExpr){ ** table. The cursor number of the temporary table has already ** been put in iTable by sqliteExprResolveInSelect(). */ - sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0); + sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1); if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) ); }else if( pExpr->pList ){ /* Case 2: expr IN (exprlist) diff --git a/src/insert.c b/src/insert.c index a3f41dc6fd..cd124f158f 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.30 2001/12/31 02:48:51 drh Exp $ +** $Id: insert.c,v 1.31 2002/01/04 03:09:30 drh Exp $ */ #include "sqliteInt.h" @@ -84,7 +84,7 @@ void sqliteInsert( } /* Figure out how many columns of data are supplied. If the data - ** is comming from a SELECT statement, then this step has to generate + ** is coming from a SELECT statement, then this step has to generate ** all the code to implement the SELECT statement and leave the data ** in a temporary table. If data is coming from an expression list, ** then we just have to count the number of expressions. diff --git a/src/select.c b/src/select.c index 399c56389a..cc4a2d9813 100644 --- a/src/select.c +++ b/src/select.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** -** $Id: select.c,v 1.52 2001/12/31 02:48:51 drh Exp $ +** $Id: select.c,v 1.53 2002/01/04 03:09:30 drh Exp $ */ #include "sqliteInt.h" @@ -546,7 +546,7 @@ static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ return 1; } if( p->op!=TK_ALL ){ - sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); + sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1); sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1); }else{ sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); @@ -608,7 +608,7 @@ static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ return 1; } - sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0); + sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1); sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1); /* Code the SELECTs to our left into temporary table "tab1". @@ -618,7 +618,7 @@ static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ /* Code the current SELECT into temporary table "tab2" */ - sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0); + sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1); sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1); p->pPrior = 0; rc = sqliteSelect(pParse, p, SRT_Union, tab2); @@ -932,7 +932,7 @@ int sqliteSelect( /* Begin the database scan */ if( isDistinct ){ - sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0); + sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1); } pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0); if( pWInfo==0 ) return 1; diff --git a/src/vdbe.c b/src/vdbe.c index 7079e179e1..dd23959a8c 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -30,7 +30,7 @@ ** But other routines are also provided to help in building up ** a program instruction by instruction. ** -** $Id: vdbe.c,v 1.103 2001/12/31 02:48:51 drh Exp $ +** $Id: vdbe.c,v 1.104 2002/01/04 03:09:30 drh Exp $ */ #include "sqliteInt.h" #include @@ -2537,12 +2537,16 @@ case OP_Open: { break; } -/* Opcode: OpenTemp P1 * * +/* Opcode: OpenTemp P1 P2 * ** -** Open a new cursor that points to a table in a temporary database -** file. The temporary file is opened read/write even if the main -** database is read-only. The temporary file is deleted when the -** cursor is closed. +** Open a new cursor that points to a table or index in a temporary +** database file. The temporary file is opened read/write even if +** the main database is read-only. The temporary file is deleted +** when the cursor is closed. +** +** The cursor points to a BTree table if P2==0 and to a BTree index +** if P2==1. A BTree table must have an integer key and can have arbitrary +** data. A BTree index has no data but can have an arbitrary key. ** ** This opcode is used for tables that exist for the duration of a single ** SQL statement only. Tables created using CREATE TEMPORARY TABLE @@ -2570,10 +2574,18 @@ case OP_OpenTemp: { memset(pCx, 0, sizeof(*pCx)); rc = sqliteBtreeOpen(0, 0, TEMP_PAGES, &pCx->pBt); if( rc==SQLITE_OK ){ - rc = sqliteBtreeCursor(pCx->pBt, 2, 1, &pCx->pCursor); + rc = sqliteBtreeBeginTrans(pCx->pBt); } if( rc==SQLITE_OK ){ - rc = sqliteBtreeBeginTrans(pCx->pBt); + if( pOp->p2 ){ + int pgno; + rc = sqliteBtreeCreateIndex(pCx->pBt, &pgno); + if( rc==SQLITE_OK ){ + rc = sqliteBtreeCursor(pCx->pBt, pgno, 1, &pCx->pCursor); + } + }else{ + rc = sqliteBtreeCursor(pCx->pBt, 2, 1, &pCx->pCursor); + } } break; } @@ -3224,14 +3236,19 @@ case OP_Clear: { ** number into the parser's internal data structures that describe the ** new table. ** +** The difference between a table and an index is this: A table must +** have a 4-byte integer key and can have arbitrary data. An index +** has an arbitrary key but no data. +** ** See also: CreateIndex */ /* Opcode: CreateIndex * P2 P3 ** -** This instruction does exactly the same thing as CreateTable. It -** has a different name for historical reasons. +** Allocate a new index in the main database file if P2==0 or in the +** auxiliary database file if P2==1. Push the page number of the +** root page of the new index onto the stack. ** -** See also: CreateTable +** See documentation on OP_CreateTable for additional information. */ case OP_CreateIndex: case OP_CreateTable: { @@ -3239,7 +3256,11 @@ case OP_CreateTable: { int pgno; VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; ) assert( pOp->p3!=0 && pOp->p3type==P3_POINTER ); - rc = sqliteBtreeCreateTable(pOp->p2 ? db->pBeTemp : pBt, &pgno); + if( pOp->opcode==OP_CreateTable ){ + rc = sqliteBtreeCreateTable(pOp->p2 ? db->pBeTemp : pBt, &pgno); + }else{ + rc = sqliteBtreeCreateIndex(pOp->p2 ? db->pBeTemp : pBt, &pgno); + } if( rc==SQLITE_OK ){ aStack[i].i = pgno; aStack[i].flags = STK_Int; diff --git a/src/where.c b/src/where.c index 54a2cddcbd..4fe59e782e 100644 --- a/src/where.c +++ b/src/where.c @@ -13,7 +13,7 @@ ** the WHERE clause of SQL statements. Also found here are subroutines ** to generate VDBE code to evaluate expressions. ** -** $Id: where.c,v 1.29 2001/12/22 14:49:26 drh Exp $ +** $Id: where.c,v 1.30 2002/01/04 03:09:30 drh Exp $ */ #include "sqliteInt.h" @@ -439,6 +439,7 @@ WhereInfo *sqliteWhereBegin( sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk); if( i==pTabList->nId-1 && pushKey ){ haveKey = 1; + sqliteVdbeAddOp(v, OP_Distinct, base+idx, brk); }else{ sqliteVdbeAddOp(v, OP_NotFound, base+idx, brk); haveKey = 0; diff --git a/test/rowid.test b/test/rowid.test index 7e589c3627..edb9da2209 100644 --- a/test/rowid.test +++ b/test/rowid.test @@ -12,7 +12,7 @@ # focus of this file is testing the magic ROWID column that is # found on all tables. # -# $Id: rowid.test,v 1.6 2001/11/08 00:45:22 drh Exp $ +# $Id: rowid.test,v 1.7 2002/01/04 03:09:30 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -263,4 +263,25 @@ do_test rowid-5.1 { execsql {SELECT max(x) FROM t1} } {8} +# Make sure a "WHERE rowid=X" clause works when there is no ROWID of X. +# +do_test rowid-6.1 { + execsql { + SELECT x FROM t1 + } +} {1 2 3 4 5 6 7 8} +do_test rowid-6.2 { + for {set ::norow 1} {[execsql {SELECT x FROM t1 WHERE rowid=10}]!=""} \ + {incr ::norow} {} + execsql [subst { + DELETE FROM t1 WHERE rowid=$::norow + }] +} {} +do_test rowid-6.3 { + execsql { + SELECT x FROM t1 + } +} {1 2 3 4 5 6 7 8} + + finish_test diff --git a/www/changes.tcl b/www/changes.tcl index ec7b49a6e7..0244232ccb 100644 --- a/www/changes.tcl +++ b/www/changes.tcl @@ -17,6 +17,11 @@ proc chng {date desc} { puts "

    $desc

" } +chng {2002 Jan 3 (2.2.1)} { +
  • Bug fix: An attempt to delete a single row of a table with a WHERE + clause of "ROWID=x" when no such rowid exists was causing an error.
  • +} + chng {2001 Dec 22 (2.2.0)} {
  • Columns of type INTEGER PRIMARY KEY are actually used as the primary key in underlying B-Tree representation of the table.