sqlite/test/upfrom3.test
dan be952c11dc Add test cases and fixes for UPDATE...FROM statements that modify primary key columns.
FossilOrigin-Name: 47c87af3e52bce10fbcc2cbe832d659b0c204bfb3368d9314fa1b01120129254
2020-07-13 20:10:29 +00:00

167 lines
3.6 KiB
Plaintext

# 2020 July 14
#
# 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 upfrom3
foreach {tn wo} {
1 ""
2 "WITHOUT ROWID"
} {
reset_db
eval [string map [list %WO% $wo %TN% $tn] {
do_execsql_test 1.%TN%.0 {
CREATE TABLE log(t TEXT);
CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z UNIQUE) %WO%;
CREATE INDEX t1y ON t1(y);
INSERT INTO t1 VALUES(1, 'i', 'one');
INSERT INTO t1 VALUES(2, 'ii', 'two');
INSERT INTO t1 VALUES(3, 'iii', 'three');
INSERT INTO t1 VALUES(4, 'iv', 'four');
}
do_execsql_test 1.%TN%.1 {
CREATE TABLE x1(o, n);
INSERT INTO x1 VALUES(1, 11);
INSERT INTO x1 VALUES(2, 12);
INSERT INTO x1 VALUES(3, 13);
INSERT INTO x1 VALUES(4, 14);
UPDATE t1 SET x=n FROM x1 WHERE x=o;
SELECT x, y, z FROM t1 ORDER BY 1;
} {
11 i one
12 ii two
13 iii three
14 iv four
}
do_test 1.%TN%.2 { db changes } 4
do_execsql_test 1.%TN%.3 {
INSERT INTO x1 VALUES(11, 21);
INSERT INTO x1 VALUES(12, 22);
INSERT INTO x1 VALUES(13, 23);
INSERT INTO x1 VALUES(14, 24);
INSERT INTO x1 VALUES(21, 31);
INSERT INTO x1 VALUES(22, 32);
INSERT INTO x1 VALUES(23, 33);
INSERT INTO x1 VALUES(24, 34);
UPDATE t1 SET x=n FROM x1 WHERE x=o;
SELECT x, y, z FROM t1 ORDER BY 1;
} {
21 i one
22 ii two
23 iii three
24 iv four
}
do_execsql_test 1.%TN%.4 {
UPDATE t1 SET x=n FROM x1 WHERE x=o;
SELECT x, y, z FROM t1 ORDER BY 1;
} {
31 i one
32 ii two
33 iii three
34 iv four
}
do_execsql_test 1.%TN%.5 {
INSERT INTO x1 VALUES(31, 32);
INSERT INTO x1 VALUES(33, 34);
UPDATE OR REPLACE t1 SET x=n FROM x1 WHERE x=o;
SELECT x, y, z FROM t1 ORDER BY 1;
} {
32 i one
34 iii three
}
do_execsql_test 1.%TN%.6 {
INSERT INTO t1 VALUES(33, 'ii', 'two');
INSERT INTO t1 VALUES(35, 'iv', 'four');
}
do_execsql_test 1.%TN%.7 {
CREATE TABLE x2(o, n, zz);
INSERT INTO x2 VALUES(32, 41, 'four');
INSERT INTO x2 VALUES(33, 42, 'three');
UPDATE OR IGNORE t1 SET x=n, z=zz FROM x2 WHERE x=o;
SELECT x, y, z FROM t1 ORDER BY 1;
} {
32 i one
33 ii two
34 iii three
35 iv four
}
do_execsql_test 1.%TN%.8 {
UPDATE OR REPLACE t1 SET x=n, z=zz FROM x2 WHERE x=o;
SELECT x, y, z FROM t1 ORDER BY 1;
} {
41 i four
42 ii three
}
}]
}
do_execsql_test 2.1.1 {
CREATE TABLE u1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID;
INSERT INTO u1 VALUES(0, 0, 0);
INSERT INTO u1 VALUES(1, 0, 1);
INSERT INTO u1 VALUES(2, 1, 0);
INSERT INTO u1 VALUES(3, 1, 1);
}
do_execsql_test 2.1.2 {
CREATE TABLE map(f, t);
INSERT INTO map VALUES(0, 10);
INSERT INTO map VALUES(1, 11);
UPDATE u1 SET c=t FROM map WHERE c=f;
SELECT * FROM u1 ORDER BY a;
} {
0 0 10
1 0 11
2 1 10
3 1 11
}
do_execsql_test 2.1.3 {
UPDATE u1 SET b=t FROM map WHERE b=f;
SELECT * FROM u1 ORDER BY a;
} {
0 10 10
1 10 11
2 11 10
3 11 11
}
do_execsql_test 2.1.4 {
CREATE TABLE map2(o1, o2, n1, n2);
INSERT INTO map2 VALUES
(10, 10, 50, 50), (10, 11, 50, 60),
(11, 10, 60, 50), (11, 11, 60, 60);
UPDATE u1 SET b=n1, c=n2 FROM map2 WHERE b=o1 AND c=o2;
SELECT * FROM u1 ORDER BY a;
} {
0 50 50
1 50 60
2 60 50
3 60 60
}
finish_test