]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix problems in trigger and foreign key handling when doing REPLACE on a
authordrh <drh@noemail.net>
Thu, 5 Jan 2017 13:56:17 +0000 (13:56 +0000)
committerdrh <drh@noemail.net>
Thu, 5 Jan 2017 13:56:17 +0000 (13:56 +0000)
WITHOUT ROWID table that has no secondary indexes.
Fix for ticket [30027b613b4].

FossilOrigin-Name: 8a788594e2097ab5f3fe32f1d6b58caef2887d20

manifest
manifest.uuid
src/insert.c
test/fkey8.test
test/triggerF.test [new file with mode: 0644]

index dac2da099d0532c4768ae340ccd2cd7daac4f926..fd21316a056c8ce7b65e4c80072480c07a898ff0 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Ensure\sthat\sthe\ssqlite3_value_text()\sinterface\sreturns\sa\sbuffer\sthat\sis\slong\nenough\sto\shold\sthe\scomplete\sstring\splus\sthe\szero\sterminator\seven\swhen\sthe\ninput\sis\sa\szeroblob.\s\sFix\sfor\sa\sproblem\sdetected\sby\sOSS-Fuzz.
-D 2017-01-05T13:52:54.033
+C Fix\sproblems\sin\strigger\sand\sforeign\skey\shandling\swhen\sdoing\sREPLACE\son\sa\nWITHOUT\sROWID\stable\sthat\shas\sno\ssecondary\sindexes.\nFix\sfor\sticket\s[30027b613b4].
+D 2017-01-05T13:56:17.926
 F Makefile.in 41bd4cad981487345c4a84081074bcdb876e4b2e
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da
@@ -350,7 +350,7 @@ F src/hash.c 63d0ee752a3b92d4695b2b1f5259c4621b2cfebd
 F src/hash.h ab34c5c54a9e9de2e790b24349ba5aab3dbb4fd4
 F src/hwtime.h 747c1bbe9df21a92e9c50f3bbec1de841dc5e5da
 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
-F src/insert.c 91ba5d0143e66479081536ebbaff1850ec9f57d9
+F src/insert.c 7af46a3be2656f5e13791464625d93d6b07b8612
 F src/legacy.c 75d3023be8f0d2b99d60f905090341a03358c58e
 F src/loadext.c 5d6642d141c07d366e43d359e94ec9de47add41d
 F src/main.c e207b81542d13b9f13d61e78ca441f9781f055b0
@@ -692,7 +692,7 @@ F test/fkey4.test 86446017011273aad8f9a99c1a65019e7bd9ca9d
 F test/fkey5.test 5a373303f201ac03c22ba1ef17a733d3f56e611a
 F test/fkey6.test d078a1e323a740062bed38df32b8a736fd320dc0
 F test/fkey7.test 72e915890ee4a005daaf3002cb208e8fe973ac13
-F test/fkey8.test 7bd1dd0174a0e29a90c62c517b9e2a410a0b345d
+F test/fkey8.test e5372e32cdb4481f121ec3550703eeb7b4e0762c
 F test/fkey_malloc.test 594a7ea1fbab553c036c70813cd8bd9407d63749
 F test/fordelete.test eb93a2f34137bb87bdab88fcab06c0bd92719aff
 F test/format4.test 1f0cac8ff3895e9359ed87e41aaabee982a812eb
@@ -1336,6 +1336,7 @@ F test/triggerB.test 56780c031b454abac2340dbb3b71ac5c56c3d7fe
 F test/triggerC.test 302d8995f5ffe63bbc15053abb3ef7a39cf5a092
 F test/triggerD.test 8e7f3921a92a5797d472732108109e44575fa650
 F test/triggerE.test 15fa63f1097db1f83dd62d121616006978063d1f
+F test/triggerF.test 55b1eb13433997faac3a4948c1d8252f6c8c636b
 F test/tt3_checkpoint.c 9e75cf7c1c364f52e1c47fd0f14c4340a9db0fe1
 F test/tt3_index.c 39eec10a35f57672225be4d182862152896dee4a
 F test/tt3_lookaside1.c 0377e202c3c2a50d688cb65ba203afeda6fafeb9
@@ -1541,8 +1542,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 979f04392853b8053817a3eea2fc679947b437fd
-Q +2dc7eeb5b4d2eaf1d843eda56f339fd4cc80d78e
-R abe304f6cf5138efa11a7d944267ac9d
+P ca185808ade195875918b5bad1762bcd8ec39457
+Q +571f166ea8721e2322965b6f23e758b78d13baca
+R 7fc60f59e29caa0a05fb66d57cf3b6aa
 U drh
-Z 2f1a7774661688b3371053728c269ae5
+Z 616ccd5e6ddd26ab90b66221b967a4f1
index 4701fff9efc7ac025f78de4e6f726b3ef1b8c1c0..7fe4a42e0cfdf1589de7278e083547356445afa5 100644 (file)
@@ -1 +1 @@
-ca185808ade195875918b5bad1762bcd8ec39457
\ No newline at end of file
+8a788594e2097ab5f3fe32f1d6b58caef2887d20
\ No newline at end of file
index 27e05d4de6165f229179d24ce3119b0a044d00a3..e261c1994e860eacf3b9418983fc416f0d9df8b2 100644 (file)
@@ -1549,12 +1549,25 @@ void sqlite3GenerateConstraintChecks(
       onError = OE_Abort;
     }
 
