xref: /illumos-gate/usr/src/lib/libsqlite/test/attach2.test (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1#
2# 2003 July 1
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
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.
10#
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.
15#
16# $Id: attach2.test,v 1.5 2004/02/12 15:31:22 drh Exp $
17#
18
19
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23# Ticket #354
24#
25do_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 {}}
41
42# Ticket #514
43#
44proc 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
50}
51db eval {DETACH t2}
52do_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}}
59do_test attach2-2.2 {
60  # make sure test2.db did not get attached.
61  db_list db
62} {0 main 1 temp}
63do_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 {}}
70do_test attach2-2.4 {
71  db_list db
72} {0 main 1 temp 2 t2}
73do_test attach2-2.5 {
74  catchsql {
75    SELECT name FROM t2.sqlite_master;
76  }
77} {0 {t1 x1}}
78do_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}}
85do_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}}
91do_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 {}}
98do_test attach2-2.9 {
99  execsql {
100    SELECT * FROM t1
101  }
102} {8 9}
103do_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}}
109do_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} {}
117do_test attach2-2.12 {
118  catchsql {
119    COMMIT
120  }
121} {1 {cannot commit - no transaction is active}}
122
123# Ticket #574:  Make sure it works usingi the non-callback API
124#
125do_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}
133do_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}
139
140db close
141for {set i 2} {$i<=15} {incr i} {
142  catch {db$i close}
143}
144file delete -force test2.db
145
146
147finish_test
148