explicitly disable backup for encrypted databases
[sqlcipher.git] / test / pragma.test
blob15289846540909b4a9013cebddc80bc775e10099
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}
254 } ;# ifcapable pager_pragmas
256 # Test turning "flag" pragmas on and off.
258 ifcapable debug {
259   # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
260   #
261   do_test pragma-1.15 {
262     execsql {
263       PRAGMA vdbe_listing=YES;
264       PRAGMA vdbe_listing;
265     }
266   } {1}
267   do_test pragma-1.16 {
268     execsql {
269       PRAGMA vdbe_listing=NO;
270       PRAGMA vdbe_listing;
271     }
272   } {0}
275 do_test pragma-1.17 {
276   execsql {
277     PRAGMA parser_trace=ON;
278     PRAGMA parser_trace=OFF;
279   }
280 } {}
281 do_test pragma-1.18 {
282   execsql {
283     PRAGMA bogus = -1234;  -- Parsing of negative values
284   }
285 } {}
287 # Test modifying the safety_level of an attached database.
288 ifcapable pager_pragmas&&attach {
289   do_test pragma-2.1 {
290     forcedelete test2.db
291     forcedelete test2.db-journal
292     execsql {
293       ATTACH 'test2.db' AS aux;
294     } 
295   } {}
296   do_test pragma-2.2 {
297     execsql {
298       pragma aux.synchronous;
299     } 
300   } {2}
301   do_test pragma-2.3 {
302     execsql {
303       pragma aux.synchronous = OFF;
304       pragma aux.synchronous;
305       pragma synchronous;
306     } 
307   } {0 2}
308   do_test pragma-2.4 {
309     execsql {
310       pragma aux.synchronous = ON;
311       pragma synchronous;
312       pragma aux.synchronous;
313     } 
314   } {2 1}
315 } ;# ifcapable pager_pragmas
317 # Construct a corrupted index and make sure the integrity_check
318 # pragma finds it.
320 # These tests won't work if the database is encrypted
322 do_test pragma-3.1 {
323   db close
324   forcedelete test.db test.db-journal
325   sqlite3 db test.db
326   execsql {
327     PRAGMA auto_vacuum=OFF;
328     BEGIN;
329     CREATE TABLE t2(a,b,c);
330     CREATE INDEX i2 ON t2(a);
331     INSERT INTO t2 VALUES(11,2,3);
332     INSERT INTO t2 VALUES(22,3,4);
333     COMMIT;
334     SELECT rowid, * from t2;
335   }
336 } {1 11 2 3 2 22 3 4}
337 ifcapable attach {
338   if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
339     do_test pragma-3.2 {
340       db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
341       set pgsz [db eval {PRAGMA page_size}]
342       # overwrite the header on the rootpage of the index in order to
343       # make the index appear to be empty.
344       #
345       set offset [expr {$pgsz*($rootpage-1)}]
346       hexio_write test.db $offset 0a00000000040000000000
347       db close
348       sqlite3 db test.db
349       execsql {PRAGMA integrity_check}
350     } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
351     do_test pragma-3.3 {
352       execsql {PRAGMA integrity_check=1}
353     } {{row 1 missing from index i2}}
354     do_test pragma-3.4 {
355       execsql {
356         ATTACH DATABASE 'test.db' AS t2;
357         PRAGMA integrity_check
358       }
359     } {{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} {wrong # of entries in index i2}}
360     do_test pragma-3.5 {
361       execsql {
362         PRAGMA integrity_check=4
363       }
364     } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
365     do_test pragma-3.6 {
366       execsql {
367         PRAGMA integrity_check=xyz
368       }
369     } {{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} {wrong # of entries in index i2}}
370     do_test pragma-3.7 {
371       execsql {
372         PRAGMA integrity_check=0
373       }
374     } {{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} {wrong # of entries in index i2}}
375   
376     # Add additional corruption by appending unused pages to the end of
377     # the database file testerr.db
378     #
379     do_test pragma-3.8 {
380       execsql {DETACH t2}
381       forcedelete testerr.db testerr.db-journal
382       set out [open testerr.db w]
383       fconfigure $out -translation binary
384       set in [open test.db r]
385       fconfigure $in -translation binary
386       puts -nonewline $out [read $in]
387       seek $in 0
388       puts -nonewline $out [read $in]
389       close $in
390       close $out
391       hexio_write testerr.db 28 00000000
392       execsql {REINDEX t2}
393       execsql {PRAGMA integrity_check}
394     } {ok}
395     do_test pragma-3.8.1 {
396       execsql {PRAGMA quick_check}
397     } {ok}
398     do_test pragma-3.8.2 {
399       execsql {PRAGMA QUICK_CHECK}
400     } {ok}
401     do_test pragma-3.9 {
402       execsql {
403         ATTACH 'testerr.db' AS t2;
404         PRAGMA integrity_check
405       }
406     } {{*** in database t2 ***
407 Page 4 is never used
408 Page 5 is never used
409 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
410     do_test pragma-3.10 {
411       execsql {
412         PRAGMA integrity_check=1
413       }
414     } {{*** in database t2 ***
415 Page 4 is never used}}
416     do_test pragma-3.11 {
417       execsql {
418         PRAGMA integrity_check=5
419       }
420     } {{*** in database t2 ***
421 Page 4 is never used
422 Page 5 is never used
423 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
424     do_test pragma-3.12 {
425       execsql {
426         PRAGMA integrity_check=4
427       }
428     } {{*** in database t2 ***
429 Page 4 is never used
430 Page 5 is never used
431 Page 6 is never used} {row 1 missing from index i2}}
432     do_test pragma-3.13 {
433       execsql {
434         PRAGMA integrity_check=3
435       }
436     } {{*** in database t2 ***
437 Page 4 is never used
438 Page 5 is never used
439 Page 6 is never used}}
440     do_test pragma-3.14 {
441       execsql {
442         PRAGMA integrity_check(2)
443       }
444     } {{*** in database t2 ***
445 Page 4 is never used
446 Page 5 is never used}}
447     do_test pragma-3.15 {
448       execsql {
449         ATTACH 'testerr.db' AS t3;
450         PRAGMA integrity_check
451       }
452     } {{*** in database t2 ***
453 Page 4 is never used
454 Page 5 is never used
455 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
456 Page 4 is never used
457 Page 5 is never used
458 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
459     do_test pragma-3.16 {
460       execsql {
461         PRAGMA integrity_check(10)
462       }
463     } {{*** in database t2 ***
464 Page 4 is never used
465 Page 5 is never used
466 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
467 Page 4 is never used
468 Page 5 is never used
469 Page 6 is never used} {row 1 missing from index i2}}
470     do_test pragma-3.17 {
471       execsql {
472         PRAGMA integrity_check=8
473       }
474     } {{*** in database t2 ***
475 Page 4 is never used
476 Page 5 is never used
477 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
478 Page 4 is never used
479 Page 5 is never used}}
480     do_test pragma-3.18 {
481       execsql {
482         PRAGMA integrity_check=4
483       }
484     } {{*** in database t2 ***
485 Page 4 is never used
486 Page 5 is never used
487 Page 6 is never used} {row 1 missing from index i2}}
488   }
489   do_test pragma-3.19 {
490     catch {db close}
491     forcedelete test.db test.db-journal
492     sqlite3 db test.db
493     db eval {PRAGMA integrity_check}
494   } {ok}
497 # Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
498 # constraint violations.
500 sqlite3_db_config db DEFENSIVE 0
501 do_execsql_test pragma-3.20 {
502   CREATE TABLE t1(a,b);
503   CREATE INDEX t1a ON t1(a);
504   INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
505   PRAGMA writable_schema=ON;
506   UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
507    WHERE name='t1a';
508   UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
509    WHERE name='t1';
510   PRAGMA writable_schema=OFF;
511   ALTER TABLE t1 RENAME TO t1x;
512   PRAGMA integrity_check;
513 } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
514 do_execsql_test pragma-3.21 {
515   PRAGMA integrity_check(3);
516 } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
517 do_execsql_test pragma-3.22 {
518   PRAGMA integrity_check(2);
519 } {{non-unique entry in index t1a} {NULL value in t1x.a}}
520 do_execsql_test pragma-3.23 {
521   PRAGMA integrity_check(1);
522 } {{non-unique entry in index t1a}}
524 # PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
525 # interface) used to leave index cursors in an inconsistent state
526 # which could result in an assertion fault in sqlite3BtreeKey()
527 # called from saveCursorPosition() if content is removed from the
528 # index while the integrity_check is still running.  This test verifies
529 # that problem has been fixed.
531 do_test pragma-3.30 {
532   db close
533   delete_file test.db
534   sqlite3 db test.db
535   db eval {
536     CREATE TABLE t1(a,b,c);
537     WITH RECURSIVE
538       c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
539     INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
540     CREATE INDEX t1a ON t1(a);
541     CREATE INDEX t1bc ON t1(b,c);
542   }
543   db eval {PRAGMA integrity_check} {
544      db eval {DELETE FROM t1}
545   }
546 } {}
548 # Test modifying the cache_size of an attached database.
549 ifcapable pager_pragmas&&attach {
550 do_test pragma-4.1 {
551   execsql {
552     ATTACH 'test2.db' AS aux;
553     pragma aux.cache_size;
554     pragma aux.default_cache_size;
555   } 
556 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
557 do_test pragma-4.2 {
558   execsql {
559     pragma aux.cache_size = 50;
560     pragma aux.cache_size;
561     pragma aux.default_cache_size;
562   } 
563 } [list 50 $DFLT_CACHE_SZ]
564 do_test pragma-4.3 {
565   execsql {
566     pragma aux.default_cache_size = 456;
567     pragma aux.cache_size;
568     pragma aux.default_cache_size;
569   } 
570 } {456 456}
571 do_test pragma-4.4 {
572   execsql {
573     pragma cache_size;
574     pragma default_cache_size;
575   } 
576 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
577 do_test pragma-4.5 {
578   execsql {
579     DETACH aux;
580     ATTACH 'test3.db' AS aux;
581     pragma aux.cache_size;
582     pragma aux.default_cache_size;
583   } 
584 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
585 do_test pragma-4.6 {
586   execsql {
587     DETACH aux;
588     ATTACH 'test2.db' AS aux;
589     pragma aux.cache_size;
590     pragma aux.default_cache_size;
591   } 
592 } {456 456}
593 } ;# ifcapable pager_pragmas
595 # Test that modifying the sync-level in the middle of a transaction is
596 # disallowed.
597 ifcapable pager_pragmas {
598 do_test pragma-5.0 {
599   execsql {
600     pragma synchronous;
601   } 
602 } {2}
603 do_test pragma-5.1 {
604   catchsql {
605     BEGIN;
606     pragma synchronous = OFF;
607   } 
608 } {1 {Safety level may not be changed inside a transaction}}
609 do_test pragma-5.2 {
610   execsql {
611     pragma synchronous;
612   } 
613 } {2}
614 catchsql {COMMIT;}
615 } ;# ifcapable pager_pragmas
617 # Test schema-query pragmas
619 ifcapable schema_pragmas {
620 ifcapable tempdb&&attach {
621   do_test pragma-6.1 {
622     set res {}
623     execsql {SELECT * FROM sqlite_temp_master}
624     foreach {idx name file} [execsql {pragma database_list}] {
625       lappend res $idx $name
626     }
627     set res
628   } {0 main 1 temp 2 aux}
630 do_test pragma-6.2 {
631   execsql {
632     CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
633     pragma table_info(t2)
634   }
635 } {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
636 do_test pragma-6.2.1 {
637   execsql {
638     pragma table_info;
639   }
640 } {}
641 db nullvalue <<NULL>>
642 do_test pragma-6.2.2 {
643   execsql {
644     CREATE TABLE t5(
645       a TEXT DEFAULT CURRENT_TIMESTAMP, 
646       b DEFAULT (5+3),
647       c TEXT,
648       d INTEGER DEFAULT NULL,
649       e TEXT DEFAULT '',
650       UNIQUE(b,c,d),
651       PRIMARY KEY(e,b,c)
652     );
653     PRAGMA table_info(t5);
654   }
655 } {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}
656 db nullvalue {}
657 do_test pragma-6.2.3 {
658   execsql {
659     CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
660     pragma table_info(t2_3)
661   }
662 } {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
663 ifcapable {foreignkey} {
664   do_test pragma-6.3.1 {
665     execsql {
666       CREATE TABLE t3(a int references t2(b), b UNIQUE);
667       pragma foreign_key_list(t3);
668     }
669   } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
670   do_test pragma-6.3.2 {
671     execsql {
672       pragma foreign_key_list;
673     }
674   } {}
675   do_test pragma-6.3.3 {
676     execsql {
677       pragma foreign_key_list(t3_bogus);
678     }
679   } {}
680   do_test pragma-6.3.4 {
681     execsql {
682       pragma foreign_key_list(t5);
683     }
684   } {}
685   do_test pragma-6.4 {
686     capture_pragma db out {
687       pragma index_list(t3);
688     }
689     db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
690   } {0 sqlite_autoindex_t3_1 1}
692 ifcapable {!foreignkey} {
693   execsql {CREATE TABLE t3(a,b UNIQUE)}
695 do_test pragma-6.5.1 {
696   execsql {
697     CREATE INDEX t3i1 ON t3(a,b);
698   }
699   capture_pragma db out {
700     pragma index_info(t3i1);
701   }
702   db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
703 } {0 0 a 1 1 b}
705 # EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
706 # by the index_info pragma, but they are listed by the index_xinfo
707 # pragma.
709 do_test pragma-6.5.1b {
710   capture_pragma db out {PRAGMA index_xinfo(t3i1)}
711   db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
712 } {0 0 a 1 1 b 2 -1 {}}
715 # EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
716 # pragma returns one row for each key column in the named index.
718 # (The first column of output from PRAGMA index_info is...)
719 # EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
720 # means left-most.)
722 # (The second column of output from PRAGMA index_info is...)
723 # EVIDENCE-OF: R-65019-08383 The rank of the column within the table
724 # being indexed.
726 # (The third column of output from PRAGMA index_info is...)
727 # EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
729 do_execsql_test pragma-6.5.1c {
730   CREATE INDEX t3i2 ON t3(b,a);
731   PRAGMA index_info='t3i2';
732   DROP INDEX t3i2;
733 } {0 1 b 1 0 a}
735 do_test pragma-6.5.2 {
736   execsql {
737     pragma index_info(t3i1_bogus);
738   }
739 } {}
741 ifcapable tempdb {
742   # Test for ticket #3320. When a temp table of the same name exists, make
743   # sure the schema of the main table can still be queried using 
744   # "pragma table_info":
745   do_test pragma-6.6.1 {
746     execsql {
747       CREATE TABLE trial(col_main);
748       CREATE TEMP TABLE trial(col_temp);
749     }
750   } {}
751   do_test pragma-6.6.2 {
752     execsql {
753       PRAGMA table_info(trial);
754     }
755   } {0 col_temp {} 0 {} 0}
756   do_test pragma-6.6.3 {
757     execsql {
758       PRAGMA temp.table_info(trial);
759     }
760   } {0 col_temp {} 0 {} 0}
761   do_test pragma-6.6.4 {
762     execsql {
763       PRAGMA main.table_info(trial);
764     }
765   } {0 col_main {} 0 {} 0}
768 do_test pragma-6.7 {
769   execsql {
770     CREATE TABLE test_table(
771       one INT NOT NULL DEFAULT -1, 
772       two text,
773       three VARCHAR(45, 65) DEFAULT 'abcde',
774       four REAL DEFAULT X'abcdef',
775       five DEFAULT CURRENT_TIME
776     );
777   }
778   capture_pragma db out {PRAGMA table_info(test_table)}
779   db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
780             ORDER BY cid}
781 } [concat \
782   {0 one INT 1 -1 0} \
783   {1 two text 0 {} 0} \
784   {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
785   {3 four REAL 0 X'abcdef' 0} \
786   {4 five {} 0 CURRENT_TIME 0} \
788 do_test pragma-6.8 {
789   execsql {
790     CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
791     PRAGMA table_info(t68);
792   }
793 } [concat \
794   {0 a {} 0 {} 1} \
795   {1 b {} 0 {} 2} \
796   {2 c {} 0 {} 4} \
798 } ;# ifcapable schema_pragmas
799 # Miscellaneous tests
801 ifcapable schema_pragmas {
802 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
803 # pragma returns one row for each index associated with the given table.
805 do_test pragma-7.1.1 {
806   # Make sure a pragma knows to read the schema if it needs to
807   db close
808   sqlite3 db test.db
809   capture_pragma db out "PRAGMA index_list(t3)"
810   db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
811 } {t3i1 c sqlite_autoindex_t3_1 u}
812 do_test pragma-7.1.2 {
813   execsql {
814     pragma index_list(t3_bogus);
815   }
816 } {}
817 } ;# ifcapable schema_pragmas
818 ifcapable {utf16} {
819   if {[permutation] == ""} {
820     do_test pragma-7.2 {
821       db close
822       sqlite3 db test.db
823       catchsql {
824         pragma encoding=bogus;
825       }
826     } {1 {unsupported encoding: bogus}}
827   }
829 ifcapable tempdb {
830   do_test pragma-7.3 {
831     db close
832     sqlite3 db test.db
833     execsql {
834       pragma lock_status;
835     }
836   } {main unlocked temp closed}
837 } else {
838   do_test pragma-7.3 {
839     db close
840     sqlite3 db test.db
841     execsql {
842       pragma lock_status;
843     }
844   } {main unlocked}
848 #----------------------------------------------------------------------
849 # Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
850 # user_version" statements.
852 # pragma-8.1: PRAGMA schema_version
853 # pragma-8.2: PRAGMA user_version
856 ifcapable schema_version {
858 # First check that we can set the schema version and then retrieve the
859 # same value.
860 do_test pragma-8.1.1 {
861   execsql {
862     PRAGMA schema_version = 105;
863   }
864 } {}
865 do_test pragma-8.1.2 {
866   execsql2 {
867     PRAGMA schema_version;
868   }
869 } {schema_version 105}
870 do_test pragma-8.1.3 {
871   execsql {
872     PRAGMA schema_version = 106;
873   }
874 } {}
875 do_test pragma-8.1.4 {
876   execsql {
877     PRAGMA schema_version;
878   }
879 } 106
881 # Check that creating a table modifies the schema-version (this is really
882 # to verify that the value being read is in fact the schema version).
883 do_test pragma-8.1.5 {
884   execsql {
885     CREATE TABLE t4(a, b, c);
886     INSERT INTO t4 VALUES(1, 2, 3);
887     SELECT * FROM t4;
888   }
889 } {1 2 3}
890 do_test pragma-8.1.6 {
891   execsql {
892     PRAGMA schema_version;
893   }
894 } 107
896 # Now open a second connection to the database. Ensure that changing the
897 # schema-version using the first connection forces the second connection
898 # to reload the schema. This has to be done using the C-API test functions,
899 # because the TCL API accounts for SCHEMA_ERROR and retries the query.
900 do_test pragma-8.1.7 {
901   sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
902   execsql {
903     SELECT * FROM t4;
904   } db2
905 } {1 2 3}
906 do_test pragma-8.1.8 {
907   execsql {
908     PRAGMA schema_version = 108;
909   }
910 } {}
911 do_test pragma-8.1.9 {
912   set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
913   sqlite3_step $::STMT
914 } SQLITE_ERROR
915 do_test pragma-8.1.10 {
916   sqlite3_finalize $::STMT
917 } SQLITE_SCHEMA
919 # Make sure the schema-version can be manipulated in an attached database.
920 forcedelete test2.db
921 forcedelete test2.db-journal
922 ifcapable attach {
923   do_test pragma-8.1.11 {
924     execsql {
925       ATTACH 'test2.db' AS aux;
926       CREATE TABLE aux.t1(a, b, c);
927       PRAGMA aux.schema_version = 205;
928     }
929   } {}
930   do_test pragma-8.1.12 {
931     execsql {
932       PRAGMA aux.schema_version;
933     }
934   } 205
936 do_test pragma-8.1.13 {
937   execsql {
938     PRAGMA schema_version;
939   }
940 } 108
942 # And check that modifying the schema-version in an attached database
943 # forces the second connection to reload the schema.
944 ifcapable attach {
945   do_test pragma-8.1.14 {
946     sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
947     execsql {
948       ATTACH 'test2.db' AS aux;
949       SELECT * FROM aux.t1;
950     } db2
951   } {}
952   do_test pragma-8.1.15 {
953     execsql {
954       PRAGMA aux.schema_version = 206;
955     }
956   } {}
957   do_test pragma-8.1.16 {
958     set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
959     sqlite3_step $::STMT
960   } SQLITE_ERROR
961   do_test pragma-8.1.17 {
962     sqlite3_finalize $::STMT
963   } SQLITE_SCHEMA
964   do_test pragma-8.1.18 {
965     db2 close
966   } {}
969 # Now test that the user-version can be read and written (and that we aren't
970 # accidentally manipulating the schema-version instead).
971 do_test pragma-8.2.1 {
972   execsql2 {
973     PRAGMA user_version;
974   }
975 } {user_version 0}
976 do_test pragma-8.2.2 {
977   execsql {
978     PRAGMA user_version = 2;
979   }
980 } {}
981 do_test pragma-8.2.3.1 {
982   execsql2 {
983     PRAGMA user_version;
984   }
985 } {user_version 2}
986 do_test pragma-8.2.3.2 {
987   db close
988   sqlite3 db test.db
989   execsql {
990     PRAGMA user_version;
991   }
992 } {2}
993 do_test pragma-8.2.4.1 {
994   execsql {
995     PRAGMA schema_version;
996   }
997 } {108}
998 ifcapable vacuum {
999   do_test pragma-8.2.4.2 {
1000     execsql {
1001       VACUUM;
1002       PRAGMA user_version;
1003     }
1004   } {2}
1005   do_test pragma-8.2.4.3 {
1006     execsql {
1007       PRAGMA schema_version;
1008     }
1009   } {109}
1012 ifcapable attach {
1013   db eval {ATTACH 'test2.db' AS aux}
1014   
1015   # Check that the user-version in the auxilary database can be manipulated (
1016   # and that we aren't accidentally manipulating the same in the main db).
1017   do_test pragma-8.2.5 {
1018     execsql {
1019       PRAGMA aux.user_version;
1020     }
1021   } {0}
1022   do_test pragma-8.2.6 {
1023     execsql {
1024       PRAGMA aux.user_version = 3;
1025     }
1026   } {}
1027   do_test pragma-8.2.7 {
1028     execsql {
1029       PRAGMA aux.user_version;
1030     }
1031   } {3}
1032   do_test pragma-8.2.8 {
1033     execsql {
1034       PRAGMA main.user_version;
1035     }
1036   } {2}
1037   
1038   # Now check that a ROLLBACK resets the user-version if it has been modified
1039   # within a transaction.
1040   do_test pragma-8.2.9 {
1041     execsql {
1042       BEGIN;
1043       PRAGMA aux.user_version = 10;
1044       PRAGMA user_version = 11;
1045     }
1046   } {}
1047   do_test pragma-8.2.10 {
1048     execsql {
1049       PRAGMA aux.user_version;
1050     }
1051   } {10}
1052   do_test pragma-8.2.11 {
1053     execsql {
1054       PRAGMA main.user_version;
1055     }
1056   } {11}
1057   do_test pragma-8.2.12 {
1058     execsql {
1059       ROLLBACK;
1060       PRAGMA aux.user_version;
1061     }
1062   } {3}
1063   do_test pragma-8.2.13 {
1064     execsql {
1065       PRAGMA main.user_version;
1066     }
1067   } {2}
1070 # Try a negative value for the user-version
1071 do_test pragma-8.2.14 {
1072   execsql {
1073     PRAGMA user_version = -450;
1074   }
1075 } {}
1076 do_test pragma-8.2.15 {
1077   execsql {
1078     PRAGMA user_version;
1079   }
1080 } {-450}
1081 } ; # ifcapable schema_version
1083 # Check to see if TEMP_STORE is memory or disk.  Return strings
1084 # "memory" or "disk" as appropriate.
1086 proc check_temp_store {} {
1087   db eval {
1088     PRAGMA temp.cache_size = 1;
1089     CREATE TEMP TABLE IF NOT EXISTS a(b);
1090     DELETE FROM a;
1091     INSERT INTO a VALUES(randomblob(1000));
1092     INSERT INTO a SELECT * FROM a;
1093     INSERT INTO a SELECT * FROM a;
1094     INSERT INTO a SELECT * FROM a;
1095     INSERT INTO a SELECT * FROM a;
1096     INSERT INTO a SELECT * FROM a;
1097     INSERT INTO a SELECT * FROM a;
1098     INSERT INTO a SELECT * FROM a;
1099     INSERT INTO a SELECT * FROM a;
1100   }
1101   db eval {PRAGMA database_list} {
1102     if {$name=="temp"} {
1103       set bt [btree_from_db db 1]
1104       if {[btree_ismemdb $bt]} {
1105         return "memory"
1106       }
1107       return "disk"
1108     }
1109   }
1110   return "unknown"
1113 # Application_ID
1115 do_test pragma-8.3.1 {
1116   execsql {
1117     PRAGMA application_id;
1118   }
1119 } {0}
1120 do_test pragma-8.3.2 {
1121   execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1122 } {12345}
1124 # Test temp_store and temp_store_directory pragmas
1126 ifcapable pager_pragmas {
1127 do_test pragma-9.1 {
1128   db close
1129   sqlite3 db test.db
1130   execsql {
1131     PRAGMA temp_store;
1132   }
1133 } {0}
1134 if {$TEMP_STORE<=1} {
1135   do_test pragma-9.1.1 {
1136     check_temp_store
1137   } {disk}
1138 } else {
1139   do_test pragma-9.1.1 {
1140     check_temp_store
1141   } {memory}
1144 do_test pragma-9.2 {
1145   db close
1146   sqlite3 db test.db
1147   execsql {
1148     PRAGMA temp_store=file;
1149     PRAGMA temp_store;
1150   }
1151 } {1}
1152 if {$TEMP_STORE==3} {
1153   # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1154   do_test pragma-9.2.1 {
1155     check_temp_store
1156   } {memory}
1157 } else {
1158   do_test pragma-9.2.1 {
1159     check_temp_store
1160   } {disk}
1163 do_test pragma-9.3 {
1164   db close
1165   sqlite3 db test.db
1166   execsql {
1167     PRAGMA temp_store=memory;
1168     PRAGMA temp_store;
1169   }
1170 } {2}
1171 if {$TEMP_STORE==0} {
1172   # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1173   do_test pragma-9.3.1 {
1174     check_temp_store
1175   } {disk}
1176 } else {
1177   do_test pragma-9.3.1 {
1178     check_temp_store
1179   } {memory}
1182 do_test pragma-9.4 {
1183   execsql {
1184     PRAGMA temp_store_directory;
1185   }
1186 } {}
1187 ifcapable wsd {
1188   do_test pragma-9.5 {
1189     set pwd [string map {' ''} [file nativename [get_pwd]]]
1190     execsql "
1191       PRAGMA temp_store_directory='$pwd';
1192     "
1193   } {}
1194   do_test pragma-9.6 {
1195     execsql { 
1196       PRAGMA temp_store_directory;
1197     }
1198   } [list [file nativename [get_pwd]]]
1199   do_test pragma-9.7 {
1200     catchsql { 
1201       PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1202     }
1203   } {1 {not a writable directory}}
1204   do_test pragma-9.8 {
1205     execsql { 
1206       PRAGMA temp_store_directory='';
1207     }
1208   } {}
1209   if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1210     ifcapable tempdb {
1211       do_test pragma-9.9 {
1212         execsql { 
1213           PRAGMA temp_store_directory;
1214           PRAGMA temp_store=FILE;
1215           CREATE TEMP TABLE temp_store_directory_test(a integer);
1216           INSERT INTO temp_store_directory_test values (2);
1217           SELECT * FROM temp_store_directory_test;
1218         }
1219       } {2}
1220       do_test pragma-9.10 {
1221         catchsql "
1222           PRAGMA temp_store_directory='$pwd';
1223           SELECT * FROM temp_store_directory_test;
1224         "
1225       } {1 {no such table: temp_store_directory_test}}
1226     }
1227   }
1229 do_test pragma-9.11 {
1230   execsql {
1231     PRAGMA temp_store = 0;
1232     PRAGMA temp_store;
1233   }
1234 } {0}
1235 do_test pragma-9.12 {
1236   execsql {
1237     PRAGMA temp_store = 1;
1238     PRAGMA temp_store;
1239   }
1240 } {1}
1241 do_test pragma-9.13 {
1242   execsql {
1243     PRAGMA temp_store = 2;
1244     PRAGMA temp_store;
1245   }
1246 } {2}
1247 do_test pragma-9.14 {
1248   execsql {
1249     PRAGMA temp_store = 3;
1250     PRAGMA temp_store;
1251   }
1252 } {0}
1253 do_test pragma-9.15 {
1254   catchsql {
1255     BEGIN EXCLUSIVE;
1256     CREATE TEMP TABLE temp_table(t);
1257     INSERT INTO temp_table VALUES('valuable data');
1258     PRAGMA temp_store = 1;
1259   }
1260 } {1 {temporary storage cannot be changed from within a transaction}}
1261 do_test pragma-9.16 {
1262   execsql {
1263     SELECT * FROM temp_table;
1264     COMMIT;
1265   }
1266 } {{valuable data}}
1268 do_test pragma-9.17 {
1269   execsql {
1270     INSERT INTO temp_table VALUES('valuable data II');
1271     SELECT * FROM temp_table;
1272   }
1273 } {{valuable data} {valuable data II}}
1275 do_test pragma-9.18 {
1276   set rc [catch {
1277     db eval {SELECT t FROM temp_table} {
1278       execsql {pragma temp_store = 1}
1279     }
1280   } msg]
1281   list $rc $msg
1282 } {1 {temporary storage cannot be changed from within a transaction}}
1284 } ;# ifcapable pager_pragmas
1286 ifcapable trigger {
1288 do_test pragma-10.0 {
1289   catchsql {
1290     DROP TABLE main.t1;
1291   }
1292   execsql {
1293     PRAGMA count_changes = 1;
1295     CREATE TABLE t1(a PRIMARY KEY);
1296     CREATE TABLE t1_mirror(a);
1297     CREATE TABLE t1_mirror2(a);
1298     CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN 
1299       INSERT INTO t1_mirror VALUES(new.a);
1300     END;
1301     CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN 
1302       INSERT INTO t1_mirror2 VALUES(new.a);
1303     END;
1304     CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN 
1305       UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1306     END;
1307     CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN 
1308       UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1309     END;
1310     CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN 
1311       DELETE FROM t1_mirror WHERE a = old.a;
1312     END;
1313     CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN 
1314       DELETE FROM t1_mirror2 WHERE a = old.a;
1315     END;
1316   }
1317 } {}
1319 do_test pragma-10.1 {
1320   execsql {
1321     INSERT INTO t1 VALUES(randstr(10,10));
1322   }
1323 } {1}
1324 do_test pragma-10.2 {
1325   execsql {
1326     UPDATE t1 SET a = randstr(10,10);
1327   }
1328 } {1}
1329 do_test pragma-10.3 {
1330   execsql {
1331     DELETE FROM t1;
1332   }
1333 } {1}
1335 } ;# ifcapable trigger
1337 ifcapable schema_pragmas {
1338   do_test pragma-11.1 {
1339     execsql2 {
1340       pragma collation_list;
1341     }
1342   } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
1343   do_test pragma-11.2 {
1344     db collate New_Collation blah...
1345     execsql {
1346       pragma collation_list;
1347     }
1348   } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
1351 ifcapable schema_pragmas&&tempdb {
1352   do_test pragma-12.1 {
1353     sqlite3 db2 test.db
1354     execsql {
1355       PRAGMA temp.table_info('abc');
1356     } db2
1357   } {}
1358   db2 close
1360   do_test pragma-12.2 {
1361     sqlite3 db2 test.db
1362     execsql {
1363       PRAGMA temp.default_cache_size = 200;
1364       PRAGMA temp.default_cache_size;
1365     } db2
1366   } {200}
1367   db2 close
1369   do_test pragma-12.3 {
1370     sqlite3 db2 test.db
1371     execsql {
1372       PRAGMA temp.cache_size = 400;
1373       PRAGMA temp.cache_size;
1374     } db2
1375   } {400}
1376   db2 close
1379 ifcapable bloblit {
1381 do_test pragma-13.1 {
1382   execsql {
1383     DROP TABLE IF EXISTS t4;
1384     PRAGMA vdbe_trace=on;
1385     PRAGMA vdbe_listing=on;
1386     PRAGMA sql_trace=on;
1387     CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1388     INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1389     INSERT INTO t4(b) VALUES(randstr(30,30));
1390     INSERT INTO t4(b) VALUES(1.23456);
1391     INSERT INTO t4(b) VALUES(NULL);
1392     INSERT INTO t4(b) VALUES(0);
1393     INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1394     SELECT * FROM t4;
1395   }
1396   execsql {
1397     PRAGMA vdbe_trace=off;
1398     PRAGMA vdbe_listing=off;
1399     PRAGMA sql_trace=off;
1400   }
1401 } {}
1403 } ;# ifcapable bloblit 
1405 ifcapable pager_pragmas {
1406   db close
1407   forcedelete test.db
1408   sqlite3 db test.db
1410   # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1411   # number of pages in the database file.
1412   #
1413   do_test pragma-14.1 {
1414     execsql { pragma auto_vacuum = 0 }
1415     execsql { pragma page_count; pragma main.page_count }
1416   } {0 0}
1418   do_test pragma-14.2 {
1419     execsql { 
1420       CREATE TABLE abc(a, b, c);
1421       PRAGMA page_count;
1422       PRAGMA main.page_count;
1423       PRAGMA temp.page_count;
1424     }
1425   } {2 2 0}
1426   do_test pragma-14.2uc {
1427     execsql {pragma PAGE_COUNT}
1428   } {2}
1430   do_test pragma-14.3 {
1431     execsql { 
1432       BEGIN;
1433       CREATE TABLE def(a, b, c);
1434       PRAGMA page_count;
1435     }
1436   } {3}
1437   do_test pragma-14.3uc {
1438     execsql {pragma PAGE_COUNT}
1439   } {3}
1441   do_test pragma-14.4 {
1442     set page_size [db one {pragma page_size}]
1443     expr [file size test.db] / $page_size
1444   } {2}
1446   do_test pragma-14.5 {
1447     execsql {
1448       ROLLBACK;
1449       PRAGMA page_count;
1450     }
1451   } {2}
1453   do_test pragma-14.6 {
1454     forcedelete test2.db
1455     sqlite3 db2 test2.db
1456     execsql {
1457       PRAGMA auto_vacuum = 0;
1458       CREATE TABLE t1(a, b, c);
1459       CREATE TABLE t2(a, b, c);
1460       CREATE TABLE t3(a, b, c);
1461       CREATE TABLE t4(a, b, c);
1462     } db2
1463     db2 close
1464     execsql {
1465       ATTACH 'test2.db' AS aux;
1466       PRAGMA aux.page_count;
1467     } 
1468   } {5}
1469   do_test pragma-14.6uc {
1470     execsql {pragma AUX.PAGE_COUNT}
1471   } {5}
1474 # Test that the value set using the cache_size pragma is not reset when the
1475 # schema is reloaded.
1477 ifcapable pager_pragmas {
1478   db close
1479   sqlite3 db test.db
1480   do_test pragma-15.1 {
1481     execsql {
1482       PRAGMA cache_size=59;
1483       PRAGMA cache_size;
1484     }
1485   } {59}
1486   do_test pragma-15.2 {
1487     sqlite3 db2 test.db
1488     execsql {
1489       CREATE TABLE newtable(a, b, c);
1490     } db2
1491     db2 close
1492   } {}
1493   do_test pragma-15.3 {
1494     # Evaluating this statement will cause the schema to be reloaded (because
1495     # the schema was changed by another connection in pragma-15.2). At one
1496     # point there was a bug that reset the cache_size to its default value
1497     # when this happened. 
1498     execsql { SELECT * FROM sqlite_master }
1499     execsql { PRAGMA cache_size }
1500   } {59}
1503 # Reset the sqlite3_temp_directory variable for the next run of tests:
1504 sqlite3 dbX :memory:
1505 dbX eval {PRAGMA temp_store_directory = ""}
1506 dbX close
1508 ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1509   set sqlite_hostid_num 1
1511   set using_proxy 0
1512   foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1513     set using_proxy $value
1514   }
1516   # Test the lock_proxy_file pragmas.
1517   #
1518   db close
1519   set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1521   sqlite3 db test.db
1522   do_test pragma-16.1 {
1523     execsql {
1524       PRAGMA lock_proxy_file="mylittleproxy";
1525       select * from sqlite_master;
1526     }
1527     execsql {
1528       PRAGMA lock_proxy_file;
1529     } 
1530   } {mylittleproxy}
1532   do_test pragma-16.2 {
1533     sqlite3 db2 test.db
1534     execsql {
1535       PRAGMA lock_proxy_file="mylittleproxy";
1536     } db2
1537   } {}
1539   db2 close
1540   do_test pragma-16.2.1 {
1541     sqlite3 db2 test.db
1542     execsql {
1543       PRAGMA lock_proxy_file=":auto:";
1544       select * from sqlite_master;
1545     } db2
1546     execsql {
1547       PRAGMA lock_proxy_file;
1548     } db2
1549   } {mylittleproxy}
1551   db2 close
1552   do_test pragma-16.3 {
1553     sqlite3 db2 test.db
1554     execsql {
1555       PRAGMA lock_proxy_file="myotherproxy";
1556     } db2
1557     catchsql {
1558       select * from sqlite_master;
1559     } db2
1560   } {1 {database is locked}}
1562   do_test pragma-16.4 {
1563     db2 close
1564     db close
1565     sqlite3 db2 test.db
1566     execsql {
1567       PRAGMA lock_proxy_file="myoriginalproxy";
1568       PRAGMA lock_proxy_file="myotherproxy";
1569       PRAGMA lock_proxy_file;
1570     } db2
1571   } {myotherproxy}
1573   db2 close
1574   set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1575   do_test pragma-16.5 {
1576     sqlite3 db2 test.db
1577     execsql {
1578       PRAGMA lock_proxy_file=":auto:";
1579       PRAGMA lock_proxy_file;
1580     } db2
1581   } {myotherproxy}
1582   
1583   do_test pragma-16.6 {
1584     db2 close
1585     sqlite3 db2 test2.db
1586     set lockpath [execsql {
1587       PRAGMA lock_proxy_file=":auto:";
1588       PRAGMA lock_proxy_file;
1589     } db2]
1590     string match "*test2.db:auto:" $lockpath
1591   } {1}
1592   
1593   set sqlite_hostid_num 2
1594   do_test pragma-16.7 {
1595     list [catch {
1596       sqlite3 db test2.db
1597       execsql { 
1598         PRAGMA lock_proxy_file=":auto:";
1599         select * from sqlite_master;
1600       }
1601     } msg] $msg
1602   } {1 {database is locked}}
1603   db close
1604   
1605   do_test pragma-16.8 {
1606     list [catch {
1607       sqlite3 db test2.db
1608       execsql { select * from sqlite_master } 
1609     } msg] $msg
1610   } {1 {database is locked}}
1612   db2 close
1613   do_test pragma-16.8.1 {
1614     execsql {
1615       PRAGMA lock_proxy_file="yetanotherproxy";
1616       PRAGMA lock_proxy_file;
1617     } 
1618   } {yetanotherproxy}
1619   do_test pragma-16.8.2 {
1620     execsql {
1621       create table mine(x);
1622     } 
1623   } {}
1625   db close
1626   do_test pragma-16.9 {
1627     sqlite3 db proxytest.db
1628     set lockpath2 [execsql {
1629       PRAGMA lock_proxy_file=":auto:";
1630       PRAGMA lock_proxy_file;
1631     } db]
1632     string match "*proxytest.db:auto:" $lockpath2
1633   } {1}
1635   set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1636   set sqlite_hostid_num 0
1639 # Parsing of auto_vacuum settings.
1641 foreach {autovac_setting val} {
1642   0 0
1643   1 1
1644   2 2
1645   3 0
1646   -1 0
1647   none 0
1648   NONE 0
1649   NoNe 0
1650   full 1
1651   FULL 1
1652   incremental 2
1653   INCREMENTAL 2
1654   -1234 0
1655   1234 0
1656 } {
1657   do_test pragma-17.1.$autovac_setting {
1658     catch {db close}
1659     sqlite3 db :memory:
1660     execsql "
1661       PRAGMA auto_vacuum=$::autovac_setting;
1662       PRAGMA auto_vacuum;
1663     "
1664   } $val
1667 # Parsing of temp_store settings.
1669 foreach {temp_setting val} {
1670   0 0
1671   1 1
1672   2 2
1673   3 0
1674   -1 0
1675   file 1
1676   FILE 1
1677   fIlE 1
1678   memory 2
1679   MEMORY 2
1680   MeMoRy 2
1681 } {
1682   do_test pragma-18.1.$temp_setting {
1683     catch {db close}
1684     sqlite3 db :memory:
1685     execsql "
1686       PRAGMA temp_store=$::temp_setting;
1687       PRAGMA temp_store=$::temp_setting;
1688       PRAGMA temp_store;
1689     "
1690   } $val
1693 # The SQLITE_FCNTL_PRAGMA logic, with error handling.
1695 db close
1696 testvfs tvfs
1697 sqlite3 db test.db -vfs tvfs
1698 do_test pragma-19.1 {
1699   catchsql {PRAGMA error}
1700 } {1 {SQL logic error}}
1701 do_test pragma-19.2 {
1702   catchsql {PRAGMA error='This is the error message'}
1703 } {1 {This is the error message}}
1704 do_test pragma-19.3 {
1705   catchsql {PRAGMA error='7 This is the error message'}
1706 } {1 {This is the error message}}
1707 do_test pragma-19.4 {
1708   catchsql {PRAGMA error=7}
1709 } {1 {out of memory}}
1710 do_test pragma-19.5 {
1711   file tail [lindex [execsql {PRAGMA filename}] 0]
1712 } {test.db}
1714 if {$tcl_platform(platform)=="windows"} {
1715 # Test data_store_directory pragma
1717 db close
1718 sqlite3 db test.db
1719 file mkdir data_dir
1720 do_test pragma-20.1 {
1721   catchsql {PRAGMA data_store_directory}
1722 } {0 {}}
1723 do_test pragma-20.2 {
1724   set pwd [string map {' ''} [file nativename [get_pwd]]]
1725   catchsql "PRAGMA data_store_directory='$pwd';"
1726 } {0 {}}
1727 do_test pragma-20.3 {
1728   catchsql {PRAGMA data_store_directory}
1729 } [list 0 [list [file nativename [get_pwd]]]]
1730 do_test pragma-20.4 {
1731   set pwd [string map {' ''} [file nativename \
1732     [file join [get_pwd] data_dir]]]
1733   catchsql "PRAGMA data_store_directory='$pwd';"
1734 } {0 {}}
1735 do_test pragma-20.5 {
1736   sqlite3 db2 test2.db
1737   catchsql "PRAGMA database_list;" db2
1738 } [list 0 [list 0 main [file nativename \
1739     [file join [get_pwd] data_dir test2.db]]]]
1740 catch {db2 close}
1741 do_test pragma-20.6 {
1742   sqlite3 db2 [file join [get_pwd] test2.db]
1743   catchsql "PRAGMA database_list;" db2
1744 } [list 0 [list 0 main [file nativename \
1745     [file join [get_pwd] test2.db]]]]
1746 catch {db2 close}
1747 do_test pragma-20.7 {
1748   catchsql "PRAGMA data_store_directory='';"
1749 } {0 {}}
1750 do_test pragma-20.8 {
1751   catchsql {PRAGMA data_store_directory}
1752 } {0 {}}
1754 forcedelete data_dir
1755 } ;# endif windows
1757 database_may_be_corrupt
1758 if {![nonzero_reserved_bytes]} {
1760   do_test 21.1 {
1761     # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1762     #
1763     db close
1764     forcedelete test.db
1765     sqlite3 db test.db
1766     execsql { 
1767       PRAGMA page_size = 1024;
1768       PRAGMA auto_vacuum = 0;
1769       CREATE TABLE t1(a PRIMARY KEY, b);
1770       INSERT INTO t1 VALUES(1, 1);
1771     }
1772     for {set i 0} {$i < 10} {incr i} {
1773       execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1774     }
1775     db close
1776     forcecopy test.db testerr.db
1777     hexio_write testerr.db 15000 [string repeat 55 100]
1778   } {100}
1779   
1780   set mainerr {*** in database main ***
1781 Multiple uses for byte 672 of page 15}
1782   set auxerr {*** in database aux ***
1783 Multiple uses for byte 672 of page 15}
1784   
1785   set mainerr {/{\*\*\* in database main \*\*\*
1786 Multiple uses for byte 672 of page 15}.*/}
1787   set auxerr {/{\*\*\* in database aux \*\*\*
1788 Multiple uses for byte 672 of page 15}.*/}
1789   
1790   do_test 22.2 {
1791     catch { db close }
1792     sqlite3 db testerr.db
1793     execsql { PRAGMA integrity_check }
1794   } $mainerr
1795   
1796   do_test 22.3.1 {
1797     catch { db close }
1798     sqlite3 db test.db
1799     execsql { 
1800       ATTACH 'testerr.db' AS 'aux';
1801       PRAGMA integrity_check;
1802     }
1803   } $auxerr
1804   do_test 22.3.2 {
1805     execsql { PRAGMA main.integrity_check; }
1806   } {ok}
1807   do_test 22.3.3 {
1808     execsql { PRAGMA aux.integrity_check; }
1809   } $auxerr
1810   
1811   do_test 22.4.1 {
1812     catch { db close }
1813     sqlite3 db testerr.db
1814     execsql { 
1815       ATTACH 'test.db' AS 'aux';
1816       PRAGMA integrity_check;
1817     }
1818   } $mainerr
1819   do_test 22.4.2 {
1820     execsql { PRAGMA main.integrity_check; }
1821   } $mainerr
1822   do_test 22.4.3 {
1823     execsql { PRAGMA aux.integrity_check; }
1824   } {ok}
1826   
1827 db close
1828 forcedelete test.db test.db-wal test.db-journal
1829 sqlite3 db test.db
1830 sqlite3 db2 test.db
1831 do_test 23.1 {
1832   db eval {
1833     CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1834     CREATE INDEX i1 ON t1(b,c);
1835     CREATE INDEX i2 ON t1(c,d);
1836     CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
1837     CREATE TABLE t2(x INTEGER REFERENCES t1);
1838   }
1839   db2 eval {SELECT name FROM sqlite_master}
1840 } {t1 i1 i2 i2x t2}
1841 do_test 23.2a {
1842   db eval {
1843     DROP INDEX i2;
1844     CREATE INDEX i2 ON t1(c,d,b);
1845   }
1846   capture_pragma db2 out {PRAGMA index_info(i2)}
1847   db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1848 } {2 c | 3 d | 1 b |}
1850 # EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1851 # pragma returns information about every column in an index.
1853 # EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1854 # returns information about every column in the index, not just the key
1855 # columns.
1857 do_test 23.2b {
1858   capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1859   db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1860 } {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
1862 # (The first column of output from PRAGMA index_xinfo is...)
1863 # EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1864 # means left-most. Key columns come before auxiliary columns.)
1866 # (The second column of output from PRAGMA index_xinfo is...)
1867 # EVIDENCE-OF: R-40889-06838 The rank of the column within the table
1868 # being indexed, or -1 if the index-column is the rowid of the table
1869 # being indexed.
1871 # (The third column of output from PRAGMA index_xinfo is...)
1872 # EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
1873 # NULL if the index-column is the rowid of the table being indexed.
1875 # (The fourth column of output from PRAGMA index_xinfo is...)
1876 # EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1877 # (DESC) order by the index and 0 otherwise.
1879 # (The fifth column of output from PRAGMA index_xinfo is...)
1880 # EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1881 # compare values in the index-column.
1883 # (The sixth column of output from PRAGMA index_xinfo is...)
1884 # EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1885 # if the index-column is an auxiliary column.
1887 do_test 23.2c {
1888   db2 eval {PRAGMA index_xinfo(i2)}
1889 } {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1890 do_test 23.2d {
1891   db2 eval {PRAGMA index_xinfo(i2x)}
1892 } {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
1894 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1895 # pragma returns one row for each index associated with the given table.
1897 # (The first column of output from PRAGMA index_list is...)
1898 # EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1899 # for internal tracking purposes.
1901 # (The second column of output from PRAGMA index_list is...)
1902 # EVIDENCE-OF: R-35496-03635 The name of the index.
1904 # (The third column of output from PRAGMA index_list is...)
1905 # EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1907 # (The fourth column of output from PRAGMA index_list is...)
1908 # EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1909 # INDEX statement, "u" if the index was created by a UNIQUE constraint,
1910 # or "pk" if the index was created by a PRIMARY KEY constraint.
1912 do_test 23.3 {
1913   db eval {
1914     CREATE INDEX i3 ON t1(d,b,c);
1915   }
1916   capture_pragma db2 out {PRAGMA index_list(t1)}
1917   db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1918 } {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
1919 do_test 23.4 {
1920   db eval {
1921     ALTER TABLE t1 ADD COLUMN e;
1922   }
1923   db2 eval {
1924     PRAGMA table_info(t1);
1925   }
1926 } {/4 e {} 0 {} 0/}
1927 do_test 23.5 {
1928   db eval {
1929     DROP TABLE t2;
1930     CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1931   }
1932   db2 eval {
1933     PRAGMA foreign_key_list(t2);
1934   }
1935 } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1936 db2 close
1938 ifcapable !has_codec {
1939   reset_db
1940   do_execsql_test 24.0 {
1941     PRAGMA page_size = 1024;
1942     CREATE TABLE t1(a, b, c);
1943     CREATE INDEX i1 ON t1(b);
1944     INSERT INTO t1 VALUES('a', 'b', 'c');
1945     PRAGMA integrity_check;
1946   } {ok}
1947   
1948   set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
1949   db close
1950   hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
1951   
1952   sqlite3 db test.db
1953   do_catchsql_test 24.1 {
1954     SELECT * FROM t1;
1955   } {1 {database disk image is malformed}}
1956   do_catchsql_test 24.2 {
1957     PRAGMA integrity_check;
1958   } {0 {{database disk image is malformed}}}
1959 }  
1960 database_never_corrupt
1961 finish_test