Add new sessions API sqlite3changegroup_add_change().
[sqlite.git] / test / pragma.test
blobe823a6763024dc9e86700fdd948387cfb5c6048f
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.5.2 {
391       PRAGMA integrity_check='4'
392     } {1 {no such table: 4}}
393     do_catchsql_test pragma-3.6 {
394       PRAGMA integrity_check=xyz
395     } {1 {no such table: xyz}}
396     do_catchsql_test pragma-3.6b {
397       PRAGMA integrity_check=t2
398     } {0 {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}}
399     do_catchsql_test pragma-3.6c {
400       PRAGMA integrity_check=sqlite_schema
401     } {0 ok}
402     do_test pragma-3.7 {
403       execsql {
404         PRAGMA integrity_check=0
405       }
406     } {{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}}
407   
408     # Add additional corruption by appending unused pages to the end of
409     # the database file testerr.db
410     #
411     do_test pragma-3.8 {
412       execsql {DETACH t2}
413       forcedelete testerr.db testerr.db-journal
414       set out [open testerr.db w]
415       fconfigure $out -translation binary
416       set in [open test.db r]
417       fconfigure $in -translation binary
418       puts -nonewline $out [read $in]
419       seek $in 0
420       puts -nonewline $out [read $in]
421       close $in
422       close $out
423       hexio_write testerr.db 28 00000000
424       execsql {REINDEX t2}
425       execsql {PRAGMA integrity_check}
426     } {ok}
427     do_test pragma-3.8.1 {
428       execsql {PRAGMA quick_check}
429     } {ok}
430     do_test pragma-3.8.2 {
431       execsql {PRAGMA QUICK_CHECK}
432     } {ok}
433     do_test pragma-3.9a {
434       execsql {
435         ATTACH 'testerr.db' AS t2;
436         PRAGMA integrity_check
437       }
438     } {{*** in database t2 ***
439 Page 4: never used
440 Page 5: never used
441 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
442     do_execsql_test pragma-3.9b {
443       PRAGMA t2.integrity_check=t2;
444     } {{wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
445     do_execsql_test pragma-3.9c {
446       PRAGMA t2.integrity_check=sqlite_schema;
447     } {ok}
448     do_test pragma-3.10 {
449       execsql {
450         PRAGMA integrity_check=1
451       }
452     } {{*** in database t2 ***
453 Page 4: never used}}
454     do_test pragma-3.11 {
455       execsql {
456         PRAGMA integrity_check=5
457       }
458     } {{*** in database t2 ***
459 Page 4: never used
460 Page 5: never used
461 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2}}
462     do_test pragma-3.12 {
463       execsql {
464         PRAGMA integrity_check=4
465       }
466     } {{*** in database t2 ***
467 Page 4: never used
468 Page 5: never used
469 Page 6: never used} {wrong # of entries in index i2}}
470     do_test pragma-3.13 {
471       execsql {
472         PRAGMA integrity_check=3
473       }
474     } {{*** in database t2 ***
475 Page 4: never used
476 Page 5: never used
477 Page 6: never used}}
478     do_test pragma-3.14 {
479       execsql {
480         PRAGMA integrity_check(2)
481       }
482     } {{*** in database t2 ***
483 Page 4: never used
484 Page 5: never used}}
485     do_test pragma-3.15 {
486       execsql {
487         ATTACH 'testerr.db' AS t3;
488         PRAGMA integrity_check
489       }
490     } {{*** in database t2 ***
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} {*** in database t3 ***
494 Page 4: never used
495 Page 5: never used
496 Page 6: never used} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2}}
497     do_test pragma-3.16 {
498       execsql {
499         PRAGMA integrity_check(10)
500       }
501     } {{*** in database t2 ***
502 Page 4: never used
503 Page 5: never used
504 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 ***
505 Page 4: never used
506 Page 5: never used
507 Page 6: never used} {wrong # of entries in index i2}}
508     do_test pragma-3.17 {
509       execsql {
510         PRAGMA integrity_check=8
511       }
512     } {{*** in database t2 ***
513 Page 4: never used
514 Page 5: never used
515 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 ***
516 Page 4: never used
517 Page 5: never used}}
518     do_test pragma-3.18 {
519       execsql {
520         PRAGMA integrity_check=4
521       }
522     } {{*** in database t2 ***
523 Page 4: never used
524 Page 5: never used
525 Page 6: never used} {wrong # of entries in index i2}}
526   }
527   do_test pragma-3.19 {
528     catch {db close}
529     forcedelete test.db test.db-journal
530     sqlite3 db test.db
531     db eval {PRAGMA integrity_check}
532   } {ok}
535 # Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
536 # constraint violations.
538 ifcapable altertable {
539   sqlite3_db_config db DEFENSIVE 0
540     do_execsql_test pragma-3.20 {
541       CREATE TABLE t1(a,b);
542       CREATE INDEX t1a ON t1(a);
543       INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
544       PRAGMA writable_schema=ON;
545       UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
546         WHERE name='t1a';
547       UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
548         WHERE name='t1';
549       PRAGMA writable_schema=OFF;
550       ALTER TABLE t1 RENAME TO t1x;
551       PRAGMA integrity_check;
552     } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
553   do_execsql_test pragma-3.21 {
554     PRAGMA integrity_check(3);
555   } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
556   do_execsql_test pragma-3.22 {
557     PRAGMA integrity_check(2);
558   } {{non-unique entry in index t1a} {NULL value in t1x.a}}
559   do_execsql_test pragma-3.23 {
560     PRAGMA integrity_check(1);
561   } {{non-unique entry in index t1a}}
563   # forum post https://sqlite.org/forum/forumpost/ee4f6fa5ab
564   do_execsql_test pragma-3.24 {
565     DROP TABLE IF EXISTS t1;
566     CREATE TABLE t1(a);
567     INSERT INTO t1 VALUES (1);
568     ALTER TABLE t1 ADD COLUMN b NOT NULL DEFAULT 0.25;
569     SELECT * FROM t1;
570     PRAGMA integrity_check(t1);
571   } {1 0.25 ok}
572   do_execsql_test pragma-3.25 {
573     ALTER TABLE t1 ADD COLUMN c CHECK (1);
574     SELECT * FROM t1;
575     PRAGMA integrity_check(t1);
576   } {1 0.25 {} ok}
579 # PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
580 # interface) used to leave index cursors in an inconsistent state
581 # which could result in an assertion fault in sqlite3BtreeKey()
582 # called from saveCursorPosition() if content is removed from the
583 # index while the integrity_check is still running.  This test verifies
584 # that problem has been fixed.
586 do_test pragma-3.30 {
587   catch { db close }
588   delete_file test.db
589   sqlite3 db test.db
590   db eval {
591     CREATE TABLE t1(a,b,c);
592     WITH RECURSIVE
593       c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
594     INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
595     CREATE INDEX t1a ON t1(a);
596     CREATE INDEX t1bc ON t1(b,c);
597   }
598   db eval {PRAGMA integrity_check} {
599      db eval {DELETE FROM t1}
600   }
601 } {}
603 # The values stored in indexes must be byte-for-byte identical to the
604 # values stored in tables.
606 reset_db
607 do_execsql_test pragma-3.40 {
608   CREATE TABLE t1(
609     a INTEGER PRIMARY KEY,
610     b TEXT COLLATE nocase,
611     c INT COLLATE nocase,
612     d TEXT
613   );
614   INSERT INTO t1(a,b,c,d) VALUES
615     (1, 'one','one','one'),
616     (2, 'two','two','two'),
617     (3, 'three','three','three'),
618     (4, 'four','four','four'),
619     (5, 'five','five','five');
620   CREATE INDEX t1bcd ON t1(b,c,d);
621   CREATE TABLE t2(
622     a INTEGER PRIMARY KEY,
623     b TEXT COLLATE nocase,
624     c INT COLLATE nocase,
625     d TEXT
626   );
627   INSERT INTO t2(a,b,c,d) VALUES
628     (1, 'one','one','one'),
629     (2, 'two','two','TWO'),
630     (3, 'three','THREE','three'),
631     (4, 'FOUR','four','four'),
632     (5, 'FIVE','FIVE','five');
633   CREATE INDEX t2bcd ON t2(b,c,d);
634   CREATE TEMP TABLE saved_schema AS SELECT name, rootpage FROM sqlite_schema;
635   PRAGMA writable_schema=ON;
636   UPDATE sqlite_schema
637      SET rootpage=(SELECT rootpage FROM saved_schema WHERE name='t2bcd')
638    WHERE name='t1bcd';
639   UPDATE sqlite_schema
640      SET rootpage=(SELECT rootpage FROM saved_schema WHERE name='t1bcd')
641    WHERE name='t2bcd';
642   PRAGMA Writable_schema=RESET;
644 ifcapable vtab {
645   do_execsql_test pragma-3.41 {
646     SELECT integrity_check AS x FROM pragma_integrity_check ORDER BY 1;
647   } {
648     {row 2 missing from index t1bcd}
649     {row 2 missing from index t2bcd}
650     {row 3 values differ from index t1bcd}
651     {row 3 values differ from index t2bcd}
652     {row 4 values differ from index t1bcd}
653     {row 4 values differ from index t2bcd}
654     {row 5 values differ from index t1bcd}
655     {row 5 values differ from index t2bcd}
656   }
658 db eval {DROP TABLE t2}
660 # Test modifying the cache_size of an attached database.
661 ifcapable pager_pragmas&&attach {
662 do_test pragma-4.1 {
663   execsql {
664     ATTACH 'test2.db' AS aux;
665     pragma aux.cache_size;
666     pragma aux.default_cache_size;
667   } 
668 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
669 do_test pragma-4.2 {
670   execsql {
671     pragma aux.cache_size = 50;
672     pragma aux.cache_size;
673     pragma aux.default_cache_size;
674   } 
675 } [list 50 $DFLT_CACHE_SZ]
676 do_test pragma-4.3 {
677   execsql {
678     pragma aux.default_cache_size = 456;
679     pragma aux.cache_size;
680     pragma aux.default_cache_size;
681   } 
682 } {456 456}
683 do_test pragma-4.4 {
684   execsql {
685     pragma cache_size;
686     pragma default_cache_size;
687   } 
688 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
689 do_test pragma-4.5 {
690   execsql {
691     DETACH aux;
692     ATTACH 'test3.db' AS aux;
693     pragma aux.cache_size;
694     pragma aux.default_cache_size;
695   } 
696 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
697 do_test pragma-4.6 {
698   execsql {
699     DETACH aux;
700     ATTACH 'test2.db' AS aux;
701     pragma aux.cache_size;
702     pragma aux.default_cache_size;
703   } 
704 } {456 456}
705 } ;# ifcapable pager_pragmas
707 # Test that modifying the sync-level in the middle of a transaction is
708 # disallowed.
709 ifcapable pager_pragmas {
710 do_test pragma-5.0 {
711   execsql {
712     pragma synchronous;
713   } 
714 } {2}
715 do_test pragma-5.1 {
716   catchsql {
717     BEGIN;
718     pragma synchronous = OFF;
719   } 
720 } {1 {Safety level may not be changed inside a transaction}}
721 do_test pragma-5.2 {
722   execsql {
723     pragma synchronous;
724   } 
725 } {2}
726 catchsql {COMMIT;}
727 } ;# ifcapable pager_pragmas
729 # Test schema-query pragmas
731 ifcapable schema_pragmas {
732 ifcapable tempdb&&attach {
733   do_test pragma-6.1 {
734     set res {}
735     execsql {SELECT * FROM sqlite_temp_master}
736     foreach {idx name file} [execsql {pragma database_list}] {
737       lappend res $idx $name
738     }
739     set res
740   } {0 main 1 temp 2 aux}
742 do_test pragma-6.2 {
743   execsql {
744     CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
745     pragma table_info(t2)
746   }
747 } {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
748 do_test pragma-6.2.1 {
749   execsql {
750     pragma table_info;
751   }
752 } {}
753 db nullvalue <<NULL>>
754 do_test pragma-6.2.2 {
755   execsql {
756     CREATE TABLE t5(
757       a TEXT DEFAULT CURRENT_TIMESTAMP, 
758       b DEFAULT (5+3),
759       c TEXT,
760       d INTEGER DEFAULT NULL,
761       e TEXT DEFAULT '',
762       UNIQUE(b,c,d),
763       PRIMARY KEY(e,b,c)
764     );
765     PRAGMA table_info(t5);
766   }
767 } {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}
768 db nullvalue {}
769 do_test pragma-6.2.3 {
770   execsql {
771     CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
772     pragma table_info(t2_3)
773   }
774 } {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
775 ifcapable {foreignkey} {
776   do_test pragma-6.3.1 {
777     execsql {
778       CREATE TABLE t3(a int references t2(b), b UNIQUE);
779       pragma foreign_key_list(t3);
780     }
781   } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
782   do_test pragma-6.3.2 {
783     execsql {
784       pragma foreign_key_list;
785     }
786   } {}
787   do_test pragma-6.3.3 {
788     execsql {
789       pragma foreign_key_list(t3_bogus);
790     }
791   } {}
792   do_test pragma-6.3.4 {
793     execsql {
794       pragma foreign_key_list(t5);
795     }
796   } {}
797   do_test pragma-6.4 {
798     capture_pragma db out {
799       pragma index_list(t3);
800     }
801     db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
802   } {0 sqlite_autoindex_t3_1 1}
804 ifcapable {!foreignkey} {
805   execsql {CREATE TABLE t3(a,b UNIQUE)}
807 do_test pragma-6.5.1 {
808   execsql {
809     CREATE INDEX t3i1 ON t3(a,b);
810   }
811   capture_pragma db out {
812     pragma index_info(t3i1);
813   }
814   db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
815 } {0 0 a 1 1 b}
817 # EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
818 # by the index_info pragma, but they are listed by the index_xinfo
819 # pragma.
821 do_test pragma-6.5.1b {
822   capture_pragma db out {PRAGMA index_xinfo(t3i1)}
823   db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
824 } {0 0 a 1 1 b 2 -1 {}}
827 # EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
828 # pragma returns one row for each key column in the named index.
830 # (The first column of output from PRAGMA index_info is...)
831 # EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
832 # means left-most.)
834 # (The second column of output from PRAGMA index_info is...)
835 # EVIDENCE-OF: R-65019-08383 The rank of the column within the table
836 # being indexed.
838 # (The third column of output from PRAGMA index_info is...)
839 # EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
841 do_execsql_test pragma-6.5.1c {
842   CREATE INDEX t3i2 ON t3(b,a);
843   PRAGMA index_info='t3i2';
844   DROP INDEX t3i2;
845 } {0 1 b 1 0 a}
847 do_test pragma-6.5.2 {
848   execsql {
849     pragma index_info(t3i1_bogus);
850   }
851 } {}
853 ifcapable tempdb {
854   # Test for ticket #3320. When a temp table of the same name exists, make
855   # sure the schema of the main table can still be queried using 
856   # "pragma table_info":
857   do_test pragma-6.6.1 {
858     execsql {
859       CREATE TABLE trial(col_main);
860       CREATE TEMP TABLE trial(col_temp);
861     }
862   } {}
863   do_test pragma-6.6.2 {
864     execsql {
865       PRAGMA table_info(trial);
866     }
867   } {0 col_temp {} 0 {} 0}
868   do_test pragma-6.6.3 {
869     execsql {
870       PRAGMA temp.table_info(trial);
871     }
872   } {0 col_temp {} 0 {} 0}
873   do_test pragma-6.6.4 {
874     execsql {
875       PRAGMA main.table_info(trial);
876     }
877   } {0 col_main {} 0 {} 0}
880 do_test pragma-6.7 {
881   execsql {
882     CREATE TABLE test_table(
883       one INT NOT NULL DEFAULT -1, 
884       two text,
885       three VARCHAR(45, 65) DEFAULT 'abcde',
886       four REAL DEFAULT X'abcdef',
887       five DEFAULT CURRENT_TIME
888     );
889   }
890   capture_pragma db out {PRAGMA table_info(test_table)}
891   db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
892             ORDER BY cid}
893 } [concat \
894   {0 one INT 1 -1 0} \
895   {1 two TEXT 0 {} 0} \
896   {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
897   {3 four REAL 0 X'abcdef' 0} \
898   {4 five {} 0 CURRENT_TIME 0} \
900 do_test pragma-6.8 {
901   execsql {
902     CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
903     PRAGMA table_info(t68);
904   }
905 } [concat \
906   {0 a {} 0 {} 1} \
907   {1 b {} 0 {} 2} \
908   {2 c {} 0 {} 4} \
910 } ;# ifcapable schema_pragmas
911 # Miscellaneous tests
913 ifcapable schema_pragmas {
914 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
915 # pragma returns one row for each index associated with the given table.
917 do_test pragma-7.1.1 {
918   # Make sure a pragma knows to read the schema if it needs to
919   db close
920   sqlite3 db test.db
921   capture_pragma db out "PRAGMA index_list(t3)"
922   db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
923 } {t3i1 c sqlite_autoindex_t3_1 u}
924 do_test pragma-7.1.2 {
925   execsql {
926     pragma index_list(t3_bogus);
927   }
928 } {}
929 } ;# ifcapable schema_pragmas
930 ifcapable {utf16} {
931   if {[permutation] == ""} {
932     do_test pragma-7.2 {
933       db close
934       sqlite3 db test.db
935       catchsql {
936         pragma encoding=bogus;
937       }
938     } {1 {unsupported encoding: bogus}}
939   }
941 ifcapable tempdb {
942   do_test pragma-7.3 {
943     db close
944     sqlite3 db test.db
945     execsql {
946       pragma lock_status;
947     }
948   } {main unlocked temp closed}
949 } else {
950   do_test pragma-7.3 {
951     db close
952     sqlite3 db test.db
953     execsql {
954       pragma lock_status;
955     }
956   } {main unlocked}
960 #----------------------------------------------------------------------
961 # Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
962 # user_version" statements.
964 # pragma-8.1: PRAGMA schema_version
965 # pragma-8.2: PRAGMA user_version
968 ifcapable schema_version {
970 # First check that we can set the schema version and then retrieve the
971 # same value.
972 do_test pragma-8.1.1 {
973   execsql {
974     PRAGMA schema_version = 105;
975   }
976 } {}
977 do_test pragma-8.1.2 {
978   execsql2 {
979     PRAGMA schema_version;
980   }
981 } {schema_version 105}
982 sqlite3_db_config db DEFENSIVE 1
983 do_execsql_test pragma-8.1.3 {
984   PRAGMA schema_version = 106;
985   PRAGMA schema_version;
986 } 105
987 sqlite3_db_config db DEFENSIVE 0
988 do_execsql_test pragma-8.1.4 {
989   PRAGMA schema_version = 106;
990   PRAGMA schema_version;
991 } 106
993 # Check that creating a table modifies the schema-version (this is really
994 # to verify that the value being read is in fact the schema version).
995 do_test pragma-8.1.5 {
996   execsql {
997     CREATE TABLE t4(a, b, c);
998     INSERT INTO t4 VALUES(1, 2, 3);
999     SELECT * FROM t4;
1000   }
1001 } {1 2 3}
1002 do_test pragma-8.1.6 {
1003   execsql {
1004     PRAGMA schema_version;
1005   }
1006 } 107
1008 # Now open a second connection to the database. Ensure that changing the
1009 # schema-version using the first connection forces the second connection
1010 # to reload the schema. This has to be done using the C-API test functions,
1011 # because the TCL API accounts for SCHEMA_ERROR and retries the query.
1012 do_test pragma-8.1.7 {
1013   sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
1014   execsql {
1015     SELECT * FROM t4;
1016   } db2
1017 } {1 2 3}
1018 do_test pragma-8.1.8 {
1019   execsql {
1020     PRAGMA schema_version = 108;
1021   }
1022 } {}
1023 do_test pragma-8.1.9 {
1024   set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
1025   sqlite3_step $::STMT
1026 } SQLITE_ERROR
1027 do_test pragma-8.1.10 {
1028   sqlite3_finalize $::STMT
1029 } SQLITE_SCHEMA
1031 # Make sure the schema-version can be manipulated in an attached database.
1032 forcedelete test2.db
1033 forcedelete test2.db-journal
1034 ifcapable attach {
1035   do_test pragma-8.1.11 {
1036     execsql {
1037       ATTACH 'test2.db' AS aux;
1038       CREATE TABLE aux.t1(a, b, c);
1039       PRAGMA aux.schema_version = 205;
1040     }
1041   } {}
1042   do_test pragma-8.1.12 {
1043     execsql {
1044       PRAGMA aux.schema_version;
1045     }
1046   } 205
1048 do_test pragma-8.1.13 {
1049   execsql {
1050     PRAGMA schema_version;
1051   }
1052 } 108
1054 # And check that modifying the schema-version in an attached database
1055 # forces the second connection to reload the schema.
1056 ifcapable attach {
1057   do_test pragma-8.1.14 {
1058     sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
1059     execsql {
1060       ATTACH 'test2.db' AS aux;
1061       SELECT * FROM aux.t1;
1062     } db2
1063   } {}
1064   do_test pragma-8.1.15 {
1065     execsql {
1066       PRAGMA aux.schema_version = 206;
1067     }
1068   } {}
1069   do_test pragma-8.1.16 {
1070     set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
1071     sqlite3_step $::STMT
1072   } SQLITE_ERROR
1073   do_test pragma-8.1.17 {
1074     sqlite3_finalize $::STMT
1075   } SQLITE_SCHEMA
1076   do_test pragma-8.1.18 {
1077     db2 close
1078   } {}
1081 # Now test that the user-version can be read and written (and that we aren't
1082 # accidentally manipulating the schema-version instead).
1083 do_test pragma-8.2.1 {
1084   execsql2 {
1085     PRAGMA user_version;
1086   }
1087 } {user_version 0}
1088 do_test pragma-8.2.2 {
1089   execsql {
1090     PRAGMA user_version = 2;
1091   }
1092 } {}
1093 do_test pragma-8.2.3.1 {
1094   execsql2 {
1095     PRAGMA user_version;
1096   }
1097 } {user_version 2}
1098 do_test pragma-8.2.3.2 {
1099   db close
1100   sqlite3 db test.db
1101   execsql {
1102     PRAGMA user_version;
1103   }
1104 } {2}
1105 do_test pragma-8.2.4.1 {
1106   execsql {
1107     PRAGMA schema_version;
1108   }
1109 } {108}
1110 ifcapable vacuum {
1111   do_test pragma-8.2.4.2 {
1112     execsql {
1113       VACUUM;
1114       PRAGMA user_version;
1115     }
1116   } {2}
1117   do_test pragma-8.2.4.3 {
1118     execsql {
1119       PRAGMA schema_version;
1120     }
1121   } {109}
1124 ifcapable attach {
1125   db eval {ATTACH 'test2.db' AS aux}
1126   
1127   # Check that the user-version in the auxilary database can be manipulated (
1128   # and that we aren't accidentally manipulating the same in the main db).
1129   do_test pragma-8.2.5 {
1130     execsql {
1131       PRAGMA aux.user_version;
1132     }
1133   } {0}
1134   do_test pragma-8.2.6 {
1135     execsql {
1136       PRAGMA aux.user_version = 3;
1137     }
1138   } {}
1139   do_test pragma-8.2.7 {
1140     execsql {
1141       PRAGMA aux.user_version;
1142     }
1143   } {3}
1144   do_test pragma-8.2.8 {
1145     execsql {
1146       PRAGMA main.user_version;
1147     }
1148   } {2}
1149   
1150   # Now check that a ROLLBACK resets the user-version if it has been modified
1151   # within a transaction.
1152   do_test pragma-8.2.9 {
1153     execsql {
1154       BEGIN;
1155       PRAGMA aux.user_version = 10;
1156       PRAGMA user_version = 11;
1157     }
1158   } {}
1159   do_test pragma-8.2.10 {
1160     execsql {
1161       PRAGMA aux.user_version;
1162     }
1163   } {10}
1164   do_test pragma-8.2.11 {
1165     execsql {
1166       PRAGMA main.user_version;
1167     }
1168   } {11}
1169   do_test pragma-8.2.12 {
1170     execsql {
1171       ROLLBACK;
1172       PRAGMA aux.user_version;
1173     }
1174   } {3}
1175   do_test pragma-8.2.13 {
1176     execsql {
1177       PRAGMA main.user_version;
1178     }
1179   } {2}
1182 # Try a negative value for the user-version
1183 do_test pragma-8.2.14 {
1184   execsql {
1185     PRAGMA user_version = -450;
1186   }
1187 } {}
1188 do_test pragma-8.2.15 {
1189   execsql {
1190     PRAGMA user_version;
1191   }
1192 } {-450}
1193 } ; # ifcapable schema_version
1195 # Check to see if TEMP_STORE is memory or disk.  Return strings
1196 # "memory" or "disk" as appropriate.
1198 proc check_temp_store {} {
1199   db eval {
1200     PRAGMA temp.cache_size = 1;
1201     CREATE TEMP TABLE IF NOT EXISTS a(b);
1202     DELETE FROM a;
1203     INSERT INTO a VALUES(randomblob(1000));
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     INSERT INTO a SELECT * FROM a;
1210     INSERT INTO a SELECT * FROM a;
1211     INSERT INTO a SELECT * FROM a;
1212   }
1213   db eval {PRAGMA database_list} {
1214     if {$name=="temp"} {
1215       set bt [btree_from_db db 1]
1216       if {[btree_ismemdb $bt]} {
1217         return "memory"
1218       }
1219       return "disk"
1220     }
1221   }
1222   return "unknown"
1225 # Application_ID
1227 do_test pragma-8.3.1 {
1228   execsql {
1229     PRAGMA application_id;
1230   }
1231 } {0}
1232 do_test pragma-8.3.2 {
1233   execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1234 } {12345}
1236 # Test temp_store and temp_store_directory pragmas
1238 ifcapable pager_pragmas {
1239 do_test pragma-9.1 {
1240   db close
1241   sqlite3 db test.db
1242   execsql {
1243     PRAGMA temp_store;
1244   }
1245 } {0}
1246 if {$TEMP_STORE<=1} {
1247   do_test pragma-9.1.1 {
1248     check_temp_store
1249   } {disk}
1250 } else {
1251   do_test pragma-9.1.1 {
1252     check_temp_store
1253   } {memory}
1256 do_test pragma-9.2 {
1257   db close
1258   sqlite3 db test.db
1259   execsql {
1260     PRAGMA temp_store=file;
1261     PRAGMA temp_store;
1262   }
1263 } {1}
1264 if {$TEMP_STORE==3} {
1265   # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1266   do_test pragma-9.2.1 {
1267     check_temp_store
1268   } {memory}
1269 } else {
1270   do_test pragma-9.2.1 {
1271     check_temp_store
1272   } {disk}
1275 do_test pragma-9.3 {
1276   db close
1277   sqlite3 db test.db
1278   execsql {
1279     PRAGMA temp_store=memory;
1280     PRAGMA temp_store;
1281   }
1282 } {2}
1283 if {$TEMP_STORE==0} {
1284   # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1285   do_test pragma-9.3.1 {
1286     check_temp_store
1287   } {disk}
1288 } else {
1289   do_test pragma-9.3.1 {
1290     check_temp_store
1291   } {memory}
1294 do_test pragma-9.4 {
1295   execsql {
1296     PRAGMA temp_store_directory;
1297   }
1298 } {}
1299 ifcapable wsd {
1300   do_test pragma-9.5 {
1301     set pwd [string map {' ''} [file nativename [get_pwd]]]
1302     execsql "
1303       PRAGMA temp_store_directory='$pwd';
1304     "
1305   } {}
1306   do_test pragma-9.6 {
1307     execsql { 
1308       PRAGMA temp_store_directory;
1309     }
1310   } [list [file nativename [get_pwd]]]
1311   do_test pragma-9.7 {
1312     catchsql { 
1313       PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1314     }
1315   } {1 {not a writable directory}}
1316   do_test pragma-9.8 {
1317     execsql { 
1318       PRAGMA temp_store_directory='';
1319     }
1320   } {}
1321   if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1322     ifcapable tempdb {
1323       do_test pragma-9.9 {
1324         execsql { 
1325           PRAGMA temp_store_directory;
1326           PRAGMA temp_store=FILE;
1327           CREATE TEMP TABLE temp_store_directory_test(a integer);
1328           INSERT INTO temp_store_directory_test values (2);
1329           SELECT * FROM temp_store_directory_test;
1330         }
1331       } {2}
1332       do_test pragma-9.10 {
1333         catchsql "
1334           PRAGMA temp_store_directory='$pwd';
1335           SELECT * FROM temp_store_directory_test;
1336         "
1337       } {1 {no such table: temp_store_directory_test}}
1338     }
1339   }
1341 do_test pragma-9.11 {
1342   execsql {
1343     PRAGMA temp_store = 0;
1344     PRAGMA temp_store;
1345   }
1346 } {0}
1347 do_test pragma-9.12 {
1348   execsql {
1349     PRAGMA temp_store = 1;
1350     PRAGMA temp_store;
1351   }
1352 } {1}
1353 do_test pragma-9.13 {
1354   execsql {
1355     PRAGMA temp_store = 2;
1356     PRAGMA temp_store;
1357   }
1358 } {2}
1359 do_test pragma-9.14 {
1360   execsql {
1361     PRAGMA temp_store = 3;
1362     PRAGMA temp_store;
1363   }
1364 } {0}
1365 do_test pragma-9.15 {
1366   catchsql {
1367     BEGIN EXCLUSIVE;
1368     CREATE TEMP TABLE temp_table(t);
1369     INSERT INTO temp_table VALUES('valuable data');
1370     PRAGMA temp_store = 1;
1371   }
1372 } {1 {temporary storage cannot be changed from within a transaction}}
1373 do_test pragma-9.16 {
1374   execsql {
1375     SELECT * FROM temp_table;
1376     COMMIT;
1377   }
1378 } {{valuable data}}
1380 do_test pragma-9.17 {
1381   execsql {
1382     INSERT INTO temp_table VALUES('valuable data II');
1383     SELECT * FROM temp_table;
1384   }
1385 } {{valuable data} {valuable data II}}
1387 do_test pragma-9.18 {
1388   set rc [catch {
1389     db eval {SELECT t FROM temp_table} {
1390       execsql {pragma temp_store = 1}
1391     }
1392   } msg]
1393   list $rc $msg
1394 } {1 {temporary storage cannot be changed from within a transaction}}
1396 } ;# ifcapable pager_pragmas
1398 ifcapable trigger {
1400 do_test pragma-10.0 {
1401   catchsql {
1402     DROP TABLE main.t1;
1403   }
1404   execsql {
1405     PRAGMA count_changes = 1;
1407     CREATE TABLE t1(a PRIMARY KEY);
1408     CREATE TABLE t1_mirror(a);
1409     CREATE TABLE t1_mirror2(a);
1410     CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN 
1411       INSERT INTO t1_mirror VALUES(new.a);
1412     END;
1413     CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN 
1414       INSERT INTO t1_mirror2 VALUES(new.a);
1415     END;
1416     CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN 
1417       UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1418     END;
1419     CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN 
1420       UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1421     END;
1422     CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN 
1423       DELETE FROM t1_mirror WHERE a = old.a;
1424     END;
1425     CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN 
1426       DELETE FROM t1_mirror2 WHERE a = old.a;
1427     END;
1428   }
1429 } {}
1431 do_test pragma-10.1 {
1432   execsql {
1433     INSERT INTO t1 VALUES(randstr(10,10));
1434   }
1435 } {1}
1436 do_test pragma-10.2 {
1437   execsql {
1438     UPDATE t1 SET a = randstr(10,10);
1439   }
1440 } {1}
1441 do_test pragma-10.3 {
1442   execsql {
1443     DELETE FROM t1;
1444   }
1445 } {1}
1447 } ;# ifcapable trigger
1449 ifcapable schema_pragmas {
1450   do_test pragma-11.1 {
1451     execsql2 {
1452       pragma collation_list;
1453     }
1454   } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
1455   do_test pragma-11.2 {
1456     db collate New_Collation blah...
1457     execsql {
1458       pragma collation_list;
1459     }
1460   } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
1463 ifcapable schema_pragmas&&tempdb {
1464   do_test pragma-12.1 {
1465     sqlite3 db2 test.db
1466     execsql {
1467       PRAGMA temp.table_info('abc');
1468     } db2
1469   } {}
1470   db2 close
1472   do_test pragma-12.2 {
1473     sqlite3 db2 test.db
1474     execsql {
1475       PRAGMA temp.default_cache_size = 200;
1476       PRAGMA temp.default_cache_size;
1477     } db2
1478   } {200}
1479   db2 close
1481   do_test pragma-12.3 {
1482     sqlite3 db2 test.db
1483     execsql {
1484       PRAGMA temp.cache_size = 400;
1485       PRAGMA temp.cache_size;
1486     } db2
1487   } {400}
1488   db2 close
1491 ifcapable bloblit {
1493 do_test pragma-13.1 {
1494   execsql {
1495     DROP TABLE IF EXISTS t4;
1496     PRAGMA vdbe_trace=on;
1497     PRAGMA vdbe_listing=on;
1498     PRAGMA sql_trace=on;
1499     CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1500     INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1501     INSERT INTO t4(b) VALUES(randstr(30,30));
1502     INSERT INTO t4(b) VALUES(1.23456);
1503     INSERT INTO t4(b) VALUES(NULL);
1504     INSERT INTO t4(b) VALUES(0);
1505     INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1506     SELECT * FROM t4;
1507   }
1508   execsql {
1509     PRAGMA vdbe_trace=off;
1510     PRAGMA vdbe_listing=off;
1511     PRAGMA sql_trace=off;
1512   }
1513 } {}
1515 } ;# ifcapable bloblit 
1517 ifcapable pager_pragmas {
1518   db close
1519   forcedelete test.db
1520   sqlite3 db test.db
1522   # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1523   # number of pages in the database file.
1524   #
1525   do_test pragma-14.1 {
1526     execsql { pragma auto_vacuum = 0 }
1527     execsql { pragma page_count; pragma main.page_count }
1528   } {0 0}
1530   do_test pragma-14.2 {
1531     execsql { 
1532       CREATE TABLE abc(a, b, c);
1533       PRAGMA page_count;
1534       PRAGMA main.page_count;
1535       PRAGMA temp.page_count;
1536     }
1537   } {2 2 0}
1538   do_test pragma-14.2uc {
1539     execsql {pragma PAGE_COUNT}
1540   } {2}
1542   do_test pragma-14.3 {
1543     execsql { 
1544       BEGIN;
1545       CREATE TABLE def(a, b, c);
1546       PRAGMA page_count;
1547     }
1548   } {3}
1549   do_test pragma-14.3uc {
1550     execsql {pragma PAGE_COUNT}
1551   } {3}
1553   do_test pragma-14.4 {
1554     set page_size [db one {pragma page_size}]
1555     expr [file size test.db] / $page_size
1556   } {2}
1558   do_test pragma-14.5 {
1559     execsql {
1560       ROLLBACK;
1561       PRAGMA page_count;
1562     }
1563   } {2}
1565   do_test pragma-14.6 {
1566     forcedelete test2.db
1567     sqlite3 db2 test2.db
1568     execsql {
1569       PRAGMA auto_vacuum = 0;
1570       CREATE TABLE t1(a, b, c);
1571       CREATE TABLE t2(a, b, c);
1572       CREATE TABLE t3(a, b, c);
1573       CREATE TABLE t4(a, b, c);
1574     } db2
1575     db2 close
1576     execsql {
1577       ATTACH 'test2.db' AS aux;
1578       PRAGMA aux.page_count;
1579     } 
1580   } {5}
1581   do_test pragma-14.6uc {
1582     execsql {pragma AUX.PAGE_COUNT}
1583   } {5}
1586 # Test that the value set using the cache_size pragma is not reset when the
1587 # schema is reloaded.
1589 ifcapable pager_pragmas {
1590   db close
1591   sqlite3 db test.db
1592   do_test pragma-15.1 {
1593     execsql {
1594       PRAGMA cache_size=59;
1595       PRAGMA cache_size;
1596     }
1597   } {59}
1598   do_test pragma-15.2 {
1599     sqlite3 db2 test.db
1600     execsql {
1601       CREATE TABLE newtable(a, b, c);
1602     } db2
1603     db2 close
1604   } {}
1605   do_test pragma-15.3 {
1606     # Evaluating this statement will cause the schema to be reloaded (because
1607     # the schema was changed by another connection in pragma-15.2). At one
1608     # point there was a bug that reset the cache_size to its default value
1609     # when this happened. 
1610     execsql { SELECT * FROM sqlite_master }
1611     execsql { PRAGMA cache_size }
1612   } {59}
1615 # Reset the sqlite3_temp_directory variable for the next run of tests:
1616 sqlite3 dbX :memory:
1617 dbX eval {PRAGMA temp_store_directory = ""}
1618 dbX close
1620 ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1621   set sqlite_hostid_num 1
1623   set using_proxy 0
1624   foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1625     set using_proxy $value
1626   }
1628   # Test the lock_proxy_file pragmas.
1629   #
1630   db close
1631   set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1633   sqlite3 db test.db
1634   do_test pragma-16.1 {
1635     execsql {
1636       PRAGMA lock_proxy_file="mylittleproxy";
1637       select * from sqlite_master;
1638     }
1639     execsql {
1640       PRAGMA lock_proxy_file;
1641     } 
1642   } {mylittleproxy}
1644   do_test pragma-16.2 {
1645     sqlite3 db2 test.db
1646     execsql {
1647       PRAGMA lock_proxy_file="mylittleproxy";
1648     } db2
1649   } {}
1651   db2 close
1652   do_test pragma-16.2.1 {
1653     sqlite3 db2 test.db
1654     execsql {
1655       PRAGMA lock_proxy_file=":auto:";
1656       select * from sqlite_master;
1657     } db2
1658     execsql {
1659       PRAGMA lock_proxy_file;
1660     } db2
1661   } {mylittleproxy}
1663   db2 close
1664   do_test pragma-16.3 {
1665     sqlite3 db2 test.db
1666     execsql {
1667       PRAGMA lock_proxy_file="myotherproxy";
1668     } db2
1669     catchsql {
1670       select * from sqlite_master;
1671     } db2
1672   } {1 {database is locked}}
1674   do_test pragma-16.4 {
1675     db2 close
1676     db close
1677     sqlite3 db2 test.db
1678     execsql {
1679       PRAGMA lock_proxy_file="myoriginalproxy";
1680       PRAGMA lock_proxy_file="myotherproxy";
1681       PRAGMA lock_proxy_file;
1682     } db2
1683   } {myotherproxy}
1685   db2 close
1686   set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1687   do_test pragma-16.5 {
1688     sqlite3 db2 test.db
1689     execsql {
1690       PRAGMA lock_proxy_file=":auto:";
1691       PRAGMA lock_proxy_file;
1692     } db2
1693   } {myotherproxy}
1694   
1695   do_test pragma-16.6 {
1696     db2 close
1697     sqlite3 db2 test2.db
1698     set lockpath [execsql {
1699       PRAGMA lock_proxy_file=":auto:";
1700       PRAGMA lock_proxy_file;
1701     } db2]
1702     string match "*test2.db:auto:" $lockpath
1703   } {1}
1704   
1705   set sqlite_hostid_num 2
1706   do_test pragma-16.7 {
1707     list [catch {
1708       sqlite3 db test2.db
1709       execsql { 
1710         PRAGMA lock_proxy_file=":auto:";
1711         select * from sqlite_master;
1712       }
1713     } msg] $msg
1714   } {1 {database is locked}}
1715   db close
1716   
1717   do_test pragma-16.8 {
1718     list [catch {
1719       sqlite3 db test2.db
1720       execsql { select * from sqlite_master } 
1721     } msg] $msg
1722   } {1 {database is locked}}
1724   db2 close
1725   do_test pragma-16.8.1 {
1726     execsql {
1727       PRAGMA lock_proxy_file="yetanotherproxy";
1728       PRAGMA lock_proxy_file;
1729     } 
1730   } {yetanotherproxy}
1731   do_test pragma-16.8.2 {
1732     execsql {
1733       create table mine(x);
1734     } 
1735   } {}
1737   db close
1738   do_test pragma-16.9 {
1739     sqlite3 db proxytest.db
1740     set lockpath2 [execsql {
1741       PRAGMA lock_proxy_file=":auto:";
1742       PRAGMA lock_proxy_file;
1743     } db]
1744     string match "*proxytest.db:auto:" $lockpath2
1745   } {1}
1747   set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1748   set sqlite_hostid_num 0
1751 # Parsing of auto_vacuum settings.
1753 foreach {autovac_setting val} {
1754   0 0
1755   1 1
1756   2 2
1757   3 0
1758   -1 0
1759   none 0
1760   NONE 0
1761   NoNe 0
1762   full 1
1763   FULL 1
1764   incremental 2
1765   INCREMENTAL 2
1766   -1234 0
1767   1234 0
1768 } {
1769   do_test pragma-17.1.$autovac_setting {
1770     catch {db close}
1771     sqlite3 db :memory:
1772     execsql "
1773       PRAGMA auto_vacuum=$::autovac_setting;
1774       PRAGMA auto_vacuum;
1775     "
1776   } $val
1779 # Parsing of temp_store settings.
1781 foreach {temp_setting val} {
1782   0 0
1783   1 1
1784   2 2
1785   3 0
1786   -1 0
1787   file 1
1788   FILE 1
1789   fIlE 1
1790   memory 2
1791   MEMORY 2
1792   MeMoRy 2
1793 } {
1794   do_test pragma-18.1.$temp_setting {
1795     catch {db close}
1796     sqlite3 db :memory:
1797     execsql "
1798       PRAGMA temp_store=$::temp_setting;
1799       PRAGMA temp_store=$::temp_setting;
1800       PRAGMA temp_store;
1801     "
1802   } $val
1805 # The SQLITE_FCNTL_PRAGMA logic, with error handling.
1807 db close
1808 testvfs tvfs
1809 sqlite3 db test.db -vfs tvfs
1810 do_test pragma-19.1 {
1811   catchsql {PRAGMA error}
1812 } {1 {SQL logic error}}
1813 do_test pragma-19.2 {
1814   catchsql {PRAGMA error='This is the error message'}
1815 } {1 {This is the error message}}
1816 do_test pragma-19.3 {
1817   catchsql {PRAGMA error='7 This is the error message'}
1818 } {1 {This is the error message}}
1819 do_test pragma-19.4 {
1820   catchsql {PRAGMA error=7}
1821 } {1 {out of memory}}
1822 do_test pragma-19.5 {
1823   file tail [lindex [execsql {PRAGMA filename}] 0]
1824 } {test.db}
1826 if {$tcl_platform(platform)=="windows"} {
1827 # Test data_store_directory pragma
1829 db close
1830 sqlite3 db test.db
1831 file mkdir data_dir
1832 do_test pragma-20.1 {
1833   catchsql {PRAGMA data_store_directory}
1834 } {0 {}}
1835 do_test pragma-20.2 {
1836   set pwd [string map {' ''} [file nativename [get_pwd]]]
1837   catchsql "PRAGMA data_store_directory='$pwd';"
1838 } {0 {}}
1839 do_test pragma-20.3 {
1840   catchsql {PRAGMA data_store_directory}
1841 } [list 0 [list [file nativename [get_pwd]]]]
1842 do_test pragma-20.4 {
1843   set pwd [string map {' ''} [file nativename \
1844     [file join [get_pwd] data_dir]]]
1845   catchsql "PRAGMA data_store_directory='$pwd';"
1846 } {0 {}}
1847 do_test pragma-20.5 {
1848   sqlite3 db2 test2.db
1849   catchsql "PRAGMA database_list;" db2
1850 } [list 0 [list 0 main [file nativename \
1851     [file join [get_pwd] data_dir test2.db]]]]
1852 catch {db2 close}
1853 do_test pragma-20.6 {
1854   sqlite3 db2 [file join [get_pwd] test2.db]
1855   catchsql "PRAGMA database_list;" db2
1856 } [list 0 [list 0 main [file nativename \
1857     [file join [get_pwd] test2.db]]]]
1858 catch {db2 close}
1859 do_test pragma-20.7 {
1860   catchsql "PRAGMA data_store_directory='';"
1861 } {0 {}}
1862 do_test pragma-20.8 {
1863   catchsql {PRAGMA data_store_directory}
1864 } {0 {}}
1866 forcedelete data_dir
1867 } ;# endif windows
1869 database_may_be_corrupt
1870 if {![nonzero_reserved_bytes]} {
1872   do_test 21.1 {
1873     # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1874     #
1875     db close
1876     forcedelete test.db
1877     sqlite3 db test.db
1878     execsql { 
1879       PRAGMA page_size = 1024;
1880       PRAGMA auto_vacuum = 0;
1881       CREATE TABLE t1(a PRIMARY KEY, b);
1882       INSERT INTO t1 VALUES(1, 1);
1883     }
1884     for {set i 0} {$i < 10} {incr i} {
1885       execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1886     }
1887     db close
1888     forcecopy test.db testerr.db
1889     hexio_write testerr.db 15000 [string repeat 55 100]
1890   } {100}
1891   
1892   set mainerr {*** in database main ***
1893 Multiple uses for byte 672 of page 15}
1894   set auxerr {*** in database aux ***
1895 Multiple uses for byte 672 of page 15}
1896   
1897   set mainerr {/{\*\*\* in database main \*\*\*
1898 Multiple uses for byte 672 of page 15}.*/}
1899   set auxerr {/{\*\*\* in database aux \*\*\*
1900 Multiple uses for byte 672 of page 15}.*/}
1901   
1902   do_test 22.2 {
1903     catch { db close }
1904     sqlite3 db testerr.db
1905     execsql { PRAGMA integrity_check }
1906   } $mainerr
1907   
1908   do_test 22.3.1 {
1909     catch { db close }
1910     sqlite3 db test.db
1911     execsql { 
1912       ATTACH 'testerr.db' AS 'aux';
1913       PRAGMA integrity_check;
1914     }
1915   } $auxerr
1916   do_test 22.3.2 {
1917     execsql { PRAGMA main.integrity_check; }
1918   } {ok}
1919   do_test 22.3.3 {
1920     execsql { PRAGMA aux.integrity_check; }
1921   } $auxerr
1922   
1923   do_test 22.4.1 {
1924     catch { db close }
1925     sqlite3 db testerr.db
1926     execsql { 
1927       ATTACH 'test.db' AS 'aux';
1928       PRAGMA integrity_check;
1929     }
1930   } $mainerr
1931   do_test 22.4.2 {
1932     execsql { PRAGMA main.integrity_check; }
1933   } $mainerr
1934   do_test 22.4.3 {
1935     execsql { PRAGMA aux.integrity_check; }
1936   } {ok}
1938   
1939 db close
1940 forcedelete test.db test.db-wal test.db-journal
1941 sqlite3 db test.db
1942 sqlite3 db2 test.db
1943 do_test 23.1 {
1944   db eval {
1945     CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1946     CREATE INDEX i1 ON t1(b,c);
1947     CREATE INDEX i2 ON t1(c,d);
1948     CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
1949     CREATE INDEX i3 ON t1(d,b+c,c);
1950     CREATE TABLE t2(x INTEGER REFERENCES t1);
1951   }
1952   db2 eval {SELECT name FROM sqlite_master}
1953 } {t1 i1 i2 i2x i3 t2}
1954 do_test 23.2a {
1955   db eval {
1956     DROP INDEX i2;
1957     CREATE INDEX i2 ON t1(c,d,b);
1958   }
1959   capture_pragma db2 out {PRAGMA index_info(i2)}
1960   db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1961 } {2 c | 3 d | 1 b |}
1963 # EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1964 # pragma returns information about every column in an index.
1966 # EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1967 # returns information about every column in the index, not just the key
1968 # columns.
1970 do_test 23.2b {
1971   capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1972   db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1973 } {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
1975 # (The first column of output from PRAGMA index_xinfo is...)
1976 # EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1977 # means left-most. Key columns come before auxiliary columns.)
1979 # (The second column of output from PRAGMA index_xinfo is...)
1980 # EVIDENCE-OF: R-06603-49335 The rank of the column within the table
1981 # being indexed, or -1 if the index-column is the rowid of the table
1982 # being indexed and -2 if the index is on an expression.
1984 # (The third column of output from PRAGMA index_xinfo is...)
1985 # EVIDENCE-OF: R-40641-22898 The name of the column being indexed, or
1986 # NULL if the index-column is the rowid of the table being indexed or an
1987 # expression.
1989 # (The fourth column of output from PRAGMA index_xinfo is...)
1990 # EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1991 # (DESC) order by the index and 0 otherwise.
1993 # (The fifth column of output from PRAGMA index_xinfo is...)
1994 # EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1995 # compare values in the index-column.
1997 # (The sixth column of output from PRAGMA index_xinfo is...)
1998 # EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1999 # if the index-column is an auxiliary column.
2001 do_test 23.2c {
2002   db2 eval {PRAGMA index_xinfo(i2)}
2003 } {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
2004 do_test 23.2d {
2005   db2 eval {PRAGMA index_xinfo(i2x)}
2006 } {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
2007 do_test 23.2e {
2008   db2 eval {PRAGMA index_xinfo(i3)}
2009 } {0 3 d 0 BINARY 1 1 -2 {} 0 BINARY 1 2 2 c 0 BINARY 1 3 -1 {} 0 BINARY 0}
2011 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
2012 # pragma returns one row for each index associated with the given table.
2014 # (The first column of output from PRAGMA index_list is...)
2015 # EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
2016 # for internal tracking purposes.
2018 # (The second column of output from PRAGMA index_list is...)
2019 # EVIDENCE-OF: R-35496-03635 The name of the index.
2021 # (The third column of output from PRAGMA index_list is...)
2022 # EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
2024 # (The fourth column of output from PRAGMA index_list is...)
2025 # EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
2026 # INDEX statement, "u" if the index was created by a UNIQUE constraint,
2027 # or "pk" if the index was created by a PRIMARY KEY constraint.
2029 do_test 23.3 {
2030   db eval {
2031     DROP INDEX IF EXISTS i3;
2032     CREATE INDEX i3 ON t1(d,b,c);
2033   }
2034   capture_pragma db2 out {PRAGMA index_list(t1)}
2035   db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
2036 } {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
2037 ifcapable altertable {
2038   do_test 23.4 {
2039     db eval {
2040       ALTER TABLE t1 ADD COLUMN e;
2041     }
2042     db2 eval {
2043       PRAGMA table_info(t1);
2044     }
2045   } {/4 e {} 0 {} 0/}
2047 do_test 23.5 {
2048   db eval {
2049     DROP TABLE t2;
2050     CREATE TABLE t2(x, y INTEGER REFERENCES t1);
2051   }
2052   db2 eval {
2053     PRAGMA foreign_key_list(t2);
2054   }
2055 } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
2056 db2 close
2058 ifcapable !has_codec {
2059   reset_db
2060   do_execsql_test 24.0 {
2061     PRAGMA page_size = 1024;
2062     CREATE TABLE t1(a, b, c);
2063     CREATE INDEX i1 ON t1(b);
2064     INSERT INTO t1 VALUES('a', 'b', 'c');
2065     PRAGMA integrity_check;
2066   } {ok}
2067   
2068   set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
2069   db close
2070   hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
2071   
2072   sqlite3 db test.db
2073   do_catchsql_test 24.1 {
2074     SELECT * FROM t1;
2075   } {1 {database disk image is malformed}}
2076   do_catchsql_test 24.2 {
2077     PRAGMA integrity_check;
2078   } {0 {{database disk image is malformed}}}
2079 }  
2080 database_never_corrupt
2082 # 2023-03-27.  Register allocation issue in integrity_check discovered
2083 # by new assert() statements added in [6f8b97f31a4c8552].
2084 # dbsqlfuzz dc9ab26037cf5ef797d28cd1ae0855ade584216d
2085 # tag-20230327-1
2087 reset_db
2088 do_execsql_test 25.0 {
2089   CREATE TABLE t1(a INT, b AS (a*2) NOT NULL);
2090   CREATE TEMP TABLE t2(a PRIMARY KEY, b, c UNIQUE) WITHOUT ROWID;
2091   CREATE UNIQUE INDEX t2x ON t2(c,b);
2092   PRAGMA integrity_check;
2093 } ok
2094 finish_test