From 2a5d825ea2cf5119bd3c5d676ce3573b8ba05cb4 Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 22 Aug 2008 12:30:52 +0000 Subject: [PATCH] Allow the WHERE clause in an UPDATE or DELETE against a view with an INSTEAD OF trigger to refer to the view by name. Ticket #3298. (CVS 5589) FossilOrigin-Name: 7360e5d1f37ed6672f336b625b3c90d060e0a020 --- manifest | 17 +++++++------ manifest.uuid | 2 +- src/delete.c | 13 ++++++---- src/sqliteInt.h | 4 +-- src/update.c | 4 +-- test/tkt3298.test | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 test/tkt3298.test diff --git a/manifest b/manifest index 0522da045d..13e00ca82f 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sos_unix.c\sso\sthat\sit\swill\scompile\son\sLinux\sagain.\s(CVS\s5588) -D 2008-08-22T00:47:54 +C Allow\sthe\sWHERE\sclause\sin\san\sUPDATE\sor\sDELETE\sagainst\sa\sview\swith\san\nINSTEAD\sOF\strigger\sto\srefer\sto\sthe\sview\sby\sname.\s\sTicket\s#3298.\s(CVS\s5589) +D 2008-08-22T12:30:52 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 51b727303f84cf055e29514d8248e5eaf9701379 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -103,7 +103,7 @@ F src/build.c 160c71acca8f643f436ed6c1ee2f684c88df4dfe F src/callback.c 27e55dfafc66e97dd8e7343a1d7bb295f3afd736 F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c F src/date.c 2373f20a91bec70e20e0e715520c8010d26ebb52 -F src/delete.c 5105c67c741f934379722de5e11fdd73ea4d82b5 +F src/delete.c d979a250a47b9f52effa2998070810239e38db8b F src/expr.c 4651d08422474f7e4bcb9a35da00c33ebe719a2d F src/fault.c 3638519d1e0b82bccfafcb9f5ff491918b28f8e1 F src/func.c f2e0b55f13e678a5c5ff94f0f225ed0a995acba7 @@ -149,7 +149,7 @@ F src/select.c e71462393fe0f9d2bf41378763b96659e8780e43 F src/shell.c d83b578a8ccdd3e0e7fef4388a0887ce9f810967 F src/sqlite.h.in 2373d1d70664f7fcd78e79af3c51792bb0a0753e F src/sqlite3ext.h 1e3887c9bd3ae66cb599e922824b04cd0d0f2c3e -F src/sqliteInt.h ddf6c9eb90b295bcb7c600139227e5e2a16c8063 +F src/sqliteInt.h 4f231eb750bf6901fdfa935c246a69d03c25d9b8 F src/sqliteLimit.h f435e728c6b620ef7312814d660a81f9356eb5c8 F src/status.c 8caa772cd9310bc297280f7cf0ede4d69ed5b801 F src/table.c 22744786199c9195720c15a7a42cb97b2e2728d8 @@ -182,7 +182,7 @@ F src/test_tclvar.c 9e42fa59d3d2f064b7ab8628e7ab2dc8a9fe93d4 F src/test_thread.c e297dd41db0b249646e69f97d36ec13e56e8b730 F src/tokenize.c d16ca0e9944161c76d2e4c11dc379ec88189b93b F src/trigger.c 649940b5bf5838a33721fb72372e7c9d1faf56a9 -F src/update.c 9e3786e76825bd7fa22528556cc1c467cd428075 +F src/update.c f2cf6f00d542956bd49ba4b9815c2900d9225bf2 F src/utf.c c63e6f69082f85c19ab88d62dedaf91d71ac1a50 F src/util.c afe659ccc05d1f8af9e8631dabfec3ee3a7144af F src/vacuum.c ef342828002debc97514617af3424aea8ef8522c @@ -550,6 +550,7 @@ F test/tkt3093.test fbdbc5b4969244ad11f540759003e361fcaf391f F test/tkt3121.test 536df66a02838c26a12fe98639354ca1290ca68b F test/tkt3201.test 607d433ad2c1f6a8cb1af55aaca427f63c83191b F test/tkt3292.test 962465a0984a3b8c757efe59c2c59144871ee1dd +F test/tkt3298.test fb174771c7a4d5abd57448c01008eebd13128bb0 F test/tokenize.test ce430a7aed48fc98301611429595883fdfcab5d7 F test/trace.test 951cd0f5f571e7f36bf7bfe04be70f90fb16fb00 F test/trans.test 2fd24cd7aa0b879d49a224cbd647d698f1e7ac5c @@ -622,7 +623,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P e28a2870b49509502529892ff8ddb9fcf3a27173 -R e06d422a77e6a9b567f419fccc11805c +P 2416708208357304904e38b2d36081a1adf5e44b +R f37f9f9f6612c2b90a90588052b716e7 U drh -Z e1dcaa67276ea8bb0343faa68fef8503 +Z 2e1b1719f9f299edfbb6ca6ba7f8d98c diff --git a/manifest.uuid b/manifest.uuid index fb67cf65c5..902a9ddb13 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -2416708208357304904e38b2d36081a1adf5e44b \ No newline at end of file +7360e5d1f37ed6672f336b625b3c90d060e0a020 \ No newline at end of file diff --git a/src/delete.c b/src/delete.c index 40aeb46812..72c2ec1ca7 100644 --- a/src/delete.c +++ b/src/delete.c @@ -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.172 2008/08/20 16:35:10 drh Exp $ +** $Id: delete.c,v 1.173 2008/08/22 12:30:52 drh Exp $ */ #include "sqliteInt.h" @@ -90,7 +90,7 @@ void sqlite3OpenTable( */ void sqlite3MaterializeView( Parse *pParse, /* Parsing context */ - Select *pView, /* View definition */ + Table *pView, /* View definition */ Expr *pWhere, /* Optional WHERE clause to be added */ int iCur /* Cursor number for ephemerial table */ ){ @@ -98,12 +98,15 @@ void sqlite3MaterializeView( Select *pDup; sqlite3 *db = pParse->db; - pDup = sqlite3SelectDup(db, pView); + pDup = sqlite3SelectDup(db, pView->pSelect); if( pWhere ){ SrcList *pFrom; + Token viewName; pWhere = sqlite3ExprDup(db, pWhere); - pFrom = sqlite3SrcListAppendFromTerm(pParse, 0, 0, 0, 0, pDup, 0, 0); + viewName.z = pView->zName; + viewName.n = strlen(viewName.z); + pFrom = sqlite3SrcListAppendFromTerm(pParse, 0, 0, 0, &viewName, pDup, 0,0); pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0); } sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); @@ -247,7 +250,7 @@ void sqlite3DeleteFrom( ** a ephemeral table. */ if( isView ){ - sqlite3MaterializeView(pParse, pTab->pSelect, pWhere, iCur); + sqlite3MaterializeView(pParse, pTab, pWhere, iCur); } /* Resolve the column names in the WHERE clause. diff --git a/src/sqliteInt.h b/src/sqliteInt.h index a6ff8e1d13..11af23e25e 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.759 2008/08/21 20:21:35 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.760 2008/08/22 12:30:52 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -2188,7 +2188,7 @@ int sqlite3GetBuiltinFunction(const char *, int, FuncDef **); int sqlite3SafetyCheckOk(sqlite3*); int sqlite3SafetyCheckSickOrOk(sqlite3*); void sqlite3ChangeCookie(Parse*, int); -void sqlite3MaterializeView(Parse*, Select*, Expr*, int); +void sqlite3MaterializeView(Parse*, Table*, Expr*, int); #ifndef SQLITE_OMIT_TRIGGER void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*, diff --git a/src/update.c b/src/update.c index eb715dfc9d..97bf93718c 100644 --- a/src/update.c +++ b/src/update.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle UPDATE statements. ** -** $Id: update.c,v 1.182 2008/08/20 16:35:10 drh Exp $ +** $Id: update.c,v 1.183 2008/08/22 12:30:52 drh Exp $ */ #include "sqliteInt.h" @@ -329,7 +329,7 @@ void sqlite3Update( ** a ephemeral table. */ if( isView ){ - sqlite3MaterializeView(pParse, pTab->pSelect, pWhere, iCur); + sqlite3MaterializeView(pParse, pTab, pWhere, iCur); } /* Resolve the column names in all the expressions in the diff --git a/test/tkt3298.test b/test/tkt3298.test new file mode 100644 index 0000000000..3315f70571 --- /dev/null +++ b/test/tkt3298.test @@ -0,0 +1,65 @@ +# 2008 August 12 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# This file tests changes to the name resolution logic that occurred +# in august of 2008 and where associated with tickets #3298 +# +# $Id: tkt3298.test,v 1.1 2008/08/22 12:30:52 drh Exp $ + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_test tkt3298-1.1 { + execsql { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); + INSERT INTO t1 VALUES(0, 1); + INSERT INTO t1 VALUES(1, 1); + INSERT INTO t1 VALUES(2, 1); + CREATE VIEW v1 AS SELECT a AS x, b+1 AS y FROM t1; + CREATE TRIGGER r1 INSTEAD OF UPDATE ON v1 + BEGIN + UPDATE t1 SET b=new.y-1 WHERE a=new.x; + END; + CREATE TRIGGER r2 INSTEAD OF DELETE ON v1 + BEGIN + DELETE FROM t1 WHERE a=old.x; + END; + SELECT * FROM v1 ORDER BY x; + } +} {0 2 1 2 2 2} +do_test tkt3298-1.2 { + execsql { + UPDATE v1 SET y=3 WHERE x=0; + SELECT * FROM v1 ORDER by x; + } +} {0 3 1 2 2 2} +do_test tkt3298-1.3 { + execsql { + UPDATE v1 SET y=4 WHERE v1.x=2; + SELECT * FROM v1 ORDER by x; + } +} {0 3 1 2 2 4} +do_test tkt3298-1.4 { + execsql { + DELETE FROM v1 WHERE x=1; + SELECT * FROM v1 ORDER BY x; + } +} {0 3 2 4} +do_test tkt3298-1.5 { + execsql { + DELETE FROM v1 WHERE v1.x=2; + SELECT * FROM v1 ORDER BY x; + } +} {0 3} + + + +finish_test