Fix some problems that can occur if a trigger has the same name as another database object.

FossilOrigin-Name: 655991ec8a781d67d69fc353853403b9a811acf2
This commit is contained in:
dan 2010-09-29 07:16:46 +00:00
parent c98a4c6db7
commit 39f1bcb1df
6 changed files with 195 additions and 23 deletions

View File

@ -1,8 +1,5 @@
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
C Merge\saccidental\sfork.
D 2010-09-28T17:37:44
C Fix\ssome\sproblems\sthat\scan\soccur\sif\sa\strigger\shas\sthe\ssame\sname\sas\sanother\sdatabase\sobject.
D 2010-09-29T07:16:47
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -114,7 +111,7 @@ F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca
F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc
F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad
F src/alter.c 8dc27638e7e2553e80b2b621f232be5eb1e85ef3
F src/alter.c 6a0c176e64a34929a4436048066a84ef4f1445b3
F src/analyze.c 1ea594a912a14138e20a05db09d9065e46f29a2a
F src/attach.c 252c4f7e36cc219349451ed63e278c60e80b26f3
F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34
@ -124,7 +121,7 @@ F src/btmutex.c 96a12f50f7a17475155971a241d85ec5171573ff
F src/btree.c d878577184112d982d00ea05afcc7487cd9f06f5
F src/btree.h 2d1a83ad509047e8cc314fda7e054f99ff52414d
F src/btreeInt.h c424f2f131cc61ddf130f9bd736b3df12c8a51f0
F src/build.c 0451673aeae6606d349c8760a0606ab9523a6fb4
F src/build.c e70451e17a84afd84ed03c9b6c9afcf7c353a11e
F src/callback.c a1d1b1c9c85415dff013af033e2fed9c8382d33b
F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac
F src/ctime.c 4f3aadad62c6c9f0d4e5a96718516ac4e3c598df
@ -236,7 +233,7 @@ F src/vdbeaux.c de0b06b11a25293e820a49159eca9f1c51a64716
F src/vdbeblob.c 258a6010ba7a82b72b327fb24c55790655689256
F src/vdbemem.c 8ff4f80a35d51f3690c191ec44778ef4bf949ac0
F src/vdbetrace.c 864cef96919323482ebd9986f2132435115e9cc2
F src/vtab.c 0e8e0cb30dffb078367e843e84e37ef99236c7e4
F src/vtab.c 6c90e3e65b2f026fc54703a8f3c917155f419d87
F src/wal.c 7081f148cb52b0cf2280e6384196402dc58130a3
F src/wal.h 96669b645e27cd5a111ba59f0cae7743a207bc3c
F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f
@ -597,6 +594,7 @@ F test/savepoint6.test 76d3948568b2cdc0c13a671cadcae75009b183d6
F test/schema.test 8f7999be894260f151adf15c2c7540f1c6d6a481
F test/schema2.test 906408621ea881fdb496d878b1822572a34e32c5
F test/schema3.test 1bc1008e1f8cb5654b248c55f27249366eb7ed38
F test/schema4.test e6a66e20cc69f0e306667c08be7fda3d11707dc5
F test/securedel.test 328d2921c0ca49bdd3352e516b0377fc07143254
F test/select1.test f67ca2dfc05df41c7b86eb32ca409b427a5f43b0
F test/select2.test 352480e0e9c66eda9c3044e412abdf5be0215b56
@ -871,14 +869,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
P 6251e587403eed822b9011ed25a3a807e8fa2a6f b7a26427afc43c9bbdf365b775379eb57187bc97
R 2150b8665a4d77e688c620ae74088521
U drh
Z 4d3d211ea9c5b7e168f16846e8aa0e05
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFMoifwoxKgR168RlERAthxAJ9/+RgDqmxkVpzT2RPcQd3+CRfM5ACcC32K
xVhEbt+a4I3tqHSMWTuKYOw=
=ku2K
-----END PGP SIGNATURE-----
P 33c8b9c710db6a1a987c11ab6f3a6bc120257ce9
R 38dc5bfefb0ef5eb542c703ca4f2f41a
U dan
Z 185a440a4979b85c6b3575ef1db29431

View File

@ -1 +1 @@
33c8b9c710db6a1a987c11ab6f3a6bc120257ce9
655991ec8a781d67d69fc353853403b9a811acf2

View File

@ -313,6 +313,11 @@ static char *whereTempTriggers(Parse *pParse, Table *pTab){
}
}
}
if( zWhere ){
char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
sqlite3DbFree(pParse->db, zWhere);
zWhere = zNew;
}
return zWhere;
}

View File

@ -2772,7 +2772,8 @@ Index *sqlite3CreateIndex(
sqlite3RefillIndex(pParse, pIndex, iMem);
sqlite3ChangeCookie(pParse, iDb);
sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0,
sqlite3MPrintf(db, "name='%q'", pIndex->zName), P4_DYNAMIC);
sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName),
P4_DYNAMIC);
sqlite3VdbeAddOp1(v, OP_Expire, 0);
}
}
@ -2900,7 +2901,7 @@ void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
if( v ){
sqlite3BeginWriteOperation(pParse, 1, iDb);
sqlite3NestedParse(pParse,
"DELETE FROM %Q.%s WHERE name=%Q",
"DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
pIndex->zName
);

View File

@ -371,7 +371,7 @@ void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
sqlite3ChangeCookie(pParse, iDb);
sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC);
sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
pTab->zName, sqlite3Strlen30(pTab->zName) + 1);

