pragma.test 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. # 2002 March 6
  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.
  12. #
  13. # This file implements tests for the PRAGMA command.
  14. #
  15. # $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. set testprefix pragma
  19. # Do not use a codec for tests in this file, as the database file is
  20. # manipulated directly using tcl scripts (using the [hexio_write] command).
  21. #
  22. do_not_use_codec
  23. # Test organization:
  24. #
  25. # pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
  26. # pragma-2.*: Test synchronous on attached db.
  27. # pragma-3.*: Test detection of table/index inconsistency by integrity_check.
  28. # pragma-4.*: Test cache_size and default_cache_size on attached db.
  29. # pragma-5.*: Test that pragma synchronous may not be used inside of a
  30. # transaction.
  31. # pragma-6.*: Test schema-query pragmas.
  32. # pragma-7.*: Miscellaneous tests.
  33. # pragma-8.*: Test user_version and schema_version pragmas.
  34. # pragma-9.*: Test temp_store and temp_store_directory.
  35. # pragma-10.*: Test the count_changes pragma in the presence of triggers.
  36. # pragma-11.*: Test the collation_list pragma.
  37. # pragma-14.*: Test the page_count pragma.
  38. # pragma-15.*: Test that the value set using the cache_size pragma is not
  39. # reset when the schema is reloaded.
  40. # pragma-16.*: Test proxy locking
  41. # pragma-20.*: Test data_store_directory.
  42. # pragma-22.*: Test that "PRAGMA [db].integrity_check" respects the "db"
  43. # directive - if it is present.
  44. #
  45. ifcapable !pragma {
  46. finish_test
  47. return
  48. }
  49. # Delete the preexisting database to avoid the special setup
  50. # that the "all.test" script does.
  51. #
  52. db close
  53. delete_file test.db test.db-journal
  54. delete_file test3.db test3.db-journal
  55. sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
  56. ifcapable pager_pragmas {
  57. set DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
  58. set TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
  59. do_test pragma-1.1 {
  60. execsql {
  61. PRAGMA cache_size;
  62. PRAGMA default_cache_size;
  63. PRAGMA synchronous;
  64. }
  65. } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
  66. do_test pragma-1.2 {
  67. execsql {
  68. PRAGMA synchronous=OFF;
  69. PRAGMA cache_size=1234;
  70. PRAGMA cache_size;
  71. PRAGMA default_cache_size;
  72. PRAGMA synchronous;
  73. }
  74. } [list 1234 $DFLT_CACHE_SZ 0]
  75. do_test pragma-1.3 {
  76. db close
  77. sqlite3 db test.db
  78. execsql {
  79. PRAGMA cache_size;
  80. PRAGMA default_cache_size;
  81. PRAGMA synchronous;
  82. }
  83. } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
  84. do_test pragma-1.4 {
  85. execsql {
  86. PRAGMA synchronous=OFF;
  87. PRAGMA cache_size;
  88. PRAGMA default_cache_size;
  89. PRAGMA synchronous;
  90. }
  91. } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
  92. do_test pragma-1.5 {
  93. execsql {
  94. PRAGMA cache_size=-4321;
  95. PRAGMA cache_size;
  96. PRAGMA default_cache_size;
  97. PRAGMA synchronous;
  98. }
  99. } [list -4321 $DFLT_CACHE_SZ 0]
  100. do_test pragma-1.6 {
  101. execsql {
  102. PRAGMA synchronous=ON;
  103. PRAGMA cache_size;
  104. PRAGMA default_cache_size;
  105. PRAGMA synchronous;
  106. }
  107. } [list -4321 $DFLT_CACHE_SZ 1]
  108. do_test pragma-1.7 {
  109. db close
  110. sqlite3 db test.db
  111. execsql {
  112. PRAGMA cache_size;
  113. PRAGMA default_cache_size;
  114. PRAGMA synchronous;
  115. }
  116. } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
  117. do_test pragma-1.8 {
  118. execsql {
  119. PRAGMA default_cache_size=-123;
  120. PRAGMA cache_size;
  121. PRAGMA default_cache_size;
  122. PRAGMA synchronous;
  123. }
  124. } {123 123 2}
  125. do_test pragma-1.9.1 {
  126. db close
  127. sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
  128. execsql {
  129. PRAGMA cache_size;
  130. PRAGMA default_cache_size;
  131. PRAGMA synchronous;
  132. }
  133. } {123 123 2}
  134. ifcapable vacuum {
  135. do_test pragma-1.9.2 {
  136. execsql {
  137. VACUUM;
  138. PRAGMA cache_size;
  139. PRAGMA default_cache_size;
  140. PRAGMA synchronous;
  141. }
  142. } {123 123 2}
  143. }
  144. do_test pragma-1.10 {
  145. execsql {
  146. PRAGMA synchronous=NORMAL;
  147. PRAGMA cache_size;
  148. PRAGMA default_cache_size;
  149. PRAGMA synchronous;
  150. }
  151. } {123 123 1}
  152. do_test pragma-1.11 {
  153. execsql {
  154. PRAGMA synchronous=FULL;
  155. PRAGMA cache_size;
  156. PRAGMA default_cache_size;
  157. PRAGMA synchronous;
  158. }
  159. } {123 123 2}
  160. do_test pragma-1.12 {
  161. db close
  162. sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
  163. execsql {
  164. PRAGMA cache_size;
  165. PRAGMA default_cache_size;
  166. PRAGMA synchronous;
  167. }
  168. } {123 123 2}
  169. # Make sure the pragma handler understands numeric values in addition
  170. # to keywords like "off" and "full".
  171. #
  172. do_test pragma-1.13 {
  173. execsql {
  174. PRAGMA synchronous=0;
  175. PRAGMA synchronous;
  176. }
  177. } {0}
  178. do_test pragma-1.14 {
  179. execsql {
  180. PRAGMA synchronous=2;
  181. PRAGMA synchronous;
  182. }
  183. } {2}
  184. } ;# ifcapable pager_pragmas
  185. # Test turning "flag" pragmas on and off.
  186. #
  187. ifcapable debug {
  188. # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
  189. #
  190. do_test pragma-1.15 {
  191. execsql {
  192. PRAGMA vdbe_listing=YES;
  193. PRAGMA vdbe_listing;
  194. }
  195. } {1}
  196. do_test pragma-1.16 {
  197. execsql {
  198. PRAGMA vdbe_listing=NO;
  199. PRAGMA vdbe_listing;
  200. }
  201. } {0}
  202. }
  203. do_test pragma-1.17 {
  204. execsql {
  205. PRAGMA parser_trace=ON;
  206. PRAGMA parser_trace=OFF;
  207. }
  208. } {}
  209. do_test pragma-1.18 {
  210. execsql {
  211. PRAGMA bogus = -1234; -- Parsing of negative values
  212. }
  213. } {}
  214. # Test modifying the safety_level of an attached database.
  215. ifcapable pager_pragmas&&attach {
  216. do_test pragma-2.1 {
  217. forcedelete test2.db
  218. forcedelete test2.db-journal
  219. execsql {
  220. ATTACH 'test2.db' AS aux;
  221. }
  222. } {}
  223. do_test pragma-2.2 {
  224. execsql {
  225. pragma aux.synchronous;
  226. }
  227. } {2}
  228. do_test pragma-2.3 {
  229. execsql {
  230. pragma aux.synchronous = OFF;
  231. pragma aux.synchronous;
  232. pragma synchronous;
  233. }
  234. } {0 2}
  235. do_test pragma-2.4 {
  236. execsql {
  237. pragma aux.synchronous = ON;
  238. pragma synchronous;
  239. pragma aux.synchronous;
  240. }
  241. } {2 1}
  242. } ;# ifcapable pager_pragmas
  243. # Construct a corrupted index and make sure the integrity_check
  244. # pragma finds it.
  245. #
  246. # These tests won't work if the database is encrypted
  247. #
  248. do_test pragma-3.1 {
  249. db close
  250. forcedelete test.db test.db-journal
  251. sqlite3 db test.db
  252. execsql {
  253. PRAGMA auto_vacuum=OFF;
  254. BEGIN;
  255. CREATE TABLE t2(a,b,c);
  256. CREATE INDEX i2 ON t2(a);
  257. INSERT INTO t2 VALUES(11,2,3);
  258. INSERT INTO t2 VALUES(22,3,4);
  259. COMMIT;
  260. SELECT rowid, * from t2;
  261. }
  262. } {1 11 2 3 2 22 3 4}
  263. ifcapable attach {
  264. if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
  265. do_test pragma-3.2 {
  266. db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
  267. set pgsz [db eval {PRAGMA page_size}]
  268. # overwrite the header on the rootpage of the index in order to
  269. # make the index appear to be empty.
  270. #
  271. set offset [expr {$pgsz*($rootpage-1)}]
  272. hexio_write test.db $offset 0a00000000040000000000
  273. db close
  274. sqlite3 db test.db
  275. execsql {PRAGMA integrity_check}
  276. } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
  277. do_test pragma-3.3 {
  278. execsql {PRAGMA integrity_check=1}
  279. } {{rowid 1 missing from index i2}}
  280. do_test pragma-3.4 {
  281. execsql {
  282. ATTACH DATABASE 'test.db' AS t2;
  283. PRAGMA integrity_check
  284. }
  285. } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
  286. do_test pragma-3.5 {
  287. execsql {
  288. PRAGMA integrity_check=4
  289. }
  290. } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2}}
  291. do_test pragma-3.6 {
  292. execsql {
  293. PRAGMA integrity_check=xyz
  294. }
  295. } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
  296. do_test pragma-3.7 {
  297. execsql {
  298. PRAGMA integrity_check=0
  299. }
  300. } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
  301. # Add additional corruption by appending unused pages to the end of
  302. # the database file testerr.db
  303. #
  304. do_test pragma-3.8 {
  305. execsql {DETACH t2}
  306. forcedelete testerr.db testerr.db-journal
  307. set out [open testerr.db w]
  308. fconfigure $out -translation binary
  309. set in [open test.db r]
  310. fconfigure $in -translation binary
  311. puts -nonewline $out [read $in]
  312. seek $in 0
  313. puts -nonewline $out [read $in]
  314. close $in
  315. close $out
  316. hexio_write testerr.db 28 00000000
  317. execsql {REINDEX t2}
  318. execsql {PRAGMA integrity_check}
  319. } {ok}
  320. do_test pragma-3.8.1 {
  321. execsql {PRAGMA quick_check}
  322. } {ok}
  323. do_test pragma-3.8.2 {
  324. execsql {PRAGMA QUICK_CHECK}
  325. } {ok}
  326. do_test pragma-3.9 {
  327. execsql {
  328. ATTACH 'testerr.db' AS t2;
  329. PRAGMA integrity_check
  330. }
  331. } {{*** in database t2 ***
  332. Page 4 is never used
  333. Page 5 is never used
  334. Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
  335. do_test pragma-3.10 {
  336. execsql {
  337. PRAGMA integrity_check=1
  338. }
  339. } {{*** in database t2 ***
  340. Page 4 is never used}}
  341. do_test pragma-3.11 {
  342. execsql {
  343. PRAGMA integrity_check=5
  344. }
  345. } {{*** in database t2 ***
  346. Page 4 is never used
  347. Page 5 is never used
  348. Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2}}
  349. do_test pragma-3.12 {
  350. execsql {
  351. PRAGMA integrity_check=4
  352. }
  353. } {{*** in database t2 ***
  354. Page 4 is never used
  355. Page 5 is never used
  356. Page 6 is never used} {rowid 1 missing from index i2}}
  357. do_test pragma-3.13 {
  358. execsql {
  359. PRAGMA integrity_check=3
  360. }
  361. } {{*** in database t2 ***
  362. Page 4 is never used
  363. Page 5 is never used
  364. Page 6 is never used}}
  365. do_test pragma-3.14 {
  366. execsql {
  367. PRAGMA integrity_check(2)
  368. }
  369. } {{*** in database t2 ***
  370. Page 4 is never used
  371. Page 5 is never used}}
  372. do_test pragma-3.15 {
  373. execsql {
  374. ATTACH 'testerr.db' AS t3;
  375. PRAGMA integrity_check
  376. }
  377. } {{*** in database t2 ***
  378. Page 4 is never used
  379. Page 5 is never used
  380. Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
  381. Page 4 is never used
  382. Page 5 is never used
  383. Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
  384. do_test pragma-3.16 {
  385. execsql {
  386. PRAGMA integrity_check(10)
  387. }
  388. } {{*** in database t2 ***
  389. Page 4 is never used
  390. Page 5 is never used
  391. Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
  392. Page 4 is never used
  393. Page 5 is never used
  394. Page 6 is never used} {rowid 1 missing from index i2}}
  395. do_test pragma-3.17 {
  396. execsql {
  397. PRAGMA integrity_check=8
  398. }
  399. } {{*** in database t2 ***
  400. Page 4 is never used
  401. Page 5 is never used
  402. Page 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
  403. Page 4 is never used
  404. Page 5 is never used}}
  405. do_test pragma-3.18 {
  406. execsql {
  407. PRAGMA integrity_check=4
  408. }
  409. } {{*** in database t2 ***
  410. Page 4 is never used
  411. Page 5 is never used
  412. Page 6 is never used} {rowid 1 missing from index i2}}
  413. }
  414. do_test pragma-3.19 {
  415. catch {db close}
  416. forcedelete test.db test.db-journal
  417. sqlite3 db test.db
  418. db eval {PRAGMA integrity_check}
  419. } {ok}
  420. }
  421. #exit
  422. # Test modifying the cache_size of an attached database.
  423. ifcapable pager_pragmas&&attach {
  424. do_test pragma-4.1 {
  425. execsql {
  426. ATTACH 'test2.db' AS aux;
  427. pragma aux.cache_size;
  428. pragma aux.default_cache_size;
  429. }
  430. } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
  431. do_test pragma-4.2 {
  432. execsql {
  433. pragma aux.cache_size = 50;
  434. pragma aux.cache_size;
  435. pragma aux.default_cache_size;
  436. }
  437. } [list 50 $DFLT_CACHE_SZ]
  438. do_test pragma-4.3 {
  439. execsql {
  440. pragma aux.default_cache_size = 456;
  441. pragma aux.cache_size;
  442. pragma aux.default_cache_size;
  443. }
  444. } {456 456}
  445. do_test pragma-4.4 {
  446. execsql {
  447. pragma cache_size;
  448. pragma default_cache_size;
  449. }
  450. } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
  451. do_test pragma-4.5 {
  452. execsql {
  453. DETACH aux;
  454. ATTACH 'test3.db' AS aux;
  455. pragma aux.cache_size;
  456. pragma aux.default_cache_size;
  457. }
  458. } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
  459. do_test pragma-4.6 {
  460. execsql {
  461. DETACH aux;
  462. ATTACH 'test2.db' AS aux;
  463. pragma aux.cache_size;
  464. pragma aux.default_cache_size;
  465. }
  466. } {456 456}
  467. } ;# ifcapable pager_pragmas
  468. # Test that modifying the sync-level in the middle of a transaction is
  469. # disallowed.
  470. ifcapable pager_pragmas {
  471. do_test pragma-5.0 {
  472. execsql {
  473. pragma synchronous;
  474. }
  475. } {2}
  476. do_test pragma-5.1 {
  477. catchsql {
  478. BEGIN;
  479. pragma synchronous = OFF;
  480. }
  481. } {1 {Safety level may not be changed inside a transaction}}
  482. do_test pragma-5.2 {
  483. execsql {
  484. pragma synchronous;
  485. }
  486. } {2}
  487. catchsql {COMMIT;}
  488. } ;# ifcapable pager_pragmas
  489. # Test schema-query pragmas
  490. #
  491. ifcapable schema_pragmas {
  492. ifcapable tempdb&&attach {
  493. do_test pragma-6.1 {
  494. set res {}
  495. execsql {SELECT * FROM sqlite_temp_master}
  496. foreach {idx name file} [execsql {pragma database_list}] {
  497. lappend res $idx $name
  498. }
  499. set res
  500. } {0 main 1 temp 2 aux}
  501. }
  502. do_test pragma-6.2 {
  503. execsql {
  504. CREATE TABLE t2(a,b,c);
  505. pragma table_info(t2)
  506. }
  507. } {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
  508. do_test pragma-6.2.1 {
  509. execsql {
  510. pragma table_info;
  511. }
  512. } {}
  513. db nullvalue <<NULL>>
  514. do_test pragma-6.2.2 {
  515. execsql {
  516. CREATE TABLE t5(
  517. a TEXT DEFAULT CURRENT_TIMESTAMP,
  518. b DEFAULT (5+3),
  519. c TEXT,
  520. d INTEGER DEFAULT NULL,
  521. e TEXT DEFAULT '',
  522. UNIQUE(b,c,d),
  523. PRIMARY KEY(e,b,c)
  524. );
  525. PRAGMA table_info(t5);
  526. }
  527. } {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 2 2 c TEXT 0 <<NULL>> 3 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 1}
  528. db nullvalue {}
  529. do_test pragma-6.2.3 {
  530. execsql {
  531. CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
  532. pragma table_info(t2_3)
  533. }
  534. } {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
  535. ifcapable {foreignkey} {
  536. do_test pragma-6.3.1 {
  537. execsql {
  538. CREATE TABLE t3(a int references t2(b), b UNIQUE);
  539. pragma foreign_key_list(t3);
  540. }
  541. } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
  542. do_test pragma-6.3.2 {
  543. execsql {
  544. pragma foreign_key_list;
  545. }
  546. } {}
  547. do_test pragma-6.3.3 {
  548. execsql {
  549. pragma foreign_key_list(t3_bogus);
  550. }
  551. } {}
  552. do_test pragma-6.3.4 {
  553. execsql {
  554. pragma foreign_key_list(t5);
  555. }
  556. } {}
  557. do_test pragma-6.4 {
  558. execsql {
  559. pragma index_list(t3);
  560. }
  561. } {0 sqlite_autoindex_t3_1 1}
  562. }
  563. ifcapable {!foreignkey} {
  564. execsql {CREATE TABLE t3(a,b UNIQUE)}
  565. }
  566. do_test pragma-6.5.1 {
  567. execsql {
  568. CREATE INDEX t3i1 ON t3(a,b);
  569. pragma index_info(t3i1);
  570. }
  571. } {0 0 a 1 1 b}
  572. do_test pragma-6.5.2 {
  573. execsql {
  574. pragma index_info(t3i1_bogus);
  575. }
  576. } {}
  577. ifcapable tempdb {
  578. # Test for ticket #3320. When a temp table of the same name exists, make
  579. # sure the schema of the main table can still be queried using
  580. # "pragma table_info":
  581. do_test pragma-6.6.1 {
  582. execsql {
  583. CREATE TABLE trial(col_main);
  584. CREATE TEMP TABLE trial(col_temp);
  585. }
  586. } {}
  587. do_test pragma-6.6.2 {
  588. execsql {
  589. PRAGMA table_info(trial);
  590. }
  591. } {0 col_temp {} 0 {} 0}
  592. do_test pragma-6.6.3 {
  593. execsql {
  594. PRAGMA temp.table_info(trial);
  595. }
  596. } {0 col_temp {} 0 {} 0}
  597. do_test pragma-6.6.4 {
  598. execsql {
  599. PRAGMA main.table_info(trial);
  600. }
  601. } {0 col_main {} 0 {} 0}
  602. }
  603. do_test pragma-6.7 {
  604. execsql {
  605. CREATE TABLE test_table(
  606. one INT NOT NULL DEFAULT -1,
  607. two text,
  608. three VARCHAR(45, 65) DEFAULT 'abcde',
  609. four REAL DEFAULT X'abcdef',
  610. five DEFAULT CURRENT_TIME
  611. );
  612. PRAGMA table_info(test_table);
  613. }
  614. } [concat \
  615. {0 one INT 1 -1 0} \
  616. {1 two text 0 {} 0} \
  617. {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
  618. {3 four REAL 0 X'abcdef' 0} \
  619. {4 five {} 0 CURRENT_TIME 0} \
  620. ]
  621. } ;# ifcapable schema_pragmas
  622. # Miscellaneous tests
  623. #
  624. ifcapable schema_pragmas {
  625. do_test pragma-7.1.1 {
  626. # Make sure a pragma knows to read the schema if it needs to
  627. db close
  628. sqlite3 db test.db
  629. execsql {
  630. pragma index_list(t3);
  631. }
  632. } {0 t3i1 0 1 sqlite_autoindex_t3_1 1}
  633. do_test pragma-7.1.2 {
  634. execsql {
  635. pragma index_list(t3_bogus);
  636. }
  637. } {}
  638. } ;# ifcapable schema_pragmas
  639. ifcapable {utf16} {
  640. if {[permutation] == ""} {
  641. do_test pragma-7.2 {
  642. db close
  643. sqlite3 db test.db
  644. catchsql {
  645. pragma encoding=bogus;
  646. }
  647. } {1 {unsupported encoding: bogus}}
  648. }
  649. }
  650. ifcapable tempdb {
  651. do_test pragma-7.3 {
  652. db close
  653. sqlite3 db test.db
  654. execsql {
  655. pragma lock_status;
  656. }
  657. } {main unlocked temp closed}
  658. } else {
  659. do_test pragma-7.3 {
  660. db close
  661. sqlite3 db test.db
  662. execsql {
  663. pragma lock_status;
  664. }
  665. } {main unlocked}
  666. }
  667. #----------------------------------------------------------------------
  668. # Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
  669. # user_version" statements.
  670. #
  671. # pragma-8.1: PRAGMA schema_version
  672. # pragma-8.2: PRAGMA user_version
  673. #
  674. ifcapable schema_version {
  675. # First check that we can set the schema version and then retrieve the
  676. # same value.
  677. do_test pragma-8.1.1 {
  678. execsql {
  679. PRAGMA schema_version = 105;
  680. }
  681. } {}
  682. do_test pragma-8.1.2 {
  683. execsql2 {
  684. PRAGMA schema_version;
  685. }
  686. } {schema_version 105}
  687. do_test pragma-8.1.3 {
  688. execsql {
  689. PRAGMA schema_version = 106;
  690. }
  691. } {}
  692. do_test pragma-8.1.4 {
  693. execsql {
  694. PRAGMA schema_version;
  695. }
  696. } 106
  697. # Check that creating a table modifies the schema-version (this is really
  698. # to verify that the value being read is in fact the schema version).
  699. do_test pragma-8.1.5 {
  700. execsql {
  701. CREATE TABLE t4(a, b, c);
  702. INSERT INTO t4 VALUES(1, 2, 3);
  703. SELECT * FROM t4;
  704. }
  705. } {1 2 3}
  706. do_test pragma-8.1.6 {
  707. execsql {
  708. PRAGMA schema_version;
  709. }
  710. } 107
  711. # Now open a second connection to the database. Ensure that changing the
  712. # schema-version using the first connection forces the second connection
  713. # to reload the schema. This has to be done using the C-API test functions,
  714. # because the TCL API accounts for SCHEMA_ERROR and retries the query.
  715. do_test pragma-8.1.7 {
  716. sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
  717. execsql {
  718. SELECT * FROM t4;
  719. } db2
  720. } {1 2 3}
  721. do_test pragma-8.1.8 {
  722. execsql {
  723. PRAGMA schema_version = 108;
  724. }
  725. } {}
  726. do_test pragma-8.1.9 {
  727. set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
  728. sqlite3_step $::STMT
  729. } SQLITE_ERROR
  730. do_test pragma-8.1.10 {
  731. sqlite3_finalize $::STMT
  732. } SQLITE_SCHEMA
  733. # Make sure the schema-version can be manipulated in an attached database.
  734. forcedelete test2.db
  735. forcedelete test2.db-journal
  736. ifcapable attach {
  737. do_test pragma-8.1.11 {
  738. execsql {
  739. ATTACH 'test2.db' AS aux;
  740. CREATE TABLE aux.t1(a, b, c);
  741. PRAGMA aux.schema_version = 205;
  742. }
  743. } {}
  744. do_test pragma-8.1.12 {
  745. execsql {
  746. PRAGMA aux.schema_version;
  747. }
  748. } 205
  749. }
  750. do_test pragma-8.1.13 {
  751. execsql {
  752. PRAGMA schema_version;
  753. }
  754. } 108
  755. # And check that modifying the schema-version in an attached database
  756. # forces the second connection to reload the schema.
  757. ifcapable attach {
  758. do_test pragma-8.1.14 {
  759. sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
  760. execsql {
  761. ATTACH 'test2.db' AS aux;
  762. SELECT * FROM aux.t1;
  763. } db2
  764. } {}
  765. do_test pragma-8.1.15 {
  766. execsql {
  767. PRAGMA aux.schema_version = 206;
  768. }
  769. } {}
  770. do_test pragma-8.1.16 {
  771. set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
  772. sqlite3_step $::STMT
  773. } SQLITE_ERROR
  774. do_test pragma-8.1.17 {
  775. sqlite3_finalize $::STMT
  776. } SQLITE_SCHEMA
  777. do_test pragma-8.1.18 {
  778. db2 close
  779. } {}
  780. }
  781. # Now test that the user-version can be read and written (and that we aren't
  782. # accidentally manipulating the schema-version instead).
  783. do_test pragma-8.2.1 {
  784. execsql2 {
  785. PRAGMA user_version;
  786. }
  787. } {user_version 0}
  788. do_test pragma-8.2.2 {
  789. execsql {
  790. PRAGMA user_version = 2;
  791. }
  792. } {}
  793. do_test pragma-8.2.3.1 {
  794. execsql2 {
  795. PRAGMA user_version;
  796. }
  797. } {user_version 2}
  798. do_test pragma-8.2.3.2 {
  799. db close
  800. sqlite3 db test.db
  801. execsql {
  802. PRAGMA user_version;
  803. }
  804. } {2}
  805. do_test pragma-8.2.4.1 {
  806. execsql {
  807. PRAGMA schema_version;
  808. }
  809. } {108}
  810. ifcapable vacuum {
  811. do_test pragma-8.2.4.2 {
  812. execsql {
  813. VACUUM;
  814. PRAGMA user_version;
  815. }
  816. } {2}
  817. do_test pragma-8.2.4.3 {
  818. execsql {
  819. PRAGMA schema_version;
  820. }
  821. } {109}
  822. }
  823. ifcapable attach {
  824. db eval {ATTACH 'test2.db' AS aux}
  825. # Check that the user-version in the auxilary database can be manipulated (
  826. # and that we aren't accidentally manipulating the same in the main db).
  827. do_test pragma-8.2.5 {
  828. execsql {
  829. PRAGMA aux.user_version;
  830. }
  831. } {0}
  832. do_test pragma-8.2.6 {
  833. execsql {
  834. PRAGMA aux.user_version = 3;
  835. }
  836. } {}
  837. do_test pragma-8.2.7 {
  838. execsql {
  839. PRAGMA aux.user_version;
  840. }
  841. } {3}
  842. do_test pragma-8.2.8 {
  843. execsql {
  844. PRAGMA main.user_version;
  845. }
  846. } {2}
  847. # Now check that a ROLLBACK resets the user-version if it has been modified
  848. # within a transaction.
  849. do_test pragma-8.2.9 {
  850. execsql {
  851. BEGIN;
  852. PRAGMA aux.user_version = 10;
  853. PRAGMA user_version = 11;
  854. }
  855. } {}
  856. do_test pragma-8.2.10 {
  857. execsql {
  858. PRAGMA aux.user_version;
  859. }
  860. } {10}
  861. do_test pragma-8.2.11 {
  862. execsql {
  863. PRAGMA main.user_version;
  864. }
  865. } {11}
  866. do_test pragma-8.2.12 {
  867. execsql {
  868. ROLLBACK;
  869. PRAGMA aux.user_version;
  870. }
  871. } {3}
  872. do_test pragma-8.2.13 {
  873. execsql {
  874. PRAGMA main.user_version;
  875. }
  876. } {2}
  877. }
  878. # Try a negative value for the user-version
  879. do_test pragma-8.2.14 {
  880. execsql {
  881. PRAGMA user_version = -450;
  882. }
  883. } {}
  884. do_test pragma-8.2.15 {
  885. execsql {
  886. PRAGMA user_version;
  887. }
  888. } {-450}
  889. } ; # ifcapable schema_version
  890. # Check to see if TEMP_STORE is memory or disk. Return strings
  891. # "memory" or "disk" as appropriate.
  892. #
  893. proc check_temp_store {} {
  894. db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
  895. db eval {PRAGMA database_list} {
  896. if {$name=="temp"} {
  897. set bt [btree_from_db db 1]
  898. if {[btree_ismemdb $bt]} {
  899. return "memory"
  900. }
  901. return "disk"
  902. }
  903. }
  904. return "unknown"
  905. }
  906. # Application_ID
  907. #
  908. do_test pragma-8.3.1 {
  909. execsql {
  910. PRAGMA application_id;
  911. }
  912. } {0}
  913. do_test pragma-8.3.2 {
  914. execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
  915. } {12345}
  916. # Test temp_store and temp_store_directory pragmas
  917. #
  918. ifcapable pager_pragmas {
  919. do_test pragma-9.1 {
  920. db close
  921. sqlite3 db test.db
  922. execsql {
  923. PRAGMA temp_store;
  924. }
  925. } {0}
  926. if {$TEMP_STORE<=1} {
  927. do_test pragma-9.1.1 {
  928. check_temp_store
  929. } {disk}
  930. } else {
  931. do_test pragma-9.1.1 {
  932. check_temp_store
  933. } {memory}
  934. }
  935. do_test pragma-9.2 {
  936. db close
  937. sqlite3 db test.db
  938. execsql {
  939. PRAGMA temp_store=file;
  940. PRAGMA temp_store;
  941. }
  942. } {1}
  943. if {$TEMP_STORE==3} {
  944. # When TEMP_STORE is 3, always use memory regardless of pragma settings.
  945. do_test pragma-9.2.1 {
  946. check_temp_store
  947. } {memory}
  948. } else {
  949. do_test pragma-9.2.1 {
  950. check_temp_store
  951. } {disk}
  952. }
  953. do_test pragma-9.3 {
  954. db close
  955. sqlite3 db test.db
  956. execsql {
  957. PRAGMA temp_store=memory;
  958. PRAGMA temp_store;
  959. }
  960. } {2}
  961. if {$TEMP_STORE==0} {
  962. # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
  963. do_test pragma-9.3.1 {
  964. check_temp_store
  965. } {disk}
  966. } else {
  967. do_test pragma-9.3.1 {
  968. check_temp_store
  969. } {memory}
  970. }
  971. do_test pragma-9.4 {
  972. execsql {
  973. PRAGMA temp_store_directory;
  974. }
  975. } {}
  976. ifcapable wsd {
  977. do_test pragma-9.5 {
  978. set pwd [string map {' ''} [file nativename [get_pwd]]]
  979. execsql "
  980. PRAGMA temp_store_directory='$pwd';
  981. "
  982. } {}
  983. do_test pragma-9.6 {
  984. execsql {
  985. PRAGMA temp_store_directory;
  986. }
  987. } [list [file nativename [get_pwd]]]
  988. do_test pragma-9.7 {
  989. catchsql {
  990. PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
  991. }
  992. } {1 {not a writable directory}}
  993. do_test pragma-9.8 {
  994. execsql {
  995. PRAGMA temp_store_directory='';
  996. }
  997. } {}
  998. if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
  999. ifcapable tempdb {
  1000. do_test pragma-9.9 {
  1001. execsql {
  1002. PRAGMA temp_store_directory;
  1003. PRAGMA temp_store=FILE;
  1004. CREATE TEMP TABLE temp_store_directory_test(a integer);
  1005. INSERT INTO temp_store_directory_test values (2);
  1006. SELECT * FROM temp_store_directory_test;
  1007. }
  1008. } {2}
  1009. do_test pragma-9.10 {
  1010. catchsql "
  1011. PRAGMA temp_store_directory='$pwd';
  1012. SELECT * FROM temp_store_directory_test;
  1013. "
  1014. } {1 {no such table: temp_store_directory_test}}
  1015. }
  1016. }
  1017. }
  1018. do_test pragma-9.11 {
  1019. execsql {
  1020. PRAGMA temp_store = 0;
  1021. PRAGMA temp_store;
  1022. }
  1023. } {0}
  1024. do_test pragma-9.12 {
  1025. execsql {
  1026. PRAGMA temp_store = 1;
  1027. PRAGMA temp_store;
  1028. }
  1029. } {1}
  1030. do_test pragma-9.13 {
  1031. execsql {
  1032. PRAGMA temp_store = 2;
  1033. PRAGMA temp_store;
  1034. }
  1035. } {2}
  1036. do_test pragma-9.14 {
  1037. execsql {
  1038. PRAGMA temp_store = 3;
  1039. PRAGMA temp_store;
  1040. }
  1041. } {0}
  1042. do_test pragma-9.15 {
  1043. catchsql {
  1044. BEGIN EXCLUSIVE;
  1045. CREATE TEMP TABLE temp_table(t);
  1046. INSERT INTO temp_table VALUES('valuable data');
  1047. PRAGMA temp_store = 1;
  1048. }
  1049. } {1 {temporary storage cannot be changed from within a transaction}}
  1050. do_test pragma-9.16 {
  1051. execsql {
  1052. SELECT * FROM temp_table;
  1053. COMMIT;
  1054. }
  1055. } {{valuable data}}
  1056. do_test pragma-9.17 {
  1057. execsql {
  1058. INSERT INTO temp_table VALUES('valuable data II');
  1059. SELECT * FROM temp_table;
  1060. }
  1061. } {{valuable data} {valuable data II}}
  1062. do_test pragma-9.18 {
  1063. set rc [catch {
  1064. db eval {SELECT t FROM temp_table} {
  1065. execsql {pragma temp_store = 1}
  1066. }
  1067. } msg]
  1068. list $rc $msg
  1069. } {1 {temporary storage cannot be changed from within a transaction}}
  1070. } ;# ifcapable pager_pragmas
  1071. ifcapable trigger {
  1072. do_test pragma-10.0 {
  1073. catchsql {
  1074. DROP TABLE main.t1;
  1075. }
  1076. execsql {
  1077. PRAGMA count_changes = 1;
  1078. CREATE TABLE t1(a PRIMARY KEY);
  1079. CREATE TABLE t1_mirror(a);
  1080. CREATE TABLE t1_mirror2(a);
  1081. CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
  1082. INSERT INTO t1_mirror VALUES(new.a);
  1083. END;
  1084. CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
  1085. INSERT INTO t1_mirror2 VALUES(new.a);
  1086. END;
  1087. CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
  1088. UPDATE t1_mirror SET a = new.a WHERE a = old.a;
  1089. END;
  1090. CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
  1091. UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
  1092. END;
  1093. CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
  1094. DELETE FROM t1_mirror WHERE a = old.a;
  1095. END;
  1096. CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
  1097. DELETE FROM t1_mirror2 WHERE a = old.a;
  1098. END;
  1099. }
  1100. } {}
  1101. do_test pragma-10.1 {
  1102. execsql {
  1103. INSERT INTO t1 VALUES(randstr(10,10));
  1104. }
  1105. } {1}
  1106. do_test pragma-10.2 {
  1107. execsql {
  1108. UPDATE t1 SET a = randstr(10,10);
  1109. }
  1110. } {1}
  1111. do_test pragma-10.3 {
  1112. execsql {
  1113. DELETE FROM t1;
  1114. }
  1115. } {1}
  1116. } ;# ifcapable trigger
  1117. ifcapable schema_pragmas {
  1118. do_test pragma-11.1 {
  1119. execsql2 {
  1120. pragma collation_list;
  1121. }
  1122. } {seq 0 name NOCASE seq 1 name RTRIM seq 2 name BINARY}
  1123. do_test pragma-11.2 {
  1124. db collate New_Collation blah...
  1125. execsql {
  1126. pragma collation_list;
  1127. }
  1128. } {0 New_Collation 1 NOCASE 2 RTRIM 3 BINARY}
  1129. }
  1130. ifcapable schema_pragmas&&tempdb {
  1131. do_test pragma-12.1 {
  1132. sqlite3 db2 test.db
  1133. execsql {
  1134. PRAGMA temp.table_info('abc');
  1135. } db2
  1136. } {}
  1137. db2 close
  1138. do_test pragma-12.2 {
  1139. sqlite3 db2 test.db
  1140. execsql {
  1141. PRAGMA temp.default_cache_size = 200;
  1142. PRAGMA temp.default_cache_size;
  1143. } db2
  1144. } {200}
  1145. db2 close
  1146. do_test pragma-12.3 {
  1147. sqlite3 db2 test.db
  1148. execsql {
  1149. PRAGMA temp.cache_size = 400;
  1150. PRAGMA temp.cache_size;
  1151. } db2
  1152. } {400}
  1153. db2 close
  1154. }
  1155. ifcapable bloblit {
  1156. do_test pragma-13.1 {
  1157. execsql {
  1158. DROP TABLE IF EXISTS t4;
  1159. PRAGMA vdbe_trace=on;
  1160. PRAGMA vdbe_listing=on;
  1161. PRAGMA sql_trace=on;
  1162. CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
  1163. INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
  1164. INSERT INTO t4(b) VALUES(randstr(30,30));
  1165. INSERT INTO t4(b) VALUES(1.23456);
  1166. INSERT INTO t4(b) VALUES(NULL);
  1167. INSERT INTO t4(b) VALUES(0);
  1168. INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
  1169. SELECT * FROM t4;
  1170. }
  1171. execsql {
  1172. PRAGMA vdbe_trace=off;
  1173. PRAGMA vdbe_listing=off;
  1174. PRAGMA sql_trace=off;
  1175. }
  1176. } {}
  1177. } ;# ifcapable bloblit
  1178. ifcapable pager_pragmas {
  1179. db close
  1180. forcedelete test.db
  1181. sqlite3 db test.db
  1182. do_test pragma-14.1 {
  1183. execsql { pragma auto_vacuum = 0 }
  1184. execsql { pragma page_count }
  1185. } {0}
  1186. do_test pragma-14.2 {
  1187. execsql {
  1188. CREATE TABLE abc(a, b, c);
  1189. PRAGMA page_count;
  1190. }
  1191. } {2}
  1192. do_test pragma-14.2uc {
  1193. execsql {pragma PAGE_COUNT}
  1194. } {2}
  1195. do_test pragma-14.3 {
  1196. execsql {
  1197. BEGIN;
  1198. CREATE TABLE def(a, b, c);
  1199. PRAGMA page_count;
  1200. }
  1201. } {3}
  1202. do_test pragma-14.3uc {
  1203. execsql {pragma PAGE_COUNT}
  1204. } {3}
  1205. do_test pragma-14.4 {
  1206. set page_size [db one {pragma page_size}]
  1207. expr [file size test.db] / $page_size
  1208. } {2}
  1209. do_test pragma-14.5 {
  1210. execsql {
  1211. ROLLBACK;
  1212. PRAGMA page_count;
  1213. }
  1214. } {2}
  1215. do_test pragma-14.6 {
  1216. forcedelete test2.db
  1217. sqlite3 db2 test2.db
  1218. execsql {
  1219. PRAGMA auto_vacuum = 0;
  1220. CREATE TABLE t1(a, b, c);
  1221. CREATE TABLE t2(a, b, c);
  1222. CREATE TABLE t3(a, b, c);
  1223. CREATE TABLE t4(a, b, c);
  1224. } db2
  1225. db2 close
  1226. execsql {
  1227. ATTACH 'test2.db' AS aux;
  1228. PRAGMA aux.page_count;
  1229. }
  1230. } {5}
  1231. do_test pragma-14.6uc {
  1232. execsql {pragma AUX.PAGE_COUNT}
  1233. } {5}
  1234. }
  1235. # Test that the value set using the cache_size pragma is not reset when the
  1236. # schema is reloaded.
  1237. #
  1238. ifcapable pager_pragmas {
  1239. db close
  1240. sqlite3 db test.db
  1241. do_test pragma-15.1 {
  1242. execsql {
  1243. PRAGMA cache_size=59;
  1244. PRAGMA cache_size;
  1245. }
  1246. } {59}
  1247. do_test pragma-15.2 {
  1248. sqlite3 db2 test.db
  1249. execsql {
  1250. CREATE TABLE newtable(a, b, c);
  1251. } db2
  1252. db2 close
  1253. } {}
  1254. do_test pragma-15.3 {
  1255. # Evaluating this statement will cause the schema to be reloaded (because
  1256. # the schema was changed by another connection in pragma-15.2). At one
  1257. # point there was a bug that reset the cache_size to its default value
  1258. # when this happened.
  1259. execsql { SELECT * FROM sqlite_master }
  1260. execsql { PRAGMA cache_size }
  1261. } {59}
  1262. }
  1263. # Reset the sqlite3_temp_directory variable for the next run of tests:
  1264. sqlite3 dbX :memory:
  1265. dbX eval {PRAGMA temp_store_directory = ""}
  1266. dbX close
  1267. ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
  1268. set sqlite_hostid_num 1
  1269. set using_proxy 0
  1270. foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
  1271. set using_proxy $value
  1272. }
  1273. # Test the lock_proxy_file pragmas.
  1274. #
  1275. db close
  1276. set env(SQLITE_FORCE_PROXY_LOCKING) "0"
  1277. sqlite3 db test.db
  1278. do_test pragma-16.1 {
  1279. execsql {
  1280. PRAGMA lock_proxy_file="mylittleproxy";
  1281. select * from sqlite_master;
  1282. }
  1283. execsql {
  1284. PRAGMA lock_proxy_file;
  1285. }
  1286. } {mylittleproxy}
  1287. do_test pragma-16.2 {
  1288. sqlite3 db2 test.db
  1289. execsql {
  1290. PRAGMA lock_proxy_file="mylittleproxy";
  1291. } db2
  1292. } {}
  1293. db2 close
  1294. do_test pragma-16.2.1 {
  1295. sqlite3 db2 test.db
  1296. execsql {
  1297. PRAGMA lock_proxy_file=":auto:";
  1298. select * from sqlite_master;
  1299. } db2
  1300. execsql {
  1301. PRAGMA lock_proxy_file;
  1302. } db2
  1303. } {mylittleproxy}
  1304. db2 close
  1305. do_test pragma-16.3 {
  1306. sqlite3 db2 test.db
  1307. execsql {
  1308. PRAGMA lock_proxy_file="myotherproxy";
  1309. } db2
  1310. catchsql {
  1311. select * from sqlite_master;
  1312. } db2
  1313. } {1 {database is locked}}
  1314. do_test pragma-16.4 {
  1315. db2 close
  1316. db close
  1317. sqlite3 db2 test.db
  1318. execsql {
  1319. PRAGMA lock_proxy_file="myoriginalproxy";
  1320. PRAGMA lock_proxy_file="myotherproxy";
  1321. PRAGMA lock_proxy_file;
  1322. } db2
  1323. } {myotherproxy}
  1324. db2 close
  1325. set env(SQLITE_FORCE_PROXY_LOCKING) "1"
  1326. do_test pragma-16.5 {
  1327. sqlite3 db2 test.db
  1328. execsql {
  1329. PRAGMA lock_proxy_file=":auto:";
  1330. PRAGMA lock_proxy_file;
  1331. } db2
  1332. } {myotherproxy}
  1333. do_test pragma-16.6 {
  1334. db2 close
  1335. sqlite3 db2 test2.db
  1336. set lockpath [execsql {
  1337. PRAGMA lock_proxy_file=":auto:";
  1338. PRAGMA lock_proxy_file;
  1339. } db2]
  1340. string match "*test2.db:auto:" $lockpath
  1341. } {1}
  1342. set sqlite_hostid_num 2
  1343. do_test pragma-16.7 {
  1344. list [catch {
  1345. sqlite3 db test2.db
  1346. execsql {
  1347. PRAGMA lock_proxy_file=":auto:";
  1348. select * from sqlite_master;
  1349. }
  1350. } msg] $msg
  1351. } {1 {database is locked}}
  1352. db close
  1353. do_test pragma-16.8 {
  1354. list [catch {
  1355. sqlite3 db test2.db
  1356. execsql { select * from sqlite_master }
  1357. } msg] $msg
  1358. } {1 {database is locked}}
  1359. db2 close
  1360. do_test pragma-16.8.1 {
  1361. execsql {
  1362. PRAGMA lock_proxy_file="yetanotherproxy";
  1363. PRAGMA lock_proxy_file;
  1364. }
  1365. } {yetanotherproxy}
  1366. do_test pragma-16.8.2 {
  1367. execsql {
  1368. create table mine(x);
  1369. }
  1370. } {}
  1371. db close
  1372. do_test pragma-16.9 {
  1373. sqlite3 db proxytest.db
  1374. set lockpath2 [execsql {
  1375. PRAGMA lock_proxy_file=":auto:";
  1376. PRAGMA lock_proxy_file;
  1377. } db]
  1378. string match "*proxytest.db:auto:" $lockpath2
  1379. } {1}
  1380. set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
  1381. set sqlite_hostid_num 0
  1382. }
  1383. # Parsing of auto_vacuum settings.
  1384. #
  1385. foreach {autovac_setting val} {
  1386. 0 0
  1387. 1 1
  1388. 2 2
  1389. 3 0
  1390. -1 0
  1391. none 0
  1392. NONE 0
  1393. NoNe 0
  1394. full 1
  1395. FULL 1
  1396. incremental 2
  1397. INCREMENTAL 2
  1398. -1234 0
  1399. 1234 0
  1400. } {
  1401. do_test pragma-17.1.$autovac_setting {
  1402. catch {db close}
  1403. sqlite3 db :memory:
  1404. execsql "
  1405. PRAGMA auto_vacuum=$::autovac_setting;
  1406. PRAGMA auto_vacuum;
  1407. "
  1408. } $val
  1409. }
  1410. # Parsing of temp_store settings.
  1411. #
  1412. foreach {temp_setting val} {
  1413. 0 0
  1414. 1 1
  1415. 2 2
  1416. 3 0
  1417. -1 0
  1418. file 1
  1419. FILE 1
  1420. fIlE 1
  1421. memory 2
  1422. MEMORY 2
  1423. MeMoRy 2
  1424. } {
  1425. do_test pragma-18.1.$temp_setting {
  1426. catch {db close}
  1427. sqlite3 db :memory:
  1428. execsql "
  1429. PRAGMA temp_store=$::temp_setting;
  1430. PRAGMA temp_store=$::temp_setting;
  1431. PRAGMA temp_store;
  1432. "
  1433. } $val
  1434. }
  1435. # The SQLITE_FCNTL_PRAGMA logic, with error handling.
  1436. #
  1437. db close
  1438. testvfs tvfs
  1439. sqlite3 db test.db -vfs tvfs
  1440. do_test pragma-19.1 {
  1441. catchsql {PRAGMA error}
  1442. } {1 {SQL logic error or missing database}}
  1443. do_test pragma-19.2 {
  1444. catchsql {PRAGMA error='This is the error message'}
  1445. } {1 {This is the error message}}
  1446. do_test pragma-19.3 {
  1447. catchsql {PRAGMA error='7 This is the error message'}
  1448. } {1 {This is the error message}}
  1449. do_test pragma-19.4 {
  1450. catchsql {PRAGMA error=7}
  1451. } {1 {out of memory}}
  1452. do_test pragma-19.5 {
  1453. file tail [lindex [execsql {PRAGMA filename}] 0]
  1454. } {test.db}
  1455. if {$tcl_platform(platform)=="windows"} {
  1456. # Test data_store_directory pragma
  1457. #
  1458. db close
  1459. sqlite3 db test.db
  1460. file mkdir data_dir
  1461. do_test pragma-20.1 {
  1462. catchsql {PRAGMA data_store_directory}
  1463. } {0 {}}
  1464. do_test pragma-20.2 {
  1465. set pwd [string map {' ''} [file nativename [get_pwd]]]
  1466. catchsql "PRAGMA data_store_directory='$pwd';"
  1467. } {0 {}}
  1468. do_test pragma-20.3 {
  1469. catchsql {PRAGMA data_store_directory}
  1470. } [list 0 [list [file nativename [get_pwd]]]]
  1471. do_test pragma-20.4 {
  1472. set pwd [string map {' ''} [file nativename \
  1473. [file join [get_pwd] data_dir]]]
  1474. catchsql "PRAGMA data_store_directory='$pwd';"
  1475. } {0 {}}
  1476. do_test pragma-20.5 {
  1477. sqlite3 db2 test2.db
  1478. catchsql "PRAGMA database_list;" db2
  1479. } [list 0 [list 0 main [file nativename \
  1480. [file join [get_pwd] data_dir test2.db]]]]
  1481. catch {db2 close}
  1482. do_test pragma-20.6 {
  1483. sqlite3 db2 [file join [get_pwd] test2.db]
  1484. catchsql "PRAGMA database_list;" db2
  1485. } [list 0 [list 0 main [file nativename \
  1486. [file join [get_pwd] test2.db]]]]
  1487. catch {db2 close}
  1488. do_test pragma-20.7 {
  1489. catchsql "PRAGMA data_store_directory='';"
  1490. } {0 {}}
  1491. do_test pragma-20.8 {
  1492. catchsql {PRAGMA data_store_directory}
  1493. } {0 {}}
  1494. forcedelete data_dir
  1495. } ;# endif windows
  1496. do_test 21.1 {
  1497. # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
  1498. #
  1499. db close
  1500. forcedelete test.db
  1501. sqlite3 db test.db
  1502. execsql {
  1503. PRAGMA page_size = 1024;
  1504. PRAGMA auto_vacuum = 0;
  1505. CREATE TABLE t1(a PRIMARY KEY, b);
  1506. INSERT INTO t1 VALUES(1, 1);
  1507. }
  1508. for {set i 0} {$i < 10} {incr i} {
  1509. execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
  1510. }
  1511. db close
  1512. forcecopy test.db testerr.db
  1513. hexio_write testerr.db 15000 [string repeat 55 100]
  1514. } {100}
  1515. set mainerr {*** in database main ***
  1516. Multiple uses for byte 672 of page 15}
  1517. set auxerr {*** in database aux ***
  1518. Multiple uses for byte 672 of page 15}
  1519. do_test 22.2 {
  1520. catch { db close }
  1521. sqlite3 db testerr.db
  1522. execsql { PRAGMA integrity_check }
  1523. } [list $mainerr]
  1524. do_test 22.3.1 {
  1525. catch { db close }
  1526. sqlite3 db test.db
  1527. execsql {
  1528. ATTACH 'testerr.db' AS 'aux';
  1529. PRAGMA integrity_check;
  1530. }
  1531. } [list $auxerr]
  1532. do_test 22.3.2 {
  1533. execsql { PRAGMA main.integrity_check; }
  1534. } {ok}
  1535. do_test 22.3.3 {
  1536. execsql { PRAGMA aux.integrity_check; }
  1537. } [list $auxerr]
  1538. do_test 22.4.1 {
  1539. catch { db close }
  1540. sqlite3 db testerr.db
  1541. execsql {
  1542. ATTACH 'test.db' AS 'aux';
  1543. PRAGMA integrity_check;
  1544. }
  1545. } [list $mainerr]
  1546. do_test 22.4.2 {
  1547. execsql { PRAGMA main.integrity_check; }
  1548. } [list $mainerr]
  1549. do_test 22.4.3 {
  1550. execsql { PRAGMA aux.integrity_check; }
  1551. } {ok}
  1552. db close
  1553. forcedelete test.db test.db-wal test.db-journal
  1554. sqlite3 db test.db
  1555. sqlite3 db2 test.db
  1556. do_test 23.1 {
  1557. db eval {
  1558. CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
  1559. CREATE INDEX i1 ON t1(b,c);
  1560. CREATE INDEX i2 ON t1(c,d);
  1561. CREATE TABLE t2(x INTEGER REFERENCES t1);
  1562. }
  1563. db2 eval {SELECT name FROM sqlite_master}
  1564. } {t1 i1 i2 t2}
  1565. do_test 23.2 {
  1566. db eval {
  1567. DROP INDEX i2;
  1568. CREATE INDEX i2 ON t1(c,d,b);
  1569. }
  1570. db2 eval {PRAGMA index_info(i2)}
  1571. } {0 2 c 1 3 d 2 1 b}
  1572. do_test 23.3 {
  1573. db eval {
  1574. CREATE INDEX i3 ON t1(d,b,c);
  1575. }
  1576. db2 eval {PRAGMA index_list(t1)}
  1577. } {0 i3 0 1 i2 0 2 i1 0}
  1578. do_test 23.4 {
  1579. db eval {
  1580. ALTER TABLE t1 ADD COLUMN e;
  1581. }
  1582. db2 eval {
  1583. PRAGMA table_info(t1);
  1584. }
  1585. } {/4 e {} 0 {} 0/}
  1586. do_test 23.5 {
  1587. db eval {
  1588. DROP TABLE t2;
  1589. CREATE TABLE t2(x, y INTEGER REFERENCES t1);
  1590. }
  1591. db2 eval {
  1592. PRAGMA foreign_key_list(t2);
  1593. }
  1594. } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
  1595. finish_test