1# 2003 April 4 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 6# May you do good and not evil. 7# May you find forgiveness for yourself and forgive others. 8# May you share freely, never taking more than you give. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. The 12# focus of this script is testing the ATTACH and DETACH commands 13# and related functionality. 14# 15# $Id: attach.test,v 1.52 2009/05/29 14:39:08 drh Exp $ 16# 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21ifcapable !attach { 22 finish_test 23 return 24} 25 26for {set i 2} {$i<=15} {incr i} { 27 forcedelete test$i.db 28 forcedelete test$i.db-journal 29} 30 31do_test attach-1.1 { 32 execsql { 33 CREATE TABLE t1(a,b); 34 INSERT INTO t1 VALUES(1,2); 35 INSERT INTO t1 VALUES(3,4); 36 SELECT * FROM t1; 37 } 38} {1 2 3 4} 39do_test attach-1.2 { 40 sqlite3 db2 test2.db 41 execsql { 42 CREATE TABLE t2(x,y); 43 INSERT INTO t2 VALUES(1,'x'); 44 INSERT INTO t2 VALUES(2,'y'); 45 SELECT * FROM t2; 46 } db2 47} {1 x 2 y} 48do_test attach-1.3 { 49 execsql { 50 ATTACH DATABASE 'test2.db' AS two; 51 SELECT * FROM two.t2; 52 } 53} {1 x 2 y} 54 55# Tests for the sqlite3_db_filename interface 56# 57do_test attach-1.3.1 { 58 file tail [sqlite3_db_filename db main] 59} {test.db} 60do_test attach-1.3.2 { 61 file tail [sqlite3_db_filename db MAIN] 62} {test.db} 63do_test attach-1.3.3 { 64 file tail [sqlite3_db_filename db temp] 65} {} 66do_test attach-1.3.4 { 67 file tail [sqlite3_db_filename db two] 68} {test2.db} 69do_test attach-1.3.5 { 70 file tail [sqlite3_db_filename db three] 71} {} 72 73 74do_test attach-1.4 { 75 execsql { 76 SELECT * FROM t2; 77 } 78} {1 x 2 y} 79do_test attach-1.5 { 80 execsql { 81 DETACH DATABASE two; 82 SELECT * FROM t1; 83 } 84} {1 2 3 4} 85do_test attach-1.6 { 86 catchsql { 87 SELECT * FROM t2; 88 } 89} {1 {no such table: t2}} 90do_test attach-1.7 { 91 catchsql { 92 SELECT * FROM two.t2; 93 } 94} {1 {no such table: two.t2}} 95do_test attach-1.8 { 96 catchsql { 97 ATTACH DATABASE 'test3.db' AS three; 98 } 99} {0 {}} 100do_test attach-1.9 { 101 catchsql { 102 SELECT * FROM three.sqlite_master; 103 } 104} {0 {}} 105do_test attach-1.10 { 106 catchsql { 107 DETACH DATABASE [three]; 108 } 109} {0 {}} 110do_test attach-1.11 { 111 execsql { 112 ATTACH 'test.db' AS db2; 113 ATTACH 'test.db' AS db3; 114 ATTACH 'test.db' AS db4; 115 ATTACH 'test.db' AS db5; 116 ATTACH 'test.db' AS db6; 117 ATTACH 'test.db' AS db7; 118 ATTACH 'test.db' AS db8; 119 ATTACH 'test.db' AS db9; 120 } 121} {} 122proc db_list {db} { 123 set list {} 124 foreach {idx name file} [execsql {PRAGMA database_list} $db] { 125 lappend list $idx $name 126 } 127 return $list 128} 129ifcapable schema_pragmas { 130do_test attach-1.11b { 131 db_list db 132} {0 main 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9} 133} ;# ifcapable schema_pragmas 134do_test attach-1.12 { 135 catchsql { 136 ATTACH 'test.db' as db2; 137 } 138} {1 {database db2 is already in use}} 139do_test attach-1.12.2 { 140 db errorcode 141} {1} 142do_test attach-1.13 { 143 catchsql { 144 ATTACH 'test.db' as db5; 145 } 146} {1 {database db5 is already in use}} 147do_test attach-1.14 { 148 catchsql { 149 ATTACH 'test.db' as db9; 150 } 151} {1 {database db9 is already in use}} 152do_test attach-1.15 { 153 catchsql { 154 ATTACH 'test.db' as main; 155 } 156} {1 {database main is already in use}} 157ifcapable tempdb { 158 do_test attach-1.16 { 159 catchsql { 160 ATTACH 'test.db' as temp; 161 } 162 } {1 {database temp is already in use}} 163} 164do_test attach-1.17 { 165 catchsql { 166 ATTACH 'test.db' as MAIN; 167 } 168} {1 {database MAIN is already in use}} 169do_test attach-1.18 { 170 catchsql { 171 ATTACH 'test.db' as db10; 172 ATTACH 'test.db' as db11; 173 } 174} {0 {}} 175if {$SQLITE_MAX_ATTACHED==10} { 176 do_test attach-1.19 { 177 catchsql { 178 ATTACH 'test.db' as db12; 179 } 180 } {1 {too many attached databases - max 10}} 181 do_test attach-1.19.1 { 182 db errorcode 183 } {1} 184} 185do_test attach-1.20.1 { 186 execsql { 187 DETACH db5; 188 } 189} {} 190ifcapable schema_pragmas { 191do_test attach-1.20.2 { 192 db_list db 193} {0 main 2 db2 3 db3 4 db4 5 db6 6 db7 7 db8 8 db9 9 db10 10 db11} 194} ;# ifcapable schema_pragmas 195integrity_check attach-1.20.3 196ifcapable tempdb { 197 execsql {select * from sqlite_temp_master} 198} 199do_test attach-1.21 { 200 catchsql { 201 ATTACH 'test.db' as db12; 202 } 203} {0 {}} 204if {$SQLITE_MAX_ATTACHED==10} { 205 do_test attach-1.22 { 206 catchsql { 207 ATTACH 'test.db' as db13; 208 } 209 } {1 {too many attached databases - max 10}} 210 do_test attach-1.22.1 { 211 db errorcode 212 } {1} 213} 214do_test attach-1.23 { 215 catchsql { 216 DETACH "db14"; 217 } 218} {1 {no such database: db14}} 219do_test attach-1.24 { 220 catchsql { 221 DETACH db12; 222 } 223} {0 {}} 224do_test attach-1.25 { 225 catchsql { 226 DETACH db12; 227 } 228} {1 {no such database: db12}} 229do_test attach-1.26 { 230 catchsql { 231 DETACH main; 232 } 233} {1 {cannot detach database main}} 234 235ifcapable tempdb { 236 do_test attach-1.27 { 237 catchsql { 238 DETACH Temp; 239 } 240 } {1 {cannot detach database Temp}} 241} else { 242 do_test attach-1.27 { 243 catchsql { 244 DETACH Temp; 245 } 246 } {1 {no such database: Temp}} 247} 248 249do_test attach-1.28 { 250 catchsql { 251 DETACH db11; 252 DETACH db10; 253 DETACH db9; 254 DETACH db8; 255 DETACH db7; 256 DETACH db6; 257 DETACH db4; 258 DETACH db3; 259 DETACH db2; 260 } 261} {0 {}} 262ifcapable schema_pragmas { 263 ifcapable tempdb { 264 do_test attach-1.29 { 265 db_list db 266 } {0 main 1 temp} 267 } else { 268 do_test attach-1.29 { 269 db_list db 270 } {0 main} 271 } 272} ;# ifcapable schema_pragmas 273 274 275ifcapable {trigger} { # Only do the following tests if triggers are enabled 276do_test attach-2.1 { 277 execsql { 278 CREATE TABLE tx(x1,x2,y1,y2); 279 CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN 280 INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y); 281 END; 282 SELECT * FROM tx; 283 } db2; 284} {} 285do_test attach-2.2 { 286 execsql { 287 UPDATE t2 SET x=x+10; 288 SELECT * FROM tx; 289 } db2; 290} {1 11 x x 2 12 y y} 291do_test attach-2.3 { 292 execsql { 293 CREATE TABLE tx(x1,x2,y1,y2); 294 SELECT * FROM tx; 295 } 296} {} 297do_test attach-2.4 { 298 execsql { 299 ATTACH 'test2.db' AS db2; 300 } 301} {} 302do_test attach-2.5 { 303 execsql { 304 UPDATE db2.t2 SET x=x+10; 305 SELECT * FROM db2.tx; 306 } 307} {1 11 x x 2 12 y y 11 21 x x 12 22 y y} 308do_test attach-2.6 { 309 execsql { 310 SELECT * FROM main.tx; 311 } 312} {} 313do_test attach-2.7 { 314 execsql { 315 SELECT type, name, tbl_name FROM db2.sqlite_master; 316 } 317} {table t2 t2 table tx tx trigger r1 t2} 318 319ifcapable schema_pragmas&&tempdb { 320 do_test attach-2.8 { 321 db_list db 322 } {0 main 1 temp 2 db2} 323} ;# ifcapable schema_pragmas&&tempdb 324ifcapable schema_pragmas&&!tempdb { 325 do_test attach-2.8 { 326 db_list db 327 } {0 main 2 db2} 328} ;# ifcapable schema_pragmas&&!tempdb 329 330do_test attach-2.9 { 331 execsql { 332 CREATE INDEX i2 ON t2(x); 333 SELECT * FROM t2 WHERE x>5; 334 } db2 335} {21 x 22 y} 336do_test attach-2.10 { 337 execsql { 338 SELECT type, name, tbl_name FROM sqlite_master; 339 } db2 340} {table t2 t2 table tx tx trigger r1 t2 index i2 t2} 341#do_test attach-2.11 { 342# catchsql { 343# SELECT * FROM t2 WHERE x>5; 344# } 345#} {1 {database schema has changed}} 346ifcapable schema_pragmas { 347 ifcapable tempdb { 348 do_test attach-2.12 { 349 db_list db 350 } {0 main 1 temp 2 db2} 351 } else { 352 do_test attach-2.12 { 353 db_list db 354 } {0 main 2 db2} 355 } 356} ;# ifcapable schema_pragmas 357do_test attach-2.13 { 358 catchsql { 359 SELECT * FROM t2 WHERE x>5; 360 } 361} {0 {21 x 22 y}} 362do_test attach-2.14 { 363 execsql { 364 SELECT type, name, tbl_name FROM sqlite_master; 365 } 366} {table t1 t1 table tx tx} 367do_test attach-2.15 { 368 execsql { 369 SELECT type, name, tbl_name FROM db2.sqlite_master; 370 } 371} {table t2 t2 table tx tx trigger r1 t2 index i2 t2} 372do_test attach-2.16 { 373 db close 374 sqlite3 db test.db 375 execsql { 376 ATTACH 'test2.db' AS db2; 377 SELECT type, name, tbl_name FROM db2.sqlite_master; 378 } 379} {table t2 t2 table tx tx trigger r1 t2 index i2 t2} 380} ;# End of ifcapable {trigger} 381 382do_test attach-3.1 { 383 db close 384 db2 close 385 sqlite3 db test.db 386 sqlite3 db2 test2.db 387 execsql { 388 SELECT * FROM t1 389 } 390} {1 2 3 4} 391 392# If we are testing a version of the code that lacks trigger support, 393# adjust the database contents so that they are the same if triggers 394# had been enabled. 395ifcapable {!trigger} { 396 db2 eval { 397 DELETE FROM t2; 398 INSERT INTO t2 VALUES(21, 'x'); 399 INSERT INTO t2 VALUES(22, 'y'); 400 CREATE TABLE tx(x1,x2,y1,y2); 401 INSERT INTO tx VALUES(1, 11, 'x', 'x'); 402 INSERT INTO tx VALUES(2, 12, 'y', 'y'); 403 INSERT INTO tx VALUES(11, 21, 'x', 'x'); 404 INSERT INTO tx VALUES(12, 22, 'y', 'y'); 405 CREATE INDEX i2 ON t2(x); 406 } 407} 408 409do_test attach-3.2 { 410 catchsql { 411 SELECT * FROM t2 412 } 413} {1 {no such table: t2}} 414do_test attach-3.3 { 415 catchsql { 416 ATTACH DATABASE 'test2.db' AS db2; 417 SELECT * FROM t2 418 } 419} {0 {21 x 22 y}} 420 421# Even though 'db' has started a transaction, it should not yet have 422# a lock on test2.db so 'db2' should be readable. 423do_test attach-3.4 { 424 execsql BEGIN 425 catchsql { 426 SELECT * FROM t2; 427 } db2; 428} {0 {21 x 22 y}} 429 430# Reading from test2.db from db within a transaction should not 431# prevent test2.db from being read by db2. 432do_test attach-3.5 { 433 execsql {SELECT * FROM t2} 434 catchsql { 435 SELECT * FROM t2; 436 } db2; 437} {0 {21 x 22 y}} 438 439# Making a change to test2.db through db causes test2.db to get 440# a reserved lock. It should still be accessible through db2. 441do_test attach-3.6 { 442 execsql { 443 UPDATE t2 SET x=x+1 WHERE x=50; 444 } 445 catchsql { 446 SELECT * FROM t2; 447 } db2; 448} {0 {21 x 22 y}} 449 450do_test attach-3.7 { 451 execsql ROLLBACK 452 execsql {SELECT * FROM t2} db2 453} {21 x 22 y} 454 455# Start transactions on both db and db2. Once again, just because 456# we make a change to test2.db using db2, only a RESERVED lock is 457# obtained, so test2.db should still be readable using db. 458# 459do_test attach-3.8 { 460 execsql BEGIN 461 execsql BEGIN db2 462 execsql {UPDATE t2 SET x=0 WHERE 0} db2 463 catchsql {SELECT * FROM t2} 464} {0 {21 x 22 y}} 465 466# It is also still accessible from db2. 467do_test attach-3.9 { 468 catchsql {SELECT * FROM t2} db2 469} {0 {21 x 22 y}} 470 471do_test attach-3.10 { 472 execsql {SELECT * FROM t1} 473} {1 2 3 4} 474 475do_test attach-3.11 { 476 catchsql {UPDATE t1 SET a=a+1} 477} {0 {}} 478do_test attach-3.12 { 479 execsql {SELECT * FROM t1} 480} {2 2 4 4} 481 482# db2 has a RESERVED lock on test2.db, so db cannot write to any tables 483# in test2.db. 484do_test attach-3.13 { 485 catchsql {UPDATE t2 SET x=x+1 WHERE x=50} 486} {1 {database is locked}} 487 488# Change for version 3. Transaction is no longer rolled back 489# for a locked database. 490execsql {ROLLBACK} 491 492# db is able to reread its schema because db2 still only holds a 493# reserved lock. 494do_test attach-3.14 { 495 catchsql {SELECT * FROM t1} 496} {0 {1 2 3 4}} 497do_test attach-3.15 { 498 execsql COMMIT db2 499 execsql {SELECT * FROM t1} 500} {1 2 3 4} 501 502# Ticket #323 503do_test attach-4.1 { 504 execsql {DETACH db2} 505 db2 close 506 sqlite3 db2 test2.db 507 execsql { 508 CREATE TABLE t3(x,y); 509 CREATE UNIQUE INDEX t3i1 ON t3(x); 510 INSERT INTO t3 VALUES(1,2); 511 SELECT * FROM t3; 512 } db2; 513} {1 2} 514do_test attach-4.2 { 515 execsql { 516 CREATE TABLE t3(a,b); 517 CREATE UNIQUE INDEX t3i1b ON t3(a); 518 INSERT INTO t3 VALUES(9,10); 519 SELECT * FROM t3; 520 } 521} {9 10} 522do_test attach-4.3 { 523 execsql { 524 ATTACH DATABASE 'test2.db' AS db2; 525 SELECT * FROM db2.t3; 526 } 527} {1 2} 528do_test attach-4.4 { 529 execsql { 530 SELECT * FROM main.t3; 531 } 532} {9 10} 533do_test attach-4.5 { 534 execsql { 535 INSERT INTO db2.t3 VALUES(9,10); 536 SELECT * FROM db2.t3; 537 } 538} {1 2 9 10} 539execsql { 540 DETACH db2; 541} 542ifcapable {trigger} { 543 do_test attach-4.6 { 544 execsql { 545 CREATE TABLE t4(x); 546 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN 547 INSERT INTO t4 VALUES('db2.' || NEW.x); 548 END; 549 INSERT INTO t3 VALUES(6,7); 550 SELECT * FROM t4; 551 } db2 552 } {db2.6} 553 do_test attach-4.7 { 554 execsql { 555 CREATE TABLE t4(y); 556 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN 557 INSERT INTO t4 VALUES('main.' || NEW.a); 558 END; 559 INSERT INTO main.t3 VALUES(11,12); 560 SELECT * FROM main.t4; 561 } 562 } {main.11} 563} 564ifcapable {!trigger} { 565 # When we do not have trigger support, set up the table like they 566 # would have been had triggers been there. The tests that follow need 567 # this setup. 568 execsql { 569 CREATE TABLE t4(x); 570 INSERT INTO t3 VALUES(6,7); 571 INSERT INTO t4 VALUES('db2.6'); 572 INSERT INTO t4 VALUES('db2.13'); 573 } db2 574 execsql { 575 CREATE TABLE t4(y); 576 INSERT INTO main.t3 VALUES(11,12); 577 INSERT INTO t4 VALUES('main.11'); 578 } 579} 580 581 582# This one is tricky. On the UNION ALL select, we have to make sure 583# the schema for both main and db2 is valid before starting to execute 584# the first query of the UNION ALL. If we wait to test the validity of 585# the schema for main until after the first query has run, that test will 586# fail and the query will abort but we will have already output some 587# results. When the query is retried, the results will be repeated. 588# 589ifcapable compound { 590do_test attach-4.8 { 591 execsql { 592 ATTACH DATABASE 'test2.db' AS db2; 593 INSERT INTO db2.t3 VALUES(13,14); 594 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4; 595 } 596} {db2.6 db2.13 main.11} 597 598do_test attach-4.9 { 599 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}} 600 execsql { 601 INSERT INTO main.t3 VALUES(15,16); 602 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4; 603 } 604} {db2.6 db2.13 main.11 main.15} 605} ;# ifcapable compound 606 607ifcapable !compound { 608 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}} 609 execsql { 610 ATTACH DATABASE 'test2.db' AS db2; 611 INSERT INTO db2.t3 VALUES(13,14); 612 INSERT INTO main.t3 VALUES(15,16); 613 } 614} ;# ifcapable !compound 615 616ifcapable view { 617do_test attach-4.10 { 618 execsql { 619 DETACH DATABASE db2; 620 } 621 execsql { 622 CREATE VIEW v3 AS SELECT x*100+y FROM t3; 623 SELECT * FROM v3; 624 } db2 625} {102 910 607 1314} 626do_test attach-4.11 { 627 execsql { 628 CREATE VIEW v3 AS SELECT a*100+b FROM t3; 629 SELECT * FROM v3; 630 } 631} {910 1112 1516} 632do_test attach-4.12 { 633 execsql { 634 ATTACH DATABASE 'test2.db' AS db2; 635 SELECT * FROM db2.v3; 636 } 637} {102 910 607 1314} 638do_test attach-4.13 { 639 execsql { 640 SELECT * FROM main.v3; 641 } 642} {910 1112 1516} 643} ;# ifcapable view 644 645# Tests for the sqliteFix...() routines in attach.c 646# 647ifcapable {trigger} { 648do_test attach-5.1 { 649 db close 650 sqlite3 db test.db 651 db2 close 652 forcedelete test2.db 653 sqlite3 db2 test2.db 654 catchsql { 655 ATTACH DATABASE 'test.db' AS orig; 656 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN 657 SELECT 'no-op'; 658 END; 659 } db2 660} {1 {trigger r1 cannot reference objects in database orig}} 661do_test attach-5.2 { 662 catchsql { 663 CREATE TABLE t5(x,y); 664 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 665 SELECT 'no-op'; 666 END; 667 } db2 668} {0 {}} 669do_test attach-5.3 { 670 catchsql { 671 DROP TRIGGER r5; 672 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 673 SELECT 'no-op' FROM orig.t1; 674 END; 675 } db2 676} {1 {trigger r5 cannot reference objects in database orig}} 677ifcapable tempdb { 678 do_test attach-5.4 { 679 catchsql { 680 CREATE TEMP TABLE t6(p,q,r); 681 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 682 SELECT 'no-op' FROM temp.t6; 683 END; 684 } db2 685 } {1 {trigger r5 cannot reference objects in database temp}} 686} 687ifcapable subquery { 688 do_test attach-5.5 { 689 catchsql { 690 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 691 SELECT 'no-op' || (SELECT * FROM temp.t6); 692 END; 693 } db2 694 } {1 {trigger r5 cannot reference objects in database temp}} 695 do_test attach-5.6 { 696 catchsql { 697 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 698 SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6); 699 END; 700 } db2 701 } {1 {trigger r5 cannot reference objects in database temp}} 702 do_test attach-5.7 { 703 catchsql { 704 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 705 SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6); 706 END; 707 } db2 708 } {1 {trigger r5 cannot reference objects in database temp}} 709 do_test attach-5.7 { 710 catchsql { 711 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 712 SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1; 713 END; 714 } db2 715 } {1 {trigger r5 cannot reference objects in database temp}} 716 do_test attach-5.8 { 717 catchsql { 718 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 719 INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5); 720 END; 721 } db2 722 } {1 {trigger r5 cannot reference objects in database temp}} 723 do_test attach-5.9 { 724 catchsql { 725 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN 726 DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6); 727 END; 728 } db2 729 } {1 {trigger r5 cannot reference objects in database temp}} 730} ;# endif subquery 731} ;# endif trigger 732 733# Check to make sure we get a sensible error if unable to open 734# the file that we are trying to attach. 735# 736do_test attach-6.1 { 737 catchsql { 738 ATTACH DATABASE 'no-such-file' AS nosuch; 739 } 740} {0 {}} 741if {$tcl_platform(platform)=="unix"} { 742 do_test attach-6.2 { 743 sqlite3 dbx cannot-read 744 dbx eval {CREATE TABLE t1(a,b,c)} 745 dbx close 746 file attributes cannot-read -permission 0000 747 if {[file writable cannot-read]} { 748 puts "\n**** Tests do not work when run as root ****" 749 forcedelete cannot-read 750 exit 1 751 } 752 catchsql { 753 ATTACH DATABASE 'cannot-read' AS noread; 754 } 755 } {1 {unable to open database: cannot-read}} 756 do_test attach-6.2.2 { 757 db errorcode 758 } {14} 759 forcedelete cannot-read 760} 761 762# Check the error message if we try to access a database that has 763# not been attached. 764do_test attach-6.3 { 765 catchsql { 766 CREATE TABLE no_such_db.t1(a, b, c); 767 } 768} {1 {unknown database no_such_db}} 769for {set i 2} {$i<=15} {incr i} { 770 catch {db$i close} 771} 772db close 773forcedelete test2.db 774forcedelete no-such-file 775 776ifcapable subquery { 777 do_test attach-7.1 { 778 forcedelete test.db test.db-journal 779 sqlite3 db test.db 780 catchsql { 781 DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY 782 REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL ) 783 } 784 } {1 {no such table: AAAAAA}} 785} 786 787# Create a malformed file (a file that is not a valid database) 788# and try to attach it 789# 790do_test attach-8.1 { 791 set fd [open test2.db w] 792 puts $fd "This file is not a valid SQLite database" 793 close $fd 794 catchsql { 795 ATTACH 'test2.db' AS t2; 796 } 797} {1 {file is encrypted or is not a database}} 798do_test attach-8.2 { 799 db errorcode 800} {26} 801forcedelete test2.db 802do_test attach-8.3 { 803 sqlite3 db2 test2.db 804 db2 eval {CREATE TABLE t1(x); BEGIN EXCLUSIVE} 805 catchsql { 806 ATTACH 'test2.db' AS t2; 807 } 808} {1 {database is locked}} 809do_test attach-8.4 { 810 db errorcode 811} {5} 812db2 close 813forcedelete test2.db 814 815# Test that it is possible to attach the same database more than 816# once when not in shared-cache mode. That this is not possible in 817# shared-cache mode is tested in shared7.test. 818do_test attach-9.1 { 819 forcedelete test4.db 820 execsql { 821 ATTACH 'test4.db' AS aux1; 822 CREATE TABLE aux1.t1(a, b); 823 INSERT INTO aux1.t1 VALUES(1, 2); 824 ATTACH 'test4.db' AS aux2; 825 SELECT * FROM aux2.t1; 826 } 827} {1 2} 828do_test attach-9.2 { 829 catchsql { 830 BEGIN; 831 INSERT INTO aux1.t1 VALUES(3, 4); 832 INSERT INTO aux2.t1 VALUES(5, 6); 833 } 834} {1 {database is locked}} 835do_test attach-9.3 { 836 execsql { 837 COMMIT; 838 SELECT * FROM aux2.t1; 839 } 840} {1 2 3 4} 841 842# Ticket [abe728bbc311d81334dae9762f0db87c07a98f79]. 843# Multi-database commit on an attached TEMP database. 844# 845do_test attach-10.1 { 846 execsql { 847 ATTACH '' AS noname; 848 ATTACH ':memory:' AS inmem; 849 BEGIN; 850 CREATE TABLE noname.noname(x); 851 CREATE TABLE inmem.inmem(y); 852 CREATE TABLE main.main(z); 853 COMMIT; 854 SELECT name FROM noname.sqlite_master; 855 SELECT name FROM inmem.sqlite_master; 856 } 857} {noname inmem} 858do_test attach-10.2 { 859 lrange [execsql { 860 PRAGMA database_list; 861 }] 9 end 862} {4 noname {} 5 inmem {}} 863 864finish_test 865