Fix a problem causing the recovery extension to use excessive memory and CPU time...
[sqlite.git] / test / pragma.test
blob048dee8386695883679bb4d9c0077df949e6d0b3
1 # 2002 March 6
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for the PRAGMA command.
15 # $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix pragma
21 # Do not use a codec for tests in this file, as the database file is
22 # manipulated directly using tcl scripts (using the [hexio_write] command).
24 do_not_use_codec
26 # Test organization:
28 # pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
29 # pragma-2.*: Test synchronous on attached db.
30 # pragma-3.*: Test detection of table/index inconsistency by integrity_check.
31 # pragma-4.*: Test cache_size and default_cache_size on attached db.
32 # pragma-5.*: Test that pragma synchronous may not be used inside of a
33 #             transaction.
34 # pragma-6.*: Test schema-query pragmas.
35 # pragma-7.*: Miscellaneous tests.
36 # pragma-8.*: Test user_version and schema_version pragmas.
37 # pragma-9.*: Test temp_store and temp_store_directory.
38 # pragma-10.*: Test the count_changes pragma in the presence of triggers.
39 # pragma-11.*: Test the collation_list pragma.
40 # pragma-14.*: Test the page_count pragma.
41 # pragma-15.*: Test that the value set using the cache_size pragma is not
42 #              reset when the schema is reloaded.
43 # pragma-16.*: Test proxy locking
44 # pragma-20.*: Test data_store_directory.
45 # pragma-22.*: Test that "PRAGMA [db].integrity_check" respects the "db"
46 #              directive - if it is present.
49 ifcapable !pragma {
50   finish_test
51   return
54 # Capture the output of a pragma in a TEMP table.
56 proc capture_pragma {db tabname sql} {
57   $db eval "DROP TABLE IF EXISTS temp.$tabname"
58   set once 1
59   $db eval $sql x {
60     if {$once} {
61       set once 0
62       set ins "INSERT INTO $tabname VALUES"
63       set crtab "CREATE TEMP TABLE $tabname "
64       set sep "("
65       foreach col $x(*) {
66         append ins ${sep}\$x($col)
67         append crtab ${sep}\"$col\"
68         set sep ,
69       }
70       append ins )
71       append crtab )
72       $db eval $crtab
73     }
74     $db eval $ins
75   }
78 # Delete the preexisting database to avoid the special setup
79 # that the "all.test" script does.
81 db close
82 delete_file test.db test.db-journal
83 delete_file test3.db test3.db-journal
84 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
86 # EVIDENCE-OF: R-13861-56665 PRAGMA schema.cache_size; PRAGMA
87 # schema.cache_size = pages; PRAGMA schema.cache_size = -kibibytes;
88 # Query or change the suggested maximum number of database disk pages
89 # that SQLite will hold in memory at once per open database file.
91 ifcapable pager_pragmas {
92 set DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
93 set TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
94 do_test pragma-1.1 {
95   execsql {
96     PRAGMA cache_size;
97     PRAGMA default_cache_size;
98     PRAGMA synchronous;
99   }
100 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
101 do_test pragma-1.2 {
102   # EVIDENCE-OF: R-42059-47211 If the argument N is positive then the
103   # suggested cache size is set to N.
104   execsql {
105     PRAGMA synchronous=OFF;
106     PRAGMA cache_size=1234;
107     PRAGMA cache_size;
108     PRAGMA default_cache_size;
109     PRAGMA synchronous;
110   }
111 } [list 1234 $DFLT_CACHE_SZ 0]
112 do_test pragma-1.3 {
113   db close
114   sqlite3 db test.db
115   execsql {
116     PRAGMA cache_size;
117     PRAGMA default_cache_size;
118     PRAGMA synchronous;
119   }
120 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
121 do_test pragma-1.4 {
122   execsql {
123     PRAGMA synchronous=OFF;
124     PRAGMA cache_size;
125     PRAGMA default_cache_size;
126     PRAGMA synchronous;
127   }
128 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
129 do_test pragma-1.5 {
130   execsql {
131     PRAGMA cache_size=-4321;
132     PRAGMA cache_size;
133     PRAGMA default_cache_size;
134     PRAGMA synchronous;
135   }
136 } [list -4321 $DFLT_CACHE_SZ 0]
137 do_test pragma-1.6 {
138   execsql {
139     PRAGMA synchronous=ON;
140     PRAGMA cache_size;
141     PRAGMA default_cache_size;
142     PRAGMA synchronous;
143   }
144 } [list -4321 $DFLT_CACHE_SZ 1]
145 do_test pragma-1.7 {
146   db close
147   sqlite3 db test.db
148   execsql {
149     PRAGMA cache_size;
150     PRAGMA default_cache_size;
151     PRAGMA synchronous;
152   }
153 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
154 do_test pragma-1.8 {
155   execsql {
156     PRAGMA default_cache_size=-123;
157     PRAGMA cache_size;
158     PRAGMA default_cache_size;
159     PRAGMA synchronous;
160   }
161 } {123 123 2}
162 do_test pragma-1.9.1 {
163   db close
164   sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
165   execsql {
166     PRAGMA cache_size;
167     PRAGMA default_cache_size;
168     PRAGMA synchronous;
169   }
170 } {123 123 2}
171 ifcapable vacuum {
172   do_test pragma-1.9.2 {
173     execsql {
174       VACUUM;
175       PRAGMA cache_size;
176       PRAGMA default_cache_size;
177       PRAGMA synchronous;
178     }
179   } {123 123 2}
181 do_test pragma-1.10 {
182   execsql {
183     PRAGMA synchronous=NORMAL;
184     PRAGMA cache_size;
185     PRAGMA default_cache_size;
186     PRAGMA synchronous;
187   }
188 } {123 123 1}
189 do_test pragma-1.11.1 {
190   execsql {
191     PRAGMA synchronous=EXTRA;
192     PRAGMA cache_size;
193     PRAGMA default_cache_size;
194     PRAGMA synchronous;
195   }
196 } {123 123 3}
197 do_test pragma-1.11.2 {
198   execsql {
199     PRAGMA synchronous=FULL;
200     PRAGMA cache_size;
201     PRAGMA default_cache_size;
202     PRAGMA synchronous;
203   }
204 } {123 123 2}
205 do_test pragma-1.12 {
206   db close
207   sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
208   execsql {
209     PRAGMA cache_size;
210     PRAGMA default_cache_size;
211     PRAGMA synchronous;
212   }
213 } {123 123 2}
215 # Make sure the pragma handler understands numeric values in addition
216 # to keywords like "off" and "full".
218 do_test pragma-1.13 {
219   execsql {
220     PRAGMA synchronous=0;
221     PRAGMA synchronous;
222   }
223 } {0}
224 do_test pragma-1.14 {
225   execsql {
226     PRAGMA synchronous=2;
227     PRAGMA synchronous;
228   }
229 } {2}
230 do_test pragma-1.14.1 {
231   execsql {
232     PRAGMA synchronous=4;
233     PRAGMA synchronous;
234   }
235 } {4}
236 do_test pragma-1.14.2 {
237   execsql {
238     PRAGMA synchronous=3;
239     PRAGMA synchronous;
240   }
241 } {3}
242 do_test pragma-1.14.3 {
243   execsql {
244     PRAGMA synchronous=8;
245     PRAGMA synchronous;
246   }
247 } {0}
248 do_test pragma-1.14.4 {
249   execsql {
250     PRAGMA synchronous=10;
251     PRAGMA synchronous;
252   }
253 } {2}
255 do_execsql_test 1.15.1 {
256   PRAGMA default_cache_size = 0;
258 do_execsql_test 1.15.2 {
259   PRAGMA default_cache_size;
260 } $DFLT_CACHE_SZ
261 do_execsql_test 1.15.3 {
262   PRAGMA default_cache_size = -500;
264 do_execsql_test 1.15.4 {
265   PRAGMA default_cache_size;
266 } 500
267 do_execsql_test 1.15.3 {
268   PRAGMA default_cache_size = 500;
270 do_execsql_test 1.15.4 {
271   PRAGMA default_cache_size;
272 } 500
273 db close
274 hexio_write test.db 48 FFFFFF00
275 sqlite3 db test.db
276 do_execsql_test 1.15.4 {
277   PRAGMA default_cache_size;
278 } 256
279 } ;# ifcapable pager_pragmas
281 # Test turning "flag" pragmas on and off.
283 ifcapable debug {
284   # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
285   #
286   do_test pragma-1.15 {
287     execsql {
288       PRAGMA vdbe_listing=YES;
289       PRAGMA vdbe_listing;
290     }
291   } {1}
292   do_test pragma-1.16 {
293     execsql {
294       PRAGMA vdbe_listing=NO;
295       PRAGMA vdbe_listing;
296     }
297   } {0}
300 do_test pragma-1.17 {
301   execsql {
302     PRAGMA parser_trace=ON;
303     PRAGMA parser_trace=OFF;
304   }
305 } {}
306 do_test pragma-1.18 {
307   execsql {
308     PRAGMA bogus = -1234;  -- Parsing of negative values
309   }
310 } {}
312 # Test modifying the safety_level of an attached database.
313 ifcapable pager_pragmas&&attach {
314   do_test pragma-2.1 {
315     forcedelete test2.db
316     forcedelete test2.db-journal
317     execsql {
318       ATTACH 'test2.db' AS aux;
319     } 
320   } {}
321   do_test pragma-2.2 {
322     execsql {
323       pragma aux.synchronous;
324     } 
325   } {2}
326   do_test pragma-2.3 {
327     execsql {
328       pragma aux.synchronous = OFF;
329       pragma aux.synchronous;
330       pragma synchronous;
331     } 
332   } {0 2}
333   do_test pragma-2.4 {
334     execsql {
335       pragma aux.synchronous = ON;
336       pragma synchronous;
337       pragma aux.synchronous;
338     } 
339   } {2 1}
340 } ;# ifcapable pager_pragmas
342 # Construct a corrupted index and make sure the integrity_check
343 # pragma finds it.
345 # These tests won't work if the database is encrypted
347 do_test pragma-3.1 {
348   db close
349   forcedelete test.db test.db-journal
350   sqlite3 db test.db
351   execsql {
352     PRAGMA auto_vacuum=OFF;
353     BEGIN;
354     CREATE TABLE t2(a,b,c);
355     CREATE INDEX i2 ON t2(a);
356     INSERT INTO t2 VALUES(11,2,3);
357     INSERT INTO t2 VALUES(22,3,4);
358     COMMIT;
359     SELECT rowid, * from t2;
360   }
361 } {1 11 2 3 2 22 3 4}
362 ifcapable attach {
363   if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
364     do_test pragma-3.2 {
365       db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
366       set pgsz [db eval {PRAGMA page_size}]
367       # overwrite the header on the rootpage of the index in order to
368       # make the index appear to be empty.
369       #
370       set offset [expr {$pgsz*($rootpage-1)}]
371       hexio_write test.db $offset 0a00000000040000000000
372       db close
373       sqlite3 db test.db
374       execsql {PRAGMA integrity_check}
375     } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
376     do_test pragma-3.3 {
377       execsql {PRAGMA integrity_check=1}
378     } {{wrong # of entries in index i2}}
379     do_test pragma-3.4 {
380       execsql {
381         ATTACH DATABASE 'test.db' AS t2;
382         PRAGMA integrity_check
383       }
384     } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
385     do_test pragma-3.5 {
386       execsql {
387         PRAGMA integrity_check=4
388       }
389     } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
390     do_catchsql_test pragma-3.6 {
391       PRAGMA integrity_check=xyz
392     } {1 {no such table: xyz}}
393     do_catchsql_test pragma-3.6b {
394       PRAGMA integrity_check=t2
395     } {0 {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}}
396     do_catchsql_test pragma-3.6c {
397       PRAGMA integrity_check=sqlite_schema
398     } {0 ok}
399     do_test pragma-3.7 {
400       execsql {
401         PRAGMA integrity_check=0
402       }
403     } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
404   
405     # Add additional corruption by appending unused pages to the end of
406     # the database file testerr.db
407     #
408     do_test pragma-3.8 {
409       execsql {DETACH t2}
410       forcedelete testerr.db testerr.db-journal
411       set out [open testerr.db w]
412       fconfigure $out -translation binary
413       set in [open test.db r]
414       fconfigure $in -translation binary
415       puts -nonewline $out [read $in]
416       seek $in 0
417       puts -nonewline $out [read $in]
418       close $in
419       close $out
420       hexio_write testerr.db 28 00000000
421       execsql {REINDEX t2}
422       execsql {PRAGMA integrity_check}
423     } {ok}
424     do_test pragma-3.8.1 {
425       execsql {PRAGMA quick_check}
426     } {ok}
427     do_test pragma-3.8.2 {
428       execsql {PRAGMA QUICK_CHECK}
429     } {ok}
430     do_test pragma-3.9a {
431       execsql {
432         ATTACH 'testerr.db' AS t2;
433         PRAGMA integrity_check
434       }
435     } {{*** in database t2 ***
436 Page 4: never used
437 Page 5: never used
438 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
439     do_execsql_test pragma-3.9b {
440       PRAGMA t2.integrity_check=t2;
441     } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
442     do_execsql_test pragma-3.9c {
443       PRAGMA t2.integrity_check=sqlite_schema;
444     } {ok}
445     do_test pragma-3.10 {
446       execsql {
447         PRAGMA integrity_check=1
448       }
449     } {{*** in database t2 ***
450 Page 4: never used}}
451     do_test pragma-3.11 {
452       execsql {
453         PRAGMA integrity_check=5
454       }
455     } {{*** in database t2 ***
456 Page 4: never used
457 Page 5: never used
458 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2}}
459     do_test pragma-3.12 {
460       execsql {
461         PRAGMA integrity_check=4
462       }
463     } {{*** in database t2 ***
464 Page 4: never used
465 Page 5: never used
466 Page 6: never used} {wrong # of entries in index i2}}
467     do_test pragma-3.13 {
468       execsql {
469         PRAGMA integrity_check=3
470       }
471     } {{*** in database t2 ***
472 Page 4: never used
473 Page 5: never used
474 Page 6: never used}}
475     do_test pragma-3.14 {
476       execsql {
477         PRAGMA integrity_check(2)
478       }
479     } {{*** in database t2 ***
480 Page 4: never used
481 Page 5: never used}}
482     do_test pragma-3.15 {
483       execsql {
484         ATTACH 'testerr.db' AS t3;
485         PRAGMA integrity_check
486       }
487     } {{*** in database t2 ***
488 Page 4: never used
489 Page 5: never used
490 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {*** in database t3 ***
491 Page 4: never used
492 Page 5: never used
493 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
494     do_test pragma-3.16 {
495       execsql {
496         PRAGMA integrity_check(10)
497       }
498     } {{*** in database t2 ***
499 Page 4: never used
500 Page 5: never used
501 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {*** in database t3 ***
502 Page 4: never used
503 Page 5: never used
504 Page 6: never used} {wrong # of entries in index i2}}
505     do_test pragma-3.17 {
506       execsql {
507         PRAGMA integrity_check=8
508       }
509     } {{*** in database t2 ***
510 Page 4: never used
511 Page 5: never used
512 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {*** in database t3 ***
513 Page 4: never used
514 Page 5: never used}}
515     do_test pragma-3.18 {
516       execsql {
517         PRAGMA integrity_check=4
518       }
519     } {{*** in database t2 ***
520 Page 4: never used
521 Page 5: never used
522 Page 6: never used} {wrong # of entries in index i2}}
523   }
524   do_test pragma-3.19 {
525     catch {db close}
526     forcedelete test.db test.db-journal
527     sqlite3 db test.db
528     db eval {PRAGMA integrity_check}
529   } {ok}
532 # Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
533 # constraint violations.
535 ifcapable altertable {
536   sqlite3_db_config db DEFENSIVE 0
537     do_execsql_test pragma-3.20 {
538       CREATE TABLE t1(a,b);
539       CREATE INDEX t1a ON t1(a);
540       INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
541       PRAGMA writable_schema=ON;
542       UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
543         WHERE name='t1a';
544       UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
545         WHERE name='t1';
546       PRAGMA writable_schema=OFF;
547       ALTER TABLE t1 RENAME TO t1x;
548       PRAGMA integrity_check;
549     } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
550   do_execsql_test pragma-3.21 {
551     PRAGMA integrity_check(3);
552   } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
553   do_execsql_test pragma-3.22 {
554     PRAGMA integrity_check(2);
555   } {{non-unique entry in index t1a} {NULL value in t1x.a}}
556   do_execsql_test pragma-3.23 {
557     PRAGMA integrity_check(1);
558   } {{non-unique entry in index t1a}}
560   # forum post https://sqlite.org/forum/forumpost/ee4f6fa5ab
561   do_execsql_test pragma-3.24 {
562     DROP TABLE IF EXISTS t1;
563     CREATE TABLE t1(a);
564     INSERT INTO t1 VALUES (1);
565     ALTER TABLE t1 ADD COLUMN b NOT NULL DEFAULT 0.25;
566     SELECT * FROM t1;
567     PRAGMA integrity_check(t1);
568   } {1 0.25 ok}
569   do_execsql_test pragma-3.25 {
570     ALTER TABLE t1 ADD COLUMN c CHECK (1);
571     SELECT * FROM t1;
572     PRAGMA integrity_check(t1);
573   } {1 0.25 {} ok}
576 # PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
577 # interface) used to leave index cursors in an inconsistent state
578 # which could result in an assertion fault in sqlite3BtreeKey()
579 # called from saveCursorPosition() if content is removed from the
580 # index while the integrity_check is still running.  This test verifies
581 # that problem has been fixed.
583 do_test pragma-3.30 {
584   catch { db close }
585   delete_file test.db
586   sqlite3 db test.db
587   db eval {
588     CREATE TABLE t1(a,b,c);
589     WITH RECURSIVE
590       c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
591     INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
592     CREATE INDEX t1a ON t1(a);
593     CREATE INDEX t1bc ON t1(b,c);
594   }
595   db eval {PRAGMA integrity_check} {
596      db eval {DELETE FROM t1}
597   }
598 } {}
600 # The values stored in indexes must be byte-for-byte identical to the
601 # values stored in tables.
603 reset_db
604 do_execsql_test pragma-3.40 {
605   CREATE TABLE t1(
606     a INTEGER PRIMARY KEY,
607     b TEXT COLLATE nocase,
608     c INT COLLATE nocase,
609     d TEXT
610   );
611   INSERT INTO t1(a,b,c,d) VALUES
612     (1, 'one','one','one'),
613     (2, 'two','two','two'),
614     (3, 'three','three','three'),
615     (4, 'four','four','four'),
616     (5, 'five','five','five');
617   CREATE INDEX t1bcd ON t1(b,c,d);
618   CREATE TABLE t2(
619     a INTEGER PRIMARY KEY,
620     b TEXT COLLATE nocase,
621     c INT COLLATE nocase,
622     d TEXT
623   );
624   INSERT INTO t2(a,b,c,d) VALUES
625     (1, 'one','one','one'),
626     (2, 'two','two','TWO'),
627     (3, 'three','THREE','three'),
628     (4, 'FOUR','four','four'),
629     (5, 'FIVE','FIVE','five');
630   CREATE INDEX t2bcd ON t2(b,c,d);
631   CREATE TEMP TABLE saved_schema AS SELECT name, rootpage FROM sqlite_schema;
632   PRAGMA writable_schema=ON;
633   UPDATE sqlite_schema
634      SET rootpage=(SELECT rootpage FROM saved_schema WHERE name='t2bcd')
635    WHERE name='t1bcd';
636   UPDATE sqlite_schema
637      SET rootpage=(SELECT rootpage FROM saved_schema WHERE name='t1bcd')
638    WHERE name='t2bcd';
639   PRAGMA Writable_schema=RESET;
641 ifcapable vtab {
642   do_execsql_test pragma-3.41 {
643     SELECT integrity_check AS x FROM pragma_integrity_check ORDER BY 1;
644   } {
645     {row 2 missing from index t1bcd}
646     {row 2 missing from index t2bcd}
647     {row 3 values differ from index t1bcd}
648     {row 3 values differ from index t2bcd}
649     {row 4 values differ from index t1bcd}
650     {row 4 values differ from index t2bcd}
651     {row 5 values differ from index t1bcd}
652     {row 5 values differ from index t2bcd}
653   }
655 db eval {DROP TABLE t2}
657 # Test modifying the cache_size of an attached database.
658 ifcapable pager_pragmas&&attach {
659 do_test pragma-4.1 {
660   execsql {
661     ATTACH 'test2.db' AS aux;
662     pragma aux.cache_size;
663     pragma aux.default_cache_size;
664   } 
665 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
666 do_test pragma-4.2 {
667   execsql {
668     pragma aux.cache_size = 50;
669     pragma aux.cache_size;
670     pragma aux.default_cache_size;
671   } 
672 } [list 50 $DFLT_CACHE_SZ]
673 do_test pragma-4.3 {
674   execsql {
675     pragma aux.default_cache_size = 456;
676     pragma aux.cache_size;
677     pragma aux.default_cache_size;
678   } 
679 } {456 456}
680 do_test pragma-4.4 {
681   execsql {
682     pragma cache_size;
683     pragma default_cache_size;
684   } 
685 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
686 do_test pragma-4.5 {
687   execsql {
688     DETACH aux;
689     ATTACH 'test3.db' AS aux;
690     pragma aux.cache_size;
691     pragma aux.default_cache_size;
692   } 
693 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
694 do_test pragma-4.6 {
695   execsql {
696     DETACH aux;
697     ATTACH 'test2.db' AS aux;
698     pragma aux.cache_size;
699     pragma aux.default_cache_size;
700   } 
701 } {456 456}
702 } ;# ifcapable pager_pragmas
704 # Test that modifying the sync-level in the middle of a transaction is
705 # disallowed.
706 ifcapable pager_pragmas {
707 do_test pragma-5.0 {
708   execsql {
709     pragma synchronous;
710   } 
711 } {2}
712 do_test pragma-5.1 {
713   catchsql {
714     BEGIN;
715     pragma synchronous = OFF;
716   } 
717 } {1 {Safety level may not be changed inside a transaction}}
718 do_test pragma-5.2 {
719   execsql {
720     pragma synchronous;
721   } 
722 } {2}
723 catchsql {COMMIT;}
724 } ;# ifcapable pager_pragmas
726 # Test schema-query pragmas
728 ifcapable schema_pragmas {
729 ifcapable tempdb&&attach {
730   do_test pragma-6.1 {
731     set res {}
732     execsql {SELECT * FROM sqlite_temp_master}
733     foreach {idx name file} [execsql {pragma database_list}] {
734       lappend res $idx $name
735     }
736     set res
737   } {0 main 1 temp 2 aux}
739 do_test pragma-6.2 {
740   execsql {
741     CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
742     pragma table_info(t2)
743   }
744 } {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
745 do_test pragma-6.2.1 {
746   execsql {
747     pragma table_info;
748   }
749 } {}
750 db nullvalue <<NULL>>
751 do_test pragma-6.2.2 {
752   execsql {
753     CREATE TABLE t5(
754       a TEXT DEFAULT CURRENT_TIMESTAMP, 
755       b DEFAULT (5+3),
756       c TEXT,
757       d INTEGER DEFAULT NULL,
758       e TEXT DEFAULT '',
759       UNIQUE(b,c,d),
760       PRIMARY KEY(e,b,c)
761     );
762     PRAGMA table_info(t5);
763   }
764 } {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}
765 db nullvalue {}
766 do_test pragma-6.2.3 {
767   execsql {
768     CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
769     pragma table_info(t2_3)
770   }
771 } {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
772 ifcapable {foreignkey} {
773   do_test pragma-6.3.1 {
774     execsql {
775       CREATE TABLE t3(a int references t2(b), b UNIQUE);
776       pragma foreign_key_list(t3);
777     }
778   } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
779   do_test pragma-6.3.2 {
780     execsql {
781       pragma foreign_key_list;
782     }
783   } {}
784   do_test pragma-6.3.3 {
785     execsql {
786       pragma foreign_key_list(t3_bogus);
787     }
788   } {}
789   do_test pragma-6.3.4 {
790     execsql {
791       pragma foreign_key_list(t5);
792     }
793   } {}
794   do_test pragma-6.4 {
795     capture_pragma db out {
796       pragma index_list(t3);
797     }
798     db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
799   } {0 sqlite_autoindex_t3_1 1}
801 ifcapable {!foreignkey} {
802   execsql {CREATE TABLE t3(a,b UNIQUE)}
804 do_test pragma-6.5.1 {
805   execsql {
806     CREATE INDEX t3i1 ON t3(a,b);
807   }
808   capture_pragma db out {
809     pragma index_info(t3i1);
810   }
811   db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
812 } {0 0 a 1 1 b}
814 # EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
815 # by the index_info pragma, but they are listed by the index_xinfo
816 # pragma.
818 do_test pragma-6.5.1b {
819   capture_pragma db out {PRAGMA index_xinfo(t3i1)}
820   db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
821 } {0 0 a 1 1 b 2 -1 {}}
824 # EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
825 # pragma returns one row for each key column in the named index.
827 # (The first column of output from PRAGMA index_info is...)
828 # EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
829 # means left-most.)
831 # (The second column of output from PRAGMA index_info is...)
832 # EVIDENCE-OF: R-65019-08383 The rank of the column within the table
833 # being indexed.
835 # (The third column of output from PRAGMA index_info is...)
836 # EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
838 do_execsql_test pragma-6.5.1c {
839   CREATE INDEX t3i2 ON t3(b,a);
840   PRAGMA index_info='t3i2';
841   DROP INDEX t3i2;
842 } {0 1 b 1 0 a}
844 do_test pragma-6.5.2 {
845   execsql {
846     pragma index_info(t3i1_bogus);
847   }
848 } {}
850 ifcapable tempdb {
851   # Test for ticket #3320. When a temp table of the same name exists, make
852   # sure the schema of the main table can still be queried using 
853   # "pragma table_info":
854   do_test pragma-6.6.1 {
855     execsql {
856       CREATE TABLE trial(col_main);
857       CREATE TEMP TABLE trial(col_temp);
858     }
859   } {}
860   do_test pragma-6.6.2 {
861     execsql {
862       PRAGMA table_info(trial);
863     }
864   } {0 col_temp {} 0 {} 0}
865   do_test pragma-6.6.3 {
866     execsql {
867       PRAGMA temp.table_info(trial);
868     }
869   } {0 col_temp {} 0 {} 0}
870   do_test pragma-6.6.4 {
871     execsql {
872       PRAGMA main.table_info(trial);
873     }
874   } {0 col_main {} 0 {} 0}
877 do_test pragma-6.7 {
878   execsql {
879     CREATE TABLE test_table(
880       one INT NOT NULL DEFAULT -1, 
881       two text,
882       three VARCHAR(45, 65) DEFAULT 'abcde',
883       four REAL DEFAULT X'abcdef',
884       five DEFAULT CURRENT_TIME
885     );
886   }
887   capture_pragma db out {PRAGMA table_info(test_table)}
888   db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
889             ORDER BY cid}
890 } [concat \
891   {0 one INT 1 -1 0} \
892   {1 two TEXT 0 {} 0} \
893   {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
894   {3 four REAL 0 X'abcdef' 0} \
895   {4 five {} 0 CURRENT_TIME 0} \
897 do_test pragma-6.8 {
898   execsql {
899     CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
900     PRAGMA table_info(t68);
901   }
902 } [concat \
903   {0 a {} 0 {} 1} \
904   {1 b {} 0 {} 2} \
905   {2 c {} 0 {} 4} \
907 } ;# ifcapable schema_pragmas
908 # Miscellaneous tests
910 ifcapable schema_pragmas {
911 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
912 # pragma returns one row for each index associated with the given table.
914 do_test pragma-7.1.1 {
915   # Make sure a pragma knows to read the schema if it needs to
916   db close
917   sqlite3 db test.db
918   capture_pragma db out "PRAGMA index_list(t3)"
919   db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
920 } {t3i1 c sqlite_autoindex_t3_1 u}
921 do_test pragma-7.1.2 {
922   execsql {
923     pragma index_list(t3_bogus);
924   }
925 } {}
926 } ;# ifcapable schema_pragmas
927 ifcapable {utf16} {
928   if {[permutation] == ""} {
929     do_test pragma-7.2 {
930       db close
931       sqlite3 db test.db
932       catchsql {
933         pragma encoding=bogus;
934       }
935     } {1 {unsupported encoding: bogus}}
936   }
938 ifcapable tempdb {
939   do_test pragma-7.3 {
940     db close
941     sqlite3 db test.db
942     execsql {
943       pragma lock_status;
944     }
945   } {main unlocked temp closed}
946 } else {
947   do_test pragma-7.3 {
948     db close
949     sqlite3 db test.db
950     execsql {
951       pragma lock_status;
952     }
953   } {main unlocked}
957 #----------------------------------------------------------------------
958 # Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
959 # user_version" statements.
961 # pragma-8.1: PRAGMA schema_version
962 # pragma-8.2: PRAGMA user_version
965 ifcapable schema_version {
967 # First check that we can set the schema version and then retrieve the
968 # same value.
969 do_test pragma-8.1.1 {
970   execsql {
971     PRAGMA schema_version = 105;
972   }
973 } {}
974 do_test pragma-8.1.2 {
975   execsql2 {
976     PRAGMA schema_version;
977   }
978 } {schema_version 105}
979 sqlite3_db_config db DEFENSIVE 1
980 do_execsql_test pragma-8.1.3 {
981   PRAGMA schema_version = 106;
982   PRAGMA schema_version;
983 } 105
984 sqlite3_db_config db DEFENSIVE 0
985 do_execsql_test pragma-8.1.4 {
986   PRAGMA schema_version = 106;
987   PRAGMA schema_version;
988 } 106
990 # Check that creating a table modifies the schema-version (this is really
991 # to verify that the value being read is in fact the schema version).
992 do_test pragma-8.1.5 {
993   execsql {
994     CREATE TABLE t4(a, b, c);
995     INSERT INTO t4 VALUES(1, 2, 3);
996     SELECT * FROM t4;
997   }
998 } {1 2 3}
999 do_test pragma-8.1.6 {
1000   execsql {
1001     PRAGMA schema_version;
1002   }
1003 } 107
1005 # Now open a second connection to the database. Ensure that changing the
1006 # schema-version using the first connection forces the second connection
1007 # to reload the schema. This has to be done using the C-API test functions,
1008 # because the TCL API accounts for SCHEMA_ERROR and retries the query.
1009 do_test pragma-8.1.7 {
1010   sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
1011   execsql {
1012     SELECT * FROM t4;
1013   } db2
1014 } {1 2 3}
1015 do_test pragma-8.1.8 {
1016   execsql {
1017     PRAGMA schema_version = 108;
1018   }
1019 } {}
1020 do_test pragma-8.1.9 {
1021   set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
1022   sqlite3_step $::STMT
1023 } SQLITE_ERROR
1024 do_test pragma-8.1.10 {
1025   sqlite3_finalize $::STMT
1026 } SQLITE_SCHEMA
1028 # Make sure the schema-version can be manipulated in an attached database.
1029 forcedelete test2.db
1030 forcedelete test2.db-journal
1031 ifcapable attach {
1032   do_test pragma-8.1.11 {
1033     execsql {
1034       ATTACH 'test2.db' AS aux;
1035       CREATE TABLE aux.t1(a, b, c);
1036       PRAGMA aux.schema_version = 205;
1037     }
1038   } {}
1039   do_test pragma-8.1.12 {
1040     execsql {
1041       PRAGMA aux.schema_version;
1042     }
1043   } 205
1045 do_test pragma-8.1.13 {
1046   execsql {
1047     PRAGMA schema_version;
1048   }
1049 } 108
1051 # And check that modifying the schema-version in an attached database
1052 # forces the second connection to reload the schema.
1053 ifcapable attach {
1054   do_test pragma-8.1.14 {
1055     sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
1056     execsql {
1057       ATTACH 'test2.db' AS aux;
1058       SELECT * FROM aux.t1;
1059     } db2
1060   } {}
1061   do_test pragma-8.1.15 {
1062     execsql {
1063       PRAGMA aux.schema_version = 206;
1064     }
1065   } {}
1066   do_test pragma-8.1.16 {
1067     set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
1068     sqlite3_step $::STMT
1069   } SQLITE_ERROR
1070   do_test pragma-8.1.17 {
1071     sqlite3_finalize $::STMT
1072   } SQLITE_SCHEMA
1073   do_test pragma-8.1.18 {
1074     db2 close
1075   } {}
1078 # Now test that the user-version can be read and written (and that we aren't
1079 # accidentally manipulating the schema-version instead).
1080 do_test pragma-8.2.1 {
1081   execsql2 {
1082     PRAGMA user_version;
1083   }
1084 } {user_version 0}
1085 do_test pragma-8.2.2 {
1086   execsql {
1087     PRAGMA user_version = 2;
1088   }
1089 } {}
1090 do_test pragma-8.2.3.1 {
1091   execsql2 {
1092     PRAGMA user_version;
1093   }
1094 } {user_version 2}
1095 do_test pragma-8.2.3.2 {
1096   db close
1097   sqlite3 db test.db
1098   execsql {
1099     PRAGMA user_version;
1100   }
1101 } {2}
1102 do_test pragma-8.2.4.1 {
1103   execsql {
1104     PRAGMA schema_version;
1105   }
1106 } {108}
1107 ifcapable vacuum {
1108   do_test pragma-8.2.4.2 {
1109     execsql {
1110       VACUUM;
1111       PRAGMA user_version;
1112     }
1113   } {2}
1114   do_test pragma-8.2.4.3 {
1115     execsql {
1116       PRAGMA schema_version;
1117     }
1118   } {109}
1121 ifcapable attach {
1122   db eval {ATTACH 'test2.db' AS aux}
1123   
1124   # Check that the user-version in the auxilary database can be manipulated (
1125   # and that we aren't accidentally manipulating the same in the main db).
1126   do_test pragma-8.2.5 {
1127     execsql {
1128       PRAGMA aux.user_version;
1129     }
1130   } {0}
1131   do_test pragma-8.2.6 {
1132     execsql {
1133       PRAGMA aux.user_version = 3;
1134     }
1135   } {}
1136   do_test pragma-8.2.7 {
1137     execsql {
1138       PRAGMA aux.user_version;
1139     }
1140   } {3}
1141   do_test pragma-8.2.8 {
1142     execsql {
1143       PRAGMA main.user_version;
1144     }
1145   } {2}
1146   
1147   # Now check that a ROLLBACK resets the user-version if it has been modified
1148   # within a transaction.
1149   do_test pragma-8.2.9 {
1150     execsql {
1151       BEGIN;
1152       PRAGMA aux.user_version = 10;
1153       PRAGMA user_version = 11;
1154     }
1155   } {}
1156   do_test pragma-8.2.10 {
1157     execsql {
1158       PRAGMA aux.user_version;
1159     }
1160   } {10}
1161   do_test pragma-8.2.11 {
1162     execsql {
1163       PRAGMA main.user_version;
1164     }
1165   } {11}
1166   do_test pragma-8.2.12 {
1167     execsql {
1168       ROLLBACK;
1169       PRAGMA aux.user_version;
1170     }
1171   } {3}
1172   do_test pragma-8.2.13 {
1173     execsql {
1174       PRAGMA main.user_version;
1175     }
1176   } {2}
1179 # Try a negative value for the user-version
1180 do_test pragma-8.2.14 {
1181   execsql {
1182     PRAGMA user_version = -450;
1183   }
1184 } {}
1185 do_test pragma-8.2.15 {
1186   execsql {
1187     PRAGMA user_version;
1188   }
1189 } {-450}
1190 } ; # ifcapable schema_version
1192 # Check to see if TEMP_STORE is memory or disk.  Return strings
1193 # "memory" or "disk" as appropriate.
1195 proc check_temp_store {} {
1196   db eval {
1197     PRAGMA temp.cache_size = 1;
1198     CREATE TEMP TABLE IF NOT EXISTS a(b);
1199     DELETE FROM a;
1200     INSERT INTO a VALUES(randomblob(1000));
1201     INSERT INTO a SELECT * FROM a;
1202     INSERT INTO a SELECT * FROM a;
1203     INSERT INTO a SELECT * FROM a;
1204     INSERT INTO a SELECT * FROM a;
1205     INSERT INTO a SELECT * FROM a;
1206     INSERT INTO a SELECT * FROM a;
1207     INSERT INTO a SELECT * FROM a;
1208     INSERT INTO a SELECT * FROM a;
1209   }
1210   db eval {PRAGMA database_list} {
1211     if {$name=="temp"} {
1212       set bt [btree_from_db db 1]
1213       if {[btree_ismemdb $bt]} {
1214         return "memory"
1215       }
1216       return "disk"
1217     }
1218   }
1219   return "unknown"
1222 # Application_ID
1224 do_test pragma-8.3.1 {
1225   execsql {
1226     PRAGMA application_id;
1227   }
1228 } {0}
1229 do_test pragma-8.3.2 {
1230   execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1231 } {12345}
1233 # Test temp_store and temp_store_directory pragmas
1235 ifcapable pager_pragmas {
1236 do_test pragma-9.1 {
1237   db close
1238   sqlite3 db test.db
1239   execsql {
1240     PRAGMA temp_store;
1241   }
1242 } {0}
1243 if {$TEMP_STORE<=1} {
1244   do_test pragma-9.1.1 {
1245     check_temp_store
1246   } {disk}
1247 } else {
1248   do_test pragma-9.1.1 {
1249     check_temp_store
1250   } {memory}
1253 do_test pragma-9.2 {
1254   db close
1255   sqlite3 db test.db
1256   execsql {
1257     PRAGMA temp_store=file;
1258     PRAGMA temp_store;
1259   }
1260 } {1}
1261 if {$TEMP_STORE==3} {
1262   # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1263   do_test pragma-9.2.1 {
1264     check_temp_store
1265   } {memory}
1266 } else {
1267   do_test pragma-9.2.1 {
1268     check_temp_store
1269   } {disk}
1272 do_test pragma-9.3 {
1273   db close
1274   sqlite3 db test.db
1275   execsql {
1276     PRAGMA temp_store=memory;
1277     PRAGMA temp_store;
1278   }
1279 } {2}
1280 if {$TEMP_STORE==0} {
1281   # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1282   do_test pragma-9.3.1 {
1283     check_temp_store
1284   } {disk}
1285 } else {
1286   do_test pragma-9.3.1 {
1287     check_temp_store
1288   } {memory}
1291 do_test pragma-9.4 {
1292   execsql {
1293     PRAGMA temp_store_directory;
1294   }
1295 } {}
1296 ifcapable wsd {
1297   do_test pragma-9.5 {
1298     set pwd [string map {' ''} [file nativename [get_pwd]]]
1299     execsql "
1300       PRAGMA temp_store_directory='$pwd';
1301     "
1302   } {}
1303   do_test pragma-9.6 {
1304     execsql { 
1305       PRAGMA temp_store_directory;
1306     }
1307   } [list [file nativename [get_pwd]]]
1308   do_test pragma-9.7 {
1309     catchsql { 
1310       PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1311     }
1312   } {1 {not a writable directory}}
1313   do_test pragma-9.8 {
1314     execsql { 
1315       PRAGMA temp_store_directory='';
1316     }
1317   } {}
1318   if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1319     ifcapable tempdb {
1320       do_test pragma-9.9 {
1321         execsql { 
1322           PRAGMA temp_store_directory;
1323           PRAGMA temp_store=FILE;
1324           CREATE TEMP TABLE temp_store_directory_test(a integer);
1325           INSERT INTO temp_store_directory_test values (2);
1326           SELECT * FROM temp_store_directory_test;
1327         }
1328       } {2}
1329       do_test pragma-9.10 {
1330         catchsql "
1331           PRAGMA temp_store_directory='$pwd';
1332           SELECT * FROM temp_store_directory_test;
1333         "
1334       } {1 {no such table: temp_store_directory_test}}
1335     }
1336   }
1338 do_test pragma-9.11 {
1339   execsql {
1340     PRAGMA temp_store = 0;
1341     PRAGMA temp_store;
1342   }
1343 } {0}
1344 do_test pragma-9.12 {
1345   execsql {
1346     PRAGMA temp_store = 1;
1347     PRAGMA temp_store;
1348   }
1349 } {1}
1350 do_test pragma-9.13 {
1351   execsql {
1352     PRAGMA temp_store = 2;
1353     PRAGMA temp_store;
1354   }
1355 } {2}
1356 do_test pragma-9.14 {
1357   execsql {
1358     PRAGMA temp_store = 3;
1359     PRAGMA temp_store;
1360   }
1361 } {0}
1362 do_test pragma-9.15 {
1363   catchsql {
1364     BEGIN EXCLUSIVE;
1365     CREATE TEMP TABLE temp_table(t);
1366     INSERT INTO temp_table VALUES('valuable data');
1367     PRAGMA temp_store = 1;
1368   }
1369 } {1 {temporary storage cannot be changed from within a transaction}}
1370 do_test pragma-9.16 {
1371   execsql {
1372     SELECT * FROM temp_table;
1373     COMMIT;
1374   }
1375 } {{valuable data}}
1377 do_test pragma-9.17 {
1378   execsql {
1379     INSERT INTO temp_table VALUES('valuable data II');
1380     SELECT * FROM temp_table;
1381   }
1382 } {{valuable data} {valuable data II}}
1384 do_test pragma-9.18 {
1385   set rc [catch {
1386     db eval {SELECT t FROM temp_table} {
1387       execsql {pragma temp_store = 1}
1388     }
1389   } msg]
1390   list $rc $msg
1391 } {1 {temporary storage cannot be changed from within a transaction}}
1393 } ;# ifcapable pager_pragmas
1395 ifcapable trigger {
1397 do_test pragma-10.0 {
1398   catchsql {
1399     DROP TABLE main.t1;
1400   }
1401   execsql {
1402     PRAGMA count_changes = 1;
1404     CREATE TABLE t1(a PRIMARY KEY);
1405     CREATE TABLE t1_mirror(a);
1406     CREATE TABLE t1_mirror2(a);
1407     CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN 
1408       INSERT INTO t1_mirror VALUES(new.a);
1409     END;
1410     CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN 
1411       INSERT INTO t1_mirror2 VALUES(new.a);
1412     END;
1413     CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN 
1414       UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1415     END;
1416     CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN 
1417       UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1418     END;
1419     CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN 
1420       DELETE FROM t1_mirror WHERE a = old.a;
1421     END;
1422     CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN 
1423       DELETE FROM t1_mirror2 WHERE a = old.a;
1424     END;
1425   }
1426 } {}
1428 do_test pragma-10.1 {
1429   execsql {
1430     INSERT INTO t1 VALUES(randstr(10,10));
1431   }
1432 } {1}
1433 do_test pragma-10.2 {
1434   execsql {
1435     UPDATE t1 SET a = randstr(10,10);
1436   }
1437 } {1}
1438 do_test pragma-10.3 {
1439   execsql {
1440     DELETE FROM t1;
1441   }
1442 } {1}
1444 } ;# ifcapable trigger
1446 ifcapable schema_pragmas {
1447   do_test pragma-11.1 {
1448     execsql2 {
1449       pragma collation_list;
1450     }
1451   } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
1452   do_test pragma-11.2 {
1453     db collate New_Collation blah...
1454     execsql {
1455       pragma collation_list;
1456     }
1457   } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
1460 ifcapable schema_pragmas&&tempdb {
1461   do_test pragma-12.1 {
1462     sqlite3 db2 test.db
1463     execsql {
1464       PRAGMA temp.table_info('abc');
1465     } db2
1466   } {}
1467   db2 close
1469   do_test pragma-12.2 {
1470     sqlite3 db2 test.db
1471     execsql {
1472       PRAGMA temp.default_cache_size = 200;
1473       PRAGMA temp.default_cache_size;
1474     } db2
1475   } {200}
1476   db2 close
1478   do_test pragma-12.3 {
1479     sqlite3 db2 test.db
1480     execsql {
1481       PRAGMA temp.cache_size = 400;
1482       PRAGMA temp.cache_size;
1483     } db2
1484   } {400}
1485   db2 close
1488 ifcapable bloblit {
1490 do_test pragma-13.1 {
1491   execsql {
1492     DROP TABLE IF EXISTS t4;
1493     PRAGMA vdbe_trace=on;
1494     PRAGMA vdbe_listing=on;
1495     PRAGMA sql_trace=on;
1496     CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1497     INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1498     INSERT INTO t4(b) VALUES(randstr(30,30));
1499     INSERT INTO t4(b) VALUES(1.23456);
1500     INSERT INTO t4(b) VALUES(NULL);
1501     INSERT INTO t4(b) VALUES(0);
1502     INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1503     SELECT * FROM t4;
1504   }
1505   execsql {
1506     PRAGMA vdbe_trace=off;
1507     PRAGMA vdbe_listing=off;
1508     PRAGMA sql_trace=off;
1509   }
1510 } {}
1512 } ;# ifcapable bloblit 
1514 ifcapable pager_pragmas {
1515   db close
1516   forcedelete test.db
1517   sqlite3 db test.db
1519   # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1520   # number of pages in the database file.
1521   #
1522   do_test pragma-14.1 {
1523     execsql { pragma auto_vacuum = 0 }
1524     execsql { pragma page_count; pragma main.page_count }
1525   } {0 0}
1527   do_test pragma-14.2 {
1528     execsql { 
1529       CREATE TABLE abc(a, b, c);
1530       PRAGMA page_count;
1531       PRAGMA main.page_count;
1532       PRAGMA temp.page_count;
1533     }
1534   } {2 2 0}
1535   do_test pragma-14.2uc {
1536     execsql {pragma PAGE_COUNT}
1537   } {2}
1539   do_test pragma-14.3 {
1540     execsql { 
1541       BEGIN;
1542       CREATE TABLE def(a, b, c);
1543       PRAGMA page_count;
1544     }
1545   } {3}
1546   do_test pragma-14.3uc {
1547     execsql {pragma PAGE_COUNT}
1548   } {3}
1550   do_test pragma-14.4 {
1551     set page_size [db one {pragma page_size}]
1552     expr [file size test.db] / $page_size
1553   } {2}
1555   do_test pragma-14.5 {
1556     execsql {
1557       ROLLBACK;
1558       PRAGMA page_count;
1559     }
1560   } {2}
1562   do_test pragma-14.6 {
1563     forcedelete test2.db
1564     sqlite3 db2 test2.db
1565     execsql {
1566       PRAGMA auto_vacuum = 0;
1567       CREATE TABLE t1(a, b, c);
1568       CREATE TABLE t2(a, b, c);
1569       CREATE TABLE t3(a, b, c);
1570       CREATE TABLE t4(a, b, c);
1571     } db2
1572     db2 close
1573     execsql {
1574       ATTACH 'test2.db' AS aux;
1575       PRAGMA aux.page_count;
1576     } 
1577   } {5}
1578   do_test pragma-14.6uc {
1579     execsql {pragma AUX.PAGE_COUNT}
1580   } {5}
1583 # Test that the value set using the cache_size pragma is not reset when the
1584 # schema is reloaded.
1586 ifcapable pager_pragmas {
1587   db close
1588   sqlite3 db test.db
1589   do_test pragma-15.1 {
1590     execsql {
1591       PRAGMA cache_size=59;
1592       PRAGMA cache_size;
1593     }
1594   } {59}
1595   do_test pragma-15.2 {
1596     sqlite3 db2 test.db
1597     execsql {
1598       CREATE TABLE newtable(a, b, c);
1599     } db2
1600     db2 close
1601   } {}
1602   do_test pragma-15.3 {
1603     # Evaluating this statement will cause the schema to be reloaded (because
1604     # the schema was changed by another connection in pragma-15.2). At one
1605     # point there was a bug that reset the cache_size to its default value
1606     # when this happened. 
1607     execsql { SELECT * FROM sqlite_master }
1608     execsql { PRAGMA cache_size }
1609   } {59}
1612 # Reset the sqlite3_temp_directory variable for the next run of tests:
1613 sqlite3 dbX :memory:
1614 dbX eval {PRAGMA temp_store_directory = ""}
1615 dbX close
1617 ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1618   set sqlite_hostid_num 1
1620   set using_proxy 0
1621   foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1622     set using_proxy $value
1623   }
1625   # Test the lock_proxy_file pragmas.
1626   #
1627   db close
1628   set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1630   sqlite3 db test.db
1631   do_test pragma-16.1 {
1632     execsql {
1633       PRAGMA lock_proxy_file="mylittleproxy";
1634       select * from sqlite_master;
1635     }
1636     execsql {
1637       PRAGMA lock_proxy_file;
1638     } 
1639   } {mylittleproxy}
1641   do_test pragma-16.2 {
1642     sqlite3 db2 test.db
1643     execsql {
1644       PRAGMA lock_proxy_file="mylittleproxy";
1645     } db2
1646   } {}
1648   db2 close
1649   do_test pragma-16.2.1 {
1650     sqlite3 db2 test.db
1651     execsql {
1652       PRAGMA lock_proxy_file=":auto:";
1653       select * from sqlite_master;
1654     } db2
1655     execsql {
1656       PRAGMA lock_proxy_file;
1657     } db2
1658   } {mylittleproxy}
1660   db2 close
1661   do_test pragma-16.3 {
1662     sqlite3 db2 test.db
1663     execsql {
1664       PRAGMA lock_proxy_file="myotherproxy";
1665     } db2
1666     catchsql {
1667       select * from sqlite_master;
1668     } db2
1669   } {1 {database is locked}}
1671   do_test pragma-16.4 {
1672     db2 close
1673     db close
1674     sqlite3 db2 test.db
1675     execsql {
1676       PRAGMA lock_proxy_file="myoriginalproxy";
1677       PRAGMA lock_proxy_file="myotherproxy";
1678       PRAGMA lock_proxy_file;
1679     } db2
1680   } {myotherproxy}
1682   db2 close
1683   set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1684   do_test pragma-16.5 {
1685     sqlite3 db2 test.db
1686     execsql {
1687       PRAGMA lock_proxy_file=":auto:";
1688       PRAGMA lock_proxy_file;
1689     } db2
1690   } {myotherproxy}
1691   
1692   do_test pragma-16.6 {
1693     db2 close
1694     sqlite3 db2 test2.db
1695     set lockpath [execsql {
1696       PRAGMA lock_proxy_file=":auto:";
1697       PRAGMA lock_proxy_file;
1698     } db2]
1699     string match "*test2.db:auto:" $lockpath
1700   } {1}
1701   
1702   set sqlite_hostid_num 2
1703   do_test pragma-16.7 {
1704     list [catch {
1705       sqlite3 db test2.db
1706       execsql { 
1707         PRAGMA lock_proxy_file=":auto:";
1708         select * from sqlite_master;
1709       }
1710     } msg] $msg
1711   } {1 {database is locked}}
1712   db close
1713   
1714   do_test pragma-16.8 {
1715     list [catch {
1716       sqlite3 db test2.db
1717       execsql { select * from sqlite_master } 
1718     } msg] $msg
1719   } {1 {database is locked}}
1721   db2 close
1722   do_test pragma-16.8.1 {
1723     execsql {
1724       PRAGMA lock_proxy_file="yetanotherproxy";
1725       PRAGMA lock_proxy_file;
1726     } 
1727   } {yetanotherproxy}
1728   do_test pragma-16.8.2 {
1729     execsql {
1730       create table mine(x);
1731     } 
1732   } {}
1734   db close
1735   do_test pragma-16.9 {
1736     sqlite3 db proxytest.db
1737     set lockpath2 [execsql {
1738       PRAGMA lock_proxy_file=":auto:";
1739       PRAGMA lock_proxy_file;
1740     } db]
1741     string match "*proxytest.db:auto:" $lockpath2
1742   } {1}
1744   set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1745   set sqlite_hostid_num 0
1748 # Parsing of auto_vacuum settings.
1750 foreach {autovac_setting val} {
1751   0 0
1752   1 1
1753   2 2
1754   3 0
1755   -1 0
1756   none 0
1757   NONE 0
1758   NoNe 0
1759   full 1
1760   FULL 1
1761   incremental 2
1762   INCREMENTAL 2
1763   -1234 0
1764   1234 0
1765 } {
1766   do_test pragma-17.1.$autovac_setting {
1767     catch {db close}
1768     sqlite3 db :memory:
1769     execsql "
1770       PRAGMA auto_vacuum=$::autovac_setting;
1771       PRAGMA auto_vacuum;
1772     "
1773   } $val
1776 # Parsing of temp_store settings.
1778 foreach {temp_setting val} {
1779   0 0
1780   1 1
1781   2 2
1782   3 0
1783   -1 0
1784   file 1
1785   FILE 1
1786   fIlE 1
1787   memory 2
1788   MEMORY 2
1789   MeMoRy 2
1790 } {
1791   do_test pragma-18.1.$temp_setting {
1792     catch {db close}
1793     sqlite3 db :memory:
1794     execsql "
1795       PRAGMA temp_store=$::temp_setting;
1796       PRAGMA temp_store=$::temp_setting;
1797       PRAGMA temp_store;
1798     "
1799   } $val
1802 # The SQLITE_FCNTL_PRAGMA logic, with error handling.
1804 db close
1805 testvfs tvfs
1806 sqlite3 db test.db -vfs tvfs
1807 do_test pragma-19.1 {
1808   catchsql {PRAGMA error}
1809 } {1 {SQL logic error}}
1810 do_test pragma-19.2 {
1811   catchsql {PRAGMA error='This is the error message'}
1812 } {1 {This is the error message}}
1813 do_test pragma-19.3 {
1814   catchsql {PRAGMA error='7 This is the error message'}
1815 } {1 {This is the error message}}
1816 do_test pragma-19.4 {
1817   catchsql {PRAGMA error=7}
1818 } {1 {out of memory}}
1819 do_test pragma-19.5 {
1820   file tail [lindex [execsql {PRAGMA filename}] 0]
1821 } {test.db}
1823 if {$tcl_platform(platform)=="windows"} {
1824 # Test data_store_directory pragma
1826 db close
1827 sqlite3 db test.db
1828 file mkdir data_dir
1829 do_test pragma-20.1 {
1830   catchsql {PRAGMA data_store_directory}
1831 } {0 {}}
1832 do_test pragma-20.2 {
1833   set pwd [string map {' ''} [file nativename [get_pwd]]]
1834   catchsql "PRAGMA data_store_directory='$pwd';"
1835 } {0 {}}
1836 do_test pragma-20.3 {
1837   catchsql {PRAGMA data_store_directory}
1838 } [list 0 [list [file nativename [get_pwd]]]]
1839 do_test pragma-20.4 {
1840   set pwd [string map {' ''} [file nativename \
1841     [file join [get_pwd] data_dir]]]
1842   catchsql "PRAGMA data_store_directory='$pwd';"
1843 } {0 {}}
1844 do_test pragma-20.5 {
1845   sqlite3 db2 test2.db
1846   catchsql "PRAGMA database_list;" db2
1847 } [list 0 [list 0 main [file nativename \
1848     [file join [get_pwd] data_dir test2.db]]]]
1849 catch {db2 close}
1850 do_test pragma-20.6 {
1851   sqlite3 db2 [file join [get_pwd] test2.db]
1852   catchsql "PRAGMA database_list;" db2
1853 } [list 0 [list 0 main [file nativename \
1854     [file join [get_pwd] test2.db]]]]
1855 catch {db2 close}
1856 do_test pragma-20.7 {
1857   catchsql "PRAGMA data_store_directory='';"
1858 } {0 {}}
1859 do_test pragma-20.8 {
1860   catchsql {PRAGMA data_store_directory}
1861 } {0 {}}
1863 forcedelete data_dir
1864 } ;# endif windows
1866 database_may_be_corrupt
1867 if {![nonzero_reserved_bytes]} {
1869   do_test 21.1 {
1870     # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1871     #
1872     db close
1873     forcedelete test.db
1874     sqlite3 db test.db
1875     execsql { 
1876       PRAGMA page_size = 1024;
1877       PRAGMA auto_vacuum = 0;
1878       CREATE TABLE t1(a PRIMARY KEY, b);
1879       INSERT INTO t1 VALUES(1, 1);
1880     }
1881     for {set i 0} {$i < 10} {incr i} {
1882       execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1883     }
1884     db close
1885     forcecopy test.db testerr.db
1886     hexio_write testerr.db 15000 [string repeat 55 100]
1887   } {100}
1888   
1889   set mainerr {*** in database main ***
1890 Multiple uses for byte 672 of page 15}
1891   set auxerr {*** in database aux ***
1892 Multiple uses for byte 672 of page 15}
1893   
1894   set mainerr {/{\*\*\* in database main \*\*\*
1895 Multiple uses for byte 672 of page 15}.*/}
1896   set auxerr {/{\*\*\* in database aux \*\*\*
1897 Multiple uses for byte 672 of page 15}.*/}
1898   
1899   do_test 22.2 {
1900     catch { db close }
1901     sqlite3 db testerr.db
1902     execsql { PRAGMA integrity_check }
1903   } $mainerr
1904   
1905   do_test 22.3.1 {
1906     catch { db close }
1907     sqlite3 db test.db
1908     execsql { 
1909       ATTACH 'testerr.db' AS 'aux';
1910       PRAGMA integrity_check;
1911     }
1912   } $auxerr
1913   do_test 22.3.2 {
1914     execsql { PRAGMA main.integrity_check; }
1915   } {ok}
1916   do_test 22.3.3 {
1917     execsql { PRAGMA aux.integrity_check; }
1918   } $auxerr
1919   
1920   do_test 22.4.1 {
1921     catch { db close }
1922     sqlite3 db testerr.db
1923     execsql { 
1924       ATTACH 'test.db' AS 'aux';
1925       PRAGMA integrity_check;
1926     }
1927   } $mainerr
1928   do_test 22.4.2 {
1929     execsql { PRAGMA main.integrity_check; }
1930   } $mainerr
1931   do_test 22.4.3 {
1932     execsql { PRAGMA aux.integrity_check; }
1933   } {ok}
1935   
1936 db close
1937 forcedelete test.db test.db-wal test.db-journal
1938 sqlite3 db test.db
1939 sqlite3 db2 test.db
1940 do_test 23.1 {
1941   db eval {
1942     CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1943     CREATE INDEX i1 ON t1(b,c);
1944     CREATE INDEX i2 ON t1(c,d);
1945     CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
1946     CREATE INDEX i3 ON t1(d,b+c,c);
1947     CREATE TABLE t2(x INTEGER REFERENCES t1);
1948   }
1949   db2 eval {SELECT name FROM sqlite_master}
1950 } {t1 i1 i2 i2x i3 t2}
1951 do_test 23.2a {
1952   db eval {
1953     DROP INDEX i2;
1954     CREATE INDEX i2 ON t1(c,d,b);
1955   }
1956   capture_pragma db2 out {PRAGMA index_info(i2)}
1957   db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1958 } {2 c | 3 d | 1 b |}
1960 # EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1961 # pragma returns information about every column in an index.
1963 # EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1964 # returns information about every column in the index, not just the key
1965 # columns.
1967 do_test 23.2b {
1968   capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1969   db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1970 } {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
1972 # (The first column of output from PRAGMA index_xinfo is...)
1973 # EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1974 # means left-most. Key columns come before auxiliary columns.)
1976 # (The second column of output from PRAGMA index_xinfo is...)
1977 # EVIDENCE-OF: R-06603-49335 The rank of the column within the table
1978 # being indexed, or -1 if the index-column is the rowid of the table
1979 # being indexed and -2 if the index is on an expression.
1981 # (The third column of output from PRAGMA index_xinfo is...)
1982 # EVIDENCE-OF: R-40641-22898 The name of the column being indexed, or
1983 # NULL if the index-column is the rowid of the table being indexed or an
1984 # expression.
1986 # (The fourth column of output from PRAGMA index_xinfo is...)
1987 # EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1988 # (DESC) order by the index and 0 otherwise.
1990 # (The fifth column of output from PRAGMA index_xinfo is...)
1991 # EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1992 # compare values in the index-column.
1994 # (The sixth column of output from PRAGMA index_xinfo is...)
1995 # EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1996 # if the index-column is an auxiliary column.
1998 do_test 23.2c {
1999   db2 eval {PRAGMA index_xinfo(i2)}
2000 } {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
2001 do_test 23.2d {
2002   db2 eval {PRAGMA index_xinfo(i2x)}
2003 } {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
2004 do_test 23.2e {
2005   db2 eval {PRAGMA index_xinfo(i3)}
2006 } {0 3 d 0 BINARY 1 1 -2 {} 0 BINARY 1 2 2 c 0 BINARY 1 3 -1 {} 0 BINARY 0}
2008 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
2009 # pragma returns one row for each index associated with the given table.
2011 # (The first column of output from PRAGMA index_list is...)
2012 # EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
2013 # for internal tracking purposes.
2015 # (The second column of output from PRAGMA index_list is...)
2016 # EVIDENCE-OF: R-35496-03635 The name of the index.
2018 # (The third column of output from PRAGMA index_list is...)
2019 # EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
2021 # (The fourth column of output from PRAGMA index_list is...)
2022 # EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
2023 # INDEX statement, "u" if the index was created by a UNIQUE constraint,
2024 # or "pk" if the index was created by a PRIMARY KEY constraint.
2026 do_test 23.3 {
2027   db eval {
2028     DROP INDEX IF EXISTS i3;
2029     CREATE INDEX i3 ON t1(d,b,c);
2030   }
2031   capture_pragma db2 out {PRAGMA index_list(t1)}
2032   db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
2033 } {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
2034 ifcapable altertable {
2035   do_test 23.4 {
2036     db eval {
2037       ALTER TABLE t1 ADD COLUMN e;
2038     }
2039     db2 eval {
2040       PRAGMA table_info(t1);
2041     }
2042   } {/4 e {} 0 {} 0/}
2044 do_test 23.5 {
2045   db eval {
2046     DROP TABLE t2;
2047     CREATE TABLE t2(x, y INTEGER REFERENCES t1);
2048   }
2049   db2 eval {
2050     PRAGMA foreign_key_list(t2);
2051   }
2052 } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
2053 db2 close
2055 ifcapable !has_codec {
2056   reset_db
2057   do_execsql_test 24.0 {
2058     PRAGMA page_size = 1024;
2059     CREATE TABLE t1(a, b, c);
2060     CREATE INDEX i1 ON t1(b);
2061     INSERT INTO t1 VALUES('a', 'b', 'c');
2062     PRAGMA integrity_check;
2063   } {ok}
2064   
2065   set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
2066   db close
2067   hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
2068   
2069   sqlite3 db test.db
2070   do_catchsql_test 24.1 {
2071     SELECT * FROM t1;
2072   } {1 {database disk image is malformed}}
2073   do_catchsql_test 24.2 {
2074     PRAGMA integrity_check;
2075   } {0 {{database disk image is malformed}}}
2076 }  
2077 database_never_corrupt
2079 # 2023-03-27.  Register allocation issue in integrity_check discovered
2080 # by new assert() statements added in [6f8b97f31a4c8552].
2081 # dbsqlfuzz dc9ab26037cf5ef797d28cd1ae0855ade584216d
2082 # tag-20230327-1
2084 reset_db
2085 do_execsql_test 25.0 {
2086   CREATE TABLE t1(a INT, b AS (a*2) NOT NULL);
2087   CREATE TEMP TABLE t2(a PRIMARY KEY, b, c UNIQUE) WITHOUT ROWID;
2088   CREATE UNIQUE INDEX t2x ON t2(c,b);
2089   PRAGMA integrity_check;
2090 } ok
2091 finish_test