sl@0: # 2002 January 29 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # This file implements tests for the conflict resolution extension sl@0: # to SQLite. sl@0: # sl@0: # $Id: conflict.test,v 1.31 2008/01/21 16:22:46 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !conflict { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Create tables for the first group of tests. sl@0: # sl@0: do_test conflict-1.0 { sl@0: execsql { sl@0: CREATE TABLE t1(a, b, c, UNIQUE(a,b)); sl@0: CREATE TABLE t2(x); sl@0: SELECT c FROM t1 ORDER BY c; sl@0: } sl@0: } {} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # cmd An INSERT or REPLACE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "c" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t2 sl@0: # t3 Number of temporary files created by this test sl@0: # sl@0: foreach {i cmd t0 t1 t2 t3} { sl@0: 1 INSERT 1 {} 1 0 sl@0: 2 {INSERT OR IGNORE} 0 3 1 0 sl@0: 3 {INSERT OR REPLACE} 0 4 1 0 sl@0: 4 REPLACE 0 4 1 0 sl@0: 5 {INSERT OR FAIL} 1 {} 1 0 sl@0: 6 {INSERT OR ABORT} 1 {} 1 0 sl@0: 7 {INSERT OR ROLLBACK} 1 {} {} 0 sl@0: } { sl@0: do_test conflict-1.$i { sl@0: set ::sqlite_opentemp_count 0 sl@0: set r0 [catch {execsql [subst { sl@0: DELETE FROM t1; sl@0: DELETE FROM t2; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: BEGIN; sl@0: INSERT INTO t2 VALUES(1); sl@0: $cmd INTO t1 VALUES(1,2,4); sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} sl@0: set r2 [execsql {SELECT x FROM t2}] sl@0: set r3 $::sqlite_opentemp_count sl@0: list $r0 $r1 $r2 $r3 sl@0: } [list $t0 $t1 $t2 $t3] sl@0: } sl@0: sl@0: # Create tables for the first group of tests. sl@0: # sl@0: do_test conflict-2.0 { sl@0: execsql { sl@0: DROP TABLE t1; sl@0: DROP TABLE t2; sl@0: CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, UNIQUE(a,b)); sl@0: CREATE TABLE t2(x); sl@0: SELECT c FROM t1 ORDER BY c; sl@0: } sl@0: } {} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # cmd An INSERT or REPLACE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "c" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t2 sl@0: # sl@0: foreach {i cmd t0 t1 t2} { sl@0: 1 INSERT 1 {} 1 sl@0: 2 {INSERT OR IGNORE} 0 3 1 sl@0: 3 {INSERT OR REPLACE} 0 4 1 sl@0: 4 REPLACE 0 4 1 sl@0: 5 {INSERT OR FAIL} 1 {} 1 sl@0: 6 {INSERT OR ABORT} 1 {} 1 sl@0: 7 {INSERT OR ROLLBACK} 1 {} {} sl@0: } { sl@0: do_test conflict-2.$i { sl@0: set r0 [catch {execsql [subst { sl@0: DELETE FROM t1; sl@0: DELETE FROM t2; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: BEGIN; sl@0: INSERT INTO t2 VALUES(1); sl@0: $cmd INTO t1 VALUES(1,2,4); sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} sl@0: set r2 [execsql {SELECT x FROM t2}] sl@0: list $r0 $r1 $r2 sl@0: } [list $t0 $t1 $t2] sl@0: } sl@0: sl@0: # Create tables for the first group of tests. sl@0: # sl@0: do_test conflict-3.0 { sl@0: execsql { sl@0: DROP TABLE t1; sl@0: DROP TABLE t2; sl@0: CREATE TABLE t1(a, b, c INTEGER, PRIMARY KEY(c), UNIQUE(a,b)); sl@0: CREATE TABLE t2(x); sl@0: SELECT c FROM t1 ORDER BY c; sl@0: } sl@0: } {} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # cmd An INSERT or REPLACE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "c" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t2 sl@0: # sl@0: foreach {i cmd t0 t1 t2} { sl@0: 1 INSERT 1 {} 1 sl@0: 2 {INSERT OR IGNORE} 0 3 1 sl@0: 3 {INSERT OR REPLACE} 0 4 1 sl@0: 4 REPLACE 0 4 1 sl@0: 5 {INSERT OR FAIL} 1 {} 1 sl@0: 6 {INSERT OR ABORT} 1 {} 1 sl@0: 7 {INSERT OR ROLLBACK} 1 {} {} sl@0: } { sl@0: do_test conflict-3.$i { sl@0: set r0 [catch {execsql [subst { sl@0: DELETE FROM t1; sl@0: DELETE FROM t2; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: BEGIN; sl@0: INSERT INTO t2 VALUES(1); sl@0: $cmd INTO t1 VALUES(1,2,4); sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} sl@0: set r2 [execsql {SELECT x FROM t2}] sl@0: list $r0 $r1 $r2 sl@0: } [list $t0 $t1 $t2] sl@0: } sl@0: sl@0: do_test conflict-4.0 { sl@0: execsql { sl@0: DROP TABLE t2; sl@0: CREATE TABLE t2(x); sl@0: SELECT x FROM t2; sl@0: } sl@0: } {} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # conf1 The conflict resolution algorithm on the UNIQUE constraint sl@0: # cmd An INSERT or REPLACE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "c" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t2 sl@0: # sl@0: foreach {i conf1 cmd t0 t1 t2} { sl@0: 1 {} INSERT 1 {} 1 sl@0: 2 REPLACE INSERT 0 4 1 sl@0: 3 IGNORE INSERT 0 3 1 sl@0: 4 FAIL INSERT 1 {} 1 sl@0: 5 ABORT INSERT 1 {} 1 sl@0: 6 ROLLBACK INSERT 1 {} {} sl@0: 7 REPLACE {INSERT OR IGNORE} 0 3 1 sl@0: 8 IGNORE {INSERT OR REPLACE} 0 4 1 sl@0: 9 FAIL {INSERT OR IGNORE} 0 3 1 sl@0: 10 ABORT {INSERT OR REPLACE} 0 4 1 sl@0: 11 ROLLBACK {INSERT OR IGNORE } 0 3 1 sl@0: } { sl@0: do_test conflict-4.$i { sl@0: if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} sl@0: set r0 [catch {execsql [subst { sl@0: DROP TABLE t1; sl@0: CREATE TABLE t1(a,b,c,UNIQUE(a,b) $conf1); sl@0: DELETE FROM t2; sl@0: INSERT INTO t1 VALUES(1,2,3); sl@0: BEGIN; sl@0: INSERT INTO t2 VALUES(1); sl@0: $cmd INTO t1 VALUES(1,2,4); sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]} sl@0: set r2 [execsql {SELECT x FROM t2}] sl@0: list $r0 $r1 $r2 sl@0: } [list $t0 $t1 $t2] sl@0: } sl@0: sl@0: do_test conflict-5.0 { sl@0: execsql { sl@0: DROP TABLE t2; sl@0: CREATE TABLE t2(x); sl@0: SELECT x FROM t2; sl@0: } sl@0: } {} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # conf1 The conflict resolution algorithm on the NOT NULL constraint sl@0: # cmd An INSERT or REPLACE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "c" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t2 sl@0: # sl@0: foreach {i conf1 cmd t0 t1 t2} { sl@0: 1 {} INSERT 1 {} 1 sl@0: 2 REPLACE INSERT 0 5 1 sl@0: 3 IGNORE INSERT 0 {} 1 sl@0: 4 FAIL INSERT 1 {} 1 sl@0: 5 ABORT INSERT 1 {} 1 sl@0: 6 ROLLBACK INSERT 1 {} {} sl@0: 7 REPLACE {INSERT OR IGNORE} 0 {} 1 sl@0: 8 IGNORE {INSERT OR REPLACE} 0 5 1 sl@0: 9 FAIL {INSERT OR IGNORE} 0 {} 1 sl@0: 10 ABORT {INSERT OR REPLACE} 0 5 1 sl@0: 11 ROLLBACK {INSERT OR IGNORE} 0 {} 1 sl@0: 12 {} {INSERT OR IGNORE} 0 {} 1 sl@0: 13 {} {INSERT OR REPLACE} 0 5 1 sl@0: 14 {} {INSERT OR FAIL} 1 {} 1 sl@0: 15 {} {INSERT OR ABORT} 1 {} 1 sl@0: 16 {} {INSERT OR ROLLBACK} 1 {} {} sl@0: } { sl@0: if {$t0} {set t1 {t1.c may not be NULL}} sl@0: do_test conflict-5.$i { sl@0: if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} sl@0: set r0 [catch {execsql [subst { sl@0: DROP TABLE t1; sl@0: CREATE TABLE t1(a,b,c NOT NULL $conf1 DEFAULT 5); sl@0: DELETE FROM t2; sl@0: BEGIN; sl@0: INSERT INTO t2 VALUES(1); sl@0: $cmd INTO t1 VALUES(1,2,NULL); sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {!$r0} {set r1 [execsql {SELECT c FROM t1}]} sl@0: set r2 [execsql {SELECT x FROM t2}] sl@0: list $r0 $r1 $r2 sl@0: } [list $t0 $t1 $t2] sl@0: } sl@0: sl@0: do_test conflict-6.0 { sl@0: execsql { sl@0: DROP TABLE t2; sl@0: CREATE TABLE t2(a,b,c); sl@0: INSERT INTO t2 VALUES(1,2,1); sl@0: INSERT INTO t2 VALUES(2,3,2); sl@0: INSERT INTO t2 VALUES(3,4,1); sl@0: INSERT INTO t2 VALUES(4,5,4); sl@0: SELECT c FROM t2 ORDER BY b; sl@0: CREATE TABLE t3(x); sl@0: INSERT INTO t3 VALUES(1); sl@0: } sl@0: } {1 2 1 4} sl@0: sl@0: # Six columns of configuration data as follows: sl@0: # sl@0: # i The reference number of the test sl@0: # conf1 The conflict resolution algorithm on the UNIQUE constraint sl@0: # cmd An UPDATE command to execute against table t1 sl@0: # t0 True if there is an error from $cmd sl@0: # t1 Content of "b" column of t1 assuming no error in $cmd sl@0: # t2 Content of "x" column of t3 sl@0: # t3 Number of temporary files for tables sl@0: # t4 Number of temporary files for statement journals sl@0: # sl@0: # Update: Since temporary table files are now opened lazily, and none sl@0: # of the following tests use large quantities of data, t3 is always 0. sl@0: # sl@0: foreach {i conf1 cmd t0 t1 t2 t3 t4} { sl@0: 1 {} UPDATE 1 {6 7 8 9} 1 0 1 sl@0: 2 REPLACE UPDATE 0 {7 6 9} 1 0 0 sl@0: 3 IGNORE UPDATE 0 {6 7 3 9} 1 0 0 sl@0: 4 FAIL UPDATE 1 {6 7 3 4} 1 0 0 sl@0: 5 ABORT UPDATE 1 {1 2 3 4} 1 0 1 sl@0: 6 ROLLBACK UPDATE 1 {1 2 3 4} 0 0 0 sl@0: 7 REPLACE {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 sl@0: 8 IGNORE {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 sl@0: 9 FAIL {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 sl@0: 10 ABORT {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 sl@0: 11 ROLLBACK {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 sl@0: 12 {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1 0 0 sl@0: 13 {} {UPDATE OR REPLACE} 0 {7 6 9} 1 0 0 sl@0: 14 {} {UPDATE OR FAIL} 1 {6 7 3 4} 1 0 0 sl@0: 15 {} {UPDATE OR ABORT} 1 {1 2 3 4} 1 0 1 sl@0: 16 {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0 0 0 sl@0: } { sl@0: if {$t0} {set t1 {column a is not unique}} sl@0: if {[info exists TEMP_STORE] && $TEMP_STORE>=2} { sl@0: set t3 $t4 sl@0: } else { sl@0: set t3 [expr {$t3+$t4}] sl@0: } sl@0: do_test conflict-6.$i { sl@0: db close sl@0: sqlite3 db test.db sl@0: if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"} sl@0: execsql {pragma temp_store=file} sl@0: set ::sqlite_opentemp_count 0 sl@0: set r0 [catch {execsql [subst { sl@0: DROP TABLE t1; sl@0: CREATE TABLE t1(a,b,c, UNIQUE(a) $conf1); sl@0: INSERT INTO t1 SELECT * FROM t2; sl@0: UPDATE t3 SET x=0; sl@0: BEGIN; sl@0: $cmd t3 SET x=1; sl@0: $cmd t1 SET b=b*2; sl@0: $cmd t1 SET a=c+5; sl@0: }]} r1] sl@0: catch {execsql {COMMIT}} sl@0: if {!$r0} {set r1 [execsql {SELECT a FROM t1 ORDER BY b}]} sl@0: set r2 [execsql {SELECT x FROM t3}] sl@0: list $r0 $r1 $r2 $::sqlite_opentemp_count sl@0: } [list $t0 $t1 $t2 $t3] sl@0: } sl@0: sl@0: # Test to make sure a lot of IGNOREs don't cause a stack overflow sl@0: # sl@0: do_test conflict-7.1 { sl@0: execsql { sl@0: DROP TABLE t1; sl@0: DROP TABLE t2; sl@0: DROP TABLE t3; sl@0: CREATE TABLE t1(a unique, b); sl@0: } sl@0: for {set i 1} {$i<=50} {incr i} { sl@0: execsql "INSERT into t1 values($i,[expr {$i+1}]);" sl@0: } sl@0: execsql { sl@0: SELECT count(*), min(a), max(b) FROM t1; sl@0: } sl@0: } {50 1 51} sl@0: do_test conflict-7.2 { sl@0: execsql { sl@0: PRAGMA count_changes=on; sl@0: UPDATE OR IGNORE t1 SET a=1000; sl@0: } sl@0: } {1} sl@0: do_test conflict-7.2.1 { sl@0: db changes sl@0: } {1} sl@0: do_test conflict-7.3 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a=1000; sl@0: } sl@0: } {2} sl@0: do_test conflict-7.4 { sl@0: execsql { sl@0: SELECT count(*) FROM t1; sl@0: } sl@0: } {50} sl@0: do_test conflict-7.5 { sl@0: execsql { sl@0: PRAGMA count_changes=on; sl@0: UPDATE OR REPLACE t1 SET a=1001; sl@0: } sl@0: } {50} sl@0: do_test conflict-7.5.1 { sl@0: db changes sl@0: } {50} sl@0: do_test conflict-7.6 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a=1001; sl@0: } sl@0: } {51} sl@0: do_test conflict-7.7 { sl@0: execsql { sl@0: SELECT count(*) FROM t1; sl@0: } sl@0: } {1} sl@0: sl@0: # Update for version 3: A SELECT statement no longer resets the change sl@0: # counter (Test result changes from 0 to 50). sl@0: do_test conflict-7.7.1 { sl@0: db changes sl@0: } {50} sl@0: sl@0: # Make sure the row count is right for rows that are ignored on sl@0: # an insert. sl@0: # sl@0: do_test conflict-8.1 { sl@0: execsql { sl@0: DELETE FROM t1; sl@0: INSERT INTO t1 VALUES(1,2); sl@0: } sl@0: execsql { sl@0: INSERT OR IGNORE INTO t1 VALUES(2,3); sl@0: } sl@0: } {1} sl@0: do_test conflict-8.1.1 { sl@0: db changes sl@0: } {1} sl@0: do_test conflict-8.2 { sl@0: execsql { sl@0: INSERT OR IGNORE INTO t1 VALUES(2,4); sl@0: } sl@0: } {0} sl@0: do_test conflict-8.2.1 { sl@0: db changes sl@0: } {0} sl@0: do_test conflict-8.3 { sl@0: execsql { sl@0: INSERT OR REPLACE INTO t1 VALUES(2,4); sl@0: } sl@0: } {1} sl@0: do_test conflict-8.3.1 { sl@0: db changes sl@0: } {1} sl@0: do_test conflict-8.4 { sl@0: execsql { sl@0: INSERT OR IGNORE INTO t1 SELECT * FROM t1; sl@0: } sl@0: } {0} sl@0: do_test conflict-8.4.1 { sl@0: db changes sl@0: } {0} sl@0: do_test conflict-8.5 { sl@0: execsql { sl@0: INSERT OR IGNORE INTO t1 SELECT a+2,b+2 FROM t1; sl@0: } sl@0: } {2} sl@0: do_test conflict-8.5.1 { sl@0: db changes sl@0: } {2} sl@0: do_test conflict-8.6 { sl@0: execsql { sl@0: INSERT OR IGNORE INTO t1 SELECT a+3,b+3 FROM t1; sl@0: } sl@0: } {3} sl@0: do_test conflict-8.6.1 { sl@0: db changes sl@0: } {3} sl@0: sl@0: integrity_check conflict-8.99 sl@0: sl@0: do_test conflict-9.1 { sl@0: execsql { sl@0: PRAGMA count_changes=0; sl@0: CREATE TABLE t2( sl@0: a INTEGER UNIQUE ON CONFLICT IGNORE, sl@0: b INTEGER UNIQUE ON CONFLICT FAIL, sl@0: c INTEGER UNIQUE ON CONFLICT REPLACE, sl@0: d INTEGER UNIQUE ON CONFLICT ABORT, sl@0: e INTEGER UNIQUE ON CONFLICT ROLLBACK sl@0: ); sl@0: CREATE TABLE t3(x); sl@0: INSERT INTO t3 VALUES(1); sl@0: SELECT * FROM t3; sl@0: } sl@0: } {1} sl@0: do_test conflict-9.2 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(1,1,1,1,1); sl@0: INSERT INTO t2 VALUES(2,2,2,2,2); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {0 {1 1 1 1 1 2 2 2 2 2}} sl@0: do_test conflict-9.3 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(1,3,3,3,3); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {0 {1 1 1 1 1 2 2 2 2 2}} sl@0: do_test conflict-9.4 { sl@0: catchsql { sl@0: UPDATE t2 SET a=a+1 WHERE a=1; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {0 {1 1 1 1 1 2 2 2 2 2}} sl@0: do_test conflict-9.5 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(3,1,3,3,3); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column b is not unique}} sl@0: do_test conflict-9.6 { sl@0: catchsql { sl@0: UPDATE t2 SET b=b+1 WHERE b=1; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column b is not unique}} sl@0: do_test conflict-9.7 { sl@0: catchsql { sl@0: BEGIN; sl@0: UPDATE t3 SET x=x+1; sl@0: INSERT INTO t2 VALUES(3,1,3,3,3); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column b is not unique}} sl@0: do_test conflict-9.8 { sl@0: execsql {COMMIT} sl@0: execsql {SELECT * FROM t3} sl@0: } {2} sl@0: do_test conflict-9.9 { sl@0: catchsql { sl@0: BEGIN; sl@0: UPDATE t3 SET x=x+1; sl@0: UPDATE t2 SET b=b+1 WHERE b=1; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column b is not unique}} sl@0: do_test conflict-9.10 { sl@0: execsql {COMMIT} sl@0: execsql {SELECT * FROM t3} sl@0: } {3} sl@0: do_test conflict-9.11 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(3,3,3,1,3); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column d is not unique}} sl@0: do_test conflict-9.12 { sl@0: catchsql { sl@0: UPDATE t2 SET d=d+1 WHERE d=1; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column d is not unique}} sl@0: do_test conflict-9.13 { sl@0: catchsql { sl@0: BEGIN; sl@0: UPDATE t3 SET x=x+1; sl@0: INSERT INTO t2 VALUES(3,3,3,1,3); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column d is not unique}} sl@0: do_test conflict-9.14 { sl@0: execsql {COMMIT} sl@0: execsql {SELECT * FROM t3} sl@0: } {4} sl@0: do_test conflict-9.15 { sl@0: catchsql { sl@0: BEGIN; sl@0: UPDATE t3 SET x=x+1; sl@0: UPDATE t2 SET d=d+1 WHERE d=1; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column d is not unique}} sl@0: do_test conflict-9.16 { sl@0: execsql {COMMIT} sl@0: execsql {SELECT * FROM t3} sl@0: } {5} sl@0: do_test conflict-9.17 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(3,3,3,3,1); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column e is not unique}} sl@0: do_test conflict-9.18 { sl@0: catchsql { sl@0: UPDATE t2 SET e=e+1 WHERE e=1; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column e is not unique}} sl@0: do_test conflict-9.19 { sl@0: catchsql { sl@0: BEGIN; sl@0: UPDATE t3 SET x=x+1; sl@0: INSERT INTO t2 VALUES(3,3,3,3,1); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column e is not unique}} sl@0: do_test conflict-9.20 { sl@0: catch {execsql {COMMIT}} sl@0: execsql {SELECT * FROM t3} sl@0: } {5} sl@0: do_test conflict-9.21 { sl@0: catchsql { sl@0: BEGIN; sl@0: UPDATE t3 SET x=x+1; sl@0: UPDATE t2 SET e=e+1 WHERE e=1; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {1 {column e is not unique}} sl@0: do_test conflict-9.22 { sl@0: catch {execsql {COMMIT}} sl@0: execsql {SELECT * FROM t3} sl@0: } {5} sl@0: do_test conflict-9.23 { sl@0: catchsql { sl@0: INSERT INTO t2 VALUES(3,3,1,3,3); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {0 {2 2 2 2 2 3 3 1 3 3}} sl@0: do_test conflict-9.24 { sl@0: catchsql { sl@0: UPDATE t2 SET c=c-1 WHERE c=2; sl@0: SELECT * FROM t2; sl@0: } sl@0: } {0 {2 2 1 2 2}} sl@0: do_test conflict-9.25 { sl@0: catchsql { sl@0: BEGIN; sl@0: UPDATE t3 SET x=x+1; sl@0: INSERT INTO t2 VALUES(3,3,1,3,3); sl@0: SELECT * FROM t2; sl@0: } sl@0: } {0 {3 3 1 3 3}} sl@0: do_test conflict-9.26 { sl@0: catch {execsql {COMMIT}} sl@0: execsql {SELECT * FROM t3} sl@0: } {6} sl@0: sl@0: do_test conflict-10.1 { sl@0: catchsql { sl@0: DELETE FROM t1; sl@0: BEGIN; sl@0: INSERT OR ROLLBACK INTO t1 VALUES(1,2); sl@0: INSERT OR ROLLBACK INTO t1 VALUES(1,3); sl@0: COMMIT; sl@0: } sl@0: execsql {SELECT * FROM t1} sl@0: } {} sl@0: do_test conflict-10.2 { sl@0: catchsql { sl@0: CREATE TABLE t4(x); sl@0: CREATE UNIQUE INDEX t4x ON t4(x); sl@0: BEGIN; sl@0: INSERT OR ROLLBACK INTO t4 VALUES(1); sl@0: INSERT OR ROLLBACK INTO t4 VALUES(1); sl@0: COMMIT; sl@0: } sl@0: execsql {SELECT * FROM t4} sl@0: } {} sl@0: sl@0: # Ticket #1171. Make sure statement rollbacks do not sl@0: # damage the database. sl@0: # sl@0: do_test conflict-11.1 { sl@0: execsql { sl@0: -- Create a database object (pages 2, 3 of the file) sl@0: BEGIN; sl@0: CREATE TABLE abc(a UNIQUE, b, c); sl@0: INSERT INTO abc VALUES(1, 2, 3); sl@0: INSERT INTO abc VALUES(4, 5, 6); sl@0: INSERT INTO abc VALUES(7, 8, 9); sl@0: COMMIT; sl@0: } sl@0: sl@0: sl@0: # Set a small cache size so that changes will spill into sl@0: # the database file. sl@0: execsql { sl@0: PRAGMA cache_size = 10; sl@0: } sl@0: sl@0: # Make lots of changes. Because of the small cache, some sl@0: # (most?) of these changes will spill into the disk file. sl@0: # In other words, some of the changes will not be held in sl@0: # cache. sl@0: # sl@0: execsql { sl@0: BEGIN; sl@0: -- Make sure the pager is in EXCLUSIVE state. sl@0: CREATE TABLE def(d, e, f); sl@0: INSERT INTO def VALUES sl@0: ('xxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzz'); sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: DELETE FROM abc WHERE a = 4; sl@0: } sl@0: sl@0: # Execute a statement that does a statement rollback due to sl@0: # a constraint failure. sl@0: # sl@0: catchsql { sl@0: INSERT INTO abc SELECT 10, 20, 30 FROM def; sl@0: } sl@0: sl@0: # Rollback the database. Verify that the state of the ABC table sl@0: # is unchanged from the beginning of the transaction. In other words, sl@0: # make sure the DELETE on table ABC that occurred within the transaction sl@0: # had no effect. sl@0: # sl@0: execsql { sl@0: ROLLBACK; sl@0: SELECT * FROM abc; sl@0: } sl@0: } {1 2 3 4 5 6 7 8 9} sl@0: integrity_check conflict-11.2 sl@0: sl@0: # Repeat test conflict-11.1 but this time commit. sl@0: # sl@0: do_test conflict-11.3 { sl@0: execsql { sl@0: BEGIN; sl@0: -- Make sure the pager is in EXCLUSIVE state. sl@0: UPDATE abc SET a=a+1; sl@0: CREATE TABLE def(d, e, f); sl@0: INSERT INTO def VALUES sl@0: ('xxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzz'); sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: DELETE FROM abc WHERE a = 4; sl@0: } sl@0: catchsql { sl@0: INSERT INTO abc SELECT 10, 20, 30 FROM def; sl@0: } sl@0: execsql { sl@0: ROLLBACK; sl@0: SELECT * FROM abc; sl@0: } sl@0: } {1 2 3 4 5 6 7 8 9} sl@0: # Repeat test conflict-11.1 but this time commit. sl@0: # sl@0: do_test conflict-11.5 { sl@0: execsql { sl@0: BEGIN; sl@0: -- Make sure the pager is in EXCLUSIVE state. sl@0: CREATE TABLE def(d, e, f); sl@0: INSERT INTO def VALUES sl@0: ('xxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzz'); sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: INSERT INTO def SELECT * FROM def; sl@0: DELETE FROM abc WHERE a = 4; sl@0: } sl@0: catchsql { sl@0: INSERT INTO abc SELECT 10, 20, 30 FROM def; sl@0: } sl@0: execsql { sl@0: COMMIT; sl@0: SELECT * FROM abc; sl@0: } sl@0: } {1 2 3 7 8 9} sl@0: integrity_check conflict-11.6 sl@0: sl@0: # Make sure UPDATE OR REPLACE works on tables that have only sl@0: # an INTEGER PRIMARY KEY. sl@0: # sl@0: do_test conflict-12.1 { sl@0: execsql { sl@0: CREATE TABLE t5(a INTEGER PRIMARY KEY, b text); sl@0: INSERT INTO t5 VALUES(1,'one'); sl@0: INSERT INTO t5 VALUES(2,'two'); sl@0: SELECT * FROM t5 sl@0: } sl@0: } {1 one 2 two} sl@0: do_test conflict-12.2 { sl@0: execsql { sl@0: UPDATE OR IGNORE t5 SET a=a+1 WHERE a=1; sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 one 2 two} sl@0: do_test conflict-12.3 { sl@0: catchsql { sl@0: UPDATE t5 SET a=a+1 WHERE a=1; sl@0: } sl@0: } {1 {PRIMARY KEY must be unique}} sl@0: do_test conflict-12.4 { sl@0: execsql { sl@0: UPDATE OR REPLACE t5 SET a=a+1 WHERE a=1; sl@0: SELECT * FROM t5; sl@0: } sl@0: } {2 one} sl@0: sl@0: sl@0: finish_test