xref: /sqlite-3.40.0/test/attach4.test (revision 8210233c)
156089737Sdan# 200 July 1
256089737Sdan#
356089737Sdan# The author disclaims copyright to this source code.  In place of
456089737Sdan# a legal notice, here is a blessing:
556089737Sdan#
656089737Sdan#    May you do good and not evil.
756089737Sdan#    May you find forgiveness for yourself and forgive others.
856089737Sdan#    May you share freely, never taking more than you give.
956089737Sdan#
1056089737Sdan#***********************************************************************
1156089737Sdan# This file implements regression tests for SQLite library.  The
1256089737Sdan# focus of this script is attaching many database files to a single
1356089737Sdan# connection.
1456089737Sdan#
1556089737Sdan
1656089737Sdanset testdir [file dirname $argv0]
1756089737Sdansource $testdir/tester.tcl
1856089737Sdan
1956089737Sdanset testprefix attach4
2056089737Sdan
2156089737Sdanifcapable !attach {
2256089737Sdan  finish_test
2356089737Sdan  return
2456089737Sdan}
2556089737Sdan
2656089737Sdanputs "Testing with SQLITE_MAX_ATTACHED=$SQLITE_MAX_ATTACHED"
2756089737Sdan
2856089737Sdanset files {main test.db}
2956089737Sdanfor {set ii 0} {$ii < $SQLITE_MAX_ATTACHED} {incr ii} {
3056089737Sdan  lappend files aux$ii "test.db$ii"
3156089737Sdan}
3256089737Sdan
3356089737Sdando_test 1.1 {
3456089737Sdan  sqlite3_limit db SQLITE_LIMIT_ATTACHED -1
3556089737Sdan} $SQLITE_MAX_ATTACHED
3656089737Sdan
3756089737Sdando_test 1.2.1 {
3856089737Sdan  db close
3956089737Sdan  foreach {name f} $files { forcedelete $f }
4056089737Sdan  sqlite3 db test.db
4156089737Sdan
4256089737Sdan  foreach {name f} $files {
4356089737Sdan    if {$name == "main"} continue
4456089737Sdan    execsql "ATTACH '$f' AS $name"
4556089737Sdan  }
4656089737Sdan
4756089737Sdan  db eval {PRAGMA database_list} {
4856089737Sdan    lappend L $name [file tail $file]
4956089737Sdan  }
5056089737Sdan  set L
5156089737Sdan} $files
5256089737Sdan
5356089737Sdando_catchsql_test 1.2.2 {
5456089737Sdan  ATTACH 'x.db' AS next;
5556089737Sdan} [list 1 "too many attached databases - max $SQLITE_MAX_ATTACHED"]
5656089737Sdan
5756089737Sdando_test 1.3 {
5856089737Sdan  execsql BEGIN;
5956089737Sdan  foreach {name f} $files {
6056089737Sdan    execsql "CREATE TABLE $name.tbl(x)"
6156089737Sdan    execsql "INSERT INTO $name.tbl VALUES('$f')"
6256089737Sdan  }
6356089737Sdan  execsql COMMIT;
6456089737Sdan} {}
6556089737Sdan
6656089737Sdando_test 1.4 {
6756089737Sdan  set L [list]
6856089737Sdan  foreach {name f} $files {
6956089737Sdan    lappend L $name [execsql "SELECT x FROM $name.tbl"]
7056089737Sdan  }
7156089737Sdan  set L
7256089737Sdan} $files
7356089737Sdan
7456089737Sdanset L [list]
7556089737Sdanset S ""
7656089737Sdanforeach {name f} $files {
77b73da5bdSdan  if {[permutation] == "journaltest"} {
785209132aSdan    set mode delete
79b73da5bdSdan  } else {
805209132aSdan    set mode wal
81b73da5bdSdan  }
825209132aSdan  ifcapable !wal { set mode delete }
835209132aSdan  lappend L $mode
8456089737Sdan  append S "
8556089737Sdan    PRAGMA $name.journal_mode = WAL;
8656089737Sdan    UPDATE $name.tbl SET x = '$name';
8756089737Sdan  "
8856089737Sdan}
8956089737Sdando_execsql_test 1.5 $S $L
9056089737Sdan
9156089737Sdando_test 1.6 {
9256089737Sdan  set L [list]
9356089737Sdan  foreach {name f} $files {
9456089737Sdan    lappend L [execsql "SELECT x FROM $name.tbl"] $f
9556089737Sdan  }
9656089737Sdan  set L
9756089737Sdan} $files
9856089737Sdan
9956089737Sdando_test 1.7 {
10056089737Sdan  execsql BEGIN;
10156089737Sdan  foreach {name f} $files {
10256089737Sdan    execsql "UPDATE $name.tbl SET x = '$f'"
10356089737Sdan  }
10456089737Sdan  execsql COMMIT;
10556089737Sdan} {}
10656089737Sdan
10756089737Sdando_test 1.8 {
10856089737Sdan  set L [list]
10956089737Sdan  foreach {name f} $files {
11056089737Sdan    lappend L $name [execsql "SELECT x FROM $name.tbl"]
11156089737Sdan  }
11256089737Sdan  set L
11356089737Sdan} $files
11456089737Sdan
11556089737Sdandb close
11656089737Sdanforeach {name f} $files { forcedelete $f }
11756089737Sdan
118*0232dadeSdan#-------------------------------------------------------------------------
119*0232dadeSdanreset_db
120*0232dadeSdando_execsql_test 2.0 {
121*0232dadeSdan  ATTACH DATABASE '' AS aux;
122*0232dadeSdan  CREATE TABLE IF NOT EXISTS aux.t1(a, b);
123*0232dadeSdan  CREATE TEMPORARY TRIGGER tr1 DELETE ON t1 BEGIN
124*0232dadeSdan    DELETE FROM t1;
125*0232dadeSdan  END;
126*0232dadeSdan  CREATE TABLE temp.t1(a, b);
127*0232dadeSdan}
128*0232dadeSdan
129*0232dadeSdando_execsql_test 2.1 {
130*0232dadeSdan  DETACH DATABASE aux;
131*0232dadeSdan}
132*0232dadeSdan
133*0232dadeSdando_execsql_test 2.2 {
134*0232dadeSdan  DROP TRIGGER tr1;
135*0232dadeSdan}
136*0232dadeSdan
13756089737Sdanfinish_test
138