xref: /titanic_41/usr/src/lib/libsqlite/test/attach.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2003 April 4
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
15*c5c4113dSnw141292# focus of this script is testing the ATTACH and DETACH commands
16*c5c4113dSnw141292# and related functionality.
17*c5c4113dSnw141292#
18*c5c4113dSnw141292# $Id: attach.test,v 1.13 2004/02/14 01:39:50 drh Exp $
19*c5c4113dSnw141292#
20*c5c4113dSnw141292
21*c5c4113dSnw141292set testdir [file dirname $argv0]
22*c5c4113dSnw141292source $testdir/tester.tcl
23*c5c4113dSnw141292
24*c5c4113dSnw141292for {set i 2} {$i<=15} {incr i} {
25*c5c4113dSnw141292  file delete -force test$i.db
26*c5c4113dSnw141292  file delete -force test$i.db-journal
27*c5c4113dSnw141292}
28*c5c4113dSnw141292
29*c5c4113dSnw141292do_test attach-1.1 {
30*c5c4113dSnw141292  execsql {
31*c5c4113dSnw141292    CREATE TABLE t1(a,b);
32*c5c4113dSnw141292    INSERT INTO t1 VALUES(1,2);
33*c5c4113dSnw141292    INSERT INTO t1 VALUES(3,4);
34*c5c4113dSnw141292    SELECT * FROM t1;
35*c5c4113dSnw141292  }
36*c5c4113dSnw141292} {1 2 3 4}
37*c5c4113dSnw141292do_test attach-1.2 {
38*c5c4113dSnw141292  sqlite db2 test2.db
39*c5c4113dSnw141292  execsql {
40*c5c4113dSnw141292    CREATE TABLE t2(x,y);
41*c5c4113dSnw141292    INSERT INTO t2 VALUES(1,'x');
42*c5c4113dSnw141292    INSERT INTO t2 VALUES(2,'y');
43*c5c4113dSnw141292    SELECT * FROM t2;
44*c5c4113dSnw141292  } db2
45*c5c4113dSnw141292} {1 x 2 y}
46*c5c4113dSnw141292do_test attach-1.3 {
47*c5c4113dSnw141292  execsql {
48*c5c4113dSnw141292    ATTACH DATABASE 'test2.db' AS two;
49*c5c4113dSnw141292    SELECT * FROM two.t2;
50*c5c4113dSnw141292  }
51*c5c4113dSnw141292} {1 x 2 y}
52*c5c4113dSnw141292do_test attach-1.4 {
53*c5c4113dSnw141292  execsql {
54*c5c4113dSnw141292    SELECT * FROM t2;
55*c5c4113dSnw141292  }
56*c5c4113dSnw141292} {1 x 2 y}
57*c5c4113dSnw141292do_test attach-1.5 {
58*c5c4113dSnw141292  execsql {
59*c5c4113dSnw141292    DETACH DATABASE two;
60*c5c4113dSnw141292    SELECT * FROM t1;
61*c5c4113dSnw141292  }
62*c5c4113dSnw141292} {1 2 3 4}
63*c5c4113dSnw141292do_test attach-1.6 {
64*c5c4113dSnw141292  catchsql {
65*c5c4113dSnw141292    SELECT * FROM t2;
66*c5c4113dSnw141292  }
67*c5c4113dSnw141292} {1 {no such table: t2}}
68*c5c4113dSnw141292do_test attach-1.7 {
69*c5c4113dSnw141292  catchsql {
70*c5c4113dSnw141292    SELECT * FROM two.t2;
71*c5c4113dSnw141292  }
72*c5c4113dSnw141292} {1 {no such table: two.t2}}
73*c5c4113dSnw141292do_test attach-1.8 {
74*c5c4113dSnw141292  catchsql {
75*c5c4113dSnw141292    ATTACH DATABASE 'test3.db' AS three;
76*c5c4113dSnw141292  }
77*c5c4113dSnw141292} {1 {cannot attach empty database: three}}
78*c5c4113dSnw141292do_test attach-1.9 {
79*c5c4113dSnw141292  catchsql {
80*c5c4113dSnw141292    SELECT * FROM three.sqlite_master;
81*c5c4113dSnw141292  }
82*c5c4113dSnw141292} {1 {no such table: three.sqlite_master}}
83*c5c4113dSnw141292do_test attach-1.10 {
84*c5c4113dSnw141292  catchsql {
85*c5c4113dSnw141292    DETACH DATABASE three;
86*c5c4113dSnw141292  }
87*c5c4113dSnw141292} {1 {no such database: three}}
88*c5c4113dSnw141292do_test attach-1.11 {
89*c5c4113dSnw141292  execsql {
90*c5c4113dSnw141292    ATTACH 'test.db' AS db2;
91*c5c4113dSnw141292    ATTACH 'test.db' AS db3;
92*c5c4113dSnw141292    ATTACH 'test.db' AS db4;
93*c5c4113dSnw141292    ATTACH 'test.db' AS db5;
94*c5c4113dSnw141292    ATTACH 'test.db' AS db6;
95*c5c4113dSnw141292    ATTACH 'test.db' AS db7;
96*c5c4113dSnw141292    ATTACH 'test.db' AS db8;
97*c5c4113dSnw141292    ATTACH 'test.db' AS db9;
98*c5c4113dSnw141292  }
99*c5c4113dSnw141292} {}
100*c5c4113dSnw141292proc db_list {db} {
101*c5c4113dSnw141292  set list {}
102*c5c4113dSnw141292  foreach {idx name file} [execsql {PRAGMA database_list} $db] {
103*c5c4113dSnw141292    lappend list $idx $name
104*c5c4113dSnw141292  }
105*c5c4113dSnw141292  return $list
106*c5c4113dSnw141292}
107*c5c4113dSnw141292do_test attach-1.11b {
108*c5c4113dSnw141292  db_list db
109*c5c4113dSnw141292} {0 main 1 temp 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9}
110*c5c4113dSnw141292do_test attach-1.12 {
111*c5c4113dSnw141292  catchsql {
112*c5c4113dSnw141292    ATTACH 'test.db' as db2;
113*c5c4113dSnw141292  }
114*c5c4113dSnw141292} {1 {database db2 is already in use}}
115*c5c4113dSnw141292do_test attach-1.13 {
116*c5c4113dSnw141292  catchsql {
117*c5c4113dSnw141292    ATTACH 'test.db' as db5;
118*c5c4113dSnw141292  }
119*c5c4113dSnw141292} {1 {database db5 is already in use}}
120*c5c4113dSnw141292do_test attach-1.14 {
121*c5c4113dSnw141292  catchsql {
122*c5c4113dSnw141292    ATTACH 'test.db' as db9;
123*c5c4113dSnw141292  }
124*c5c4113dSnw141292} {1 {database db9 is already in use}}
125*c5c4113dSnw141292do_test attach-1.15 {
126*c5c4113dSnw141292  catchsql {
127*c5c4113dSnw141292    ATTACH 'test.db' as main;
128*c5c4113dSnw141292  }
129*c5c4113dSnw141292} {1 {database main is already in use}}
130*c5c4113dSnw141292do_test attach-1.16 {
131*c5c4113dSnw141292  catchsql {
132*c5c4113dSnw141292    ATTACH 'test.db' as temp;
133*c5c4113dSnw141292  }
134*c5c4113dSnw141292} {1 {database temp is already in use}}
135*c5c4113dSnw141292do_test attach-1.17 {
136*c5c4113dSnw141292  catchsql {
137*c5c4113dSnw141292    ATTACH 'test.db' as MAIN;
138*c5c4113dSnw141292  }
139*c5c4113dSnw141292} {1 {database MAIN is already in use}}
140*c5c4113dSnw141292do_test attach-1.18 {
141*c5c4113dSnw141292  catchsql {
142*c5c4113dSnw141292    ATTACH 'test.db' as db10;
143*c5c4113dSnw141292    ATTACH 'test.db' as db11;
144*c5c4113dSnw141292  }
145*c5c4113dSnw141292} {0 {}}
146*c5c4113dSnw141292do_test attach-1.19 {
147*c5c4113dSnw141292  catchsql {
148*c5c4113dSnw141292    ATTACH 'test.db' as db12;
149*c5c4113dSnw141292  }
150*c5c4113dSnw141292} {1 {too many attached databases - max 10}}
151*c5c4113dSnw141292do_test attach-1.20.1 {
152*c5c4113dSnw141292  execsql {
153*c5c4113dSnw141292    DETACH db5;
154*c5c4113dSnw141292  }
155*c5c4113dSnw141292  db_list db
156*c5c4113dSnw141292} {0 main 1 temp 2 db2 3 db3 4 db4 5 db11 6 db6 7 db7 8 db8 9 db9 10 db10}
157*c5c4113dSnw141292integrity_check attach-1.20.2
158*c5c4113dSnw141292do_test attach-1.21 {
159*c5c4113dSnw141292  catchsql {
160*c5c4113dSnw141292    ATTACH 'test.db' as db12;
161*c5c4113dSnw141292  }
162*c5c4113dSnw141292} {0 {}}
163*c5c4113dSnw141292do_test attach-1.22 {
164*c5c4113dSnw141292  catchsql {
165*c5c4113dSnw141292    ATTACH 'test.db' as db13;
166*c5c4113dSnw141292  }
167*c5c4113dSnw141292} {1 {too many attached databases - max 10}}
168*c5c4113dSnw141292do_test attach-1.23 {
169*c5c4113dSnw141292  catchsql {
170*c5c4113dSnw141292    DETACH db14;
171*c5c4113dSnw141292  }
172*c5c4113dSnw141292} {1 {no such database: db14}}
173*c5c4113dSnw141292do_test attach-1.24 {
174*c5c4113dSnw141292  catchsql {
175*c5c4113dSnw141292    DETACH db12;
176*c5c4113dSnw141292  }
177*c5c4113dSnw141292} {0 {}}
178*c5c4113dSnw141292do_test attach-1.25 {
179*c5c4113dSnw141292  catchsql {
180*c5c4113dSnw141292    DETACH db12;
181*c5c4113dSnw141292  }
182*c5c4113dSnw141292} {1 {no such database: db12}}
183*c5c4113dSnw141292do_test attach-1.26 {
184*c5c4113dSnw141292  catchsql {
185*c5c4113dSnw141292    DETACH main;
186*c5c4113dSnw141292  }
187*c5c4113dSnw141292} {1 {cannot detach database main}}
188*c5c4113dSnw141292do_test attach-1.27 {
189*c5c4113dSnw141292  catchsql {
190*c5c4113dSnw141292    DETACH Temp;
191*c5c4113dSnw141292  }
192*c5c4113dSnw141292} {1 {cannot detach database Temp}}
193*c5c4113dSnw141292do_test attach-1.28 {
194*c5c4113dSnw141292  catchsql {
195*c5c4113dSnw141292    DETACH db11;
196*c5c4113dSnw141292    DETACH db10;
197*c5c4113dSnw141292    DETACH db9;
198*c5c4113dSnw141292    DETACH db8;
199*c5c4113dSnw141292    DETACH db7;
200*c5c4113dSnw141292    DETACH db6;
201*c5c4113dSnw141292    DETACH db4;
202*c5c4113dSnw141292    DETACH db3;
203*c5c4113dSnw141292    DETACH db2;
204*c5c4113dSnw141292  }
205*c5c4113dSnw141292} {0 {}}
206*c5c4113dSnw141292do_test attach-1.29 {
207*c5c4113dSnw141292  db_list db
208*c5c4113dSnw141292} {0 main 1 temp}
209*c5c4113dSnw141292
210*c5c4113dSnw141292do_test attach-2.1 {
211*c5c4113dSnw141292  execsql {
212*c5c4113dSnw141292    CREATE TABLE tx(x1,x2,y1,y2);
213*c5c4113dSnw141292    CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
214*c5c4113dSnw141292      INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y);
215*c5c4113dSnw141292    END;
216*c5c4113dSnw141292    SELECT * FROM tx;
217*c5c4113dSnw141292  } db2;
218*c5c4113dSnw141292} {}
219*c5c4113dSnw141292do_test attach-2.2 {
220*c5c4113dSnw141292  execsql {
221*c5c4113dSnw141292    UPDATE t2 SET x=x+10;
222*c5c4113dSnw141292    SELECT * FROM tx;
223*c5c4113dSnw141292  } db2;
224*c5c4113dSnw141292} {1 11 x x 2 12 y y}
225*c5c4113dSnw141292do_test attach-2.3 {
226*c5c4113dSnw141292  execsql {
227*c5c4113dSnw141292    CREATE TABLE tx(x1,x2,y1,y2);
228*c5c4113dSnw141292    SELECT * FROM tx;
229*c5c4113dSnw141292  }
230*c5c4113dSnw141292} {}
231*c5c4113dSnw141292do_test attach-2.4 {
232*c5c4113dSnw141292  execsql {
233*c5c4113dSnw141292    ATTACH 'test2.db' AS db2;
234*c5c4113dSnw141292  }
235*c5c4113dSnw141292} {}
236*c5c4113dSnw141292do_test attach-2.5 {
237*c5c4113dSnw141292  execsql {
238*c5c4113dSnw141292    UPDATE db2.t2 SET x=x+10;
239*c5c4113dSnw141292    SELECT * FROM db2.tx;
240*c5c4113dSnw141292  }
241*c5c4113dSnw141292} {1 11 x x 2 12 y y 11 21 x x 12 22 y y}
242*c5c4113dSnw141292do_test attach-2.6 {
243*c5c4113dSnw141292  execsql {
244*c5c4113dSnw141292    SELECT * FROM main.tx;
245*c5c4113dSnw141292  }
246*c5c4113dSnw141292} {}
247*c5c4113dSnw141292do_test attach-2.7 {
248*c5c4113dSnw141292  execsql {
249*c5c4113dSnw141292    SELECT type, name, tbl_name FROM db2.sqlite_master;
250*c5c4113dSnw141292  }
251*c5c4113dSnw141292} {table t2 t2 table tx tx trigger r1 t2}
252*c5c4113dSnw141292do_test attach-2.8 {
253*c5c4113dSnw141292  db_list db
254*c5c4113dSnw141292} {0 main 1 temp 2 db2}
255*c5c4113dSnw141292do_test attach-2.9 {
256*c5c4113dSnw141292  execsql {
257*c5c4113dSnw141292    CREATE INDEX i2 ON t2(x);
258*c5c4113dSnw141292    SELECT * FROM t2 WHERE x>5;
259*c5c4113dSnw141292  } db2
260*c5c4113dSnw141292} {21 x 22 y}
261*c5c4113dSnw141292do_test attach-2.10 {
262*c5c4113dSnw141292  execsql {
263*c5c4113dSnw141292    SELECT type, name, tbl_name FROM sqlite_master;
264*c5c4113dSnw141292  } db2
265*c5c4113dSnw141292} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
266*c5c4113dSnw141292#do_test attach-2.11 {
267*c5c4113dSnw141292#  catchsql {
268*c5c4113dSnw141292#    SELECT * FROM t2 WHERE x>5;
269*c5c4113dSnw141292#  }
270*c5c4113dSnw141292#} {1 {database schema has changed}}
271*c5c4113dSnw141292do_test attach-2.12 {
272*c5c4113dSnw141292  db_list db
273*c5c4113dSnw141292} {0 main 1 temp 2 db2}
274*c5c4113dSnw141292do_test attach-2.13 {
275*c5c4113dSnw141292  catchsql {
276*c5c4113dSnw141292    SELECT * FROM t2 WHERE x>5;
277*c5c4113dSnw141292  }
278*c5c4113dSnw141292} {0 {21 x 22 y}}
279*c5c4113dSnw141292do_test attach-2.14 {
280*c5c4113dSnw141292  execsql {
281*c5c4113dSnw141292    SELECT type, name, tbl_name FROM sqlite_master;
282*c5c4113dSnw141292  }
283*c5c4113dSnw141292} {table t1 t1 table tx tx}
284*c5c4113dSnw141292do_test attach-2.15 {
285*c5c4113dSnw141292  execsql {
286*c5c4113dSnw141292    SELECT type, name, tbl_name FROM db2.sqlite_master;
287*c5c4113dSnw141292  }
288*c5c4113dSnw141292} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
289*c5c4113dSnw141292do_test attach-2.16 {
290*c5c4113dSnw141292  db close
291*c5c4113dSnw141292  sqlite db test.db
292*c5c4113dSnw141292  execsql {
293*c5c4113dSnw141292    ATTACH 'test2.db' AS db2;
294*c5c4113dSnw141292    SELECT type, name, tbl_name FROM db2.sqlite_master;
295*c5c4113dSnw141292  }
296*c5c4113dSnw141292} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
297*c5c4113dSnw141292
298*c5c4113dSnw141292do_test attach-3.1 {
299*c5c4113dSnw141292  db close
300*c5c4113dSnw141292  db2 close
301*c5c4113dSnw141292  sqlite db test.db
302*c5c4113dSnw141292  sqlite db2 test2.db
303*c5c4113dSnw141292  execsql {
304*c5c4113dSnw141292    SELECT * FROM t1
305*c5c4113dSnw141292  }
306*c5c4113dSnw141292} {1 2 3 4}
307*c5c4113dSnw141292do_test attach-3.2 {
308*c5c4113dSnw141292  catchsql {
309*c5c4113dSnw141292    SELECT * FROM t2
310*c5c4113dSnw141292  }
311*c5c4113dSnw141292} {1 {no such table: t2}}
312*c5c4113dSnw141292do_test attach-3.3 {
313*c5c4113dSnw141292  catchsql {
314*c5c4113dSnw141292    ATTACH DATABASE 'test2.db' AS db2;
315*c5c4113dSnw141292    SELECT * FROM t2
316*c5c4113dSnw141292  }
317*c5c4113dSnw141292} {0 {21 x 22 y}}
318*c5c4113dSnw141292
319*c5c4113dSnw141292# Even though main has a transaction, test2.db should not be locked.
320*c5c4113dSnw141292do_test attach-3.4 {
321*c5c4113dSnw141292  execsql BEGIN
322*c5c4113dSnw141292  catchsql {
323*c5c4113dSnw141292    SELECT * FROM t2;
324*c5c4113dSnw141292  } db2;
325*c5c4113dSnw141292} {0 {21 x 22 y}}
326*c5c4113dSnw141292
327*c5c4113dSnw141292# Reading from db2 should not lock test2.db
328*c5c4113dSnw141292do_test attach-3.5 {
329*c5c4113dSnw141292  execsql {SELECT * FROM t2}
330*c5c4113dSnw141292  catchsql {
331*c5c4113dSnw141292    SELECT * FROM t2;
332*c5c4113dSnw141292  } db2;
333*c5c4113dSnw141292} {0 {21 x 22 y}}
334*c5c4113dSnw141292
335*c5c4113dSnw141292# Making a change to db2 causes test2.ddb to become locked.
336*c5c4113dSnw141292do_test attach-3.6 {
337*c5c4113dSnw141292  execsql {
338*c5c4113dSnw141292    UPDATE t2 SET x=x+1 WHERE x=50;
339*c5c4113dSnw141292  }
340*c5c4113dSnw141292  catchsql {
341*c5c4113dSnw141292    SELECT * FROM t2;
342*c5c4113dSnw141292  } db2;
343*c5c4113dSnw141292} {1 {database is locked}}
344*c5c4113dSnw141292
345*c5c4113dSnw141292do_test attach-3.7 {
346*c5c4113dSnw141292  execsql ROLLBACK
347*c5c4113dSnw141292  execsql {SELECT * FROM t2} db2
348*c5c4113dSnw141292} {21 x 22 y}
349*c5c4113dSnw141292do_test attach-3.8 {
350*c5c4113dSnw141292  execsql BEGIN
351*c5c4113dSnw141292  execsql BEGIN db2
352*c5c4113dSnw141292  catchsql {SELECT * FROM t2}
353*c5c4113dSnw141292} {1 {database is locked}}
354*c5c4113dSnw141292do_test attach-3.9 {
355*c5c4113dSnw141292  catchsql {SELECT * FROM t2} db2
356*c5c4113dSnw141292} {0 {21 x 22 y}}
357*c5c4113dSnw141292do_test attach-3.10 {
358*c5c4113dSnw141292  execsql {SELECT * FROM t1}
359*c5c4113dSnw141292} {1 2 3 4}
360*c5c4113dSnw141292do_test attach-3.11 {
361*c5c4113dSnw141292  catchsql {UPDATE t1 SET a=a+1}
362*c5c4113dSnw141292} {0 {}}
363*c5c4113dSnw141292do_test attach-3.12 {
364*c5c4113dSnw141292  execsql {SELECT * FROM t1}
365*c5c4113dSnw141292} {2 2 4 4}
366*c5c4113dSnw141292do_test attach-3.13 {
367*c5c4113dSnw141292  catchsql {UPDATE t2 SET x=x+1 WHERE x=50}
368*c5c4113dSnw141292} {1 {database is locked}}
369*c5c4113dSnw141292do_test attach-3.14 {
370*c5c4113dSnw141292  # Unable to reinitialize the schema tables because the aux database
371*c5c4113dSnw141292  # is still locked.
372*c5c4113dSnw141292  catchsql {SELECT * FROM t1}
373*c5c4113dSnw141292} {1 {database is locked}}
374*c5c4113dSnw141292do_test attach-3.15 {
375*c5c4113dSnw141292  execsql COMMIT db2
376*c5c4113dSnw141292  execsql {SELECT * FROM t1}
377*c5c4113dSnw141292} {1 2 3 4}
378*c5c4113dSnw141292
379*c5c4113dSnw141292# Ticket #323
380*c5c4113dSnw141292do_test attach-4.1 {
381*c5c4113dSnw141292  execsql {DETACH db2}
382*c5c4113dSnw141292  db2 close
383*c5c4113dSnw141292  sqlite db2 test2.db
384*c5c4113dSnw141292  execsql {
385*c5c4113dSnw141292    CREATE TABLE t3(x,y);
386*c5c4113dSnw141292    CREATE UNIQUE INDEX t3i1 ON t3(x);
387*c5c4113dSnw141292    INSERT INTO t3 VALUES(1,2);
388*c5c4113dSnw141292    SELECT * FROM t3;
389*c5c4113dSnw141292  } db2;
390*c5c4113dSnw141292} {1 2}
391*c5c4113dSnw141292do_test attach-4.2 {
392*c5c4113dSnw141292  execsql {
393*c5c4113dSnw141292    CREATE TABLE t3(a,b);
394*c5c4113dSnw141292    CREATE UNIQUE INDEX t3i1b ON t3(a);
395*c5c4113dSnw141292    INSERT INTO t3 VALUES(9,10);
396*c5c4113dSnw141292    SELECT * FROM t3;
397*c5c4113dSnw141292  }
398*c5c4113dSnw141292} {9 10}
399*c5c4113dSnw141292do_test attach-4.3 {
400*c5c4113dSnw141292  execsql {
401*c5c4113dSnw141292    ATTACH DATABASE 'test2.db' AS db2;
402*c5c4113dSnw141292    SELECT * FROM db2.t3;
403*c5c4113dSnw141292  }
404*c5c4113dSnw141292} {1 2}
405*c5c4113dSnw141292do_test attach-4.4 {
406*c5c4113dSnw141292  execsql {
407*c5c4113dSnw141292    SELECT * FROM main.t3;
408*c5c4113dSnw141292  }
409*c5c4113dSnw141292} {9 10}
410*c5c4113dSnw141292do_test attach-4.5 {
411*c5c4113dSnw141292  execsql {
412*c5c4113dSnw141292    INSERT INTO db2.t3 VALUES(9,10);
413*c5c4113dSnw141292    SELECT * FROM db2.t3;
414*c5c4113dSnw141292  }
415*c5c4113dSnw141292} {1 2 9 10}
416*c5c4113dSnw141292do_test attach-4.6 {
417*c5c4113dSnw141292  execsql {
418*c5c4113dSnw141292    DETACH db2;
419*c5c4113dSnw141292  }
420*c5c4113dSnw141292  execsql {
421*c5c4113dSnw141292    CREATE TABLE t4(x);
422*c5c4113dSnw141292    CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
423*c5c4113dSnw141292      INSERT INTO t4 VALUES('db2.' || NEW.x);
424*c5c4113dSnw141292    END;
425*c5c4113dSnw141292    INSERT INTO t3 VALUES(6,7);
426*c5c4113dSnw141292    SELECT * FROM t4;
427*c5c4113dSnw141292  } db2
428*c5c4113dSnw141292} {db2.6}
429*c5c4113dSnw141292do_test attach-4.7 {
430*c5c4113dSnw141292  execsql {
431*c5c4113dSnw141292    CREATE TABLE t4(y);
432*c5c4113dSnw141292    CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
433*c5c4113dSnw141292      INSERT INTO t4 VALUES('main.' || NEW.a);
434*c5c4113dSnw141292    END;
435*c5c4113dSnw141292    INSERT INTO main.t3 VALUES(11,12);
436*c5c4113dSnw141292    SELECT * FROM main.t4;
437*c5c4113dSnw141292  }
438*c5c4113dSnw141292} {main.11}
439*c5c4113dSnw141292do_test attach-4.8 {
440*c5c4113dSnw141292  execsql {
441*c5c4113dSnw141292    ATTACH DATABASE 'test2.db' AS db2;
442*c5c4113dSnw141292    INSERT INTO db2.t3 VALUES(13,14);
443*c5c4113dSnw141292    SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
444*c5c4113dSnw141292  }
445*c5c4113dSnw141292} {db2.6 db2.13 main.11}
446*c5c4113dSnw141292do_test attach-4.9 {
447*c5c4113dSnw141292  execsql {
448*c5c4113dSnw141292    INSERT INTO main.t3 VALUES(15,16);
449*c5c4113dSnw141292    SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
450*c5c4113dSnw141292  }
451*c5c4113dSnw141292} {db2.6 db2.13 main.11 main.15}
452*c5c4113dSnw141292do_test attach-4.10 {
453*c5c4113dSnw141292  execsql {
454*c5c4113dSnw141292    DETACH DATABASE db2;
455*c5c4113dSnw141292  }
456*c5c4113dSnw141292  execsql {
457*c5c4113dSnw141292    CREATE VIEW v3 AS SELECT x*100+y FROM t3;
458*c5c4113dSnw141292    SELECT * FROM v3;
459*c5c4113dSnw141292  } db2
460*c5c4113dSnw141292} {102 910 607 1314}
461*c5c4113dSnw141292do_test attach-4.11 {
462*c5c4113dSnw141292  execsql {
463*c5c4113dSnw141292    CREATE VIEW v3 AS SELECT a*100+b FROM t3;
464*c5c4113dSnw141292    SELECT * FROM v3;
465*c5c4113dSnw141292  }
466*c5c4113dSnw141292} {910 1112 1516}
467*c5c4113dSnw141292do_test attach-4.12 {
468*c5c4113dSnw141292  execsql {
469*c5c4113dSnw141292    ATTACH DATABASE 'test2.db' AS db2;
470*c5c4113dSnw141292    SELECT * FROM db2.v3;
471*c5c4113dSnw141292  }
472*c5c4113dSnw141292} {102 910 607 1314}
473*c5c4113dSnw141292do_test attach-4.13 {
474*c5c4113dSnw141292  execsql {
475*c5c4113dSnw141292    SELECT * FROM main.v3;
476*c5c4113dSnw141292  }
477*c5c4113dSnw141292} {910 1112 1516}
478*c5c4113dSnw141292
479*c5c4113dSnw141292# Tests for the sqliteFix...() routines in attach.c
480*c5c4113dSnw141292#
481*c5c4113dSnw141292do_test attach-5.1 {
482*c5c4113dSnw141292  db close
483*c5c4113dSnw141292  sqlite db test.db
484*c5c4113dSnw141292  db2 close
485*c5c4113dSnw141292  file delete -force test2.db
486*c5c4113dSnw141292  sqlite db2 test2.db
487*c5c4113dSnw141292  catchsql {
488*c5c4113dSnw141292    ATTACH DATABASE 'test.db' AS orig;
489*c5c4113dSnw141292    CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN;
490*c5c4113dSnw141292      SELECT 'no-op';
491*c5c4113dSnw141292    END;
492*c5c4113dSnw141292  } db2
493*c5c4113dSnw141292} {1 {triggers may not be added to auxiliary database orig}}
494*c5c4113dSnw141292do_test attach-5.2 {
495*c5c4113dSnw141292  catchsql {
496*c5c4113dSnw141292    CREATE TABLE t5(x,y);
497*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
498*c5c4113dSnw141292      SELECT 'no-op';
499*c5c4113dSnw141292    END;
500*c5c4113dSnw141292  } db2
501*c5c4113dSnw141292} {0 {}}
502*c5c4113dSnw141292do_test attach-5.3 {
503*c5c4113dSnw141292  catchsql {
504*c5c4113dSnw141292    DROP TRIGGER r5;
505*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
506*c5c4113dSnw141292      SELECT 'no-op' FROM orig.t1;
507*c5c4113dSnw141292    END;
508*c5c4113dSnw141292  } db2
509*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database orig}}
510*c5c4113dSnw141292do_test attach-5.4 {
511*c5c4113dSnw141292  catchsql {
512*c5c4113dSnw141292    CREATE TEMP TABLE t6(p,q,r);
513*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
514*c5c4113dSnw141292      SELECT 'no-op' FROM temp.t6;
515*c5c4113dSnw141292    END;
516*c5c4113dSnw141292  } db2
517*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database temp}}
518*c5c4113dSnw141292do_test attach-5.5 {
519*c5c4113dSnw141292  catchsql {
520*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
521*c5c4113dSnw141292      SELECT 'no-op' || (SELECT * FROM temp.t6);
522*c5c4113dSnw141292    END;
523*c5c4113dSnw141292  } db2
524*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database temp}}
525*c5c4113dSnw141292do_test attach-5.6 {
526*c5c4113dSnw141292  catchsql {
527*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
528*c5c4113dSnw141292      SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
529*c5c4113dSnw141292    END;
530*c5c4113dSnw141292  } db2
531*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database temp}}
532*c5c4113dSnw141292do_test attach-5.7 {
533*c5c4113dSnw141292  catchsql {
534*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
535*c5c4113dSnw141292      SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6);
536*c5c4113dSnw141292    END;
537*c5c4113dSnw141292  } db2
538*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database temp}}
539*c5c4113dSnw141292do_test attach-5.7 {
540*c5c4113dSnw141292  catchsql {
541*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
542*c5c4113dSnw141292      SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1;
543*c5c4113dSnw141292    END;
544*c5c4113dSnw141292  } db2
545*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database temp}}
546*c5c4113dSnw141292do_test attach-5.8 {
547*c5c4113dSnw141292  catchsql {
548*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
549*c5c4113dSnw141292      INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5);
550*c5c4113dSnw141292    END;
551*c5c4113dSnw141292  } db2
552*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database temp}}
553*c5c4113dSnw141292do_test attach-5.9 {
554*c5c4113dSnw141292  catchsql {
555*c5c4113dSnw141292    CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
556*c5c4113dSnw141292      DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
557*c5c4113dSnw141292    END;
558*c5c4113dSnw141292  } db2
559*c5c4113dSnw141292} {1 {trigger r5 cannot reference objects in database temp}}
560*c5c4113dSnw141292
561*c5c4113dSnw141292# Check to make sure we get a sensible error if unable to open
562*c5c4113dSnw141292# the file that we are trying to attach.
563*c5c4113dSnw141292#
564*c5c4113dSnw141292do_test attach-6.1 {
565*c5c4113dSnw141292  catchsql {
566*c5c4113dSnw141292    ATTACH DATABASE 'no-such-file' AS nosuch;
567*c5c4113dSnw141292  }
568*c5c4113dSnw141292} {1 {cannot attach empty database: nosuch}}
569*c5c4113dSnw141292file delete -force no-such-file
570*c5c4113dSnw141292if {$tcl_platform(platform)=="unix"} {
571*c5c4113dSnw141292  do_test attach-6.2 {
572*c5c4113dSnw141292    sqlite dbx cannot-read
573*c5c4113dSnw141292    dbx eval {CREATE TABLE t1(a,b,c)}
574*c5c4113dSnw141292    dbx close
575*c5c4113dSnw141292    file attributes cannot-read -permission 0000
576*c5c4113dSnw141292    catchsql {
577*c5c4113dSnw141292      ATTACH DATABASE 'cannot-read' AS noread;
578*c5c4113dSnw141292    }
579*c5c4113dSnw141292  } {1 {unable to open database: cannot-read}}
580*c5c4113dSnw141292  file delete -force cannot-read
581*c5c4113dSnw141292}
582*c5c4113dSnw141292
583*c5c4113dSnw141292for {set i 2} {$i<=15} {incr i} {
584*c5c4113dSnw141292  catch {db$i close}
585*c5c4113dSnw141292}
586*c5c4113dSnw141292file delete -force test2.db
587*c5c4113dSnw141292
588*c5c4113dSnw141292
589*c5c4113dSnw141292finish_test
590