3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix upfrom3
19 # 1.*: Test UPDATE ... FROM statements that modify IPK fields. And that
20 # modify "INTEGER PRIMARY KEY" fields on WITHOUT ROWID tables.
22 # 2.*: Test UPDATE ... FROM statements that modify PK fields of WITHOUT
25 # 3.*: Test that UPDATE ... FROM statements are not confused if there
26 # are multiple tables of the same name in attached databases.
28 # 4.*: Tests for UPDATE ... FROM statements and foreign keys.
36 eval [string map [list %WO% $wo %TN% $tn] {
38 do_execsql_test 1.%TN%.0 {
39 CREATE TABLE log(t TEXT);
40 CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z UNIQUE) %WO%;
41 CREATE INDEX t1y ON t1(y);
43 INSERT INTO t1 VALUES(1, 'i', 'one');
44 INSERT INTO t1 VALUES(2, 'ii', 'two');
45 INSERT INTO t1 VALUES(3, 'iii', 'three');
46 INSERT INTO t1 VALUES(4, 'iv', 'four');
49 do_execsql_test 1.%TN%.1 {
50 CREATE TABLE x1(o, n);
51 INSERT INTO x1 VALUES(1, 11);
52 INSERT INTO x1 VALUES(2, 12);
53 INSERT INTO x1 VALUES(3, 13);
54 INSERT INTO x1 VALUES(4, 14);
55 UPDATE t1 SET x=n FROM x1 WHERE x=o;
56 SELECT x, y, z FROM t1 ORDER BY 1;
64 do_test 1.%TN%.2 { db changes } 4
66 do_execsql_test 1.%TN%.3 {
67 INSERT INTO x1 VALUES(11, 21);
68 INSERT INTO x1 VALUES(12, 22);
69 INSERT INTO x1 VALUES(13, 23);
70 INSERT INTO x1 VALUES(14, 24);
72 INSERT INTO x1 VALUES(21, 31);
73 INSERT INTO x1 VALUES(22, 32);
74 INSERT INTO x1 VALUES(23, 33);
75 INSERT INTO x1 VALUES(24, 34);
76 UPDATE t1 SET x=n FROM x1 WHERE x=o;
77 SELECT x, y, z FROM t1 ORDER BY 1;
85 do_execsql_test 1.%TN%.4 {
86 UPDATE t1 SET x=n FROM x1 WHERE x=o;
87 SELECT x, y, z FROM t1 ORDER BY 1;
95 do_execsql_test 1.%TN%.5 {
96 INSERT INTO x1 VALUES(31, 32);
97 INSERT INTO x1 VALUES(33, 34);
98 UPDATE OR REPLACE t1 SET x=n FROM x1 WHERE x=o;
99 SELECT x, y, z FROM t1 ORDER BY 1;
105 do_execsql_test 1.%TN%.6 {
106 INSERT INTO t1 VALUES(33, 'ii', 'two');
107 INSERT INTO t1 VALUES(35, 'iv', 'four');
110 do_execsql_test 1.%TN%.7 {
111 CREATE TABLE x2(o, n, zz);
112 INSERT INTO x2 VALUES(32, 41, 'four');
113 INSERT INTO x2 VALUES(33, 42, 'three');
114 UPDATE OR IGNORE t1 SET x=n, z=zz FROM x2 WHERE x=o;
115 SELECT x, y, z FROM t1 ORDER BY 1;
123 do_execsql_test 1.%TN%.8 {
124 UPDATE OR REPLACE t1 SET x=n, z=zz FROM x2 WHERE x=o;
125 SELECT x, y, z FROM t1 ORDER BY 1;
134 do_execsql_test 2.1.1 {
135 CREATE TABLE u1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID;
136 INSERT INTO u1 VALUES(0, 0, 0);
137 INSERT INTO u1 VALUES(1, 0, 1);
138 INSERT INTO u1 VALUES(2, 1, 0);
139 INSERT INTO u1 VALUES(3, 1, 1);
142 do_execsql_test 2.1.2 {
143 CREATE TABLE map(f, t);
144 INSERT INTO map VALUES(0, 10);
145 INSERT INTO map VALUES(1, 11);
146 UPDATE u1 SET c=t FROM map WHERE c=f;
147 SELECT * FROM u1 ORDER BY a;
155 do_execsql_test 2.1.3 {
156 UPDATE u1 SET b=t FROM map WHERE b=f;
157 SELECT * FROM u1 ORDER BY a;
165 do_execsql_test 2.1.4 {
166 CREATE TABLE map2(o1, o2, n1, n2);
167 INSERT INTO map2 VALUES
168 (10, 10, 50, 50), (10, 11, 50, 60),
169 (11, 10, 60, 50), (11, 11, 60, 60);
170 UPDATE u1 SET b=n1, c=n2 FROM map2 WHERE b=o1 AND c=o2;
171 SELECT * FROM u1 ORDER BY a;
179 #-------------------------------------------------------------------------
186 eval [string map [list %WO% $wo %TN% $tn] {
187 do_execsql_test 3.$tn.1 {
188 CREATE TABLE g1(a, b, c, PRIMARY KEY(a, b)) %WO%;
189 INSERT INTO g1 VALUES(1, 1, 1);
191 ATTACH 'test.db2' AS aux;
192 CREATE TABLE aux.g1(a, b, c, PRIMARY KEY(a, b)) %WO%;
193 INSERT INTO aux.g1 VALUES(10, 1, 10);
194 INSERT INTO aux.g1 VALUES(20, 2, 20);
195 INSERT INTO aux.g1 VALUES(30, 3, 30);
198 do_execsql_test 3.$tn.2 {
199 UPDATE aux.g1 SET c=101 FROM main.g1;
201 do_execsql_test 3.$tn.3 {
202 SELECT * FROM aux.g1;
203 } {10 1 101 20 2 101 30 3 101}
205 do_execsql_test 3.$tn.4 {
206 UPDATE g1 SET c=101 FROM g1 AS g2;
208 do_execsql_test 3.$tn.5 {
214 #-------------------------------------------------------------------------
222 eval [string map [list %WO% $wo %TN% $tn] {
224 do_execsql_test 4.$tn.1 {
225 CREATE TABLE p1(a INTEGER PRIMARY KEY, b) %WO%;
226 CREATE TABLE c1(x PRIMARY KEY, y REFERENCES p1 ON UPDATE CASCADE) %WO%;
227 PRAGMA foreign_keys = 1;
229 INSERT INTO p1 VALUES(1, 'one');
230 INSERT INTO p1 VALUES(11, 'eleven');
231 INSERT INTO p1 VALUES(111, 'eleventyone');
233 INSERT INTO c1 VALUES('a', 1);
234 INSERT INTO c1 VALUES('b', 11);
235 INSERT INTO c1 VALUES('c', 111);
238 do_execsql_test 4.$tn.2 {
239 CREATE TABLE map(f, t);
240 INSERT INTO map VALUES('a', 111);
241 INSERT INTO map VALUES('c', 112);
244 do_catchsql_test 4.$tn.3 {
245 UPDATE c1 SET y=t FROM map WHERE x=f;
246 } {1 {FOREIGN KEY constraint failed}}
248 do_execsql_test 4.$tn.4 {
249 INSERT INTO map VALUES('eleven', 12);
250 INSERT INTO map VALUES('eleventyone', 112);
251 UPDATE p1 SET a=t FROM map WHERE b=f;
254 do_execsql_test 4.$tn.5 {