1cd61c281Sdrh# 2002 March 6 2cd61c281Sdrh# 3cd61c281Sdrh# The author disclaims copyright to this source code. In place of 4cd61c281Sdrh# a legal notice, here is a blessing: 5cd61c281Sdrh# 6cd61c281Sdrh# May you do good and not evil. 7cd61c281Sdrh# May you find forgiveness for yourself and forgive others. 8cd61c281Sdrh# May you share freely, never taking more than you give. 9cd61c281Sdrh# 10cd61c281Sdrh#*********************************************************************** 11cd61c281Sdrh# This file implements regression tests for SQLite library. 12cd61c281Sdrh# 13cd61c281Sdrh# This file implements tests for the PRAGMA command. 14cd61c281Sdrh# 15*5260f7e9Sdrh# $Id: pragma.test,v 1.15 2004/06/26 19:35:30 drh Exp $ 16cd61c281Sdrh 17cd61c281Sdrhset testdir [file dirname $argv0] 18cd61c281Sdrhsource $testdir/tester.tcl 19cd61c281Sdrh 2091cf71b0Sdanielk1977# Test organization: 2191cf71b0Sdanielk1977# 2291cf71b0Sdanielk1977# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db. 2391cf71b0Sdanielk1977# pragma-2.*: Test synchronous on attached db. 2491cf71b0Sdanielk1977# pragma-3.*: Test detection of table/index inconsistency by integrity_check. 2591cf71b0Sdanielk1977# pragma-4.*: Test cache_size and default_cache_size on attached db. 2691cf71b0Sdanielk1977# pragma-5.*: Test that pragma synchronous may not be used inside of a 2791cf71b0Sdanielk1977# transaction. 2891cf71b0Sdanielk1977# 2991cf71b0Sdanielk1977 30cd61c281Sdrh# Delete the preexisting database to avoid the special setup 31cd61c281Sdrh# that the "all.test" script does. 32cd61c281Sdrh# 33cd61c281Sdrhdb close 34cd61c281Sdrhfile delete test.db 35ef4ac8f9Sdrhset DB [sqlite3 db test.db] 36cd61c281Sdrh 37cd61c281Sdrhdo_test pragma-1.1 { 38cd61c281Sdrh execsql { 39cd61c281Sdrh PRAGMA cache_size; 40cd61c281Sdrh PRAGMA default_cache_size; 41cd61c281Sdrh PRAGMA synchronous; 42cd61c281Sdrh } 4391cf71b0Sdanielk1977} {2000 2000 2} 44cd61c281Sdrhdo_test pragma-1.2 { 45cd61c281Sdrh execsql { 46cd61c281Sdrh PRAGMA cache_size=1234; 47cd61c281Sdrh PRAGMA cache_size; 48cd61c281Sdrh PRAGMA default_cache_size; 49cd61c281Sdrh PRAGMA synchronous; 50cd61c281Sdrh } 5191cf71b0Sdanielk1977} {1234 2000 2} 52cd61c281Sdrhdo_test pragma-1.3 { 53cd61c281Sdrh db close 54ef4ac8f9Sdrh sqlite3 db test.db 55cd61c281Sdrh execsql { 56cd61c281Sdrh PRAGMA cache_size; 57cd61c281Sdrh PRAGMA default_cache_size; 58cd61c281Sdrh PRAGMA synchronous; 59cd61c281Sdrh } 6091cf71b0Sdanielk1977} {2000 2000 2} 61cd61c281Sdrhdo_test pragma-1.4 { 62cd61c281Sdrh execsql { 63cd61c281Sdrh PRAGMA synchronous=OFF; 64cd61c281Sdrh PRAGMA cache_size; 65cd61c281Sdrh PRAGMA default_cache_size; 66cd61c281Sdrh PRAGMA synchronous; 67cd61c281Sdrh } 6891cf71b0Sdanielk1977} {2000 2000 0} 69cd61c281Sdrhdo_test pragma-1.5 { 70cd61c281Sdrh execsql { 71cd61c281Sdrh PRAGMA cache_size=4321; 72cd61c281Sdrh PRAGMA cache_size; 73cd61c281Sdrh PRAGMA default_cache_size; 74cd61c281Sdrh PRAGMA synchronous; 75cd61c281Sdrh } 7691cf71b0Sdanielk1977} {4321 2000 0} 77cd61c281Sdrhdo_test pragma-1.6 { 78cd61c281Sdrh execsql { 79cd61c281Sdrh PRAGMA synchronous=ON; 80cd61c281Sdrh PRAGMA cache_size; 81cd61c281Sdrh PRAGMA default_cache_size; 82cd61c281Sdrh PRAGMA synchronous; 83cd61c281Sdrh } 8491cf71b0Sdanielk1977} {4321 2000 1} 85cd61c281Sdrhdo_test pragma-1.7 { 86cd61c281Sdrh db close 87ef4ac8f9Sdrh sqlite3 db test.db 88cd61c281Sdrh execsql { 89cd61c281Sdrh PRAGMA cache_size; 90cd61c281Sdrh PRAGMA default_cache_size; 91cd61c281Sdrh PRAGMA synchronous; 92cd61c281Sdrh } 9391cf71b0Sdanielk1977} {2000 2000 2} 94cd61c281Sdrhdo_test pragma-1.8 { 95cd61c281Sdrh execsql { 96cd61c281Sdrh PRAGMA default_cache_size=123; 97cd61c281Sdrh PRAGMA cache_size; 98cd61c281Sdrh PRAGMA default_cache_size; 99cd61c281Sdrh PRAGMA synchronous; 100cd61c281Sdrh } 10191cf71b0Sdanielk1977} {123 123 2} 10291cf71b0Sdanielk1977do_test pragma-1.9 { 103cd61c281Sdrh db close 104ef4ac8f9Sdrh set ::DB [sqlite3 db test.db] 105cd61c281Sdrh execsql { 106cd61c281Sdrh PRAGMA cache_size; 107cd61c281Sdrh PRAGMA default_cache_size; 108cd61c281Sdrh PRAGMA synchronous; 109cd61c281Sdrh } 11091cf71b0Sdanielk1977} {123 123 2} 11191cf71b0Sdanielk1977do_test pragma-1.10 { 1125a38705eSdrh execsql { 1134303feedSdrh PRAGMA synchronous=NORMAL; 1144303feedSdrh PRAGMA cache_size; 1154303feedSdrh PRAGMA default_cache_size; 1164303feedSdrh PRAGMA synchronous; 1174303feedSdrh } 11891cf71b0Sdanielk1977} {123 123 1} 11991cf71b0Sdanielk1977do_test pragma-1.11 { 1204303feedSdrh execsql { 1214303feedSdrh PRAGMA synchronous=FULL; 1224303feedSdrh PRAGMA cache_size; 1234303feedSdrh PRAGMA default_cache_size; 1244303feedSdrh PRAGMA synchronous; 1254303feedSdrh } 12691cf71b0Sdanielk1977} {123 123 2} 12791cf71b0Sdanielk1977do_test pragma-1.12 { 1284303feedSdrh db close 129ef4ac8f9Sdrh set ::DB [sqlite3 db test.db] 1304303feedSdrh execsql { 1314303feedSdrh PRAGMA cache_size; 1324303feedSdrh PRAGMA default_cache_size; 1334303feedSdrh PRAGMA synchronous; 1344303feedSdrh } 13591cf71b0Sdanielk1977} {123 123 2} 13691cf71b0Sdanielk1977 137*5260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition 138*5260f7e9Sdrh# to keywords like "off" and "full". 139*5260f7e9Sdrh# 140*5260f7e9Sdrhdo_test pragma-1.13 { 141*5260f7e9Sdrh execsql { 142*5260f7e9Sdrh PRAGMA synchronous=0; 143*5260f7e9Sdrh PRAGMA synchronous; 144*5260f7e9Sdrh } 145*5260f7e9Sdrh} {0} 146*5260f7e9Sdrhdo_test pragma-1.14 { 147*5260f7e9Sdrh execsql { 148*5260f7e9Sdrh PRAGMA synchronous=2; 149*5260f7e9Sdrh PRAGMA synchronous; 150*5260f7e9Sdrh } 151*5260f7e9Sdrh} {2} 152*5260f7e9Sdrh 153*5260f7e9Sdrh# Test turning "flag" pragmas on and off. 154*5260f7e9Sdrh# 155*5260f7e9Sdrhdo_test pragma-1.15 { 156*5260f7e9Sdrh execsql { 157*5260f7e9Sdrh PRAGMA vdbe_listing=YES; 158*5260f7e9Sdrh PRAGMA vdbe_listing; 159*5260f7e9Sdrh } 160*5260f7e9Sdrh} {1} 161*5260f7e9Sdrhdo_test pragma-1.16 { 162*5260f7e9Sdrh execsql { 163*5260f7e9Sdrh PRAGMA vdbe_listing=NO; 164*5260f7e9Sdrh PRAGMA vdbe_listing; 165*5260f7e9Sdrh } 166*5260f7e9Sdrh} {0} 167*5260f7e9Sdrhdo_test pragma-1.17 { 168*5260f7e9Sdrh execsql { 169*5260f7e9Sdrh PRAGMA parser_trace=ON; 170*5260f7e9Sdrh PRAGMA parser_trace=OFF; 171*5260f7e9Sdrh } 172*5260f7e9Sdrh} {} 173*5260f7e9Sdrhdo_test pragma-1.18 { 174*5260f7e9Sdrh execsql { 175*5260f7e9Sdrh PRAGMA bogus = -1234; -- Parsing of negative values 176*5260f7e9Sdrh } 177*5260f7e9Sdrh} {} 178*5260f7e9Sdrh 17991cf71b0Sdanielk1977# Test modifying the safety_level of an attached database. 18091cf71b0Sdanielk1977do_test pragma-2.1 { 18191cf71b0Sdanielk1977 file delete -force test2.db 18291cf71b0Sdanielk1977 file delete -force test2.db-journal 1834303feedSdrh execsql { 18491cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 1854303feedSdrh } 18691cf71b0Sdanielk1977} {} 18791cf71b0Sdanielk1977do_test pragma-2.2 { 1884303feedSdrh execsql { 18991cf71b0Sdanielk1977 pragma aux.synchronous; 1904303feedSdrh } 19191cf71b0Sdanielk1977} {2} 19291cf71b0Sdanielk1977do_test pragma-2.3 { 19391cf71b0Sdanielk1977 execsql { 19491cf71b0Sdanielk1977 pragma aux.synchronous = OFF; 19591cf71b0Sdanielk1977 pragma aux.synchronous; 19691cf71b0Sdanielk1977 pragma synchronous; 19791cf71b0Sdanielk1977 } 19891cf71b0Sdanielk1977} {0 2} 19991cf71b0Sdanielk1977do_test pragma-2.4 { 20091cf71b0Sdanielk1977 execsql { 20191cf71b0Sdanielk1977 pragma aux.synchronous = ON; 20291cf71b0Sdanielk1977 pragma synchronous; 20391cf71b0Sdanielk1977 pragma aux.synchronous; 20491cf71b0Sdanielk1977 } 20591cf71b0Sdanielk1977} {2 1} 2064303feedSdrh 207ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check 208ed717fe3Sdrh# pragma finds it. 209ed717fe3Sdrh# 210ef4ac8f9Sdrhif {![sqlite3 -has-codec]} { 211ed717fe3Sdrhdo_test pragma-3.1 { 212ed717fe3Sdrh execsql { 213ed717fe3Sdrh BEGIN; 214ed717fe3Sdrh CREATE TABLE t2(a,b,c); 215ed717fe3Sdrh CREATE INDEX i2 ON t2(a); 216ed717fe3Sdrh INSERT INTO t2 VALUES(11,2,3); 217ed717fe3Sdrh INSERT INTO t2 VALUES(22,3,4); 218ed717fe3Sdrh COMMIT; 219ed717fe3Sdrh SELECT rowid, * from t2; 220ed717fe3Sdrh } 221ed717fe3Sdrh} {1 11 2 3 2 22 3 4} 222ed717fe3Sdrhdo_test pragma-3.2 { 223ed717fe3Sdrh set rootpage [execsql {SELECT rootpage FROM sqlite_master WHERE name='i2'}] 22496fc5fe6Sdanielk1977 set db [btree_open test.db 100 0] 225ed717fe3Sdrh btree_begin_transaction $db 226ed717fe3Sdrh set c [btree_cursor $db $rootpage 1] 227ed717fe3Sdrh btree_first $c 228ed717fe3Sdrh btree_delete $c 229ed717fe3Sdrh btree_commit $db 230ed717fe3Sdrh btree_close $db 231ed717fe3Sdrh execsql {PRAGMA integrity_check} 2324be295beSdrh} {{rowid 1 missing from index i2} {wrong # of entries in index i2}} 2339eb9e26bSdrh}; # endif has-codec 2345a38705eSdrh 23591cf71b0Sdanielk1977do_test pragma-3.3 { 23691cf71b0Sdanielk1977 execsql { 23791cf71b0Sdanielk1977 DROP INDEX i2; 23891cf71b0Sdanielk1977 } 23991cf71b0Sdanielk1977} {} 24091cf71b0Sdanielk1977 24191cf71b0Sdanielk1977# Test modifying the cache_size of an attached database. 24291cf71b0Sdanielk1977do_test pragma-4.1 { 24391cf71b0Sdanielk1977 execsql { 24491cf71b0Sdanielk1977 pragma aux.cache_size; 24591cf71b0Sdanielk1977 pragma aux.default_cache_size; 24691cf71b0Sdanielk1977 } 24791cf71b0Sdanielk1977} {2000 2000} 2481bdd9b57Sdrhdo_test pragma-4.2 { 2491bdd9b57Sdrh execsql { 25091cf71b0Sdanielk1977 pragma aux.cache_size = 50; 25191cf71b0Sdanielk1977 pragma aux.cache_size; 25291cf71b0Sdanielk1977 pragma aux.default_cache_size; 2531bdd9b57Sdrh } 25491cf71b0Sdanielk1977} {50 2000} 2551bdd9b57Sdrhdo_test pragma-4.3 { 2561bdd9b57Sdrh execsql { 25791cf71b0Sdanielk1977 pragma aux.default_cache_size = 456; 25891cf71b0Sdanielk1977 pragma aux.cache_size; 25991cf71b0Sdanielk1977 pragma aux.default_cache_size; 2601bdd9b57Sdrh } 26191cf71b0Sdanielk1977} {456 456} 2621bdd9b57Sdrhdo_test pragma-4.4 { 2631bdd9b57Sdrh execsql { 26491cf71b0Sdanielk1977 pragma cache_size; 26591cf71b0Sdanielk1977 pragma default_cache_size; 2661bdd9b57Sdrh } 26791cf71b0Sdanielk1977} {123 123} 2681bdd9b57Sdrhdo_test pragma-4.5 { 2691bdd9b57Sdrh execsql { 27091cf71b0Sdanielk1977 DETACH aux; 27191cf71b0Sdanielk1977 ATTACH 'test3.db' AS aux; 27291cf71b0Sdanielk1977 pragma aux.cache_size; 27391cf71b0Sdanielk1977 pragma aux.default_cache_size; 2741bdd9b57Sdrh } 27591cf71b0Sdanielk1977} {2000 2000} 2761bdd9b57Sdrhdo_test pragma-4.6 { 2771bdd9b57Sdrh execsql { 27891cf71b0Sdanielk1977 DETACH aux; 27991cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 28091cf71b0Sdanielk1977 pragma aux.cache_size; 28191cf71b0Sdanielk1977 pragma aux.default_cache_size; 2821bdd9b57Sdrh } 28391cf71b0Sdanielk1977} {456 456} 2841bdd9b57Sdrh 28591cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is 28691cf71b0Sdanielk1977# disallowed. 28791cf71b0Sdanielk1977do_test pragma-5.0 { 2881bdd9b57Sdrh execsql { 28991cf71b0Sdanielk1977 pragma synchronous; 2901bdd9b57Sdrh } 29191cf71b0Sdanielk1977} {2} 29291cf71b0Sdanielk1977do_test pragma-5.1 { 2931bdd9b57Sdrh catchsql { 2941bdd9b57Sdrh BEGIN; 29591cf71b0Sdanielk1977 pragma synchronous = OFF; 2961bdd9b57Sdrh } 29791cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}} 29891cf71b0Sdanielk1977do_test pragma-5.2 { 29991cf71b0Sdanielk1977 execsql { 30091cf71b0Sdanielk1977 pragma synchronous; 30191cf71b0Sdanielk1977 } 30291cf71b0Sdanielk1977} {2} 303*5260f7e9Sdrhcatchsql {COMMIT;} 304*5260f7e9Sdrh 305*5260f7e9Sdrh# Test schema-query pragmas 306*5260f7e9Sdrh# 307*5260f7e9Sdrhdo_test pragma-6.1 { 308*5260f7e9Sdrh foreach {idx name file} [execsql {pragma database_list}] { 309*5260f7e9Sdrh lappend res $idx $name 310*5260f7e9Sdrh } 311*5260f7e9Sdrh set res 312*5260f7e9Sdrh} {0 main 1 temp 2 aux} 313*5260f7e9Sdrhdo_test pragma-6.2 { 314*5260f7e9Sdrh execsql { 315*5260f7e9Sdrh pragma table_info(t2) 316*5260f7e9Sdrh } 317*5260f7e9Sdrh} {0 a numeric 0 {} 0 1 b numeric 0 {} 0 2 c numeric 0 {} 0} 318*5260f7e9Sdrhdo_test pragma-6.3 { 319*5260f7e9Sdrh execsql { 320*5260f7e9Sdrh CREATE TABLE t3(a int references t2(b), b UNIQUE); 321*5260f7e9Sdrh pragma foreign_key_list(t3); 322*5260f7e9Sdrh } 323*5260f7e9Sdrh} {0 0 t2 a b} 324*5260f7e9Sdrhdo_test pragma-6.4 { 325*5260f7e9Sdrh execsql { 326*5260f7e9Sdrh pragma index_list(t3); 327*5260f7e9Sdrh } 328*5260f7e9Sdrh} {0 sqlite_autoindex_t3_1 1} 329*5260f7e9Sdrhdo_test pragma-6.5 { 330*5260f7e9Sdrh execsql { 331*5260f7e9Sdrh CREATE INDEX t3i1 ON t3(a,b); 332*5260f7e9Sdrh pragma index_info(t3i1); 333*5260f7e9Sdrh } 334*5260f7e9Sdrh} {0 0 a 1 1 b} 335*5260f7e9Sdrh 336*5260f7e9Sdrh# Miscellaneous tests 337*5260f7e9Sdrh# 338*5260f7e9Sdrhdo_test pragma-7.1 { 339*5260f7e9Sdrh # Make sure a pragma knows to read the schema if it needs to 340*5260f7e9Sdrh db close 341*5260f7e9Sdrh sqlite3 db test.db 342*5260f7e9Sdrh execsql { 343*5260f7e9Sdrh pragma index_list(t3); 344*5260f7e9Sdrh } 345*5260f7e9Sdrh} {0 t3i1 0 1 sqlite_autoindex_t3_1 1} 346*5260f7e9Sdrhdo_test pragma-7.2 { 347*5260f7e9Sdrh db close 348*5260f7e9Sdrh sqlite3 db test.db 349*5260f7e9Sdrh catchsql { 350*5260f7e9Sdrh pragma encoding=bogus; 351*5260f7e9Sdrh } 352*5260f7e9Sdrh} {1 {unsupported encoding: bogus}} 353*5260f7e9Sdrhdo_test pragma-7.3 { 354*5260f7e9Sdrh db close 355*5260f7e9Sdrh sqlite3 db test.db 356*5260f7e9Sdrh execsql { 357*5260f7e9Sdrh pragma lock_status; 358*5260f7e9Sdrh } 359*5260f7e9Sdrh} {main unlocked temp closed} 360*5260f7e9Sdrh 361*5260f7e9Sdrh 3621bdd9b57Sdrh 363cd61c281Sdrhfinish_test 364