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
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).
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
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.
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"
62 set ins "INSERT INTO $tabname VALUES"
63 set crtab "CREATE TEMP TABLE $tabname "
66 append ins ${sep}\$x($col)
67 append crtab ${sep}\"$col\"
78 # Delete the preexisting database to avoid the special setup
79 # that the "all.test" script does.
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}]
97 PRAGMA default_cache_size;
100 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
102 # EVIDENCE-OF: R-42059-47211 If the argument N is positive then the
103 # suggested cache size is set to N.
105 PRAGMA synchronous=OFF;
106 PRAGMA cache_size=1234;
108 PRAGMA default_cache_size;
111 } [list 1234 $DFLT_CACHE_SZ 0]
117 PRAGMA default_cache_size;
120 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
123 PRAGMA synchronous=OFF;
125 PRAGMA default_cache_size;
128 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
131 PRAGMA cache_size=-4321;
133 PRAGMA default_cache_size;
136 } [list -4321 $DFLT_CACHE_SZ 0]
139 PRAGMA synchronous=ON;
141 PRAGMA default_cache_size;
144 } [list -4321 $DFLT_CACHE_SZ 1]
150 PRAGMA default_cache_size;
153 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
156 PRAGMA default_cache_size=-123;
158 PRAGMA default_cache_size;
162 do_test pragma-1.9.1 {
164 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
167 PRAGMA default_cache_size;
172 do_test pragma-1.9.2 {
176 PRAGMA default_cache_size;
181 do_test pragma-1.10 {
183 PRAGMA synchronous=NORMAL;
185 PRAGMA default_cache_size;
189 do_test pragma-1.11.1 {
191 PRAGMA synchronous=EXTRA;
193 PRAGMA default_cache_size;
197 do_test pragma-1.11.2 {
199 PRAGMA synchronous=FULL;
201 PRAGMA default_cache_size;
205 do_test pragma-1.12 {
207 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
210 PRAGMA default_cache_size;
215 # Make sure the pragma handler understands numeric values in addition
216 # to keywords like "off" and "full".
218 do_test pragma-1.13 {
220 PRAGMA synchronous=0;
224 do_test pragma-1.14 {
226 PRAGMA synchronous=2;
230 do_test pragma-1.14.1 {
232 PRAGMA synchronous=4;
236 do_test pragma-1.14.2 {
238 PRAGMA synchronous=3;
242 do_test pragma-1.14.3 {
244 PRAGMA synchronous=8;
248 do_test pragma-1.14.4 {
250 PRAGMA synchronous=10;
254 } ;# ifcapable pager_pragmas
256 # Test turning "flag" pragmas on and off.
259 # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
261 do_test pragma-1.15 {
263 PRAGMA vdbe_listing=YES;
267 do_test pragma-1.16 {
269 PRAGMA vdbe_listing=NO;
275 do_test pragma-1.17 {
277 PRAGMA parser_trace=ON;
278 PRAGMA parser_trace=OFF;
281 do_test pragma-1.18 {
283 PRAGMA bogus = -1234; -- Parsing of negative values
287 # Test modifying the safety_level of an attached database.
288 ifcapable pager_pragmas&&attach {
291 forcedelete test2.db-journal
293 ATTACH 'test2.db' AS aux;
298 pragma aux.synchronous;
303 pragma aux.synchronous = OFF;
304 pragma aux.synchronous;
310 pragma aux.synchronous = ON;
312 pragma aux.synchronous;
315 } ;# ifcapable pager_pragmas
317 # Construct a corrupted index and make sure the integrity_check
320 # These tests won't work if the database is encrypted
324 forcedelete test.db test.db-journal
327 PRAGMA auto_vacuum=OFF;
329 CREATE TABLE t2(a,b,c);
330 CREATE INDEX i2 ON t2(a);
331 INSERT INTO t2 VALUES(11,2,3);
332 INSERT INTO t2 VALUES(22,3,4);
334 SELECT rowid, * from t2;
336 } {1 11 2 3 2 22 3 4}
338 if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
340 db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
341 set pgsz [db eval {PRAGMA page_size}]
342 # overwrite the header on the rootpage of the index in order to
343 # make the index appear to be empty.
345 set offset [expr {$pgsz*($rootpage-1)}]
346 hexio_write test.db $offset 0a00000000040000000000
349 execsql {PRAGMA integrity_check}
350 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
352 execsql {PRAGMA integrity_check=1}
353 } {{row 1 missing from index i2}}
356 ATTACH DATABASE 'test.db' AS t2;
357 PRAGMA integrity_check
359 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
362 PRAGMA integrity_check=4
364 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
367 PRAGMA integrity_check=xyz
369 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
372 PRAGMA integrity_check=0
374 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
376 # Add additional corruption by appending unused pages to the end of
377 # the database file testerr.db
381 forcedelete testerr.db testerr.db-journal
382 set out [open testerr.db w]
383 fconfigure $out -translation binary
384 set in [open test.db r]
385 fconfigure $in -translation binary
386 puts -nonewline $out [read $in]
388 puts -nonewline $out [read $in]
391 hexio_write testerr.db 28 00000000
393 execsql {PRAGMA integrity_check}
395 do_test pragma-3.8.1 {
396 execsql {PRAGMA quick_check}
398 do_test pragma-3.8.2 {
399 execsql {PRAGMA QUICK_CHECK}
403 ATTACH 'testerr.db' AS t2;
404 PRAGMA integrity_check
406 } {{*** in database t2 ***
409 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
410 do_test pragma-3.10 {
412 PRAGMA integrity_check=1
414 } {{*** in database t2 ***
415 Page 4 is never used}}
416 do_test pragma-3.11 {
418 PRAGMA integrity_check=5
420 } {{*** in database t2 ***
423 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
424 do_test pragma-3.12 {
426 PRAGMA integrity_check=4
428 } {{*** in database t2 ***
431 Page 6 is never used} {row 1 missing from index i2}}
432 do_test pragma-3.13 {
434 PRAGMA integrity_check=3
436 } {{*** in database t2 ***
439 Page 6 is never used}}
440 do_test pragma-3.14 {
442 PRAGMA integrity_check(2)
444 } {{*** in database t2 ***
446 Page 5 is never used}}
447 do_test pragma-3.15 {
449 ATTACH 'testerr.db' AS t3;
450 PRAGMA integrity_check
452 } {{*** in database t2 ***
455 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
458 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
459 do_test pragma-3.16 {
461 PRAGMA integrity_check(10)
463 } {{*** in database t2 ***
466 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
469 Page 6 is never used} {row 1 missing from index i2}}
470 do_test pragma-3.17 {
472 PRAGMA integrity_check=8
474 } {{*** in database t2 ***
477 Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
479 Page 5 is never used}}
480 do_test pragma-3.18 {
482 PRAGMA integrity_check=4
484 } {{*** in database t2 ***
487 Page 6 is never used} {row 1 missing from index i2}}
489 do_test pragma-3.19 {
491 forcedelete test.db test.db-journal
493 db eval {PRAGMA integrity_check}
497 # Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
498 # constraint violations.
500 sqlite3_db_config db DEFENSIVE 0
501 do_execsql_test pragma-3.20 {
502 CREATE TABLE t1(a,b);
503 CREATE INDEX t1a ON t1(a);
504 INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
505 PRAGMA writable_schema=ON;
506 UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
508 UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
510 PRAGMA writable_schema=OFF;
511 ALTER TABLE t1 RENAME TO t1x;
512 PRAGMA integrity_check;
513 } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
514 do_execsql_test pragma-3.21 {
515 PRAGMA integrity_check(3);
516 } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
517 do_execsql_test pragma-3.22 {
518 PRAGMA integrity_check(2);
519 } {{non-unique entry in index t1a} {NULL value in t1x.a}}
520 do_execsql_test pragma-3.23 {
521 PRAGMA integrity_check(1);
522 } {{non-unique entry in index t1a}}
524 # PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
525 # interface) used to leave index cursors in an inconsistent state
526 # which could result in an assertion fault in sqlite3BtreeKey()
527 # called from saveCursorPosition() if content is removed from the
528 # index while the integrity_check is still running. This test verifies
529 # that problem has been fixed.
531 do_test pragma-3.30 {
536 CREATE TABLE t1(a,b,c);
538 c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
539 INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
540 CREATE INDEX t1a ON t1(a);
541 CREATE INDEX t1bc ON t1(b,c);
543 db eval {PRAGMA integrity_check} {
544 db eval {DELETE FROM t1}
548 # Test modifying the cache_size of an attached database.
549 ifcapable pager_pragmas&&attach {
552 ATTACH 'test2.db' AS aux;
553 pragma aux.cache_size;
554 pragma aux.default_cache_size;
556 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
559 pragma aux.cache_size = 50;
560 pragma aux.cache_size;
561 pragma aux.default_cache_size;
563 } [list 50 $DFLT_CACHE_SZ]
566 pragma aux.default_cache_size = 456;
567 pragma aux.cache_size;
568 pragma aux.default_cache_size;
574 pragma default_cache_size;
576 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
580 ATTACH 'test3.db' AS aux;
581 pragma aux.cache_size;
582 pragma aux.default_cache_size;
584 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
588 ATTACH 'test2.db' AS aux;
589 pragma aux.cache_size;
590 pragma aux.default_cache_size;
593 } ;# ifcapable pager_pragmas
595 # Test that modifying the sync-level in the middle of a transaction is
597 ifcapable pager_pragmas {
606 pragma synchronous = OFF;
608 } {1 {Safety level may not be changed inside a transaction}}
615 } ;# ifcapable pager_pragmas
617 # Test schema-query pragmas
619 ifcapable schema_pragmas {
620 ifcapable tempdb&&attach {
623 execsql {SELECT * FROM sqlite_temp_master}
624 foreach {idx name file} [execsql {pragma database_list}] {
625 lappend res $idx $name
628 } {0 main 1 temp 2 aux}
632 CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
633 pragma table_info(t2)
635 } {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
636 do_test pragma-6.2.1 {
641 db nullvalue <<NULL>>
642 do_test pragma-6.2.2 {
645 a TEXT DEFAULT CURRENT_TIMESTAMP,
648 d INTEGER DEFAULT NULL,
653 PRAGMA table_info(t5);
655 } {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 2 2 c TEXT 0 <<NULL>> 3 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 1}
657 do_test pragma-6.2.3 {
659 CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
660 pragma table_info(t2_3)
662 } {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
663 ifcapable {foreignkey} {
664 do_test pragma-6.3.1 {
666 CREATE TABLE t3(a int references t2(b), b UNIQUE);
667 pragma foreign_key_list(t3);
669 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
670 do_test pragma-6.3.2 {
672 pragma foreign_key_list;
675 do_test pragma-6.3.3 {
677 pragma foreign_key_list(t3_bogus);
680 do_test pragma-6.3.4 {
682 pragma foreign_key_list(t5);
686 capture_pragma db out {
687 pragma index_list(t3);
689 db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
690 } {0 sqlite_autoindex_t3_1 1}
692 ifcapable {!foreignkey} {
693 execsql {CREATE TABLE t3(a,b UNIQUE)}
695 do_test pragma-6.5.1 {
697 CREATE INDEX t3i1 ON t3(a,b);
699 capture_pragma db out {
700 pragma index_info(t3i1);
702 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
705 # EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
706 # by the index_info pragma, but they are listed by the index_xinfo
709 do_test pragma-6.5.1b {
710 capture_pragma db out {PRAGMA index_xinfo(t3i1)}
711 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
712 } {0 0 a 1 1 b 2 -1 {}}
715 # EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
716 # pragma returns one row for each key column in the named index.
718 # (The first column of output from PRAGMA index_info is...)
719 # EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
722 # (The second column of output from PRAGMA index_info is...)
723 # EVIDENCE-OF: R-65019-08383 The rank of the column within the table
726 # (The third column of output from PRAGMA index_info is...)
727 # EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
729 do_execsql_test pragma-6.5.1c {
730 CREATE INDEX t3i2 ON t3(b,a);
731 PRAGMA index_info='t3i2';
735 do_test pragma-6.5.2 {
737 pragma index_info(t3i1_bogus);
742 # Test for ticket #3320. When a temp table of the same name exists, make
743 # sure the schema of the main table can still be queried using
744 # "pragma table_info":
745 do_test pragma-6.6.1 {
747 CREATE TABLE trial(col_main);
748 CREATE TEMP TABLE trial(col_temp);
751 do_test pragma-6.6.2 {
753 PRAGMA table_info(trial);
755 } {0 col_temp {} 0 {} 0}
756 do_test pragma-6.6.3 {
758 PRAGMA temp.table_info(trial);
760 } {0 col_temp {} 0 {} 0}
761 do_test pragma-6.6.4 {
763 PRAGMA main.table_info(trial);
765 } {0 col_main {} 0 {} 0}
770 CREATE TABLE test_table(
771 one INT NOT NULL DEFAULT -1,
773 three VARCHAR(45, 65) DEFAULT 'abcde',
774 four REAL DEFAULT X'abcdef',
775 five DEFAULT CURRENT_TIME
778 capture_pragma db out {PRAGMA table_info(test_table)}
779 db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
783 {1 two text 0 {} 0} \
784 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
785 {3 four REAL 0 X'abcdef' 0} \
786 {4 five {} 0 CURRENT_TIME 0} \
790 CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
791 PRAGMA table_info(t68);
798 } ;# ifcapable schema_pragmas
799 # Miscellaneous tests
801 ifcapable schema_pragmas {
802 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
803 # pragma returns one row for each index associated with the given table.
805 do_test pragma-7.1.1 {
806 # Make sure a pragma knows to read the schema if it needs to
809 capture_pragma db out "PRAGMA index_list(t3)"
810 db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
811 } {t3i1 c sqlite_autoindex_t3_1 u}
812 do_test pragma-7.1.2 {
814 pragma index_list(t3_bogus);
817 } ;# ifcapable schema_pragmas
819 if {[permutation] == ""} {
824 pragma encoding=bogus;
826 } {1 {unsupported encoding: bogus}}
836 } {main unlocked temp closed}
848 #----------------------------------------------------------------------
849 # Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
850 # user_version" statements.
852 # pragma-8.1: PRAGMA schema_version
853 # pragma-8.2: PRAGMA user_version
856 ifcapable schema_version {
858 # First check that we can set the schema version and then retrieve the
860 do_test pragma-8.1.1 {
862 PRAGMA schema_version = 105;
865 do_test pragma-8.1.2 {
867 PRAGMA schema_version;
869 } {schema_version 105}
870 do_test pragma-8.1.3 {
872 PRAGMA schema_version = 106;
875 do_test pragma-8.1.4 {
877 PRAGMA schema_version;
881 # Check that creating a table modifies the schema-version (this is really
882 # to verify that the value being read is in fact the schema version).
883 do_test pragma-8.1.5 {
885 CREATE TABLE t4(a, b, c);
886 INSERT INTO t4 VALUES(1, 2, 3);
890 do_test pragma-8.1.6 {
892 PRAGMA schema_version;
896 # Now open a second connection to the database. Ensure that changing the
897 # schema-version using the first connection forces the second connection
898 # to reload the schema. This has to be done using the C-API test functions,
899 # because the TCL API accounts for SCHEMA_ERROR and retries the query.
900 do_test pragma-8.1.7 {
901 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
906 do_test pragma-8.1.8 {
908 PRAGMA schema_version = 108;
911 do_test pragma-8.1.9 {
912 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
915 do_test pragma-8.1.10 {
916 sqlite3_finalize $::STMT
919 # Make sure the schema-version can be manipulated in an attached database.
921 forcedelete test2.db-journal
923 do_test pragma-8.1.11 {
925 ATTACH 'test2.db' AS aux;
926 CREATE TABLE aux.t1(a, b, c);
927 PRAGMA aux.schema_version = 205;
930 do_test pragma-8.1.12 {
932 PRAGMA aux.schema_version;
936 do_test pragma-8.1.13 {
938 PRAGMA schema_version;
942 # And check that modifying the schema-version in an attached database
943 # forces the second connection to reload the schema.
945 do_test pragma-8.1.14 {
946 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
948 ATTACH 'test2.db' AS aux;
949 SELECT * FROM aux.t1;
952 do_test pragma-8.1.15 {
954 PRAGMA aux.schema_version = 206;
957 do_test pragma-8.1.16 {
958 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
961 do_test pragma-8.1.17 {
962 sqlite3_finalize $::STMT
964 do_test pragma-8.1.18 {
969 # Now test that the user-version can be read and written (and that we aren't
970 # accidentally manipulating the schema-version instead).
971 do_test pragma-8.2.1 {
976 do_test pragma-8.2.2 {
978 PRAGMA user_version = 2;
981 do_test pragma-8.2.3.1 {
986 do_test pragma-8.2.3.2 {
993 do_test pragma-8.2.4.1 {
995 PRAGMA schema_version;
999 do_test pragma-8.2.4.2 {
1002 PRAGMA user_version;
1005 do_test pragma-8.2.4.3 {
1007 PRAGMA schema_version;
1013 db eval {ATTACH 'test2.db' AS aux}
1015 # Check that the user-version in the auxilary database can be manipulated (
1016 # and that we aren't accidentally manipulating the same in the main db).
1017 do_test pragma-8.2.5 {
1019 PRAGMA aux.user_version;
1022 do_test pragma-8.2.6 {
1024 PRAGMA aux.user_version = 3;
1027 do_test pragma-8.2.7 {
1029 PRAGMA aux.user_version;
1032 do_test pragma-8.2.8 {
1034 PRAGMA main.user_version;
1038 # Now check that a ROLLBACK resets the user-version if it has been modified
1039 # within a transaction.
1040 do_test pragma-8.2.9 {
1043 PRAGMA aux.user_version = 10;
1044 PRAGMA user_version = 11;
1047 do_test pragma-8.2.10 {
1049 PRAGMA aux.user_version;
1052 do_test pragma-8.2.11 {
1054 PRAGMA main.user_version;
1057 do_test pragma-8.2.12 {
1060 PRAGMA aux.user_version;
1063 do_test pragma-8.2.13 {
1065 PRAGMA main.user_version;
1070 # Try a negative value for the user-version
1071 do_test pragma-8.2.14 {
1073 PRAGMA user_version = -450;
1076 do_test pragma-8.2.15 {
1078 PRAGMA user_version;
1081 } ; # ifcapable schema_version
1083 # Check to see if TEMP_STORE is memory or disk. Return strings
1084 # "memory" or "disk" as appropriate.
1086 proc check_temp_store {} {
1088 PRAGMA temp.cache_size = 1;
1089 CREATE TEMP TABLE IF NOT EXISTS a(b);
1091 INSERT INTO a VALUES(randomblob(1000));
1092 INSERT INTO a SELECT * FROM a;
1093 INSERT INTO a SELECT * FROM a;
1094 INSERT INTO a SELECT * FROM a;
1095 INSERT INTO a SELECT * FROM a;
1096 INSERT INTO a SELECT * FROM a;
1097 INSERT INTO a SELECT * FROM a;
1098 INSERT INTO a SELECT * FROM a;
1099 INSERT INTO a SELECT * FROM a;
1101 db eval {PRAGMA database_list} {
1102 if {$name=="temp"} {
1103 set bt [btree_from_db db 1]
1104 if {[btree_ismemdb $bt]} {
1115 do_test pragma-8.3.1 {
1117 PRAGMA application_id;
1120 do_test pragma-8.3.2 {
1121 execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1124 # Test temp_store and temp_store_directory pragmas
1126 ifcapable pager_pragmas {
1127 do_test pragma-9.1 {
1134 if {$TEMP_STORE<=1} {
1135 do_test pragma-9.1.1 {
1139 do_test pragma-9.1.1 {
1144 do_test pragma-9.2 {
1148 PRAGMA temp_store=file;
1152 if {$TEMP_STORE==3} {
1153 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1154 do_test pragma-9.2.1 {
1158 do_test pragma-9.2.1 {
1163 do_test pragma-9.3 {
1167 PRAGMA temp_store=memory;
1171 if {$TEMP_STORE==0} {
1172 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1173 do_test pragma-9.3.1 {
1177 do_test pragma-9.3.1 {
1182 do_test pragma-9.4 {
1184 PRAGMA temp_store_directory;
1188 do_test pragma-9.5 {
1189 set pwd [string map {' ''} [file nativename [get_pwd]]]
1191 PRAGMA temp_store_directory='$pwd';
1194 do_test pragma-9.6 {
1196 PRAGMA temp_store_directory;
1198 } [list [file nativename [get_pwd]]]
1199 do_test pragma-9.7 {
1201 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1203 } {1 {not a writable directory}}
1204 do_test pragma-9.8 {
1206 PRAGMA temp_store_directory='';
1209 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1211 do_test pragma-9.9 {
1213 PRAGMA temp_store_directory;
1214 PRAGMA temp_store=FILE;
1215 CREATE TEMP TABLE temp_store_directory_test(a integer);
1216 INSERT INTO temp_store_directory_test values (2);
1217 SELECT * FROM temp_store_directory_test;
1220 do_test pragma-9.10 {
1222 PRAGMA temp_store_directory='$pwd';
1223 SELECT * FROM temp_store_directory_test;
1225 } {1 {no such table: temp_store_directory_test}}
1229 do_test pragma-9.11 {
1231 PRAGMA temp_store = 0;
1235 do_test pragma-9.12 {
1237 PRAGMA temp_store = 1;
1241 do_test pragma-9.13 {
1243 PRAGMA temp_store = 2;
1247 do_test pragma-9.14 {
1249 PRAGMA temp_store = 3;
1253 do_test pragma-9.15 {
1256 CREATE TEMP TABLE temp_table(t);
1257 INSERT INTO temp_table VALUES('valuable data');
1258 PRAGMA temp_store = 1;
1260 } {1 {temporary storage cannot be changed from within a transaction}}
1261 do_test pragma-9.16 {
1263 SELECT * FROM temp_table;
1268 do_test pragma-9.17 {
1270 INSERT INTO temp_table VALUES('valuable data II');
1271 SELECT * FROM temp_table;
1273 } {{valuable data} {valuable data II}}
1275 do_test pragma-9.18 {
1277 db eval {SELECT t FROM temp_table} {
1278 execsql {pragma temp_store = 1}
1282 } {1 {temporary storage cannot be changed from within a transaction}}
1284 } ;# ifcapable pager_pragmas
1288 do_test pragma-10.0 {
1293 PRAGMA count_changes = 1;
1295 CREATE TABLE t1(a PRIMARY KEY);
1296 CREATE TABLE t1_mirror(a);
1297 CREATE TABLE t1_mirror2(a);
1298 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1299 INSERT INTO t1_mirror VALUES(new.a);
1301 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1302 INSERT INTO t1_mirror2 VALUES(new.a);
1304 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1305 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1307 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1308 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1310 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1311 DELETE FROM t1_mirror WHERE a = old.a;
1313 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1314 DELETE FROM t1_mirror2 WHERE a = old.a;
1319 do_test pragma-10.1 {
1321 INSERT INTO t1 VALUES(randstr(10,10));
1324 do_test pragma-10.2 {
1326 UPDATE t1 SET a = randstr(10,10);
1329 do_test pragma-10.3 {
1335 } ;# ifcapable trigger
1337 ifcapable schema_pragmas {
1338 do_test pragma-11.1 {
1340 pragma collation_list;
1342 } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
1343 do_test pragma-11.2 {
1344 db collate New_Collation blah...
1346 pragma collation_list;
1348 } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
1351 ifcapable schema_pragmas&&tempdb {
1352 do_test pragma-12.1 {
1355 PRAGMA temp.table_info('abc');
1360 do_test pragma-12.2 {
1363 PRAGMA temp.default_cache_size = 200;
1364 PRAGMA temp.default_cache_size;
1369 do_test pragma-12.3 {
1372 PRAGMA temp.cache_size = 400;
1373 PRAGMA temp.cache_size;
1381 do_test pragma-13.1 {
1383 DROP TABLE IF EXISTS t4;
1384 PRAGMA vdbe_trace=on;
1385 PRAGMA vdbe_listing=on;
1386 PRAGMA sql_trace=on;
1387 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1388 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1389 INSERT INTO t4(b) VALUES(randstr(30,30));
1390 INSERT INTO t4(b) VALUES(1.23456);
1391 INSERT INTO t4(b) VALUES(NULL);
1392 INSERT INTO t4(b) VALUES(0);
1393 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1397 PRAGMA vdbe_trace=off;
1398 PRAGMA vdbe_listing=off;
1399 PRAGMA sql_trace=off;
1403 } ;# ifcapable bloblit
1405 ifcapable pager_pragmas {
1410 # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1411 # number of pages in the database file.
1413 do_test pragma-14.1 {
1414 execsql { pragma auto_vacuum = 0 }
1415 execsql { pragma page_count; pragma main.page_count }
1418 do_test pragma-14.2 {
1420 CREATE TABLE abc(a, b, c);
1422 PRAGMA main.page_count;
1423 PRAGMA temp.page_count;
1426 do_test pragma-14.2uc {
1427 execsql {pragma PAGE_COUNT}
1430 do_test pragma-14.3 {
1433 CREATE TABLE def(a, b, c);
1437 do_test pragma-14.3uc {
1438 execsql {pragma PAGE_COUNT}
1441 do_test pragma-14.4 {
1442 set page_size [db one {pragma page_size}]
1443 expr [file size test.db] / $page_size
1446 do_test pragma-14.5 {
1453 do_test pragma-14.6 {
1454 forcedelete test2.db
1455 sqlite3 db2 test2.db
1457 PRAGMA auto_vacuum = 0;
1458 CREATE TABLE t1(a, b, c);
1459 CREATE TABLE t2(a, b, c);
1460 CREATE TABLE t3(a, b, c);
1461 CREATE TABLE t4(a, b, c);
1465 ATTACH 'test2.db' AS aux;
1466 PRAGMA aux.page_count;
1469 do_test pragma-14.6uc {
1470 execsql {pragma AUX.PAGE_COUNT}
1474 # Test that the value set using the cache_size pragma is not reset when the
1475 # schema is reloaded.
1477 ifcapable pager_pragmas {
1480 do_test pragma-15.1 {
1482 PRAGMA cache_size=59;
1486 do_test pragma-15.2 {
1489 CREATE TABLE newtable(a, b, c);
1493 do_test pragma-15.3 {
1494 # Evaluating this statement will cause the schema to be reloaded (because
1495 # the schema was changed by another connection in pragma-15.2). At one
1496 # point there was a bug that reset the cache_size to its default value
1497 # when this happened.
1498 execsql { SELECT * FROM sqlite_master }
1499 execsql { PRAGMA cache_size }
1503 # Reset the sqlite3_temp_directory variable for the next run of tests:
1504 sqlite3 dbX :memory:
1505 dbX eval {PRAGMA temp_store_directory = ""}
1508 ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1509 set sqlite_hostid_num 1
1512 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1513 set using_proxy $value
1516 # Test the lock_proxy_file pragmas.
1519 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1522 do_test pragma-16.1 {
1524 PRAGMA lock_proxy_file="mylittleproxy";
1525 select * from sqlite_master;
1528 PRAGMA lock_proxy_file;
1532 do_test pragma-16.2 {
1535 PRAGMA lock_proxy_file="mylittleproxy";
1540 do_test pragma-16.2.1 {
1543 PRAGMA lock_proxy_file=":auto:";
1544 select * from sqlite_master;
1547 PRAGMA lock_proxy_file;
1552 do_test pragma-16.3 {
1555 PRAGMA lock_proxy_file="myotherproxy";
1558 select * from sqlite_master;
1560 } {1 {database is locked}}
1562 do_test pragma-16.4 {
1567 PRAGMA lock_proxy_file="myoriginalproxy";
1568 PRAGMA lock_proxy_file="myotherproxy";
1569 PRAGMA lock_proxy_file;
1574 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1575 do_test pragma-16.5 {
1578 PRAGMA lock_proxy_file=":auto:";
1579 PRAGMA lock_proxy_file;
1583 do_test pragma-16.6 {
1585 sqlite3 db2 test2.db
1586 set lockpath [execsql {
1587 PRAGMA lock_proxy_file=":auto:";
1588 PRAGMA lock_proxy_file;
1590 string match "*test2.db:auto:" $lockpath
1593 set sqlite_hostid_num 2
1594 do_test pragma-16.7 {
1598 PRAGMA lock_proxy_file=":auto:";
1599 select * from sqlite_master;
1602 } {1 {database is locked}}
1605 do_test pragma-16.8 {
1608 execsql { select * from sqlite_master }
1610 } {1 {database is locked}}
1613 do_test pragma-16.8.1 {
1615 PRAGMA lock_proxy_file="yetanotherproxy";
1616 PRAGMA lock_proxy_file;
1619 do_test pragma-16.8.2 {
1621 create table mine(x);
1626 do_test pragma-16.9 {
1627 sqlite3 db proxytest.db
1628 set lockpath2 [execsql {
1629 PRAGMA lock_proxy_file=":auto:";
1630 PRAGMA lock_proxy_file;
1632 string match "*proxytest.db:auto:" $lockpath2
1635 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1636 set sqlite_hostid_num 0
1639 # Parsing of auto_vacuum settings.
1641 foreach {autovac_setting val} {
1657 do_test pragma-17.1.$autovac_setting {
1661 PRAGMA auto_vacuum=$::autovac_setting;
1667 # Parsing of temp_store settings.
1669 foreach {temp_setting val} {
1682 do_test pragma-18.1.$temp_setting {
1686 PRAGMA temp_store=$::temp_setting;
1687 PRAGMA temp_store=$::temp_setting;
1693 # The SQLITE_FCNTL_PRAGMA logic, with error handling.
1697 sqlite3 db test.db -vfs tvfs
1698 do_test pragma-19.1 {
1699 catchsql {PRAGMA error}
1700 } {1 {SQL logic error}}
1701 do_test pragma-19.2 {
1702 catchsql {PRAGMA error='This is the error message'}
1703 } {1 {This is the error message}}
1704 do_test pragma-19.3 {
1705 catchsql {PRAGMA error='7 This is the error message'}
1706 } {1 {This is the error message}}
1707 do_test pragma-19.4 {
1708 catchsql {PRAGMA error=7}
1709 } {1 {out of memory}}
1710 do_test pragma-19.5 {
1711 file tail [lindex [execsql {PRAGMA filename}] 0]
1714 if {$tcl_platform(platform)=="windows"} {
1715 # Test data_store_directory pragma
1720 do_test pragma-20.1 {
1721 catchsql {PRAGMA data_store_directory}
1723 do_test pragma-20.2 {
1724 set pwd [string map {' ''} [file nativename [get_pwd]]]
1725 catchsql "PRAGMA data_store_directory='$pwd';"
1727 do_test pragma-20.3 {
1728 catchsql {PRAGMA data_store_directory}
1729 } [list 0 [list [file nativename [get_pwd]]]]
1730 do_test pragma-20.4 {
1731 set pwd [string map {' ''} [file nativename \
1732 [file join [get_pwd] data_dir]]]
1733 catchsql "PRAGMA data_store_directory='$pwd';"
1735 do_test pragma-20.5 {
1736 sqlite3 db2 test2.db
1737 catchsql "PRAGMA database_list;" db2
1738 } [list 0 [list 0 main [file nativename \
1739 [file join [get_pwd] data_dir test2.db]]]]
1741 do_test pragma-20.6 {
1742 sqlite3 db2 [file join [get_pwd] test2.db]
1743 catchsql "PRAGMA database_list;" db2
1744 } [list 0 [list 0 main [file nativename \
1745 [file join [get_pwd] test2.db]]]]
1747 do_test pragma-20.7 {
1748 catchsql "PRAGMA data_store_directory='';"
1750 do_test pragma-20.8 {
1751 catchsql {PRAGMA data_store_directory}
1754 forcedelete data_dir
1757 database_may_be_corrupt
1758 if {![nonzero_reserved_bytes]} {
1761 # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1767 PRAGMA page_size = 1024;
1768 PRAGMA auto_vacuum = 0;
1769 CREATE TABLE t1(a PRIMARY KEY, b);
1770 INSERT INTO t1 VALUES(1, 1);
1772 for {set i 0} {$i < 10} {incr i} {
1773 execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1776 forcecopy test.db testerr.db
1777 hexio_write testerr.db 15000 [string repeat 55 100]
1780 set mainerr {*** in database main ***
1781 Multiple uses for byte 672 of page 15}
1782 set auxerr {*** in database aux ***
1783 Multiple uses for byte 672 of page 15}
1785 set mainerr {/{\*\*\* in database main \*\*\*
1786 Multiple uses for byte 672 of page 15}.*/}
1787 set auxerr {/{\*\*\* in database aux \*\*\*
1788 Multiple uses for byte 672 of page 15}.*/}
1792 sqlite3 db testerr.db
1793 execsql { PRAGMA integrity_check }
1800 ATTACH 'testerr.db' AS 'aux';
1801 PRAGMA integrity_check;
1805 execsql { PRAGMA main.integrity_check; }
1808 execsql { PRAGMA aux.integrity_check; }
1813 sqlite3 db testerr.db
1815 ATTACH 'test.db' AS 'aux';
1816 PRAGMA integrity_check;
1820 execsql { PRAGMA main.integrity_check; }
1823 execsql { PRAGMA aux.integrity_check; }
1828 forcedelete test.db test.db-wal test.db-journal
1833 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1834 CREATE INDEX i1 ON t1(b,c);
1835 CREATE INDEX i2 ON t1(c,d);
1836 CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
1837 CREATE TABLE t2(x INTEGER REFERENCES t1);
1839 db2 eval {SELECT name FROM sqlite_master}
1844 CREATE INDEX i2 ON t1(c,d,b);
1846 capture_pragma db2 out {PRAGMA index_info(i2)}
1847 db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1848 } {2 c | 3 d | 1 b |}
1850 # EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1851 # pragma returns information about every column in an index.
1853 # EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1854 # returns information about every column in the index, not just the key
1858 capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1859 db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1860 } {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
1862 # (The first column of output from PRAGMA index_xinfo is...)
1863 # EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1864 # means left-most. Key columns come before auxiliary columns.)
1866 # (The second column of output from PRAGMA index_xinfo is...)
1867 # EVIDENCE-OF: R-40889-06838 The rank of the column within the table
1868 # being indexed, or -1 if the index-column is the rowid of the table
1871 # (The third column of output from PRAGMA index_xinfo is...)
1872 # EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
1873 # NULL if the index-column is the rowid of the table being indexed.
1875 # (The fourth column of output from PRAGMA index_xinfo is...)
1876 # EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1877 # (DESC) order by the index and 0 otherwise.
1879 # (The fifth column of output from PRAGMA index_xinfo is...)
1880 # EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1881 # compare values in the index-column.
1883 # (The sixth column of output from PRAGMA index_xinfo is...)
1884 # EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1885 # if the index-column is an auxiliary column.
1888 db2 eval {PRAGMA index_xinfo(i2)}
1889 } {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1891 db2 eval {PRAGMA index_xinfo(i2x)}
1892 } {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
1894 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1895 # pragma returns one row for each index associated with the given table.
1897 # (The first column of output from PRAGMA index_list is...)
1898 # EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1899 # for internal tracking purposes.
1901 # (The second column of output from PRAGMA index_list is...)
1902 # EVIDENCE-OF: R-35496-03635 The name of the index.
1904 # (The third column of output from PRAGMA index_list is...)
1905 # EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1907 # (The fourth column of output from PRAGMA index_list is...)
1908 # EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1909 # INDEX statement, "u" if the index was created by a UNIQUE constraint,
1910 # or "pk" if the index was created by a PRIMARY KEY constraint.
1914 CREATE INDEX i3 ON t1(d,b,c);
1916 capture_pragma db2 out {PRAGMA index_list(t1)}
1917 db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1918 } {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
1921 ALTER TABLE t1 ADD COLUMN e;
1924 PRAGMA table_info(t1);
1930 CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1933 PRAGMA foreign_key_list(t2);
1935 } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1938 ifcapable !has_codec {
1940 do_execsql_test 24.0 {
1941 PRAGMA page_size = 1024;
1942 CREATE TABLE t1(a, b, c);
1943 CREATE INDEX i1 ON t1(b);
1944 INSERT INTO t1 VALUES('a', 'b', 'c');
1945 PRAGMA integrity_check;
1948 set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
1950 hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
1953 do_catchsql_test 24.1 {
1955 } {1 {database disk image is malformed}}
1956 do_catchsql_test 24.2 {
1957 PRAGMA integrity_check;
1958 } {0 {{database disk image is malformed}}}
1960 database_never_corrupt