1*c5c4113dSnw141292 2*c5c4113dSnw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*c5c4113dSnw141292 4*c5c4113dSnw141292# 2002 March 6 5*c5c4113dSnw141292# 6*c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 7*c5c4113dSnw141292# a legal notice, here is a blessing: 8*c5c4113dSnw141292# 9*c5c4113dSnw141292# May you do good and not evil. 10*c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 11*c5c4113dSnw141292# May you share freely, never taking more than you give. 12*c5c4113dSnw141292# 13*c5c4113dSnw141292#*********************************************************************** 14*c5c4113dSnw141292# This file implements regression tests for SQLite library. 15*c5c4113dSnw141292# 16*c5c4113dSnw141292# This file implements tests for the PRAGMA command. 17*c5c4113dSnw141292# 18*c5c4113dSnw141292# $Id: pragma.test,v 1.9 2004/04/23 17:04:45 drh Exp $ 19*c5c4113dSnw141292 20*c5c4113dSnw141292set testdir [file dirname $argv0] 21*c5c4113dSnw141292source $testdir/tester.tcl 22*c5c4113dSnw141292 23*c5c4113dSnw141292# Delete the preexisting database to avoid the special setup 24*c5c4113dSnw141292# that the "all.test" script does. 25*c5c4113dSnw141292# 26*c5c4113dSnw141292db close 27*c5c4113dSnw141292file delete test.db 28*c5c4113dSnw141292set DB [sqlite db test.db] 29*c5c4113dSnw141292 30*c5c4113dSnw141292do_test pragma-1.1 { 31*c5c4113dSnw141292 execsql { 32*c5c4113dSnw141292 PRAGMA cache_size; 33*c5c4113dSnw141292 PRAGMA default_cache_size; 34*c5c4113dSnw141292 PRAGMA synchronous; 35*c5c4113dSnw141292 PRAGMA default_synchronous; 36*c5c4113dSnw141292 } 37*c5c4113dSnw141292} {2000 2000 1 1} 38*c5c4113dSnw141292do_test pragma-1.2 { 39*c5c4113dSnw141292 execsql { 40*c5c4113dSnw141292 PRAGMA cache_size=1234; 41*c5c4113dSnw141292 PRAGMA cache_size; 42*c5c4113dSnw141292 PRAGMA default_cache_size; 43*c5c4113dSnw141292 PRAGMA synchronous; 44*c5c4113dSnw141292 PRAGMA default_synchronous; 45*c5c4113dSnw141292 } 46*c5c4113dSnw141292} {1234 2000 1 1} 47*c5c4113dSnw141292do_test pragma-1.3 { 48*c5c4113dSnw141292 db close 49*c5c4113dSnw141292 sqlite db test.db 50*c5c4113dSnw141292 execsql { 51*c5c4113dSnw141292 PRAGMA cache_size; 52*c5c4113dSnw141292 PRAGMA default_cache_size; 53*c5c4113dSnw141292 PRAGMA synchronous; 54*c5c4113dSnw141292 PRAGMA default_synchronous; 55*c5c4113dSnw141292 } 56*c5c4113dSnw141292} {2000 2000 1 1} 57*c5c4113dSnw141292do_test pragma-1.4 { 58*c5c4113dSnw141292 execsql { 59*c5c4113dSnw141292 PRAGMA synchronous=OFF; 60*c5c4113dSnw141292 PRAGMA cache_size; 61*c5c4113dSnw141292 PRAGMA default_cache_size; 62*c5c4113dSnw141292 PRAGMA synchronous; 63*c5c4113dSnw141292 PRAGMA default_synchronous; 64*c5c4113dSnw141292 } 65*c5c4113dSnw141292} {2000 2000 0 1} 66*c5c4113dSnw141292do_test pragma-1.5 { 67*c5c4113dSnw141292 execsql { 68*c5c4113dSnw141292 PRAGMA cache_size=4321; 69*c5c4113dSnw141292 PRAGMA cache_size; 70*c5c4113dSnw141292 PRAGMA default_cache_size; 71*c5c4113dSnw141292 PRAGMA synchronous; 72*c5c4113dSnw141292 PRAGMA default_synchronous; 73*c5c4113dSnw141292 } 74*c5c4113dSnw141292} {4321 2000 0 1} 75*c5c4113dSnw141292do_test pragma-1.6 { 76*c5c4113dSnw141292 execsql { 77*c5c4113dSnw141292 PRAGMA synchronous=ON; 78*c5c4113dSnw141292 PRAGMA cache_size; 79*c5c4113dSnw141292 PRAGMA default_cache_size; 80*c5c4113dSnw141292 PRAGMA synchronous; 81*c5c4113dSnw141292 PRAGMA default_synchronous; 82*c5c4113dSnw141292 } 83*c5c4113dSnw141292} {4321 2000 1 1} 84*c5c4113dSnw141292do_test pragma-1.7 { 85*c5c4113dSnw141292 db close 86*c5c4113dSnw141292 sqlite db test.db 87*c5c4113dSnw141292 execsql { 88*c5c4113dSnw141292 PRAGMA cache_size; 89*c5c4113dSnw141292 PRAGMA default_cache_size; 90*c5c4113dSnw141292 PRAGMA synchronous; 91*c5c4113dSnw141292 PRAGMA default_synchronous; 92*c5c4113dSnw141292 } 93*c5c4113dSnw141292} {2000 2000 1 1} 94*c5c4113dSnw141292do_test pragma-1.8 { 95*c5c4113dSnw141292 execsql { 96*c5c4113dSnw141292 PRAGMA default_synchronous=OFF; 97*c5c4113dSnw141292 PRAGMA cache_size; 98*c5c4113dSnw141292 PRAGMA default_cache_size; 99*c5c4113dSnw141292 PRAGMA synchronous; 100*c5c4113dSnw141292 PRAGMA default_synchronous; 101*c5c4113dSnw141292 } 102*c5c4113dSnw141292} {2000 2000 0 0} 103*c5c4113dSnw141292do_test pragma-1.9 { 104*c5c4113dSnw141292 execsql { 105*c5c4113dSnw141292 PRAGMA default_cache_size=123; 106*c5c4113dSnw141292 PRAGMA cache_size; 107*c5c4113dSnw141292 PRAGMA default_cache_size; 108*c5c4113dSnw141292 PRAGMA synchronous; 109*c5c4113dSnw141292 PRAGMA default_synchronous; 110*c5c4113dSnw141292 } 111*c5c4113dSnw141292} {123 123 0 0} 112*c5c4113dSnw141292do_test pragma-1.10 { 113*c5c4113dSnw141292 db close 114*c5c4113dSnw141292 set ::DB [sqlite db test.db] 115*c5c4113dSnw141292 execsql { 116*c5c4113dSnw141292 PRAGMA cache_size; 117*c5c4113dSnw141292 PRAGMA default_cache_size; 118*c5c4113dSnw141292 PRAGMA synchronous; 119*c5c4113dSnw141292 PRAGMA default_synchronous; 120*c5c4113dSnw141292 } 121*c5c4113dSnw141292} {123 123 0 0} 122*c5c4113dSnw141292do_test pragma-1.11 { 123*c5c4113dSnw141292 execsql { 124*c5c4113dSnw141292 PRAGMA synchronous=NORMAL; 125*c5c4113dSnw141292 PRAGMA cache_size; 126*c5c4113dSnw141292 PRAGMA default_cache_size; 127*c5c4113dSnw141292 PRAGMA synchronous; 128*c5c4113dSnw141292 PRAGMA default_synchronous; 129*c5c4113dSnw141292 } 130*c5c4113dSnw141292} {123 123 1 0} 131*c5c4113dSnw141292do_test pragma-1.12 { 132*c5c4113dSnw141292 execsql { 133*c5c4113dSnw141292 PRAGMA synchronous=FULL; 134*c5c4113dSnw141292 PRAGMA cache_size; 135*c5c4113dSnw141292 PRAGMA default_cache_size; 136*c5c4113dSnw141292 PRAGMA synchronous; 137*c5c4113dSnw141292 PRAGMA default_synchronous; 138*c5c4113dSnw141292 } 139*c5c4113dSnw141292} {123 123 2 0} 140*c5c4113dSnw141292do_test pragma-1.13 { 141*c5c4113dSnw141292 db close 142*c5c4113dSnw141292 set ::DB [sqlite db test.db] 143*c5c4113dSnw141292 execsql { 144*c5c4113dSnw141292 PRAGMA cache_size; 145*c5c4113dSnw141292 PRAGMA default_cache_size; 146*c5c4113dSnw141292 PRAGMA synchronous; 147*c5c4113dSnw141292 PRAGMA default_synchronous; 148*c5c4113dSnw141292 } 149*c5c4113dSnw141292} {123 123 0 0} 150*c5c4113dSnw141292do_test pragma-1.14 { 151*c5c4113dSnw141292 execsql { 152*c5c4113dSnw141292 PRAGMA default_synchronous=FULL; 153*c5c4113dSnw141292 PRAGMA cache_size; 154*c5c4113dSnw141292 PRAGMA default_cache_size; 155*c5c4113dSnw141292 PRAGMA synchronous; 156*c5c4113dSnw141292 PRAGMA default_synchronous; 157*c5c4113dSnw141292 } 158*c5c4113dSnw141292} {123 123 2 2} 159*c5c4113dSnw141292do_test pragma-1.15 { 160*c5c4113dSnw141292 db close 161*c5c4113dSnw141292 set ::DB [sqlite db test.db] 162*c5c4113dSnw141292 execsql { 163*c5c4113dSnw141292 PRAGMA cache_size; 164*c5c4113dSnw141292 PRAGMA default_cache_size; 165*c5c4113dSnw141292 PRAGMA synchronous; 166*c5c4113dSnw141292 PRAGMA default_synchronous; 167*c5c4113dSnw141292 } 168*c5c4113dSnw141292} {123 123 2 2} 169*c5c4113dSnw141292 170*c5c4113dSnw141292do_test pragma-2.1 { 171*c5c4113dSnw141292 execsql { 172*c5c4113dSnw141292 PRAGMA show_datatypes=on; 173*c5c4113dSnw141292 PRAGMA empty_result_callbacks=off; 174*c5c4113dSnw141292 } 175*c5c4113dSnw141292 sqlite_datatypes $::DB {SELECT * FROM sqlite_master} 176*c5c4113dSnw141292} {} 177*c5c4113dSnw141292do_test pragma-2.2 { 178*c5c4113dSnw141292 execsql { 179*c5c4113dSnw141292 PRAGMA empty_result_callbacks=on; 180*c5c4113dSnw141292 } 181*c5c4113dSnw141292 sqlite_datatypes $::DB {SELECT * FROM sqlite_master} 182*c5c4113dSnw141292} {text text text integer text} 183*c5c4113dSnw141292 184*c5c4113dSnw141292# Make sure we can read the schema when empty_result_callbacks are 185*c5c4113dSnw141292# turned on. Ticket #406 186*c5c4113dSnw141292do_test pragma-2.2.1 { 187*c5c4113dSnw141292 execsql { 188*c5c4113dSnw141292 BEGIN; 189*c5c4113dSnw141292 CREATE TABLE tabx(a,b,c,d); 190*c5c4113dSnw141292 ROLLBACK; 191*c5c4113dSnw141292 SELECT count(*) FROM sqlite_master; 192*c5c4113dSnw141292 } 193*c5c4113dSnw141292} {0} 194*c5c4113dSnw141292 195*c5c4113dSnw141292do_test pragma-2.3 { 196*c5c4113dSnw141292 execsql { 197*c5c4113dSnw141292 CREATE TABLE t1( 198*c5c4113dSnw141292 a INTEGER, 199*c5c4113dSnw141292 b TEXT, 200*c5c4113dSnw141292 c WHATEVER, 201*c5c4113dSnw141292 d CLOB, 202*c5c4113dSnw141292 e BLOB, 203*c5c4113dSnw141292 f VARCHAR(123), 204*c5c4113dSnw141292 g nVaRcHaR(432) 205*c5c4113dSnw141292 ); 206*c5c4113dSnw141292 } 207*c5c4113dSnw141292 sqlite_datatypes $::DB {SELECT * FROM t1} 208*c5c4113dSnw141292} {INTEGER TEXT WHATEVER CLOB BLOB VARCHAR(123) nVaRcHaR(432)} 209*c5c4113dSnw141292do_test pragma-2.4 { 210*c5c4113dSnw141292 sqlite_datatypes $::DB { 211*c5c4113dSnw141292 SELECT 1, 'hello', NULL 212*c5c4113dSnw141292 } 213*c5c4113dSnw141292} {NUMERIC TEXT TEXT} 214*c5c4113dSnw141292do_test pragma-2.5 { 215*c5c4113dSnw141292 sqlite_datatypes $::DB { 216*c5c4113dSnw141292 SELECT 1+2 AS X, 'hello' || 5 AS Y, NULL AS Z 217*c5c4113dSnw141292 } 218*c5c4113dSnw141292} {NUMERIC TEXT TEXT} 219*c5c4113dSnw141292do_test pragma-2.6 { 220*c5c4113dSnw141292 execsql { 221*c5c4113dSnw141292 CREATE VIEW v1 AS SELECT a+b, b||c, * FROM t1; 222*c5c4113dSnw141292 } 223*c5c4113dSnw141292 sqlite_datatypes $::DB {SELECT * FROM v1} 224*c5c4113dSnw141292} {NUMERIC TEXT INTEGER TEXT WHATEVER CLOB BLOB VARCHAR(123) nVaRcHaR(432)} 225*c5c4113dSnw141292do_test pragma-2.7 { 226*c5c4113dSnw141292 sqlite_datatypes $::DB { 227*c5c4113dSnw141292 SELECT d,e FROM t1 UNION SELECT a,c FROM t1 228*c5c4113dSnw141292 } 229*c5c4113dSnw141292} {INTEGER WHATEVER} 230*c5c4113dSnw141292do_test pragma-2.8 { 231*c5c4113dSnw141292 sqlite_datatypes $::DB { 232*c5c4113dSnw141292 SELECT d,e FROM t1 EXCEPT SELECT c,e FROM t1 233*c5c4113dSnw141292 } 234*c5c4113dSnw141292} {WHATEVER BLOB} 235*c5c4113dSnw141292do_test pragma-2.9 { 236*c5c4113dSnw141292 sqlite_datatypes $::DB { 237*c5c4113dSnw141292 SELECT d,e FROM t1 INTERSECT SELECT c,e FROM t1 238*c5c4113dSnw141292 } 239*c5c4113dSnw141292} {WHATEVER BLOB} 240*c5c4113dSnw141292do_test pragma-2.10 { 241*c5c4113dSnw141292 sqlite_datatypes $::DB { 242*c5c4113dSnw141292 SELECT d,e FROM t1 INTERSECT SELECT c,e FROM v1 243*c5c4113dSnw141292 } 244*c5c4113dSnw141292} {WHATEVER BLOB} 245*c5c4113dSnw141292 246*c5c4113dSnw141292# Construct a corrupted index and make sure the integrity_check 247*c5c4113dSnw141292# pragma finds it. 248*c5c4113dSnw141292# 249*c5c4113dSnw141292if {![sqlite -has-codec]} { 250*c5c4113dSnw141292do_test pragma-3.1 { 251*c5c4113dSnw141292 execsql { 252*c5c4113dSnw141292 BEGIN; 253*c5c4113dSnw141292 CREATE TABLE t2(a,b,c); 254*c5c4113dSnw141292 CREATE INDEX i2 ON t2(a); 255*c5c4113dSnw141292 INSERT INTO t2 VALUES(11,2,3); 256*c5c4113dSnw141292 INSERT INTO t2 VALUES(22,3,4); 257*c5c4113dSnw141292 COMMIT; 258*c5c4113dSnw141292 SELECT rowid, * from t2; 259*c5c4113dSnw141292 } 260*c5c4113dSnw141292} {1 11 2 3 2 22 3 4} 261*c5c4113dSnw141292do_test pragma-3.2 { 262*c5c4113dSnw141292 set rootpage [execsql {SELECT rootpage FROM sqlite_master WHERE name='i2'}] 263*c5c4113dSnw141292 set db [btree_open test.db] 264*c5c4113dSnw141292 btree_begin_transaction $db 265*c5c4113dSnw141292 set c [btree_cursor $db $rootpage 1] 266*c5c4113dSnw141292 btree_first $c 267*c5c4113dSnw141292 btree_delete $c 268*c5c4113dSnw141292 btree_commit $db 269*c5c4113dSnw141292 btree_close $db 270*c5c4113dSnw141292 execsql {PRAGMA integrity_check} 271*c5c4113dSnw141292} {{rowid 1 missing from index i2} {wrong # of entries in index i2}} 272*c5c4113dSnw141292}; # endif has-codec 273*c5c4113dSnw141292 274*c5c4113dSnw141292# Test the temp_store and default_temp_store pragmas 275*c5c4113dSnw141292# 276*c5c4113dSnw141292do_test pragma-4.2 { 277*c5c4113dSnw141292 execsql { 278*c5c4113dSnw141292 PRAGMA temp_store='default'; 279*c5c4113dSnw141292 PRAGMA temp_store; 280*c5c4113dSnw141292 } 281*c5c4113dSnw141292} {0} 282*c5c4113dSnw141292do_test pragma-4.3 { 283*c5c4113dSnw141292 execsql { 284*c5c4113dSnw141292 PRAGMA temp_store='file'; 285*c5c4113dSnw141292 PRAGMA temp_store; 286*c5c4113dSnw141292 } 287*c5c4113dSnw141292} {1} 288*c5c4113dSnw141292do_test pragma-4.4 { 289*c5c4113dSnw141292 execsql { 290*c5c4113dSnw141292 PRAGMA temp_store='memory'; 291*c5c4113dSnw141292 PRAGMA temp_store; 292*c5c4113dSnw141292 } 293*c5c4113dSnw141292} {2} 294*c5c4113dSnw141292do_test pragma-4.5 { 295*c5c4113dSnw141292 execsql { 296*c5c4113dSnw141292 PRAGMA default_temp_store='default'; 297*c5c4113dSnw141292 PRAGMA default_temp_store; 298*c5c4113dSnw141292 } 299*c5c4113dSnw141292} {0} 300*c5c4113dSnw141292do_test pragma-4.6 { 301*c5c4113dSnw141292 execsql { 302*c5c4113dSnw141292 PRAGMA temp_store; 303*c5c4113dSnw141292 } 304*c5c4113dSnw141292} {2} 305*c5c4113dSnw141292do_test pragma-4.7 { 306*c5c4113dSnw141292 db close 307*c5c4113dSnw141292 sqlite db test.db 308*c5c4113dSnw141292 execsql { 309*c5c4113dSnw141292 PRAGMA temp_store; 310*c5c4113dSnw141292 } 311*c5c4113dSnw141292} {0} 312*c5c4113dSnw141292do_test pragma-4.8 { 313*c5c4113dSnw141292 execsql { 314*c5c4113dSnw141292 PRAGMA default_temp_store; 315*c5c4113dSnw141292 } 316*c5c4113dSnw141292} {0} 317*c5c4113dSnw141292do_test pragma-4.9 { 318*c5c4113dSnw141292 execsql { 319*c5c4113dSnw141292 PRAGMA default_temp_store='file'; 320*c5c4113dSnw141292 PRAGMA default_temp_store; 321*c5c4113dSnw141292 } 322*c5c4113dSnw141292} {1} 323*c5c4113dSnw141292do_test pragma-4.10 { 324*c5c4113dSnw141292 execsql { 325*c5c4113dSnw141292 PRAGMA temp_store; 326*c5c4113dSnw141292 } 327*c5c4113dSnw141292} {0} 328*c5c4113dSnw141292do_test pragma-4.11 { 329*c5c4113dSnw141292 db close 330*c5c4113dSnw141292 sqlite db test.db 331*c5c4113dSnw141292 execsql { 332*c5c4113dSnw141292 PRAGMA temp_store; 333*c5c4113dSnw141292 } 334*c5c4113dSnw141292} {1} 335*c5c4113dSnw141292do_test pragma-4.12 { 336*c5c4113dSnw141292 execsql { 337*c5c4113dSnw141292 PRAGMA default_temp_store; 338*c5c4113dSnw141292 } 339*c5c4113dSnw141292} {1} 340*c5c4113dSnw141292do_test pragma-4.13 { 341*c5c4113dSnw141292 execsql { 342*c5c4113dSnw141292 PRAGMA default_temp_store='memory'; 343*c5c4113dSnw141292 PRAGMA default_temp_store; 344*c5c4113dSnw141292 } 345*c5c4113dSnw141292} {2} 346*c5c4113dSnw141292do_test pragma-4.14 { 347*c5c4113dSnw141292 execsql { 348*c5c4113dSnw141292 PRAGMA temp_store; 349*c5c4113dSnw141292 } 350*c5c4113dSnw141292} {1} 351*c5c4113dSnw141292do_test pragma-4.15 { 352*c5c4113dSnw141292 db close 353*c5c4113dSnw141292 sqlite db test.db 354*c5c4113dSnw141292 execsql { 355*c5c4113dSnw141292 PRAGMA temp_store; 356*c5c4113dSnw141292 } 357*c5c4113dSnw141292} {2} 358*c5c4113dSnw141292do_test pragma-4.16 { 359*c5c4113dSnw141292 execsql { 360*c5c4113dSnw141292 PRAGMA default_temp_store; 361*c5c4113dSnw141292 } 362*c5c4113dSnw141292} {2} 363*c5c4113dSnw141292do_test pragma-4.17 { 364*c5c4113dSnw141292 execsql { 365*c5c4113dSnw141292 PRAGMA temp_store='file'; 366*c5c4113dSnw141292 PRAGMA temp_store 367*c5c4113dSnw141292 } 368*c5c4113dSnw141292} {1} 369*c5c4113dSnw141292do_test pragma-4.18 { 370*c5c4113dSnw141292 execsql { 371*c5c4113dSnw141292 PRAGMA default_temp_store 372*c5c4113dSnw141292 } 373*c5c4113dSnw141292} {2} 374*c5c4113dSnw141292do_test pragma-4.19 { 375*c5c4113dSnw141292 db close 376*c5c4113dSnw141292 sqlite db test.db 377*c5c4113dSnw141292 execsql { 378*c5c4113dSnw141292 PRAGMA temp_store 379*c5c4113dSnw141292 } 380*c5c4113dSnw141292} {2} 381*c5c4113dSnw141292 382*c5c4113dSnw141292# Changing the TEMP_STORE deletes any existing temporary tables 383*c5c4113dSnw141292# 384*c5c4113dSnw141292do_test pragma-4.20 { 385*c5c4113dSnw141292 execsql {SELECT name FROM sqlite_temp_master} 386*c5c4113dSnw141292} {} 387*c5c4113dSnw141292do_test pragma-4.21 { 388*c5c4113dSnw141292 execsql { 389*c5c4113dSnw141292 CREATE TEMP TABLE test1(a,b,c); 390*c5c4113dSnw141292 SELECT name FROM sqlite_temp_master; 391*c5c4113dSnw141292 } 392*c5c4113dSnw141292} {test1} 393*c5c4113dSnw141292do_test pragma-4.22 { 394*c5c4113dSnw141292 execsql { 395*c5c4113dSnw141292 PRAGMA temp_store='file'; 396*c5c4113dSnw141292 SELECT name FROM sqlite_temp_master; 397*c5c4113dSnw141292 } 398*c5c4113dSnw141292} {} 399*c5c4113dSnw141292do_test pragma-4.23 { 400*c5c4113dSnw141292 execsql { 401*c5c4113dSnw141292 CREATE TEMP TABLE test1(a,b,c); 402*c5c4113dSnw141292 SELECT name FROM sqlite_temp_master; 403*c5c4113dSnw141292 } 404*c5c4113dSnw141292} {test1} 405*c5c4113dSnw141292do_test pragma-4.24 { 406*c5c4113dSnw141292 execsql { 407*c5c4113dSnw141292 PRAGMA temp_store='memory'; 408*c5c4113dSnw141292 SELECT name FROM sqlite_temp_master; 409*c5c4113dSnw141292 } 410*c5c4113dSnw141292} {} 411*c5c4113dSnw141292do_test pragma-4.25 { 412*c5c4113dSnw141292 catchsql { 413*c5c4113dSnw141292 BEGIN; 414*c5c4113dSnw141292 PRAGMA temp_store='default'; 415*c5c4113dSnw141292 COMMIT; 416*c5c4113dSnw141292 } 417*c5c4113dSnw141292} {1 {temporary storage cannot be changed from within a transaction}} 418*c5c4113dSnw141292catchsql {COMMIT} 419*c5c4113dSnw141292 420*c5c4113dSnw141292finish_test 421