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