175
test/schema4.test Normal file
View File

@ -0,0 +1,175 @@
# 2010 September 28
#
# 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 implements regression tests for SQLite library. The
# focus of this file is testing that a trigger may have the same
# name as an index, view or table in the same database.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
#--------------------------------------------------------------------------
# Test organization:
#
# schema4-1.*: Dropping and creating triggers and other objects where
# triggers and at least on other object share a name.
#
# schema4-2.*: Renaming tables where there is a trigger that shares the
# name of the table or one of its indices.
#
do_execsql_test schema4-1.1 {
CREATE TABLE log(x, a, b);
CREATE TABLE tbl(a, b);
CREATE TABLE t1(a, b);
CREATE VIEW v1 AS SELECT * FROM tbl;
CREATE INDEX i1 ON tbl(a);
} {}
do_execsql_test schema4-1.2 {
CREATE TRIGGER t1 AFTER INSERT ON tbl BEGIN
INSERT INTO log VALUES('after insert', new.a, new.b);
END;
CREATE TRIGGER v1 AFTER UPDATE ON tbl BEGIN
INSERT INTO log VALUES('after update', new.a, new.b);
END;
CREATE TRIGGER i1 AFTER DELETE ON tbl BEGIN
INSERT INTO log VALUES('after delete', old.a, old.b);
END;
} {}
do_execsql_test schema4-1.3 {
INSERT INTO tbl VALUES(1, 2);
UPDATE tbl SET b=a+b, a=a+1;
DELETE FROM tbl;
SELECT x, a, b FROM log;
} {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
do_execsql_test schema4-1.4 {
DELETE FROM log;
DROP INDEX i1;
DROP TABLE t1;
DROP VIEW v1;
INSERT INTO tbl VALUES(1, 2);
UPDATE tbl SET b=a+b, a=a+1;
DELETE FROM tbl;
SELECT x, a, b FROM log;
} {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
db close
sqlite3 db test.db
do_execsql_test schema4-1.5 {
DELETE FROM log;
INSERT INTO tbl VALUES(1, 2);
UPDATE tbl SET b=a+b, a=a+1;
DELETE FROM tbl;
SELECT x, a, b FROM log;
} {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
do_execsql_test schema4-1.6 {
CREATE TABLE t1(a, b);
CREATE VIEW v1 AS SELECT * FROM tbl;
CREATE INDEX i1 ON tbl(a);
} {}
ifcapable fts3 {
do_execsql_test schema4-1.7 {
DROP TABLE t1;
CREATE VIRTUAL TABLE t1 USING fts3;
} {}
do_execsql_test schema4-1.8 {
DELETE FROM log;
DROP TABLE t1;
INSERT INTO tbl VALUES(1, 2);
UPDATE tbl SET b=a+b, a=a+1;
DELETE FROM tbl;
SELECT x, a, b FROM log;
} {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
}
ifcapable altertable {
drop_all_tables
do_execsql_test schema4-2.1 {
CREATE TABLE log(x, a, b);
CREATE TABLE tbl(a, b);
CREATE TABLE t1(a, b);
CREATE INDEX i1 ON t1(a, b);
} {}
do_execsql_test schema4-2.2 {
CREATE TRIGGER t1 AFTER INSERT ON tbl BEGIN
INSERT INTO log VALUES('after insert', new.a, new.b);
END;
CREATE TRIGGER i1 AFTER DELETE ON tbl BEGIN
INSERT INTO log VALUES('after delete', old.a, old.b);
END;
} {}
do_execsql_test schema4-2.3 { ALTER TABLE t1 RENAME TO t2 } {}
do_execsql_test schema4-2.4 {
INSERT INTO tbl VALUES('a', 'b');
DELETE FROM tbl;
SELECT * FROM log;
} {{after insert} a b {after delete} a b}
db close
sqlite3 db test.db
do_execsql_test schema4-2.5 {
DELETE FROM log;
INSERT INTO tbl VALUES('c', 'd');
DELETE FROM tbl;
SELECT * FROM log;
} {{after insert} c d {after delete} c d}
do_execsql_test schema4-2.6 {
CREATE TEMP TRIGGER x1 AFTER UPDATE ON tbl BEGIN
INSERT INTO log VALUES('after update', new.a, new.b);
END;
CREATE TEMP TABLE x1(x);
INSERT INTO x1 VALUES(123);
} {}
do_execsql_test schema4-2.8 {
select sql from sqlite_temp_master WHERE type='table';
} {{CREATE TABLE x1(x)}}
do_execsql_test schema4-2.7 { ALTER TABLE tbl RENAME TO tbl2 } {}
do_execsql_test schema4-2.9 {
select sql from sqlite_temp_master WHERE type='table';
} {{CREATE TABLE x1(x)}}
do_execsql_test schema4-2.10 {
DELETE FROM log;
INSERT INTO tbl2 VALUES('e', 'f');
UPDATE tbl2 SET a='g', b='h';
DELETE FROM tbl2;
SELECT * FROM log;
} {{after insert} e f {after update} g h {after delete} g h}
do_execsql_test schema4-2.11 {
INSERT INTO x1 VALUES(456);
SELECT * FROM x1
} {123 456}
}
finish_test