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*6b8b8749Sdrh# $Id: conflict.test,v 1.14 2002/08/18 20:28:07 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] 70*6b8b8749Sdrh 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] 128*6b8b8749Sdrh 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; 1410d65dc0eSdrh CREATE TABLE t1(a, b, c INTEGER PRIMARY KEY, 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] 186*6b8b8749Sdrh 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 2230d65dc0eSdrh 12 REPLACE IGNORE INSERT 0 4 1 2240d65dc0eSdrh 13 IGNORE REPLACE INSERT 0 3 1 2250d65dc0eSdrh 14 FAIL IGNORE INSERT 1 {} 1 2260d65dc0eSdrh 15 ABORT REPLACE INSERT 1 {} 1 2270d65dc0eSdrh 16 ROLLBACK IGNORE INSERT 1 {} {} 2280d65dc0eSdrh} { 2290d65dc0eSdrh do_test conflict-4.$i { 2300d65dc0eSdrh if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} 2310d65dc0eSdrh if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"} 2320d65dc0eSdrh set r0 [catch {execsql [subst { 2330d65dc0eSdrh DROP TABLE t1; 2340d65dc0eSdrh CREATE TABLE t1(a,b,c,UNIQUE(a,b) $conf1); 2350d65dc0eSdrh DELETE FROM t2; 2360d65dc0eSdrh INSERT INTO t1 VALUES(1,2,3); 2370d65dc0eSdrh BEGIN $conf2; 2380d65dc0eSdrh INSERT INTO t2 VALUES(1); 2390d65dc0eSdrh $cmd INTO t1 VALUES(1,2,4); 2400d65dc0eSdrh }]} r1] 241*6b8b8749Sdrh catch {execsql {COMMIT}} 2420d65dc0eSdrh if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} 2430d65dc0eSdrh set r2 [execsql {SELECT x FROM t2}] 2440d65dc0eSdrh list $r0 $r1 $r2 2450d65dc0eSdrh } [list $t0 $t1 $t2] 246ef6764a1Sdrh} 2470d65dc0eSdrh 2480d65dc0eSdrhdo_test conflict-5.0 { 2490d65dc0eSdrh execsql { 2500d65dc0eSdrh DROP TABLE t2; 2510d65dc0eSdrh CREATE TABLE t2(x); 2520d65dc0eSdrh SELECT x FROM t2; 2530d65dc0eSdrh } 2540d65dc0eSdrh} {} 2550d65dc0eSdrh 2560d65dc0eSdrh# Six columns of configuration data as follows: 2570d65dc0eSdrh# 2580d65dc0eSdrh# i The reference number of the test 2590d65dc0eSdrh# conf1 The conflict resolution algorithm on the NOT NULL constraint 2600d65dc0eSdrh# conf2 The conflict resolution algorithm on the BEGIN statement 2610d65dc0eSdrh# cmd An INSERT or REPLACE command to execute against table t1 2620d65dc0eSdrh# t0 True if there is an error from $cmd 2630d65dc0eSdrh# t1 Content of "c" column of t1 assuming no error in $cmd 2640d65dc0eSdrh# t2 Content of "x" column of t2 2650d65dc0eSdrh# 2660d65dc0eSdrhforeach {i conf1 conf2 cmd t0 t1 t2} { 2670d65dc0eSdrh 1 {} {} INSERT 1 {} 1 2680d65dc0eSdrh 2 REPLACE {} INSERT 0 5 1 2690d65dc0eSdrh 3 IGNORE {} INSERT 0 {} 1 2700d65dc0eSdrh 4 FAIL {} INSERT 1 {} 1 2710d65dc0eSdrh 5 ABORT {} INSERT 1 {} 1 2720d65dc0eSdrh 6 ROLLBACK {} INSERT 1 {} {} 2730d65dc0eSdrh 7 REPLACE {} {INSERT OR IGNORE} 0 {} 1 2740d65dc0eSdrh 8 IGNORE {} {INSERT OR REPLACE} 0 5 1 2750d65dc0eSdrh 9 FAIL {} {INSERT OR IGNORE} 0 {} 1 2760d65dc0eSdrh 10 ABORT {} {INSERT OR REPLACE} 0 5 1 2770d65dc0eSdrh 11 ROLLBACK {} {INSERT OR IGNORE} 0 {} 1 2780d65dc0eSdrh 12 {} {} {INSERT OR IGNORE} 0 {} 1 2790d65dc0eSdrh 13 {} {} {INSERT OR REPLACE} 0 5 1 2800d65dc0eSdrh 14 {} {} {INSERT OR FAIL} 1 {} 1 2810d65dc0eSdrh 15 {} {} {INSERT OR ABORT} 1 {} 1 2820d65dc0eSdrh 16 {} {} {INSERT OR ROLLBACK} 1 {} {} 2830d65dc0eSdrh 17 {} IGNORE INSERT 0 {} 1 2840d65dc0eSdrh 18 {} REPLACE INSERT 0 5 1 2850d65dc0eSdrh 19 {} FAIL INSERT 1 {} 1 2860d65dc0eSdrh 20 {} ABORT INSERT 1 {} 1 2870d65dc0eSdrh 21 {} ROLLBACK INSERT 1 {} {} 2880d65dc0eSdrh 22 REPLACE FAIL INSERT 0 5 1 2890d65dc0eSdrh 23 IGNORE ROLLBACK INSERT 0 {} 1 2900d65dc0eSdrh} { 2910d65dc0eSdrh if {$t0} {set t1 {constraint failed}} 2920d65dc0eSdrh do_test conflict-5.$i { 2930d65dc0eSdrh if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} 2940d65dc0eSdrh if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"} 2950d65dc0eSdrh set r0 [catch {execsql [subst { 2960d65dc0eSdrh DROP TABLE t1; 2970d65dc0eSdrh CREATE TABLE t1(a,b,c NOT NULL $conf1 DEFAULT 5); 2980d65dc0eSdrh DELETE FROM t2; 2990d65dc0eSdrh BEGIN $conf2; 3000d65dc0eSdrh INSERT INTO t2 VALUES(1); 3010d65dc0eSdrh $cmd INTO t1 VALUES(1,2,NULL); 3020d65dc0eSdrh }]} r1] 303*6b8b8749Sdrh catch {execsql {COMMIT}} 3040d65dc0eSdrh if {!$r0} {set r1 [execsql {SELECT c FROM t1}]} 3050d65dc0eSdrh set r2 [execsql {SELECT x FROM t2}] 3060d65dc0eSdrh list $r0 $r1 $r2 3070d65dc0eSdrh } [list $t0 $t1 $t2] 3080d65dc0eSdrh} 3090d65dc0eSdrh 3100d65dc0eSdrhdo_test conflict-6.0 { 3110d65dc0eSdrh execsql { 3120d65dc0eSdrh DROP TABLE t2; 3130d65dc0eSdrh CREATE TABLE t2(a,b,c); 3140d65dc0eSdrh INSERT INTO t2 VALUES(1,2,1); 3150d65dc0eSdrh INSERT INTO t2 VALUES(2,3,2); 3160d65dc0eSdrh INSERT INTO t2 VALUES(3,4,1); 3170d65dc0eSdrh INSERT INTO t2 VALUES(4,5,4); 3180d65dc0eSdrh SELECT c FROM t2 ORDER BY b; 3190d65dc0eSdrh CREATE TABLE t3(x); 3200d65dc0eSdrh INSERT INTO t3 VALUES(1); 3210d65dc0eSdrh } 3220d65dc0eSdrh} {1 2 1 4} 3230d65dc0eSdrh 3240d65dc0eSdrh# Six columns of configuration data as follows: 3250d65dc0eSdrh# 3260d65dc0eSdrh# i The reference number of the test 3270d65dc0eSdrh# conf1 The conflict resolution algorithm on the UNIQUE constraint 3280d65dc0eSdrh# conf2 The conflict resolution algorithm on the BEGIN statement 3290d65dc0eSdrh# cmd An UPDATE command to execute against table t1 3300d65dc0eSdrh# t0 True if there is an error from $cmd 3310d65dc0eSdrh# t1 Content of "b" column of t1 assuming no error in $cmd 3320d65dc0eSdrh# t2 Content of "x" column of t3 3330d65dc0eSdrh# 3340d65dc0eSdrhforeach {i conf1 conf2 cmd t0 t1 t2} { 3350d65dc0eSdrh 1 {} {} UPDATE 1 {6 7 8 9} 1 3360d65dc0eSdrh 2 REPLACE {} UPDATE 0 {7 6 9} 1 3370d65dc0eSdrh 3 IGNORE {} UPDATE 0 {6 7 3 9} 1 3380d65dc0eSdrh 4 FAIL {} UPDATE 1 {6 7 3 4} 1 3390d65dc0eSdrh 5 ABORT {} UPDATE 1 {1 2 3 4} 1 3400d65dc0eSdrh 6 ROLLBACK {} UPDATE 1 {1 2 3 4} 0 3410d65dc0eSdrh 7 REPLACE {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3420d65dc0eSdrh 8 IGNORE {} {UPDATE OR REPLACE} 0 {7 6 9} 1 3430d65dc0eSdrh 9 FAIL {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3440d65dc0eSdrh 10 ABORT {} {UPDATE OR REPLACE} 0 {7 6 9} 1 3450d65dc0eSdrh 11 ROLLBACK {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3460d65dc0eSdrh 12 {} {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 3470d65dc0eSdrh 13 {} {} {UPDATE OR REPLACE} 0 {7 6 9} 1 3480d65dc0eSdrh 14 {} {} {UPDATE OR FAIL} 1 {6 7 3 4} 1 3490d65dc0eSdrh 15 {} {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 3500d65dc0eSdrh 16 {} {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0 3510d65dc0eSdrh 17 {} IGNORE UPDATE 0 {6 7 3 9} 1 3520d65dc0eSdrh 18 {} REPLACE UPDATE 0 {7 6 9} 1 3530d65dc0eSdrh 19 {} FAIL UPDATE 1 {6 7 3 4} 1 3540d65dc0eSdrh 20 {} ABORT UPDATE 1 {1 2 3 4} 1 3550d65dc0eSdrh 21 {} ROLLBACK UPDATE 1 {1 2 3 4} 0 3560d65dc0eSdrh 22 REPLACE FAIL UPDATE 0 {7 6 9} 1 3570d65dc0eSdrh 23 IGNORE ROLLBACK UPDATE 0 {6 7 3 9} 1 3580d65dc0eSdrh} { 3590d65dc0eSdrh if {$t0} {set t1 {constraint failed}} 3600d65dc0eSdrh do_test conflict-6.$i { 3610d65dc0eSdrh if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} 3620d65dc0eSdrh if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"} 3630d65dc0eSdrh set r0 [catch {execsql [subst { 3640d65dc0eSdrh DROP TABLE t1; 3650d65dc0eSdrh CREATE TABLE t1(a,b,c, UNIQUE(a) $conf1); 3660d65dc0eSdrh INSERT INTO t1 SELECT * FROM t2; 3670d65dc0eSdrh UPDATE t3 SET x=0; 3680d65dc0eSdrh BEGIN $conf2; 3690d65dc0eSdrh $cmd t3 SET x=1; 3701dd397f0Sdrh $cmd t1 SET b=b*2; 3710d65dc0eSdrh $cmd t1 SET a=c+5; 3720d65dc0eSdrh }]} r1] 373*6b8b8749Sdrh catch {execsql {COMMIT}} 3740d65dc0eSdrh if {!$r0} {set r1 [execsql {SELECT a FROM t1 ORDER BY b}]} 3750d65dc0eSdrh set r2 [execsql {SELECT x FROM t3}] 3760d65dc0eSdrh list $r0 $r1 $r2 3770d65dc0eSdrh } [list $t0 $t1 $t2] 3780d65dc0eSdrh} 379ef6764a1Sdrh 380fe1a1773Sdrh# Test to make sure a lot of IGNOREs don't cause a stack overflow 381fe1a1773Sdrh# 382fe1a1773Sdrhdo_test conflict-7.1 { 383fe1a1773Sdrh execsql { 384fe1a1773Sdrh DROP TABLE t1; 385fe1a1773Sdrh DROP TABLE t2; 386fe1a1773Sdrh DROP TABLE t3; 387fe1a1773Sdrh CREATE TABLE t1(a unique, b); 388fe1a1773Sdrh } 389fe1a1773Sdrh for {set i 1} {$i<=50} {incr i} { 390fe1a1773Sdrh execsql "INSERT into t1 values($i,[expr {$i+1}]);" 391fe1a1773Sdrh } 392fe1a1773Sdrh execsql { 393fe1a1773Sdrh SELECT count(*), min(a), max(b) FROM t1; 394fe1a1773Sdrh } 395fe1a1773Sdrh} {50 1 51} 396fe1a1773Sdrhdo_test conflict-7.2 { 397fe1a1773Sdrh execsql { 398fe1a1773Sdrh PRAGMA count_changes=on; 399fe1a1773Sdrh UPDATE OR IGNORE t1 SET a=1000; 400fe1a1773Sdrh } 401fe1a1773Sdrh} {1} 402c8d30ac1Sdrhdo_test conflict-7.2.1 { 403c8d30ac1Sdrh db changes 404c8d30ac1Sdrh} {1} 405fe1a1773Sdrhdo_test conflict-7.3 { 406fe1a1773Sdrh execsql { 407fe1a1773Sdrh SELECT b FROM t1 WHERE a=1000; 408fe1a1773Sdrh } 409fe1a1773Sdrh} {2} 410fe1a1773Sdrhdo_test conflict-7.4 { 411fe1a1773Sdrh execsql { 412fe1a1773Sdrh SELECT count(*) FROM t1; 413fe1a1773Sdrh } 414fe1a1773Sdrh} {50} 415fe1a1773Sdrhdo_test conflict-7.5 { 416fe1a1773Sdrh execsql { 417fe1a1773Sdrh PRAGMA count_changes=on; 418fe1a1773Sdrh UPDATE OR REPLACE t1 SET a=1001; 419fe1a1773Sdrh } 420fe1a1773Sdrh} {50} 421c8d30ac1Sdrhdo_test conflict-7.5.1 { 422c8d30ac1Sdrh db changes 423c8d30ac1Sdrh} {50} 424fe1a1773Sdrhdo_test conflict-7.6 { 425fe1a1773Sdrh execsql { 426fe1a1773Sdrh SELECT b FROM t1 WHERE a=1001; 427fe1a1773Sdrh } 428fe1a1773Sdrh} {51} 429fe1a1773Sdrhdo_test conflict-7.7 { 430fe1a1773Sdrh execsql { 431fe1a1773Sdrh SELECT count(*) FROM t1; 432fe1a1773Sdrh } 433fe1a1773Sdrh} {1} 434c8d30ac1Sdrhdo_test conflict-7.7.1 { 435c8d30ac1Sdrh db changes 436c8d30ac1Sdrh} {0} 437fe1a1773Sdrh 438feeb1394Sdrh# Make sure the row count is right for rows that are ignored on 439feeb1394Sdrh# an insert. 440feeb1394Sdrh# 441feeb1394Sdrhdo_test conflict-8.1 { 442feeb1394Sdrh execsql { 443feeb1394Sdrh DELETE FROM t1; 444feeb1394Sdrh INSERT INTO t1 VALUES(1,2); 445feeb1394Sdrh } 446feeb1394Sdrh execsql { 447feeb1394Sdrh INSERT OR IGNORE INTO t1 VALUES(2,3); 448feeb1394Sdrh } 449feeb1394Sdrh} {1} 450c8d30ac1Sdrhdo_test conflict-8.1.1 { 451c8d30ac1Sdrh db changes 452c8d30ac1Sdrh} {1} 453feeb1394Sdrhdo_test conflict-8.2 { 454feeb1394Sdrh execsql { 455feeb1394Sdrh INSERT OR IGNORE INTO t1 VALUES(2,4); 456feeb1394Sdrh } 457feeb1394Sdrh} {0} 458c8d30ac1Sdrhdo_test conflict-8.2.1 { 459c8d30ac1Sdrh db changes 460c8d30ac1Sdrh} {0} 461feeb1394Sdrhdo_test conflict-8.3 { 462feeb1394Sdrh execsql { 463feeb1394Sdrh INSERT OR REPLACE INTO t1 VALUES(2,4); 464feeb1394Sdrh } 465feeb1394Sdrh} {1} 466c8d30ac1Sdrhdo_test conflict-8.3.1 { 467c8d30ac1Sdrh db changes 468c8d30ac1Sdrh} {1} 469feeb1394Sdrhdo_test conflict-8.4 { 470feeb1394Sdrh execsql { 471feeb1394Sdrh INSERT OR IGNORE INTO t1 SELECT * FROM t1; 472feeb1394Sdrh } 473feeb1394Sdrh} {0} 474c8d30ac1Sdrhdo_test conflict-8.4.1 { 475c8d30ac1Sdrh db changes 476c8d30ac1Sdrh} {0} 477feeb1394Sdrhdo_test conflict-8.5 { 478feeb1394Sdrh execsql { 479feeb1394Sdrh INSERT OR IGNORE INTO t1 SELECT a+2,b+2 FROM t1; 480feeb1394Sdrh } 481feeb1394Sdrh} {2} 482c8d30ac1Sdrhdo_test conflict-8.5.1 { 483c8d30ac1Sdrh db changes 484c8d30ac1Sdrh} {2} 485feeb1394Sdrhdo_test conflict-8.6 { 486feeb1394Sdrh execsql { 487feeb1394Sdrh INSERT OR IGNORE INTO t1 SELECT a+3,b+3 FROM t1; 488feeb1394Sdrh } 489feeb1394Sdrh} {3} 490c8d30ac1Sdrhdo_test conflict-8.6.1 { 491c8d30ac1Sdrh db changes 492c8d30ac1Sdrh} {3} 493fe1a1773Sdrh 4943b167c75Sdrhintegrity_check conflict-8.99 4953b167c75Sdrh 4963b167c75Sdrhdo_test conflict-9.1 { 4973b167c75Sdrh execsql { 4983b167c75Sdrh PRAGMA count_changes=0; 4993b167c75Sdrh CREATE TABLE t2( 5003b167c75Sdrh a INTEGER UNIQUE ON CONFLICT IGNORE, 5013b167c75Sdrh b INTEGER UNIQUE ON CONFLICT FAIL, 5023b167c75Sdrh c INTEGER UNIQUE ON CONFLICT REPLACE, 5033b167c75Sdrh d INTEGER UNIQUE ON CONFLICT ABORT, 5043b167c75Sdrh e INTEGER UNIQUE ON CONFLICT ROLLBACK 5053b167c75Sdrh ); 5063b167c75Sdrh CREATE TABLE t3(x); 5073b167c75Sdrh INSERT INTO t3 VALUES(1); 5083b167c75Sdrh SELECT * FROM t3; 5093b167c75Sdrh } 5103b167c75Sdrh} {1} 5113b167c75Sdrhdo_test conflict-9.2 { 5123b167c75Sdrh catchsql { 5133b167c75Sdrh INSERT INTO t2 VALUES(1,1,1,1,1); 5143b167c75Sdrh INSERT INTO t2 VALUES(2,2,2,2,2); 5153b167c75Sdrh SELECT * FROM t2; 5163b167c75Sdrh } 5173b167c75Sdrh} {0 {1 1 1 1 1 2 2 2 2 2}} 5183b167c75Sdrhdo_test conflict-9.3 { 5193b167c75Sdrh catchsql { 5203b167c75Sdrh INSERT INTO t2 VALUES(1,3,3,3,3); 5213b167c75Sdrh SELECT * FROM t2; 5223b167c75Sdrh } 5233b167c75Sdrh} {0 {1 1 1 1 1 2 2 2 2 2}} 5243b167c75Sdrhdo_test conflict-9.4 { 5253b167c75Sdrh catchsql { 5263b167c75Sdrh UPDATE t2 SET a=a+1 WHERE a=1; 5273b167c75Sdrh SELECT * FROM t2; 5283b167c75Sdrh } 5293b167c75Sdrh} {0 {1 1 1 1 1 2 2 2 2 2}} 5303b167c75Sdrhdo_test conflict-9.5 { 5313b167c75Sdrh catchsql { 5323b167c75Sdrh INSERT INTO t2 VALUES(3,1,3,3,3); 5333b167c75Sdrh SELECT * FROM t2; 5343b167c75Sdrh } 5353b167c75Sdrh} {1 {constraint failed}} 5363b167c75Sdrhdo_test conflict-9.6 { 5373b167c75Sdrh catchsql { 5383b167c75Sdrh UPDATE t2 SET b=b+1 WHERE b=1; 5393b167c75Sdrh SELECT * FROM t2; 5403b167c75Sdrh } 5413b167c75Sdrh} {1 {constraint failed}} 5423b167c75Sdrhdo_test conflict-9.7 { 5433b167c75Sdrh catchsql { 5443b167c75Sdrh BEGIN; 5453b167c75Sdrh UPDATE t3 SET x=x+1; 5463b167c75Sdrh INSERT INTO t2 VALUES(3,1,3,3,3); 5473b167c75Sdrh SELECT * FROM t2; 5483b167c75Sdrh } 5493b167c75Sdrh} {1 {constraint failed}} 5503b167c75Sdrhdo_test conflict-9.8 { 5513b167c75Sdrh execsql {COMMIT} 5523b167c75Sdrh execsql {SELECT * FROM t3} 5533b167c75Sdrh} {2} 5543b167c75Sdrhdo_test conflict-9.6 { 5553b167c75Sdrh catchsql { 5563b167c75Sdrh BEGIN; 5573b167c75Sdrh UPDATE t3 SET x=x+1; 5583b167c75Sdrh UPDATE t2 SET b=b+1 WHERE b=1; 5593b167c75Sdrh SELECT * FROM t2; 5603b167c75Sdrh } 5613b167c75Sdrh} {1 {constraint failed}} 5623b167c75Sdrhdo_test conflict-9.10 { 5633b167c75Sdrh execsql {COMMIT} 5643b167c75Sdrh execsql {SELECT * FROM t3} 5653b167c75Sdrh} {3} 5663b167c75Sdrhdo_test conflict-9.11 { 5673b167c75Sdrh catchsql { 5683b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,1,3); 5693b167c75Sdrh SELECT * FROM t2; 5703b167c75Sdrh } 5713b167c75Sdrh} {1 {constraint failed}} 5723b167c75Sdrhdo_test conflict-9.12 { 5733b167c75Sdrh catchsql { 5743b167c75Sdrh UPDATE t2 SET d=d+1 WHERE d=1; 5753b167c75Sdrh SELECT * FROM t2; 5763b167c75Sdrh } 5773b167c75Sdrh} {1 {constraint failed}} 5783b167c75Sdrhdo_test conflict-9.13 { 5793b167c75Sdrh catchsql { 5803b167c75Sdrh BEGIN; 5813b167c75Sdrh UPDATE t3 SET x=x+1; 5823b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,1,3); 5833b167c75Sdrh SELECT * FROM t2; 5843b167c75Sdrh } 5853b167c75Sdrh} {1 {constraint failed}} 5863b167c75Sdrhdo_test conflict-9.14 { 5873b167c75Sdrh execsql {COMMIT} 5883b167c75Sdrh execsql {SELECT * FROM t3} 5893b167c75Sdrh} {4} 5903b167c75Sdrhdo_test conflict-9.15 { 5913b167c75Sdrh catchsql { 5923b167c75Sdrh BEGIN; 5933b167c75Sdrh UPDATE t3 SET x=x+1; 5943b167c75Sdrh UPDATE t2 SET d=d+1 WHERE d=1; 5953b167c75Sdrh SELECT * FROM t2; 5963b167c75Sdrh } 5973b167c75Sdrh} {1 {constraint failed}} 5983b167c75Sdrhdo_test conflict-9.16 { 5993b167c75Sdrh execsql {COMMIT} 6003b167c75Sdrh execsql {SELECT * FROM t3} 6013b167c75Sdrh} {5} 6023b167c75Sdrhdo_test conflict-9.17 { 6033b167c75Sdrh catchsql { 6043b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,3,1); 6053b167c75Sdrh SELECT * FROM t2; 6063b167c75Sdrh } 6073b167c75Sdrh} {1 {constraint failed}} 6083b167c75Sdrhdo_test conflict-9.18 { 6093b167c75Sdrh catchsql { 6103b167c75Sdrh UPDATE t2 SET e=e+1 WHERE e=1; 6113b167c75Sdrh SELECT * FROM t2; 6123b167c75Sdrh } 6133b167c75Sdrh} {1 {constraint failed}} 6143b167c75Sdrhdo_test conflict-9.19 { 6153b167c75Sdrh catchsql { 6163b167c75Sdrh BEGIN; 6173b167c75Sdrh UPDATE t3 SET x=x+1; 6183b167c75Sdrh INSERT INTO t2 VALUES(3,3,3,3,1); 6193b167c75Sdrh SELECT * FROM t2; 6203b167c75Sdrh } 6213b167c75Sdrh} {1 {constraint failed}} 6223b167c75Sdrhdo_test conflict-9.20 { 623*6b8b8749Sdrh catch {execsql {COMMIT}} 6243b167c75Sdrh execsql {SELECT * FROM t3} 6253b167c75Sdrh} {5} 6263b167c75Sdrhdo_test conflict-9.21 { 6273b167c75Sdrh catchsql { 6283b167c75Sdrh BEGIN; 6293b167c75Sdrh UPDATE t3 SET x=x+1; 6303b167c75Sdrh UPDATE t2 SET e=e+1 WHERE e=1; 6313b167c75Sdrh SELECT * FROM t2; 6323b167c75Sdrh } 6333b167c75Sdrh} {1 {constraint failed}} 6343b167c75Sdrhdo_test conflict-9.22 { 635*6b8b8749Sdrh catch {execsql {COMMIT}} 6363b167c75Sdrh execsql {SELECT * FROM t3} 6373b167c75Sdrh} {5} 6383b167c75Sdrhdo_test conflict-9.23 { 6393b167c75Sdrh catchsql { 6403b167c75Sdrh INSERT INTO t2 VALUES(3,3,1,3,3); 6413b167c75Sdrh SELECT * FROM t2; 6423b167c75Sdrh } 6433b167c75Sdrh} {0 {2 2 2 2 2 3 3 1 3 3}} 6443b167c75Sdrhdo_test conflict-9.24 { 6453b167c75Sdrh catchsql { 6463b167c75Sdrh UPDATE t2 SET c=c-1 WHERE c=2; 6473b167c75Sdrh SELECT * FROM t2; 6483b167c75Sdrh } 6493b167c75Sdrh} {0 {2 2 1 2 2}} 6503b167c75Sdrhdo_test conflict-9.25 { 6513b167c75Sdrh catchsql { 6523b167c75Sdrh BEGIN; 6533b167c75Sdrh UPDATE t3 SET x=x+1; 6543b167c75Sdrh INSERT INTO t2 VALUES(3,3,1,3,3); 6553b167c75Sdrh SELECT * FROM t2; 6563b167c75Sdrh } 6573b167c75Sdrh} {0 {3 3 1 3 3}} 6583b167c75Sdrhdo_test conflict-9.26 { 659*6b8b8749Sdrh catch {execsql {COMMIT}} 6603b167c75Sdrh execsql {SELECT * FROM t3} 6613b167c75Sdrh} {6} 662bb37fdc7Sdrh 663bb37fdc7Sdrhfinish_test 664