PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/table.test

https://bitbucket.org/aware/sqlite
Unknown | 728 lines | 688 code | 40 blank | 0 comment | 0 complexity | b54172a62703c43a57d64caf1748c1fd MD5 | raw file
  1. # 2001 September 15
  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 file is testing the CREATE TABLE statement.
  13. #
  14. # $Id:$
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create a basic table and verify it is added to sqlite_master
  18. #
  19. do_test table-1.1 {
  20. execsql {
  21. CREATE TABLE test1 (
  22. one varchar(10),
  23. two text
  24. )
  25. }
  26. execsql {
  27. SELECT sql FROM sqlite_master WHERE type!='meta'
  28. }
  29. } {{CREATE TABLE test1 (
  30. one varchar(10),
  31. two text
  32. )}}
  33. # Verify the other fields of the sqlite_master file.
  34. #
  35. do_test table-1.3 {
  36. execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
  37. } {test1 test1 table}
  38. # Close and reopen the database. Verify that everything is
  39. # still the same.
  40. #
  41. do_test table-1.4 {
  42. db close
  43. sqlite3 db test.db
  44. execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
  45. } {test1 test1 table}
  46. # Drop the database and make sure it disappears.
  47. #
  48. do_test table-1.5 {
  49. execsql {DROP TABLE test1}
  50. execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
  51. } {}
  52. # Close and reopen the database. Verify that the table is
  53. # still gone.
  54. #
  55. do_test table-1.6 {
  56. db close
  57. sqlite3 db test.db
  58. execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
  59. } {}
  60. # Repeat the above steps, but this time quote the table name.
  61. #
  62. do_test table-1.10 {
  63. execsql {CREATE TABLE "create" (f1 int)}
  64. execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
  65. } {create}
  66. do_test table-1.11 {
  67. execsql {DROP TABLE "create"}
  68. execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
  69. } {}
  70. do_test table-1.12 {
  71. execsql {CREATE TABLE test1("f1 ho" int)}
  72. execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
  73. } {test1}
  74. do_test table-1.13 {
  75. execsql {DROP TABLE "TEST1"}
  76. execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
  77. } {}
  78. # Verify that we cannot make two tables with the same name
  79. #
  80. do_test table-2.1 {
  81. execsql {CREATE TABLE TEST2(one text)}
  82. catchsql {CREATE TABLE test2(two text default 'hi')}
  83. } {1 {table test2 already exists}}
  84. do_test table-2.1.1 {
  85. catchsql {CREATE TABLE "test2" (two)}
  86. } {1 {table "test2" already exists}}
  87. do_test table-2.1b {
  88. set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
  89. lappend v $msg
  90. } {1 {object name reserved for internal use: sqlite_master}}
  91. do_test table-2.1c {
  92. db close
  93. sqlite3 db test.db
  94. set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
  95. lappend v $msg
  96. } {1 {object name reserved for internal use: sqlite_master}}
  97. do_test table-2.1d {
  98. catchsql {CREATE TABLE IF NOT EXISTS test2(x,y)}
  99. } {0 {}}
  100. do_test table-2.1e {
  101. catchsql {CREATE TABLE IF NOT EXISTS test2(x UNIQUE, y TEXT PRIMARY KEY)}
  102. } {0 {}}
  103. do_test table-2.1f {
  104. execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
  105. } {}
  106. # Verify that we cannot make a table with the same name as an index
  107. #
  108. do_test table-2.2a {
  109. execsql {CREATE TABLE test2(one text)}
  110. execsql {CREATE INDEX test3 ON test2(one)}
  111. catchsql {CREATE TABLE test3(two text)}
  112. } {1 {there is already an index named test3}}
  113. do_test table-2.2b {
  114. db close
  115. sqlite3 db test.db
  116. set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
  117. lappend v $msg
  118. } {1 {there is already an index named test3}}
  119. do_test table-2.2c {
  120. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  121. } {test2 test3}
  122. do_test table-2.2d {
  123. execsql {DROP INDEX test3}
  124. set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
  125. lappend v $msg
  126. } {0 {}}
  127. do_test table-2.2e {
  128. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  129. } {test2 test3}
  130. do_test table-2.2f {
  131. execsql {DROP TABLE test2; DROP TABLE test3}
  132. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  133. } {}
  134. # Create a table with many field names
  135. #
  136. set big_table \
  137. {CREATE TABLE big(
  138. f1 varchar(20),
  139. f2 char(10),
  140. f3 varchar(30) primary key,
  141. f4 text,
  142. f5 text,
  143. f6 text,
  144. f7 text,
  145. f8 text,
  146. f9 text,
  147. f10 text,
  148. f11 text,
  149. f12 text,
  150. f13 text,
  151. f14 text,
  152. f15 text,
  153. f16 text,
  154. f17 text,
  155. f18 text,
  156. f19 text,
  157. f20 text
  158. )}
  159. do_test table-3.1 {
  160. execsql $big_table
  161. execsql {SELECT sql FROM sqlite_master WHERE type=='table'}
  162. } \{$big_table\}
  163. do_test table-3.2 {
  164. set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
  165. lappend v $msg
  166. } {1 {table BIG already exists}}
  167. do_test table-3.3 {
  168. set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
  169. lappend v $msg
  170. } {1 {table biG already exists}}
  171. do_test table-3.4 {
  172. set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
  173. lappend v $msg
  174. } {1 {table bIg already exists}}
  175. do_test table-3.5 {
  176. db close
  177. sqlite3 db test.db
  178. set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
  179. lappend v $msg
  180. } {1 {table Big already exists}}
  181. do_test table-3.6 {
  182. execsql {DROP TABLE big}
  183. execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
  184. } {}
  185. # Try creating large numbers of tables
  186. #
  187. set r {}
  188. for {set i 1} {$i<=100} {incr i} {
  189. lappend r [format test%03d $i]
  190. }
  191. do_test table-4.1 {
  192. for {set i 1} {$i<=100} {incr i} {
  193. set sql "CREATE TABLE [format test%03d $i] ("
  194. for {set k 1} {$k<$i} {incr k} {
  195. append sql "field$k text,"
  196. }
  197. append sql "last_field text)"
  198. execsql $sql
  199. }
  200. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  201. } $r
  202. do_test table-4.1b {
  203. db close
  204. sqlite3 db test.db
  205. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  206. } $r
  207. # Drop the even numbered tables
  208. #
  209. set r {}
  210. for {set i 1} {$i<=100} {incr i 2} {
  211. lappend r [format test%03d $i]
  212. }
  213. do_test table-4.2 {
  214. for {set i 2} {$i<=100} {incr i 2} {
  215. # if {$i==38} {execsql {pragma vdbe_trace=on}}
  216. set sql "DROP TABLE [format TEST%03d $i]"
  217. execsql $sql
  218. }
  219. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  220. } $r
  221. #exit
  222. # Drop the odd number tables
  223. #
  224. do_test table-4.3 {
  225. for {set i 1} {$i<=100} {incr i 2} {
  226. set sql "DROP TABLE [format test%03d $i]"
  227. execsql $sql
  228. }
  229. execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
  230. } {}
  231. # Try to drop a table that does not exist
  232. #
  233. do_test table-5.1.1 {
  234. catchsql {DROP TABLE test009}
  235. } {1 {no such table: test009}}
  236. do_test table-5.1.2 {
  237. catchsql {DROP TABLE IF EXISTS test009}
  238. } {0 {}}
  239. # Try to drop sqlite_master
  240. #
  241. do_test table-5.2 {
  242. catchsql {DROP TABLE IF EXISTS sqlite_master}
  243. } {1 {table sqlite_master may not be dropped}}
  244. # Dropping sqlite_statN tables is OK.
  245. #
  246. do_test table-5.2.1 {
  247. db eval {
  248. ANALYZE;
  249. DROP TABLE IF EXISTS sqlite_stat1;
  250. DROP TABLE IF EXISTS sqlite_stat2;
  251. DROP TABLE IF EXISTS sqlite_stat3;
  252. SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_stat*';
  253. }
  254. } {}
  255. # Make sure an EXPLAIN does not really create a new table
  256. #
  257. do_test table-5.3 {
  258. ifcapable {explain} {
  259. execsql {EXPLAIN CREATE TABLE test1(f1 int)}
  260. }
  261. execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
  262. } {}
  263. # Make sure an EXPLAIN does not really drop an existing table
  264. #
  265. do_test table-5.4 {
  266. execsql {CREATE TABLE test1(f1 int)}
  267. ifcapable {explain} {
  268. execsql {EXPLAIN DROP TABLE test1}
  269. }
  270. execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
  271. } {test1}
  272. # Create a table with a goofy name
  273. #
  274. #do_test table-6.1 {
  275. # execsql {CREATE TABLE 'Spaces In This Name!'(x int)}
  276. # execsql {INSERT INTO 'spaces in this name!' VALUES(1)}
  277. # set list [glob -nocomplain testdb/spaces*.tbl]
  278. #} {testdb/spaces+in+this+name+.tbl}
  279. # Try using keywords as table names or column names.
  280. #
  281. do_test table-7.1 {
  282. set v [catch {execsql {
  283. CREATE TABLE weird(
  284. desc text,
  285. asc text,
  286. key int,
  287. [14_vac] boolean,
  288. fuzzy_dog_12 varchar(10),
  289. begin blob,
  290. end clob
  291. )
  292. }} msg]
  293. lappend v $msg
  294. } {0 {}}
  295. do_test table-7.2 {
  296. execsql {
  297. INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
  298. SELECT * FROM weird;
  299. }
  300. } {a b 9 0 xyz hi y'all}
  301. do_test table-7.3 {
  302. execsql2 {
  303. SELECT * FROM weird;
  304. }
  305. } {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
  306. do_test table-7.3 {
  307. execsql {
  308. CREATE TABLE savepoint(release);
  309. INSERT INTO savepoint(release) VALUES(10);
  310. UPDATE savepoint SET release = 5;
  311. SELECT release FROM savepoint;
  312. }
  313. } {5}
  314. # Try out the CREATE TABLE AS syntax
  315. #
  316. do_test table-8.1 {
  317. execsql2 {
  318. CREATE TABLE t2 AS SELECT * FROM weird;
  319. SELECT * FROM t2;
  320. }
  321. } {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
  322. do_test table-8.1.1 {
  323. execsql {
  324. SELECT sql FROM sqlite_master WHERE name='t2';
  325. }
  326. } {{CREATE TABLE t2(
  327. "desc" TEXT,
  328. "asc" TEXT,
  329. "key" INT,
  330. "14_vac" NUM,
  331. fuzzy_dog_12 TEXT,
  332. "begin",
  333. "end" TEXT
  334. )}}
  335. do_test table-8.2 {
  336. execsql {
  337. CREATE TABLE "t3""xyz"(a,b,c);
  338. INSERT INTO [t3"xyz] VALUES(1,2,3);
  339. SELECT * FROM [t3"xyz];
  340. }
  341. } {1 2 3}
  342. do_test table-8.3 {
  343. execsql2 {
  344. CREATE TABLE [t4"abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3"xyz];
  345. SELECT * FROM [t4"abc];
  346. }
  347. } {cnt 1 max(b+c) 5}
  348. # Update for v3: The declaration type of anything except a column is now a
  349. # NULL pointer, so the created table has no column types. (Changed result
  350. # from {{CREATE TABLE 't4"abc'(cnt NUMERIC,"max(b+c)" NUMERIC)}}).
  351. do_test table-8.3.1 {
  352. execsql {
  353. SELECT sql FROM sqlite_master WHERE name='t4"abc'
  354. }
  355. } {{CREATE TABLE "t4""abc"(cnt,"max(b+c)")}}
  356. ifcapable tempdb {
  357. do_test table-8.4 {
  358. execsql2 {
  359. CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3"xyz];
  360. SELECT * FROM t5;
  361. }
  362. } {y'all 1}
  363. }
  364. do_test table-8.5 {
  365. db close
  366. sqlite3 db test.db
  367. execsql2 {
  368. SELECT * FROM [t4"abc];
  369. }
  370. } {cnt 1 max(b+c) 5}
  371. do_test table-8.6 {
  372. execsql2 {
  373. SELECT * FROM t2;
  374. }
  375. } {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
  376. do_test table-8.7 {
  377. catchsql {
  378. SELECT * FROM t5;
  379. }
  380. } {1 {no such table: t5}}
  381. do_test table-8.8 {
  382. catchsql {
  383. CREATE TABLE t5 AS SELECT * FROM no_such_table;
  384. }
  385. } {1 {no such table: no_such_table}}
  386. do_test table-8.9 {
  387. execsql {
  388. CREATE TABLE t10("col.1" [char.3]);
  389. CREATE TABLE t11 AS SELECT * FROM t10;
  390. SELECT sql FROM sqlite_master WHERE name = 't11';
  391. }
  392. } {{CREATE TABLE t11("col.1" TEXT)}}
  393. do_test table-8.10 {
  394. execsql {
  395. CREATE TABLE t12(
  396. a INTEGER,
  397. b VARCHAR(10),
  398. c VARCHAR(1,10),
  399. d VARCHAR(+1,-10),
  400. e VARCHAR (+1,-10),
  401. f "VARCHAR (+1,-10, 5)",
  402. g BIG INTEGER
  403. );
  404. CREATE TABLE t13 AS SELECT * FROM t12;
  405. SELECT sql FROM sqlite_master WHERE name = 't13';
  406. }
  407. } {{CREATE TABLE t13(
  408. a INT,
  409. b TEXT,
  410. c TEXT,
  411. d TEXT,
  412. e TEXT,
  413. f TEXT,
  414. g INT
  415. )}}
  416. # Make sure we cannot have duplicate column names within a table.
  417. #
  418. do_test table-9.1 {
  419. catchsql {
  420. CREATE TABLE t6(a,b,a);
  421. }
  422. } {1 {duplicate column name: a}}
  423. do_test table-9.2 {
  424. catchsql {
  425. CREATE TABLE t6(a varchar(100), b blob, a integer);
  426. }
  427. } {1 {duplicate column name: a}}
  428. # Check the foreign key syntax.
  429. #
  430. ifcapable {foreignkey} {
  431. do_test table-10.1 {
  432. catchsql {
  433. CREATE TABLE t6(a REFERENCES t4(a) NOT NULL);
  434. INSERT INTO t6 VALUES(NULL);
  435. }
  436. } {1 {t6.a may not be NULL}}
  437. do_test table-10.2 {
  438. catchsql {
  439. DROP TABLE t6;
  440. CREATE TABLE t6(a REFERENCES t4(a) MATCH PARTIAL);
  441. }
  442. } {0 {}}
  443. do_test table-10.3 {
  444. catchsql {
  445. DROP TABLE t6;
  446. CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON DELETE SET NULL NOT NULL);
  447. }
  448. } {0 {}}
  449. do_test table-10.4 {
  450. catchsql {
  451. DROP TABLE t6;
  452. CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON UPDATE SET DEFAULT DEFAULT 1);
  453. }
  454. } {0 {}}
  455. do_test table-10.5 {
  456. catchsql {
  457. DROP TABLE t6;
  458. CREATE TABLE t6(a NOT NULL NOT DEFERRABLE INITIALLY IMMEDIATE);
  459. }
  460. } {0 {}}
  461. do_test table-10.6 {
  462. catchsql {
  463. DROP TABLE t6;
  464. CREATE TABLE t6(a NOT NULL DEFERRABLE INITIALLY DEFERRED);
  465. }
  466. } {0 {}}
  467. do_test table-10.7 {
  468. catchsql {
  469. DROP TABLE t6;
  470. CREATE TABLE t6(a,
  471. FOREIGN KEY (a) REFERENCES t4(b) DEFERRABLE INITIALLY DEFERRED
  472. );
  473. }
  474. } {0 {}}
  475. do_test table-10.8 {
  476. catchsql {
  477. DROP TABLE t6;
  478. CREATE TABLE t6(a,b,c,
  479. FOREIGN KEY (b,c) REFERENCES t4(x,y) MATCH PARTIAL
  480. ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  481. );
  482. }
  483. } {0 {}}
  484. do_test table-10.9 {
  485. catchsql {
  486. DROP TABLE t6;
  487. CREATE TABLE t6(a,b,c,
  488. FOREIGN KEY (b,c) REFERENCES t4(x)
  489. );
  490. }
  491. } {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
  492. do_test table-10.10 {
  493. catchsql {DROP TABLE t6}
  494. catchsql {
  495. CREATE TABLE t6(a,b,c,
  496. FOREIGN KEY (b,c) REFERENCES t4(x,y,z)
  497. );
  498. }
  499. } {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
  500. do_test table-10.11 {
  501. catchsql {DROP TABLE t6}
  502. catchsql {
  503. CREATE TABLE t6(a,b, c REFERENCES t4(x,y));
  504. }
  505. } {1 {foreign key on c should reference only one column of table t4}}
  506. do_test table-10.12 {
  507. catchsql {DROP TABLE t6}
  508. catchsql {
  509. CREATE TABLE t6(a,b,c,
  510. FOREIGN KEY (b,x) REFERENCES t4(x,y)
  511. );
  512. }
  513. } {1 {unknown column "x" in foreign key definition}}
  514. do_test table-10.13 {
  515. catchsql {DROP TABLE t6}
  516. catchsql {
  517. CREATE TABLE t6(a,b,c,
  518. FOREIGN KEY (x,b) REFERENCES t4(x,y)
  519. );
  520. }
  521. } {1 {unknown column "x" in foreign key definition}}
  522. } ;# endif foreignkey
  523. # Test for the "typeof" function. More tests for the
  524. # typeof() function are found in bind.test and types.test.
  525. #
  526. do_test table-11.1 {
  527. execsql {
  528. CREATE TABLE t7(
  529. a integer primary key,
  530. b number(5,10),
  531. c character varying (8),
  532. d VARCHAR(9),
  533. e clob,
  534. f BLOB,
  535. g Text,
  536. h
  537. );
  538. INSERT INTO t7(a) VALUES(1);
  539. SELECT typeof(a), typeof(b), typeof(c), typeof(d),
  540. typeof(e), typeof(f), typeof(g), typeof(h)
  541. FROM t7 LIMIT 1;
  542. }
  543. } {integer null null null null null null null}
  544. do_test table-11.2 {
  545. execsql {
  546. SELECT typeof(a+b), typeof(a||b), typeof(c+d), typeof(c||d)
  547. FROM t7 LIMIT 1;
  548. }
  549. } {null null null null}
  550. # Test that when creating a table using CREATE TABLE AS, column types are
  551. # assigned correctly for (SELECT ...) and 'x AS y' expressions.
  552. do_test table-12.1 {
  553. ifcapable subquery {
  554. execsql {
  555. CREATE TABLE t8 AS SELECT b, h, a as i, (SELECT f FROM t7) as j FROM t7;
  556. }
  557. } else {
  558. execsql {
  559. CREATE TABLE t8 AS SELECT b, h, a as i, f as j FROM t7;
  560. }
  561. }
  562. } {}
  563. do_test table-12.2 {
  564. execsql {
  565. SELECT sql FROM sqlite_master WHERE tbl_name = 't8'
  566. }
  567. } {{CREATE TABLE t8(b NUM,h,i INT,j)}}
  568. #--------------------------------------------------------------------
  569. # Test cases table-13.*
  570. #
  571. # Test the ability to have default values of CURRENT_TIME, CURRENT_DATE
  572. # and CURRENT_TIMESTAMP.
  573. #
  574. do_test table-13.1 {
  575. execsql {
  576. CREATE TABLE tablet8(
  577. a integer primary key,
  578. tm text DEFAULT CURRENT_TIME,
  579. dt text DEFAULT CURRENT_DATE,
  580. dttm text DEFAULT CURRENT_TIMESTAMP
  581. );
  582. SELECT * FROM tablet8;
  583. }
  584. } {}
  585. set i 0
  586. unset -nocomplain date time seconds
  587. foreach {date time seconds} {
  588. 1976-07-04 12:00:00 205329600
  589. 1994-04-16 14:00:00 766504800
  590. 2000-01-01 00:00:00 946684800
  591. 2003-12-31 12:34:56 1072874096
  592. } {
  593. incr i
  594. set sqlite_current_time $seconds
  595. do_test table-13.2.$i {
  596. execsql "
  597. INSERT INTO tablet8(a) VALUES($i);
  598. SELECT tm, dt, dttm FROM tablet8 WHERE a=$i;
  599. "
  600. } [list $time $date [list $date $time]]
  601. }
  602. set sqlite_current_time 0
  603. #--------------------------------------------------------------------
  604. # Test cases table-14.*
  605. #
  606. # Test that a table cannot be created or dropped while other virtual
  607. # machines are active. This is required because otherwise when in
  608. # auto-vacuum mode the btree-layer may need to move the root-pages of
  609. # a table for which there is an open cursor.
  610. #
  611. # 2007-05-02: A open btree cursor no longer blocks CREATE TABLE.
  612. # But DROP TABLE is still prohibited because we do not want to
  613. # delete a table out from under a running query.
  614. #
  615. # db eval {
  616. # pragma vdbe_trace = 0;
  617. # }
  618. # Try to create a table from within a callback:
  619. unset -nocomplain result
  620. do_test table-14.1 {
  621. set rc [
  622. catch {
  623. db eval {SELECT * FROM tablet8 LIMIT 1} {} {
  624. db eval {CREATE TABLE t9(a, b, c)}
  625. }
  626. } msg
  627. ]
  628. set result [list $rc $msg]
  629. } {0 {}}
  630. # Try to drop a table from within a callback:
  631. do_test table-14.2 {
  632. set rc [
  633. catch {
  634. db eval {SELECT * FROM tablet8 LIMIT 1} {} {
  635. db eval {DROP TABLE t9;}
  636. }
  637. } msg
  638. ]
  639. set result [list $rc $msg]
  640. } {1 {database table is locked}}
  641. ifcapable attach {
  642. # Now attach a database and ensure that a table can be created in the
  643. # attached database whilst in a callback from a query on the main database.
  644. do_test table-14.3 {
  645. forcedelete test2.db
  646. forcedelete test2.db-journal
  647. execsql {
  648. ATTACH 'test2.db' as aux;
  649. }
  650. db eval {SELECT * FROM tablet8 LIMIT 1} {} {
  651. db eval {CREATE TABLE aux.t1(a, b, c)}
  652. }
  653. } {}
  654. # On the other hand, it should be impossible to drop a table when any VMs
  655. # are active. This is because VerifyCookie instructions may have already
  656. # been executed, and btree root-pages may not move after this (which a
  657. # delete table might do).
  658. do_test table-14.4 {
  659. set rc [
  660. catch {
  661. db eval {SELECT * FROM tablet8 LIMIT 1} {} {
  662. db eval {DROP TABLE aux.t1;}
  663. }
  664. } msg
  665. ]
  666. set result [list $rc $msg]
  667. } {1 {database table is locked}}
  668. }
  669. # Create and drop 2000 tables. This is to check that the balance_shallow()
  670. # routine works correctly on the sqlite_master table. At one point it
  671. # contained a bug that would prevent the right-child pointer of the
  672. # child page from being copied to the root page.
  673. #
  674. do_test table-15.1 {
  675. execsql {BEGIN}
  676. for {set i 0} {$i<2000} {incr i} {
  677. execsql "CREATE TABLE tbl$i (a, b, c)"
  678. }
  679. execsql {COMMIT}
  680. } {}
  681. do_test table-15.2 {
  682. execsql {BEGIN}
  683. for {set i 0} {$i<2000} {incr i} {
  684. execsql "DROP TABLE tbl$i"
  685. }
  686. execsql {COMMIT}
  687. } {}
  688. finish_test