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