1bb37fdc7Sdrh# 2002 January 29 2bb37fdc7Sdrh# 3bb37fdc7Sdrh# The author disclaims copyright to this source code. In place of 4bb37fdc7Sdrh# a legal notice, here is a blessing: 5bb37fdc7Sdrh# 6bb37fdc7Sdrh# May you do good and not evil. 7bb37fdc7Sdrh# May you find forgiveness for yourself and forgive others. 8bb37fdc7Sdrh# May you share freely, never taking more than you give. 9bb37fdc7Sdrh# 10bb37fdc7Sdrh#*********************************************************************** 11bb37fdc7Sdrh# This file implements regression tests for SQLite library. 12bb37fdc7Sdrh# 13bb37fdc7Sdrh# This file implements tests for the conflict resolution extension 14bb37fdc7Sdrh# to SQLite. 15bb37fdc7Sdrh# 16*ed717fe3Sdrh# $Id: conflict.test,v 1.18 2003/06/15 23:42:25 drh Exp $ 17bb37fdc7Sdrh 18bb37fdc7Sdrhset testdir [file dirname $argv0] 19bb37fdc7Sdrhsource $testdir/tester.tcl 20bb37fdc7Sdrh 210d65dc0eSdrh# Create tables for the first group of tests. 22bb37fdc7Sdrh# 230d65dc0eSdrhdo_test conflict-1.0 { 24bb37fdc7Sdrh execsql { 25bb37fdc7Sdrh CREATE TABLE t1(a, b, c, UNIQUE(a,b)); 260d65dc0eSdrh CREATE TABLE t2(x); 27bb37fdc7Sdrh SELECT c FROM t1 ORDER BY c; 28bb37fdc7Sdrh } 290d65dc0eSdrh} {} 30bb37fdc7Sdrh 310d65dc0eSdrh# Six columns of configuration data as follows: 320d65dc0eSdrh# 330d65dc0eSdrh# i The reference number of the test 340d65dc0eSdrh# conf The conflict resolution algorithm on the BEGIN statement 350d65dc0eSdrh# cmd An INSERT or REPLACE command to execute against table t1 360d65dc0eSdrh# t0 True if there is an error from $cmd 370d65dc0eSdrh# t1 Content of "c" column of t1 assuming no error in $cmd 380d65dc0eSdrh# t2 Content of "x" column of t2 390d65dc0eSdrh# 400d65dc0eSdrhforeach {i conf cmd t0 t1 t2} { 410d65dc0eSdrh 1 {} INSERT 1 {} 1 420d65dc0eSdrh 2 {} {INSERT OR IGNORE} 0 3 1 430d65dc0eSdrh 3 {} {INSERT OR REPLACE} 0 4 1 440d65dc0eSdrh 4 {} REPLACE 0 4 1 450d65dc0eSdrh 5 {} {INSERT OR FAIL} 1 {} 1 460d65dc0eSdrh 6 {} {INSERT OR ABORT} 1 {} 1 470d65dc0eSdrh 7 {} {INSERT OR ROLLBACK} 1 {} {} 480d65dc0eSdrh 8 IGNORE INSERT 0 3 1 490d65dc0eSdrh 9 IGNORE {INSERT OR IGNORE} 0 3 1 500d65dc0eSdrh 10 IGNORE {INSERT OR REPLACE} 0 4 1 510d65dc0eSdrh 11 IGNORE REPLACE 0 4 1 520d65dc0eSdrh 12 IGNORE {INSERT OR FAIL} 1 {} 1 530d65dc0eSdrh 13 IGNORE {INSERT OR ABORT} 1 {} 1 540d65dc0eSdrh 14 IGNORE {INSERT OR ROLLBACK} 1 {} {} 550d65dc0eSdrh 15 REPLACE INSERT 0 4 1 560d65dc0eSdrh 16 FAIL INSERT 1 {} 1 570d65dc0eSdrh 17 ABORT INSERT 1 {} 1 580d65dc0eSdrh 18 ROLLBACK INSERT 1 {} {} 590d65dc0eSdrh} { 600d65dc0eSdrh do_test conflict-1.$i { 610d65dc0eSdrh if {$conf!=""} {set conf "ON CONFLICT $conf"} 620d65dc0eSdrh set r0 [catch {execsql [subst { 63bb37fdc7Sdrh DELETE FROM t1; 640d65dc0eSdrh DELETE FROM t2; 65bb37fdc7Sdrh INSERT INTO t1 VALUES(1,2,3); 660d65dc0eSdrh BEGIN $conf; 670d65dc0eSdrh INSERT INTO t2 VALUES(1); 680d65dc0eSdrh $cmd INTO t1 VALUES(1,2,4); 690d65dc0eSdrh }]} r1] 706b8b8749Sdrh catch {execsql {COMMIT}} 710d65dc0eSdrh if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} 720d65dc0eSdrh set r2 [execsql {SELECT x FROM t2}] 730d65dc0eSdrh list $r0 $r1 $r2 740d65dc0eSdrh } [list $t0 $t1 $t2] 75bb37fdc7Sdrh} 76bb37fdc7Sdrh 770d65dc0eSdrh# Create tables for the first group of tests. 780d65dc0eSdrh# 790d65dc0eSdrhdo_test conflict-2.0 { 80ef6764a1Sdrh execsql { 81ef6764a1Sdrh DROP TABLE t1; 82ef6764a1Sdrh DROP TABLE t2; 830d65dc0eSdrh CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(a,b)); 840d65dc0eSdrh CREATE TABLE t2(x); 85ef6764a1Sdrh SELECT c FROM t1 ORDER BY c; 86ef6764a1Sdrh } 870d65dc0eSdrh} {} 88ef6764a1Sdrh 890d65dc0eSdrh# Six columns of configuration data as follows: 900d65dc0eSdrh# 910d65dc0eSdrh# i The reference number of the test 920d65dc0eSdrh# conf The conflict resolution algorithm on the BEGIN statement 930d65dc0eSdrh# cmd An INSERT or REPLACE command to execute against table t1 940d65dc0eSdrh# t0 True if there is an error from $cmd 950d65dc0eSdrh# t1 Content of "c" column of t1 assuming no error in $cmd 960d65dc0eSdrh# t2 Content of "x" column of t2 970d65dc0eSdrh# 980d65dc0eSdrhforeach {i conf cmd t0 t1 t2} { 990d65dc0eSdrh 1 {} INSERT 1 {} 1 1000d65dc0eSdrh 2 {} {INSERT OR IGNORE} 0 3 1 1010d65dc0eSdrh 3 {} {INSERT OR REPLACE} 0 4 1 1020d65dc0eSdrh 4 {} REPLACE 0 4 1 1030d65dc0eSdrh 5 {} {INSERT OR FAIL} 1 {} 1 1040d65dc0eSdrh 6 {} {INSERT OR ABORT} 1 {} 1 1050d65dc0eSdrh 7 {} {INSERT OR ROLLBACK} 1 {} {} 1060d65dc0eSdrh 8 IGNORE INSERT 0 3 1 1070d65dc0eSdrh 9 IGNORE {INSERT OR IGNORE} 0 3 1 1080d65dc0eSdrh 10 IGNORE {INSERT OR REPLACE} 0 4 1 1090d65dc0eSdrh 11 IGNORE REPLACE 0 4 1 1100d65dc0eSdrh 12 IGNORE {INSERT OR FAIL} 1 {} 1 1110d65dc0eSdrh 13 IGNORE {INSERT OR ABORT} 1 {} 1 1120d65dc0eSdrh 14 IGNORE {INSERT OR ROLLBACK} 1 {} {} 1130d65dc0eSdrh 15 REPLACE INSERT 0 4 1 1140d65dc0eSdrh 16 FAIL INSERT 1 {} 1 1150d65dc0eSdrh 17 ABORT INSERT 1 {} 1 1160d65dc0eSdrh 18 ROLLBACK INSERT 1 {} {} 1170d65dc0eSdrh} { 1180d65dc0eSdrh do_test conflict-2.$i { 1190d65dc0eSdrh if {$conf!=""} {set conf "ON CONFLICT $conf"} 1200d65dc0eSdrh set r0 [catch {execsql [subst { 121ef6764a1Sdrh DELETE FROM t1; 1220d65dc0eSdrh DELETE FROM t2; 123ef6764a1Sdrh INSERT INTO t1 VALUES(1,2,3); 1240d65dc0eSdrh BEGIN $conf; 1250d65dc0eSdrh INSERT INTO t2 VALUES(1); 1260d65dc0eSdrh $cmd INTO t1 VALUES(1,2,4); 1270d65dc0eSdrh }]} r1] 1286b8b8749Sdrh catch {execsql {COMMIT}} 1290d65dc0eSdrh if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} 1300d65dc0eSdrh set r2 [execsql {SELECT x FROM t2}] 1310d65dc0eSdrh list $r0 $r1 $r2 1320d65dc0eSdrh } [list $t0 $t1 $t2] 133ef6764a1Sdrh} 1340d65dc0eSdrh 1350d65dc0eSdrh# Create tables for the first group of tests. 1360d65dc0eSdrh# 1370d65dc0eSdrhdo_test conflict-3.0 { 1380d65dc0eSdrh execsql { 1390d65dc0eSdrh DROP TABLE t1; 1400d65dc0eSdrh DROP TABLE t2; 141e0194f2bSdrh CREATE TABLE t1(a, b, c INTEGER, PRIMARY KEY(c), UNIQUE(a,b)); 1420d65dc0eSdrh CREATE TABLE t2(x); 143ef6764a1Sdrh SELECT c FROM t1 ORDER BY c; 144ef6764a1Sdrh } 1450d65dc0eSdrh} {} 1460d65dc0eSdrh 1470d65dc0eSdrh# Six columns of configuration data as follows: 1480d65dc0eSdrh# 1490d65dc0eSdrh# i The reference number of the test 1500d65dc0eSdrh# conf The conflict resolution algorithm on the BEGIN statement 1510d65dc0eSdrh# cmd An INSERT or REPLACE command to execute against table t1 1520d65dc0eSdrh# t0 True if there is an error from $cmd 1530d65dc0eSdrh# t1 Content of "c" column of t1 assuming no error in $cmd 1540d65dc0eSdrh# t2 Content of "x" column of t2 1550d65dc0eSdrh# 1560d65dc0eSdrhforeach {i conf cmd t0 t1 t2} { 1570d65dc0eSdrh 1 {} INSERT 1 {} 1 1580d65dc0eSdrh 2 {} {INSERT OR IGNORE} 0 3 1 1590d65dc0eSdrh 3 {} {INSERT OR REPLACE} 0 4 1 1600d65dc0eSdrh 4 {} REPLACE 0 4 1 1610d65dc0eSdrh 5 {} {INSERT OR FAIL} 1 {} 1 1620d65dc0eSdrh 6 {} {INSERT OR ABORT} 1 {} 1 1630d65dc0eSdrh 7 {} {INSERT OR ROLLBACK} 1 {} {} 1640d65dc0eSdrh 8 IGNORE INSERT 0 3 1 1650d65dc0eSdrh 9 IGNORE {INSERT OR IGNORE} 0 3 1 1660d65dc0eSdrh 10 IGNORE {INSERT OR REPLACE} 0 4 1 1670d65dc0eSdrh 11 IGNORE REPLACE 0 4 1 1680d65dc0eSdrh 12 IGNORE {INSERT OR FAIL} 1 {} 1 1690d65dc0eSdrh 13 IGNORE {INSERT OR ABORT} 1 {} 1 1700d65dc0eSdrh 14 IGNORE {INSERT OR ROLLBACK} 1 {} {} 1710d65dc0eSdrh 15 REPLACE INSERT 0 4 1 1720d65dc0eSdrh 16 FAIL INSERT 1 {} 1 1730d65dc0eSdrh 17 ABORT INSERT 1 {} 1 1740d65dc0eSdrh 18 ROLLBACK INSERT 1 {} {} 1750d65dc0eSdrh} { 1760d65dc0eSdrh do_test conflict-3.$i { 1770d65dc0eSdrh if {$conf!=""} {set conf "ON CONFLICT $conf"} 1780d65dc0eSdrh set r0 [catch {execsql [subst { 1790d65dc0eSdrh DELETE FROM t1; 1800d65dc0eSdrh DELETE FROM t2; 1810d65dc0eSdrh INSERT INTO t1 VALUES(1,2,3); 1820d65dc0eSdrh BEGIN $conf; 1830d65dc0eSdrh INSERT INTO t2 VALUES(1); 1840d65dc0eSdrh $cmd INTO t1 VALUES(1,2,4); 1850d65dc0eSdrh }]} r1] 1866b8b8749Sdrh catch {execsql {COMMIT}} 1870d65dc0eSdrh if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} 1880d65dc0eSdrh set r2 [execsql {SELECT x FROM t2}] 1890d65dc0eSdrh list $r0 $r1 $r2 1900d65dc0eSdrh } [list $t0 $t1 $t2] 191ef6764a1Sdrh} 1920d65dc0eSdrh 1930d65dc0eSdrhdo_test conflict-4.0 { 1940d65dc0eSdrh execsql { 1950d65dc0eSdrh DROP TABLE t2; 1960d65dc0eSdrh CREATE TABLE t2(x); 1970d65dc0eSdrh SELECT x FROM t2; 198ef6764a1Sdrh } 1990d65dc0eSdrh} {} 2000d65dc0eSdrh 2010d65dc0eSdrh# Six columns of configuration data as follows: 2020d65dc0eSdrh# 2030d65dc0eSdrh# i The reference number of the test 2040d65dc0eSdrh# conf1 The conflict resolution algorithm on the UNIQUE constraint 2050d65dc0eSdrh# conf2 The conflict resolution algorithm on the BEGIN statement 2060d65dc0eSdrh# cmd An INSERT or REPLACE command to execute against table t1 2070d65dc0eSdrh# t0 True if there is an error from $cmd 2080d65dc0eSdrh# t1 Content of "c" column of t1 assuming no error in $cmd 2090d65dc0eSdrh# t2 Content of "x" column of t2 2100d65dc0eSdrh# 2110d65dc0eSdrhforeach {i conf1 conf2 cmd t0 t1 t2} { 2120d65dc0eSdrh 1 {} {} INSERT 1 {} 1 2130d65dc0eSdrh 2 REPLACE {} INSERT 0 4 1 2140d65dc0eSdrh 3 IGNORE {} INSERT 0 3 1 2150d65dc0eSdrh 4 FAIL {} INSERT 1 {} 1 2160d65dc0eSdrh 5 ABORT {} INSERT 1 {} 1 2170d65dc0eSdrh 6 ROLLBACK {} INSERT 1 {} {} 2180d65dc0eSdrh 7 REPLACE {} {INSERT OR IGNORE} 0 3 1 2190d65dc0eSdrh 8 IGNORE {} {INSERT OR REPLACE} 0 4 1 2200d65dc0eSdrh 9 FAIL {} {INSERT OR IGNORE} 0 3 1 2210d65dc0eSdrh 10 ABORT {} {INSERT OR REPLACE} 0 4 1 2220d65dc0eSdrh 11 ROLLBACK {} {INSERT OR IGNORE } 0 3 1 223a996e477Sdrh 12 REPLACE IGNORE INSERT 0 3 1 224a996e477Sdrh 13 IGNORE REPLACE INSERT 0 4 1 225a996e477Sdrh 14 FAIL IGNORE INSERT 0 3 1 226a996e477Sdrh 15 ABORT REPLACE INSERT 0 4 1 227a996e477Sdrh 16 ROLLBACK IGNORE INSERT 0 3 1 228a996e477Sdrh 12 IGNORE REPLACE INSERT 0 4 1 229a996e477Sdrh 13 IGNORE FAIL INSERT 1 {} 1 230a996e477Sdrh 14 IGNORE ABORT INSERT 1 {} 1 231a996e477Sdrh 15 IGNORE ROLLBACK INSERT 1 {} {} 2320d65dc0eSdrh} { 2330d65dc0eSdrh do_test conflict-4.$i { 2340d65dc0eSdrh if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} 2350d65dc0eSdrh if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"} 2360d65dc0eSdrh set r0 [catch {execsql [subst { 2370d65dc0eSdrh DROP TABLE t1; 2380d65dc0eSdrh CREATE TABLE t1(a,b,c,UNIQUE(a,b) $conf1); 2390d65dc0eSdrh DELETE FROM t2; 2400d65dc0eSdrh INSERT INTO t1 VALUES(1,2,3); 2410d65dc0eSdrh BEGIN $conf2; 2420d65dc0eSdrh INSERT INTO t2 VALUES(1); 2430d65dc0eSdrh $cmd INTO t1 VALUES(1,2,4); 2440d65dc0eSdrh }]} r1] 2456b8b8749Sdrh catch {execsql {COMMIT}} 2460d65dc0eSdrh if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} 2470d65dc0eSdrh set r2 [execsql {SELECT x FROM t2}] 2480d65dc0eSdrh list $r0 $r1 $r2 2490d65dc0eSdrh } [list $t0 $t1 $t2] 250ef6764a1Sdrh} 2510d65dc0eSdrh 2520d65dc0eSdrhdo_test conflict-5.0 { 2530d65dc0eSdrh execsql { 2540d65dc0eSdrh DROP TABLE t2; 2550d65dc0eSdrh CREATE TABLE t2(x); 2560d65dc0eSdrh SELECT x FROM t2; 2570d65dc0eSdrh } 2580d65dc0eSdrh} {} 2590d65dc0eSdrh 2600d65dc0eSdrh# Six columns of configuration data as follows: 2610d65dc0eSdrh# 2620d65dc0eSdrh# i The reference number of the test 2630d65dc0eSdrh# conf1 The conflict resolution algorithm on the NOT NULL constraint 2640d65dc0eSdrh# conf2 The conflict resolution algorithm on the BEGIN statement 2650d65dc0eSdrh# cmd An INSERT or REPLACE command to execute against table t1 2660d65dc0eSdrh# t0 True if there is an error from $cmd 2670d65dc0eSdrh# t1 Content of "c" column of t1 assuming no error in $cmd 2680d65dc0eSdrh# t2 Content of "x" column of t2 2690d65dc0eSdrh# 2700d65dc0eSdrhforeach {i conf1 conf2 cmd t0 t1 t2} { 2710d65dc0eSdrh 1 {} {} INSERT 1 {} 1 2720d65dc0eSdrh 2 REPLACE {} INSERT 0 5 1 2730d65dc0eSdrh 3 IGNORE {} INSERT 0 {} 1 2740d65dc0eSdrh 4 FAIL {} INSERT 1 {} 1 2750d65dc0eSdrh 5 ABORT {} INSERT 1 {} 1 2760d65dc0eSdrh 6 ROLLBACK {} INSERT 1 {} {} 2770d65dc0eSdrh 7 REPLACE {} {INSERT OR IGNORE} 0 {} 1 2780d65dc0eSdrh 8 IGNORE {} {INSERT OR REPLACE} 0 5 1 2790d65dc0eSdrh 9 FAIL {} {INSERT OR IGNORE} 0 {} 1 2800d65dc0eSdrh 10 ABORT {} {INSERT OR REPLACE} 0 5 1 2810d65dc0eSdrh 11 ROLLBACK {} {INSERT OR IGNORE} 0 {} 1 2820d65dc0eSdrh 12 {} {} {INSERT OR IGNORE} 0 {} 1 2830d65dc0eSdrh 13 {} {} {INSERT OR REPLACE} 0 5 1 2840d65dc0eSdrh 14 {} {} {INSERT OR FAIL} 1 {} 1 2850d65dc0eSdrh 15 {} {} {INSERT OR ABORT} 1 {} 1 2860d65dc0eSdrh 16 {} {} {INSERT OR ROLLBACK} 1 {} {} 2870d65dc0eSdrh 17 {} IGNORE INSERT 0 {} 1 2880d65dc0eSdrh 18 {} REPLACE INSERT 0 5 1 2890d65dc0eSdrh 19 {} FAIL INSERT 1 {} 1 2900d65dc0eSdrh 20 {} ABORT INSERT 1 {} 1 2910d65dc0eSdrh 21 {} ROLLBACK INSERT 1 {} {} 292a996e477Sdrh 22 REPLACE FAIL INSERT 1 {} 1 293a996e477Sdrh 23 IGNORE ROLLBACK INSERT 1 {} {} 2940d65dc0eSdrh} { 295483750baSdrh if {$t0} {set t1 {t1.c may not be NULL}} 2960d65dc0eSdrh do_test conflict-5.$i { 2970d65dc0eSdrh if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} 2980d65dc0eSdrh if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"} 2990d65dc0eSdrh set r0 [catch {execsql [subst { 3000d65dc0eSdrh DROP TABLE t1; 3010d65dc0eSdrh CREATE TABLE t1(a,b,c NOT NULL $conf1 DEFAULT 5); 3020d65dc0eSdrh DELETE FROM t2; 3030d65dc0eSdrh BEGIN $conf2; 3040d65dc0eSdrh INSERT INTO t2 VALUES(1); 3050d65dc0eSdrh $cmd INTO t1 VALUES(1,2,NULL); 3060d65dc0eSdrh }]} r1] 3076b8b8749Sdrh catch {execsql {COMMIT}} 3080d65dc0eSdrh if {!$r0} {set r1 [execsql {SELECT c FROM t1}]} 3090d65dc0eSdrh set r2 [execsql {SELECT x FROM t2}] 3100d65dc0eSdrh list $r0 $r1 $r2 3110d65dc0eSdrh } [list $t0 $t1 $t2] 3120d65dc0eSdrh} 3130d65dc0eSdrh 3140d65dc0eSdrhdo_test conflict-6.0 { 3150d65dc0eSdrh execsql { 3160d65dc0eSdrh DROP TABLE t2; 3170d65dc0eSdrh CREATE TABLE t2(a,b,c); 3180d65dc0eSdrh INSERT INTO t2 VALUES(1,2,1); 3190d65dc0eSdrh INSERT INTO t2 VALUES(2,3,2); 3200d65dc0eSdrh INSERT INTO t2 VALUES(3,4,1); 3210d65dc0eSdrh INSERT INTO t2 VALUES(4,5,4); 3220d65dc0eSdrh SELECT c FROM t2 ORDER BY b; 3230d65dc0eSdrh CREATE TABLE t3(x); 3240d65dc0eSdrh INSERT INTO t3 VALUES(1); 3250d65dc0eSdrh } 3260d65dc0eSdrh} {1 2 1 4} 3270d65dc0eSdrh 3280d65dc0eSdrh# Six columns of configuration data as follows: 3290d65dc0eSdrh# 3300d65dc0eSdrh# i The reference number of the test 3310d65dc0eSdrh# conf1 The conflict resolution algorithm on the UNIQUE constraint 3320d65dc0eSdrh# conf2 The conflict resolution algorithm on the BEGIN statement 3330d65dc0eSdrh# cmd An UPDATE command to execute against table t1 3340d65dc0eSdrh# t0 True if there is an error from $cmd 3350d65dc0eSdrh# t1 Content of "b" column of t1 assuming no error in $cmd 3360d65dc0eSdrh# t2 Content of "x" column of t3 3370d65dc0eSdrh# 3380d65dc0eSdrhforeach {i conf1 conf2 cmd t0 t1 t2} { 3390d65dc0eSdrh 1 {} {} UPDATE 1 {6 7 8 9} 1 3400d65dc0eSdrh 2 REPLACE {} UPDATE 0 {7 6 9} 1 3410d65dc0eSdrh 3 IGNORE {} UPDATE 0 {6 7 3 9} 1 3420d65dc0eSdrh 4 FAIL {} UPDATE 1 {6 7 3 4} 1 3430d65dc0eSdrh 5 ABORT {} UPDATE 1 {1 2 3 4} 1 3440d65dc0eSdrh 6 ROLLBACK {} UPDATE 1 {1 2 3 4} 0 3450d65dc0eSdrh 7 REPLACE {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3460d65dc0eSdrh 8 IGNORE {} {UPDATE OR REPLACE} 0 {7 6 9} 1 3470d65dc0eSdrh 9 FAIL {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3480d65dc0eSdrh 10 ABORT {} {UPDATE OR REPLACE} 0 {7 6 9} 1 3490d65dc0eSdrh 11 ROLLBACK {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3500d65dc0eSdrh 12 {} {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3510d65dc0eSdrh 13 {} {} {UPDATE OR REPLACE} 0 {7 6 9} 1 3520d65dc0eSdrh 14 {} {} {UPDATE OR FAIL} 1 {6 7 3 4} 1 3530d65dc0eSdrh 15 {} {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 3540d65dc0eSdrh 16 {} {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0 3550d65dc0eSdrh 17 {} IGNORE UPDATE 0 {6 7 3 9} 1 3560d65dc0eSdrh 18 {} REPLACE UPDATE 0 {7 6 9} 1 3570d65dc0eSdrh 19 {} FAIL UPDATE 1 {6 7 3 4} 1 3580d65dc0eSdrh 20 {} ABORT UPDATE 1 {1 2 3 4} 1 3590d65dc0eSdrh 21 {} ROLLBACK UPDATE 1 {1 2 3 4} 0 360a996e477Sdrh 22 REPLACE IGNORE UPDATE 0 {6 7 3 9} 1 361a996e477Sdrh 23 IGNORE REPLACE UPDATE 0 {7 6 9} 1 362a996e477Sdrh 24 REPLACE FAIL UPDATE 1 {6 7 3 4} 1 363a996e477Sdrh 25 IGNORE ABORT UPDATE 1 {1 2 3 4} 1 364a996e477Sdrh 26 REPLACE ROLLBACK UPDATE 1 {1 2 3 4} 0 3650d65dc0eSdrh} { 366483750baSdrh if {$t0} {set t1 {uniqueness constraint failed}} 3670d65dc0eSdrh do_test conflict-6.$i { 3680d65dc0eSdrh if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} 3690d65dc0eSdrh if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"} 3700d65dc0eSdrh set r0 [catch {execsql [subst { 3710d65dc0eSdrh DROP TABLE t1; 3720d65dc0eSdrh CREATE TABLE t1(a,b,c, UNIQUE(a) $conf1); 3730d65dc0eSdrh INSERT INTO t1 SELECT * FROM t2; 3740d65dc0eSdrh UPDATE t3 SET x=0; 3750d65dc0eSdrh BEGIN $conf2; 3760d65dc0eSdrh $cmd t3 SET x=1; 3771dd397f0Sdrh $cmd t1 SET b=b*2; 3780d65dc0eSdrh $cmd t1 SET a=c+5; 3790d65dc0eSdrh }]} r1] 3806b8b8749Sdrh catch {execsql {COMMIT}} 3810d65dc0eSdrh if {!$r0} {set r1 [execsql {SELECT a FROM t1 ORDER BY b}]} 3820d65dc0eSdrh set r2 [execsql {SELECT x FROM t3}] 3830d65dc0eSdrh list $r0 $r1 $r2 3840d65dc0eSdrh } [list $t0 $t1 $t2] 3850d65dc0eSdrh} 386ef6764a1Sdrh 387fe1a1773Sdrh# Test to make sure a lot of IGNOREs don't cause a stack overflow 388fe1a1773Sdrh# 389fe1a1773Sdrhdo_test conflict-7.1 { 390fe1a1773Sdrh execsql { 391fe1a1773Sdrh DROP TABLE t1; 392fe1a1773Sdrh DROP TABLE t2; 393fe1a1773Sdrh DROP TABLE t3; 394fe1a1773Sdrh CREATE TABLE t1(a unique, b); 395fe1a1773Sdrh } 396fe1a1773Sdrh for {set i 1} {$i<=50} {incr i} { 397fe1a1773Sdrh execsql "INSERT into t1 values($i,[expr {$i+1}]);" 398fe1a1773Sdrh } 399fe1a1773Sdrh execsql { 400fe1a1773Sdrh SELECT count(*), min(a), max(b) FROM t1; 401fe1a1773Sdrh } 402fe1a1773Sdrh} {50 1 51} 403fe1a1773Sdrhdo_test conflict-7.2 { 404fe1a1773Sdrh execsql { 405fe1a1773Sdrh PRAGMA count_changes=on; 406fe1a1773Sdrh UPDATE OR IGNORE t1 SET a=1000; 407fe1a1773Sdrh } 408fe1a1773Sdrh} {1} 409c8d30ac1Sdrhdo_test conflict-7.2.1 { 410c8d30ac1Sdrh db changes 411c8d30ac1Sdrh} {1} 412fe1a1773Sdrhdo_test conflict-7.3 { 413fe1a1773Sdrh execsql { 414fe1a1773Sdrh SELECT b FROM t1 WHERE a=1000; 415fe1a1773Sdrh } 416fe1a1773Sdrh} {2} 417fe1a1773Sdrhdo_test conflict-7.4 { 418fe1a1773Sdrh execsql { 419fe1a1773Sdrh SELECT count(*) FROM t1; 420fe1a1773Sdrh } 421fe1a1773Sdrh} {50} 422fe1a1773Sdrhdo_test conflict-7.5 { 423fe1a1773Sdrh execsql { 424fe1a1773Sdrh PRAGMA count_changes=on; 425fe1a1773Sdrh UPDATE OR REPLACE t1 SET a=1001; 426fe1a1773Sdrh } 427fe1a1773Sdrh} {50} 428c8d30ac1Sdrhdo_test conflict-7.5.1 { 429c8d30ac1Sdrh db changes 430c8d30ac1Sdrh} {50} 431fe1a1773Sdrhdo_test conflict-7.6 { 432fe1a1773Sdrh execsql { 433fe1a1773Sdrh SELECT b FROM t1 WHERE a=1001; 434fe1a1773Sdrh } 435fe1a1773Sdrh} {51} 436fe1a1773Sdrhdo_test conflict-7.7 { 437fe1a1773Sdrh execsql { 438fe1a1773Sdrh SELECT count(*) FROM t1; 439fe1a1773Sdrh } 440fe1a1773Sdrh} {1} 441c8d30ac1Sdrhdo_test conflict-7.7.1 { 442c8d30ac1Sdrh db changes 443c8d30ac1Sdrh} {0} 444fe1a1773Sdrh 445feeb1394Sdrh# Make sure the row count is right for rows that are ignored on 446feeb1394Sdrh# an insert. 447feeb1394Sdrh# 448feeb1394Sdrhdo_test conflict-8.1 { 449feeb1394Sdrh execsql { 450feeb1394Sdrh DELETE FROM t1; 451feeb1394Sdrh INSERT INTO t1 VALUES(1,2); 452feeb1394Sdrh } 453feeb1394Sdrh execsql { 454feeb1394Sdrh INSERT OR IGNORE INTO t1 VALUES(2,3); 455feeb1394Sdrh } 456feeb1394Sdrh} {1} 457c8d30ac1Sdrhdo_test conflict-8.1.1 { 458c8d30ac1Sdrh db changes 459c8d30ac1Sdrh} {1} 460feeb1394Sdrhdo_test conflict-8.2 { 461feeb1394Sdrh execsql { 462feeb1394Sdrh INSERT OR IGNORE INTO t1 VALUES(2,4); 463feeb1394Sdrh } 464feeb1394Sdrh} {0} 465c8d30ac1Sdrhdo_test conflict-8.2.1 { 466c8d30ac1Sdrh db changes 467c8d30ac1Sdrh} {0} 468feeb1394Sdrhdo_test conflict-8.3 { 469feeb1394Sdrh execsql { 470feeb1394Sdrh INSERT OR REPLACE INTO t1 VALUES(2,4); 471feeb1394Sdrh } 472feeb1394Sdrh} {1} 473c8d30ac1Sdrhdo_test conflict-8.3.1 { 474c8d30ac1Sdrh db changes 475c8d30ac1Sdrh} {1} 476feeb1394Sdrhdo_test conflict-8.4 { 477feeb1394Sdrh execsql { 478feeb1394Sdrh INSERT OR IGNORE INTO t1 SELECT * FROM t1; 479feeb1394Sdrh } 480feeb1394Sdrh} {0} 481c8d30ac1Sdrhdo_test conflict-8.4.1 { 482c8d30ac1Sdrh db changes 483c8d30ac1Sdrh} {0} 484feeb1394Sdrhdo_test conflict-8.5 { 485feeb1394Sdrh execsql { 486feeb1394Sdrh INSERT OR IGNORE INTO t1 SELECT a+2,b+2 FROM t1; 487feeb1394Sdrh } 488feeb1394Sdrh} {2} 489c8d30ac1Sdrhdo_test conflict-8.5.1 { 490c8d30ac1Sdrh db changes 491c8d30ac1Sdrh} {2} 492feeb1394Sdrhdo_test conflict-8.6 { 493feeb1394Sdrh execsql { 494feeb1394Sdrh INSERT OR IGNORE INTO t1 SELECT a+3,b+3 FROM t1; 495feeb1394Sdrh } 496feeb1394Sdrh} {3} 497c8d30ac1Sdrhdo_test conflict-8.6.1 { 498c8d30ac1Sdrh db changes 499c8d30ac1Sdrh} {3} 500fe1a1773Sdrh 5013b167c75Sdrhintegrity_check conflict-8.99 5023b167c75Sdrh 5033b167c75Sdrhdo_test conflict-9.1 { 5043b167c75Sdrh execsql { 5053b167c75Sdrh PRAGMA count_changes=0; 5063b167c75Sdrh CREATE TABLE t2( 5073b167c75Sdrh a INTEGER UNIQUE ON CONFLICT IGNORE, 5083b167c75Sdrh b INTEGER UNIQUE ON CONFLICT FAIL, 5093b167c75Sdrh c INTEGER UNIQUE ON CONFLICT REPLACE, 5103b167c75Sdrh d INTEGER UNIQUE ON CONFLICT ABORT, 5113b167c75Sdrh e INTEGER UNIQUE ON CONFLICT ROLLBACK 5123b167c75Sdrh ); 5133b167c75Sdrh CREATE TABLE t3(x); 5143b167c75Sdrh INSERT INTO t3 VALUES(1); 5153b167c75Sdrh SELECT * FROM t3; 5163b167c75Sdrh } 5173b167c75Sdrh} {1} 5183b167c75Sdrhdo_test conflict-9.2 { 5193b167c75Sdrh catchsql { 5203b167c75Sdrh INSERT INTO t2 VALUES(1,1,1,1,1); 5213b167c75Sdrh INSERT INTO t2 VALUES(2,2,2,2,2); 5223b167c75Sdrh SELECT * FROM t2; 5233b167c75Sdrh } 5243b167c75Sdrh} {0 {1 1 1 1 1 2 2 2 2 2}} 5253b167c75Sdrhdo_test conflict-9.3 { 5263b167c75Sdrh catchsql { 5273b167c75Sdrh INSERT INTO t2 VALUES(1,3,3,3,3); 5283b167c75Sdrh SELECT * FROM t2; 5293b167c75Sdrh } 5303b167c75Sdrh} {0 {1 1 1 1 1 2 2 2 2 2}} 5313b167c75Sdrhdo_test conflict-9.4 { 5323b167c75Sdrh catchsql { 5333b167c75Sdrh UPDATE t2 SET a=a+1 WHERE a=1; 5343b167c75Sdrh SELECT * FROM t2; 5353b167c75Sdrh } 5363b167c75Sdrh} {0 {1 1 1 1 1 2 2 2 2 2}} 5373b167c75Sdrhdo_test conflict-9.5 { 5383b167c75Sdrh catchsql { 5393b167c75Sdrh INSERT INTO t2 VALUES(3,1,3,3,3); 5403b167c75Sdrh SELECT * FROM t2; 5413b167c75Sdrh } 542483750baSdrh} {1 {uniqueness constraint failed}} 5433b167c75Sdrhdo_test conflict-9.6 { 5443b167c75Sdrh catchsql { 5453b167c75Sdrh UPDATE t2 SET b=b+1 WHERE b=1; 5463b167c75Sdrh SELECT * FROM t2; 5473b167c75Sdrh } 548483750baSdrh} {1 {uniqueness constraint failed}} 5493b167c75Sdrhdo_test conflict-9.7 { 5503b167c75Sdrh catchsql { 5513b167c75Sdrh BEGIN; 5523b167c75Sdrh UPDATE t3 SET x=x+1; 5533b167c75Sdrh INSERT INTO t2 VALUES(3,1,3,3,3); 5543b167c75Sdrh SELECT * FROM t2; 5553b167c75Sdrh } 556483750baSdrh} {1 {uniqueness constraint failed}} 5573b167c75Sdrhdo_test conflict-9.8 { 5583b167c75Sdrh execsql {COMMIT} 5593b167c75Sdrh execsql {SELECT * FROM t3} 5603b167c75Sdrh} {2} 561483750baSdrhdo_test conflict-9.9 { 5623b167c75Sdrh catchsql { 5633b167c75Sdrh BEGIN; 5643b167c75Sdrh UPDATE t3 SET x=x+1; 5653b167c75Sdrh UPDATE t2 SET b=b+1 WHERE b=1; 5663b167c75Sdrh SELECT * FROM t2; 5673b167c75Sdrh } 568483750baSdrh} {1 {uniqueness constraint failed}} 5693b167c75Sdrhdo_test conflict-9.10 { 5703b167c75Sdrh execsql {COMMIT} 5713b167c75Sdrh execsql {SELECT * FROM t3} 5723b167c75Sdrh} {3} 5733b167c75Sdrhdo_test conflict-9.11 { 5743b167c75Sdrh catchsql { 5753b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,1,3); 5763b167c75Sdrh SELECT * FROM t2; 5773b167c75Sdrh } 578483750baSdrh} {1 {uniqueness constraint failed}} 5793b167c75Sdrhdo_test conflict-9.12 { 5803b167c75Sdrh catchsql { 5813b167c75Sdrh UPDATE t2 SET d=d+1 WHERE d=1; 5823b167c75Sdrh SELECT * FROM t2; 5833b167c75Sdrh } 584483750baSdrh} {1 {uniqueness constraint failed}} 5853b167c75Sdrhdo_test conflict-9.13 { 5863b167c75Sdrh catchsql { 5873b167c75Sdrh BEGIN; 5883b167c75Sdrh UPDATE t3 SET x=x+1; 5893b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,1,3); 5903b167c75Sdrh SELECT * FROM t2; 5913b167c75Sdrh } 592483750baSdrh} {1 {uniqueness constraint failed}} 5933b167c75Sdrhdo_test conflict-9.14 { 5943b167c75Sdrh execsql {COMMIT} 5953b167c75Sdrh execsql {SELECT * FROM t3} 5963b167c75Sdrh} {4} 5973b167c75Sdrhdo_test conflict-9.15 { 5983b167c75Sdrh catchsql { 5993b167c75Sdrh BEGIN; 6003b167c75Sdrh UPDATE t3 SET x=x+1; 6013b167c75Sdrh UPDATE t2 SET d=d+1 WHERE d=1; 6023b167c75Sdrh SELECT * FROM t2; 6033b167c75Sdrh } 604483750baSdrh} {1 {uniqueness constraint failed}} 6053b167c75Sdrhdo_test conflict-9.16 { 6063b167c75Sdrh execsql {COMMIT} 6073b167c75Sdrh execsql {SELECT * FROM t3} 6083b167c75Sdrh} {5} 6093b167c75Sdrhdo_test conflict-9.17 { 6103b167c75Sdrh catchsql { 6113b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,3,1); 6123b167c75Sdrh SELECT * FROM t2; 6133b167c75Sdrh } 614483750baSdrh} {1 {uniqueness constraint failed}} 6153b167c75Sdrhdo_test conflict-9.18 { 6163b167c75Sdrh catchsql { 6173b167c75Sdrh UPDATE t2 SET e=e+1 WHERE e=1; 6183b167c75Sdrh SELECT * FROM t2; 6193b167c75Sdrh } 620483750baSdrh} {1 {uniqueness constraint failed}} 6213b167c75Sdrhdo_test conflict-9.19 { 6223b167c75Sdrh catchsql { 6233b167c75Sdrh BEGIN; 6243b167c75Sdrh UPDATE t3 SET x=x+1; 6253b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,3,1); 6263b167c75Sdrh SELECT * FROM t2; 6273b167c75Sdrh } 628483750baSdrh} {1 {uniqueness constraint failed}} 6293b167c75Sdrhdo_test conflict-9.20 { 6306b8b8749Sdrh catch {execsql {COMMIT}} 6313b167c75Sdrh execsql {SELECT * FROM t3} 6323b167c75Sdrh} {5} 6333b167c75Sdrhdo_test conflict-9.21 { 6343b167c75Sdrh catchsql { 6353b167c75Sdrh BEGIN; 6363b167c75Sdrh UPDATE t3 SET x=x+1; 6373b167c75Sdrh UPDATE t2 SET e=e+1 WHERE e=1; 6383b167c75Sdrh SELECT * FROM t2; 6393b167c75Sdrh } 640483750baSdrh} {1 {uniqueness constraint failed}} 6413b167c75Sdrhdo_test conflict-9.22 { 6426b8b8749Sdrh catch {execsql {COMMIT}} 6433b167c75Sdrh execsql {SELECT * FROM t3} 6443b167c75Sdrh} {5} 6453b167c75Sdrhdo_test conflict-9.23 { 6463b167c75Sdrh catchsql { 6473b167c75Sdrh INSERT INTO t2 VALUES(3,3,1,3,3); 6483b167c75Sdrh SELECT * FROM t2; 6493b167c75Sdrh } 6503b167c75Sdrh} {0 {2 2 2 2 2 3 3 1 3 3}} 6513b167c75Sdrhdo_test conflict-9.24 { 6523b167c75Sdrh catchsql { 6533b167c75Sdrh UPDATE t2 SET c=c-1 WHERE c=2; 6543b167c75Sdrh SELECT * FROM t2; 6553b167c75Sdrh } 6563b167c75Sdrh} {0 {2 2 1 2 2}} 6573b167c75Sdrhdo_test conflict-9.25 { 6583b167c75Sdrh catchsql { 6593b167c75Sdrh BEGIN; 6603b167c75Sdrh UPDATE t3 SET x=x+1; 6613b167c75Sdrh INSERT INTO t2 VALUES(3,3,1,3,3); 6623b167c75Sdrh SELECT * FROM t2; 6633b167c75Sdrh } 6643b167c75Sdrh} {0 {3 3 1 3 3}} 6653b167c75Sdrhdo_test conflict-9.26 { 6666b8b8749Sdrh catch {execsql {COMMIT}} 6673b167c75Sdrh execsql {SELECT * FROM t3} 6683b167c75Sdrh} {6} 669bb37fdc7Sdrh 670a996e477Sdrhdo_test conflict-10.1 { 671a996e477Sdrh catchsql { 672a996e477Sdrh DELETE FROM t1; 673a996e477Sdrh BEGIN ON CONFLICT ROLLBACK; 674a996e477Sdrh INSERT INTO t1 VALUES(1,2); 675a996e477Sdrh INSERT INTO t1 VALUES(1,3); 676a996e477Sdrh COMMIT; 677a996e477Sdrh } 678a996e477Sdrh execsql {SELECT * FROM t1} 679a996e477Sdrh} {} 680a996e477Sdrhdo_test conflict-10.2 { 681a996e477Sdrh catchsql { 682a996e477Sdrh CREATE TABLE t4(x); 683a996e477Sdrh CREATE UNIQUE INDEX t4x ON t4(x); 684a996e477Sdrh BEGIN ON CONFLICT ROLLBACK; 685a996e477Sdrh INSERT INTO t4 VALUES(1); 686a996e477Sdrh INSERT INTO t4 VALUES(1); 687a996e477Sdrh COMMIT; 688a996e477Sdrh } 689a996e477Sdrh execsql {SELECT * FROM t4} 690a996e477Sdrh} {} 691a996e477Sdrh 692*ed717fe3Sdrhintegrity_check conflict-99.0 693*ed717fe3Sdrh 694bb37fdc7Sdrhfinish_test 695