xref: /onnv-gate/usr/src/lib/libsqlite/test/attach2.test (revision 4520:7dbeadedd7fe)
1*4520Snw141292
2*4520Snw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*4520Snw141292
4*4520Snw141292# 2003 July 1
5*4520Snw141292#
6*4520Snw141292# The author disclaims copyright to this source code.  In place of
7*4520Snw141292# a legal notice, here is a blessing:
8*4520Snw141292#
9*4520Snw141292#    May you do good and not evil.
10*4520Snw141292#    May you find forgiveness for yourself and forgive others.
11*4520Snw141292#    May you share freely, never taking more than you give.
12*4520Snw141292#
13*4520Snw141292#***********************************************************************
14*4520Snw141292# This file implements regression tests for SQLite library.  The
15*4520Snw141292# focus of this script is testing the ATTACH and DETACH commands
16*4520Snw141292# and related functionality.
17*4520Snw141292#
18*4520Snw141292# $Id: attach2.test,v 1.5 2004/02/12 15:31:22 drh Exp $
19*4520Snw141292#
20*4520Snw141292
21*4520Snw141292
22*4520Snw141292set testdir [file dirname $argv0]
23*4520Snw141292source $testdir/tester.tcl
24*4520Snw141292
25*4520Snw141292# Ticket #354
26*4520Snw141292#
27*4520Snw141292do_test attach2-1.1 {
28*4520Snw141292  db eval {
29*4520Snw141292    CREATE TABLE t1(a,b);
30*4520Snw141292    CREATE INDEX x1 ON t1(a);
31*4520Snw141292  }
32*4520Snw141292  file delete -force test2.db
33*4520Snw141292  file delete -force test2.db-journal
34*4520Snw141292  sqlite db2 test2.db
35*4520Snw141292  db2 eval {
36*4520Snw141292    CREATE TABLE t1(a,b);
37*4520Snw141292    CREATE INDEX x1 ON t1(a);
38*4520Snw141292  }
39*4520Snw141292  catchsql {
40*4520Snw141292    ATTACH 'test2.db' AS t2;
41*4520Snw141292  }
42*4520Snw141292} {0 {}}
43*4520Snw141292
44*4520Snw141292# Ticket #514
45*4520Snw141292#
46*4520Snw141292proc db_list {db} {
47*4520Snw141292  set list {}
48*4520Snw141292  foreach {idx name file} [execsql {PRAGMA database_list} $db] {
49*4520Snw141292    lappend list $idx $name
50*4520Snw141292  }
51*4520Snw141292  return $list
52*4520Snw141292}
53*4520Snw141292db eval {DETACH t2}
54*4520Snw141292do_test attach2-2.1 {
55*4520Snw141292  # lock test2.db then try to attach it.  Should get an error.
56*4520Snw141292  db2 eval {BEGIN}
57*4520Snw141292  catchsql {
58*4520Snw141292    ATTACH 'test2.db' AS t2;
59*4520Snw141292  }
60*4520Snw141292} {1 {database is locked}}
61*4520Snw141292do_test attach2-2.2 {
62*4520Snw141292  # make sure test2.db did not get attached.
63*4520Snw141292  db_list db
64*4520Snw141292} {0 main 1 temp}
65*4520Snw141292do_test attach2-2.3 {
66*4520Snw141292  # unlock test2.db and try to attach again.  should work this time.
67*4520Snw141292  db2 eval {COMMIT}
68*4520Snw141292  catchsql {
69*4520Snw141292    ATTACH 'test2.db' AS t2;
70*4520Snw141292  }
71*4520Snw141292} {0 {}}
72*4520Snw141292do_test attach2-2.4 {
73*4520Snw141292  db_list db
74*4520Snw141292} {0 main 1 temp 2 t2}
75*4520Snw141292do_test attach2-2.5 {
76*4520Snw141292  catchsql {
77*4520Snw141292    SELECT name FROM t2.sqlite_master;
78*4520Snw141292  }
79*4520Snw141292} {0 {t1 x1}}
80*4520Snw141292do_test attach2-2.6 {
81*4520Snw141292  # lock test2.db and try to read from it.  should get an error.
82*4520Snw141292  db2 eval BEGIN
83*4520Snw141292  catchsql {
84*4520Snw141292    SELECT name FROM t2.sqlite_master;
85*4520Snw141292  }
86*4520Snw141292} {1 {database is locked}}
87*4520Snw141292do_test attach2-2.7 {
88*4520Snw141292  # but we can still read from test1.db even though test2.db is locked.
89*4520Snw141292  catchsql {
90*4520Snw141292    SELECT name FROM main.sqlite_master;
91*4520Snw141292  }
92*4520Snw141292} {0 {t1 x1}}
93*4520Snw141292do_test attach2-2.8 {
94*4520Snw141292  # start a transaction on test.db even though test2.db is locked.
95*4520Snw141292  catchsql {
96*4520Snw141292    BEGIN;
97*4520Snw141292    INSERT INTO t1 VALUES(8,9);
98*4520Snw141292  }
99*4520Snw141292} {0 {}}
100*4520Snw141292do_test attach2-2.9 {
101*4520Snw141292  execsql {
102*4520Snw141292    SELECT * FROM t1
103*4520Snw141292  }
104*4520Snw141292} {8 9}
105*4520Snw141292do_test attach2-2.10 {
106*4520Snw141292  # now try to write to test2.db.  the write should fail
107*4520Snw141292  catchsql {
108*4520Snw141292    INSERT INTO t2.t1 VALUES(1,2);
109*4520Snw141292  }
110*4520Snw141292} {1 {database is locked}}
111*4520Snw141292do_test attach2-2.11 {
112*4520Snw141292  # when the write failed in the previous test, the transaction should
113*4520Snw141292  # have rolled back.
114*4520Snw141292  db2 eval ROLLBACK
115*4520Snw141292  execsql {
116*4520Snw141292    SELECT * FROM t1
117*4520Snw141292  }
118*4520Snw141292} {}
119*4520Snw141292do_test attach2-2.12 {
120*4520Snw141292  catchsql {
121*4520Snw141292    COMMIT
122*4520Snw141292  }
123*4520Snw141292} {1 {cannot commit - no transaction is active}}
124*4520Snw141292
125*4520Snw141292# Ticket #574:  Make sure it works usingi the non-callback API
126*4520Snw141292#
127*4520Snw141292do_test attach2-3.1 {
128*4520Snw141292  db close
129*4520Snw141292  set DB [sqlite db test.db]
130*4520Snw141292  set rc [catch {sqlite_compile $DB "ATTACH 'test2.db' AS t2" TAIL} VM]
131*4520Snw141292  if {$rc} {lappend rc $VM}
132*4520Snw141292  sqlite_finalize $VM
133*4520Snw141292  set rc
134*4520Snw141292} {0}
135*4520Snw141292do_test attach2-3.2 {
136*4520Snw141292  set rc [catch {sqlite_compile $DB "DETACH t2" TAIL} VM]
137*4520Snw141292  if {$rc} {lappend rc $VM}
138*4520Snw141292  sqlite_finalize $VM
139*4520Snw141292  set rc
140*4520Snw141292} {0}
141*4520Snw141292
142*4520Snw141292db close
143*4520Snw141292for {set i 2} {$i<=15} {incr i} {
144*4520Snw141292  catch {db$i close}
145*4520Snw141292}
146*4520Snw141292file delete -force test2.db
147*4520Snw141292
148*4520Snw141292
149*4520Snw141292finish_test
150