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