-    if( ix==0 && pPk==pIdx && onError==OE_Replace && pPk->pNext==0 ){
+    /* Collision detection may be omitted if all of the following are true:
+    **   (1) The conflict resolution algorithm is REPLACE
+    **   (2) The table is a WITHOUT ROWID table
+    **   (3) There are no secondary indexes on the table
+    **   (4) No delete triggers need to be fired if there is a conflict
+    **   (5) No FK constraint counters need to be updated if a conflict occurs.
+    */ 
+    if( (ix==0 && pIdx->pNext==0)                   /* Condition 3 */
+     && pPk==pIdx                                   /* Condition 2 */
+     && onError==OE_Replace                         /* Condition 1 */
+     && ( 0==(db->flags&SQLITE_RecTriggers) ||      /* Condition 4 */
+          0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
+     && ( 0==(db->flags&SQLITE_ForeignKeys) ||      /* Condition 5 */
+         (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
+    ){
       sqlite3VdbeResolveLabel(v, addrUniqueOk);
       continue;
     }
 
-    
     /* Check to see if the new index entry will be unique */
     sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
                          regIdx, pIdx->nKeyCol); VdbeCoverage(v);
index b4b6bb75e7bcc0b5309c5993c42ed0e4a0516243..4269c20a92c854f4a4fe9d03e70c008479e0210b 100644 (file)
@@ -101,5 +101,67 @@ foreach {tn use_stmt sql schema} {
   } $use_stmt
 }
 
+#-------------------------------------------------------------------------
+# The following tests check that foreign key constaint counters are
+# correctly updated for any implicit DELETE operations that occur
+# when a REPLACE command is executed against a WITHOUT ROWID table
+# that has no triggers or auxiliary indexes.
+#
+reset_db
+do_execsql_test 2.1.0 {
+  PRAGMA foreign_keys = on;
+  CREATE TABLE p1(a PRIMARY KEY, b) WITHOUT ROWID;
+  CREATE TABLE c1(x REFERENCES p1 DEFERRABLE INITIALLY DEFERRED);
+
+  INSERT INTO p1 VALUES(1, 'one');
+  INSERT INTO p1 VALUES(2, 'two');
+  INSERT INTO c1 VALUES(1);
+  INSERT INTO c1 VALUES(2);
+}
+
+do_catchsql_test 2.1.2 {
+  BEGIN;
+    DELETE FROM p1 WHERE a=1;
+    INSERT OR REPLACE INTO p1 VALUES(2, 'two');
+  COMMIT;
+} {1 {FOREIGN KEY constraint failed}}
+
+reset_db
+do_execsql_test 2.2.0 {
+  PRAGMA foreign_keys = on;
+  CREATE TABLE p2(a PRIMARY KEY, b);
+  CREATE TABLE c2(
+    x PRIMARY KEY,
+    y REFERENCES p2 DEFERRABLE INITIALLY DEFERRED
+  ) WITHOUT ROWID;
+}
+
+do_catchsql_test 2.2.1 {
+  BEGIN;
+    INSERT INTO c2 VALUES(13, 13);
+    INSERT OR REPLACE INTO c2 VALUES(13, 13);
+    DELETE FROM c2;
+  COMMIT;
+} {0 {}}
+
+reset_db
+do_execsql_test 2.3.0 {
+  PRAGMA foreign_keys = on;
+  CREATE TABLE p3(a PRIMARY KEY, b) WITHOUT ROWID;
+  CREATE TABLE c3(x REFERENCES p3);
+
+  INSERT INTO p3 VALUES(1, 'one');
+  INSERT INTO p3 VALUES(2, 'two');
+  INSERT INTO c3 VALUES(1);
+  INSERT INTO c3 VALUES(2);
+
+  CREATE TRIGGER p3d AFTER DELETE ON p3 WHEN old.a=1 BEGIN
+    INSERT OR REPLACE INTO p3 VALUES(2, 'three');
+  END;
+}
+
+do_catchsql_test 2.3.1 {
+  DELETE FROM p3 WHERE a=1
+} {1 {FOREIGN KEY constraint failed}}
 
 finish_test
diff --git a/test/triggerF.test b/test/triggerF.test
new file mode 100644 (file)
index 0000000..d5e3565
--- /dev/null
@@ -0,0 +1,72 @@
+# 2017 January 4
+#
+# 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.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix triggerF
+ifcapable {!trigger} {
+  finish_test
+  return
+}
+
+
+foreach {tn sql log} {
+  1 { } { }
+
+  2 { 
+    CREATE TRIGGER trd AFTER DELETE ON t1 BEGIN
+      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
+    END;
+  } {1one2 2two1 3three1}
+
+  3 { 
+    CREATE TRIGGER trd BEFORE DELETE ON t1 BEGIN
+      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
+    END;
+  } {1one3 2two2 3three2}
+
+  4 { 
+    CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN
+      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
+    END;
+    CREATE TRIGGER tr2 BEFORE DELETE ON t1 BEGIN
+      INSERT INTO log VALUES(old.a || old.b || (SELECT count(*) FROM t1));
+    END;
+  } {1one3 1one2 2two2 2two1 3three2 3three1}
+
+} {
+  reset_db
+  do_execsql_test 1.$tn.0 {
+    PRAGMA recursive_triggers = on;
+    CREATE TABLE t1(a INT PRIMARY KEY, b) WITHOUT ROWID;
+    CREATE TABLE log(t);
+  }
+  
+  execsql $sql
+
+  do_execsql_test 1.$tn.1 {
+    INSERT INTO t1 VALUES(1, 'one');
+    INSERT INTO t1 VALUES(2, 'two');
+    INSERT INTO t1 VALUES(3, 'three');
+
+    DELETE FROM t1 WHERE a=1;
+    INSERT OR REPLACE INTO t1 VALUES(2, 'three');
+    UPDATE OR REPLACE t1 SET a=3 WHERE a=2;
+  }
+
+  do_execsql_test 1.$tn.2 {
+    SELECT * FROM log ORDER BY rowid;
+  } $log
+}
+
+finish_test
+