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 do_execsql_test pragma-3.20 {
501 CREATE TABLE t1(a,b);
502 CREATE INDEX t1a ON t1(a);
503 INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
504 PRAGMA writable_schema=ON;
505 UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
507 UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
509 PRAGMA writable_schema=OFF;
510 ALTER TABLE t1 RENAME TO t1x;
511 PRAGMA integrity_check;
512 } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
513 do_execsql_test pragma-3.21 {
514 PRAGMA integrity_check(3);
515 } {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
516 do_execsql_test pragma-3.22 {
517 PRAGMA integrity_check(2);
518 } {{non-unique entry in index t1a} {NULL value in t1x.a}}
519 do_execsql_test pragma-3.23 {
520 PRAGMA integrity_check(1);
521 } {{non-unique entry in index t1a}}
523 # PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
524 # interface) used to leave index cursors in an inconsistent state
525 # which could result in an assertion fault in sqlite3BtreeKey()
526 # called from saveCursorPosition() if content is removed from the
527 # index while the integrity_check is still running. This test verifies
528 # that problem has been fixed.
530 do_test pragma-3.30 {
535 CREATE TABLE t1(a,b,c);
537 c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
538 INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
539 CREATE INDEX t1a ON t1(a);
540 CREATE INDEX t1bc ON t1(b,c);
542 db eval {PRAGMA integrity_check} {
543 db eval {DELETE FROM t1}
547 # Test modifying the cache_size of an attached database.
548 ifcapable pager_pragmas&&attach {
551 ATTACH 'test2.db' AS aux;
552 pragma aux.cache_size;
553 pragma aux.default_cache_size;
555 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
558 pragma aux.cache_size = 50;
559 pragma aux.cache_size;
560 pragma aux.default_cache_size;
562 } [list 50 $DFLT_CACHE_SZ]
565 pragma aux.default_cache_size = 456;
566 pragma aux.cache_size;
567 pragma aux.default_cache_size;
573 pragma default_cache_size;
575 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
579 ATTACH 'test3.db' AS aux;
580 pragma aux.cache_size;
581 pragma aux.default_cache_size;
583 } [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
587 ATTACH 'test2.db' AS aux;
588 pragma aux.cache_size;
589 pragma aux.default_cache_size;
592 } ;# ifcapable pager_pragmas
594 # Test that modifying the sync-level in the middle of a transaction is
596 ifcapable pager_pragmas {
605 pragma synchronous = OFF;
607 } {1 {Safety level may not be changed inside a transaction}}
614 } ;# ifcapable pager_pragmas
616 # Test schema-query pragmas
618 ifcapable schema_pragmas {
619 ifcapable tempdb&&attach {
622 execsql {SELECT * FROM sqlite_temp_master}
623 foreach {idx name file} [execsql {pragma database_list}] {
624 lappend res $idx $name
627 } {0 main 1 temp 2 aux}
631 CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
632 pragma table_info(t2)
634 } {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
635 do_test pragma-6.2.1 {
640 db nullvalue <<NULL>>
641 do_test pragma-6.2.2 {
644 a TEXT DEFAULT CURRENT_TIMESTAMP,
647 d INTEGER DEFAULT NULL,
652 PRAGMA table_info(t5);
654 } {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 2 2 c TEXT 0 <<NULL>> 3 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 1}
656 do_test pragma-6.2.3 {
658 CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
659 pragma table_info(t2_3)
661 } {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
662 ifcapable {foreignkey} {
663 do_test pragma-6.3.1 {
665 CREATE TABLE t3(a int references t2(b), b UNIQUE);
666 pragma foreign_key_list(t3);
668 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
669 do_test pragma-6.3.2 {
671 pragma foreign_key_list;
674 do_test pragma-6.3.3 {
676 pragma foreign_key_list(t3_bogus);
679 do_test pragma-6.3.4 {
681 pragma foreign_key_list(t5);
685 capture_pragma db out {
686 pragma index_list(t3);
688 db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
689 } {0 sqlite_autoindex_t3_1 1}
691 ifcapable {!foreignkey} {
692 execsql {CREATE TABLE t3(a,b UNIQUE)}
694 do_test pragma-6.5.1 {
696 CREATE INDEX t3i1 ON t3(a,b);
698 capture_pragma db out {
699 pragma index_info(t3i1);
701 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
704 # EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
705 # by the index_info pragma, but they are listed by the index_xinfo
708 do_test pragma-6.5.1b {
709 capture_pragma db out {PRAGMA index_xinfo(t3i1)}
710 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
711 } {0 0 a 1 1 b 2 -1 {}}
714 # EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
715 # pragma returns one row for each key column in the named index.
717 # (The first column of output from PRAGMA index_info is...)
718 # EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
721 # (The second column of output from PRAGMA index_info is...)
722 # EVIDENCE-OF: R-65019-08383 The rank of the column within the table
725 # (The third column of output from PRAGMA index_info is...)
726 # EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
728 do_execsql_test pragma-6.5.1c {
729 CREATE INDEX t3i2 ON t3(b,a);
730 PRAGMA index_info='t3i2';
734 do_test pragma-6.5.2 {
736 pragma index_info(t3i1_bogus);
741 # Test for ticket #3320. When a temp table of the same name exists, make
742 # sure the schema of the main table can still be queried using
743 # "pragma table_info":
744 do_test pragma-6.6.1 {
746 CREATE TABLE trial(col_main);
747 CREATE TEMP TABLE trial(col_temp);
750 do_test pragma-6.6.2 {
752 PRAGMA table_info(trial);
754 } {0 col_temp {} 0 {} 0}
755 do_test pragma-6.6.3 {
757 PRAGMA temp.table_info(trial);
759 } {0 col_temp {} 0 {} 0}
760 do_test pragma-6.6.4 {
762 PRAGMA main.table_info(trial);
764 } {0 col_main {} 0 {} 0}
769 CREATE TABLE test_table(
770 one INT NOT NULL DEFAULT -1,
772 three VARCHAR(45, 65) DEFAULT 'abcde',
773 four REAL DEFAULT X'abcdef',
774 five DEFAULT CURRENT_TIME
777 capture_pragma db out {PRAGMA table_info(test_table)}
778 db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
782 {1 two text 0 {} 0} \
783 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
784 {3 four REAL 0 X'abcdef' 0} \
785 {4 five {} 0 CURRENT_TIME 0} \
789 CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
790 PRAGMA table_info(t68);
797 } ;# ifcapable schema_pragmas
798 # Miscellaneous tests
800 ifcapable schema_pragmas {
801 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
802 # pragma returns one row for each index associated with the given table.
804 do_test pragma-7.1.1 {
805 # Make sure a pragma knows to read the schema if it needs to
808 capture_pragma db out "PRAGMA index_list(t3)"
809 db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
810 } {t3i1 c sqlite_autoindex_t3_1 u}
811 do_test pragma-7.1.2 {
813 pragma index_list(t3_bogus);
816 } ;# ifcapable schema_pragmas
818 if {[permutation] == ""} {
823 pragma encoding=bogus;
825 } {1 {unsupported encoding: bogus}}
835 } {main unlocked temp closed}
847 #----------------------------------------------------------------------
848 # Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
849 # user_version" statements.
851 # pragma-8.1: PRAGMA schema_version
852 # pragma-8.2: PRAGMA user_version
855 ifcapable schema_version {
857 # First check that we can set the schema version and then retrieve the
859 do_test pragma-8.1.1 {
861 PRAGMA schema_version = 105;
864 do_test pragma-8.1.2 {
866 PRAGMA schema_version;
868 } {schema_version 105}
869 do_test pragma-8.1.3 {
871 PRAGMA schema_version = 106;
874 do_test pragma-8.1.4 {
876 PRAGMA schema_version;
880 # Check that creating a table modifies the schema-version (this is really
881 # to verify that the value being read is in fact the schema version).
882 do_test pragma-8.1.5 {
884 CREATE TABLE t4(a, b, c);
885 INSERT INTO t4 VALUES(1, 2, 3);
889 do_test pragma-8.1.6 {
891 PRAGMA schema_version;
895 # Now open a second connection to the database. Ensure that changing the
896 # schema-version using the first connection forces the second connection
897 # to reload the schema. This has to be done using the C-API test functions,
898 # because the TCL API accounts for SCHEMA_ERROR and retries the query.
899 do_test pragma-8.1.7 {
900 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
905 do_test pragma-8.1.8 {
907 PRAGMA schema_version = 108;
910 do_test pragma-8.1.9 {
911 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
914 do_test pragma-8.1.10 {
915 sqlite3_finalize $::STMT
918 # Make sure the schema-version can be manipulated in an attached database.
920 forcedelete test2.db-journal
922 do_test pragma-8.1.11 {
924 ATTACH 'test2.db' AS aux;
925 CREATE TABLE aux.t1(a, b, c);
926 PRAGMA aux.schema_version = 205;
929 do_test pragma-8.1.12 {
931 PRAGMA aux.schema_version;
935 do_test pragma-8.1.13 {
937 PRAGMA schema_version;
941 # And check that modifying the schema-version in an attached database
942 # forces the second connection to reload the schema.
944 do_test pragma-8.1.14 {
945 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
947 ATTACH 'test2.db' AS aux;
948 SELECT * FROM aux.t1;
951 do_test pragma-8.1.15 {
953 PRAGMA aux.schema_version = 206;
956 do_test pragma-8.1.16 {
957 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
960 do_test pragma-8.1.17 {
961 sqlite3_finalize $::STMT
963 do_test pragma-8.1.18 {
968 # Now test that the user-version can be read and written (and that we aren't
969 # accidentally manipulating the schema-version instead).
970 do_test pragma-8.2.1 {
975 do_test pragma-8.2.2 {
977 PRAGMA user_version = 2;
980 do_test pragma-8.2.3.1 {
985 do_test pragma-8.2.3.2 {
992 do_test pragma-8.2.4.1 {
994 PRAGMA schema_version;
998 do_test pragma-8.2.4.2 {
1001 PRAGMA user_version;
1004 do_test pragma-8.2.4.3 {
1006 PRAGMA schema_version;
1012 db eval {ATTACH 'test2.db' AS aux}
1014 # Check that the user-version in the auxilary database can be manipulated (
1015 # and that we aren't accidentally manipulating the same in the main db).
1016 do_test pragma-8.2.5 {
1018 PRAGMA aux.user_version;
1021 do_test pragma-8.2.6 {
1023 PRAGMA aux.user_version = 3;
1026 do_test pragma-8.2.7 {
1028 PRAGMA aux.user_version;
1031 do_test pragma-8.2.8 {
1033 PRAGMA main.user_version;
1037 # Now check that a ROLLBACK resets the user-version if it has been modified
1038 # within a transaction.
1039 do_test pragma-8.2.9 {
1042 PRAGMA aux.user_version = 10;
1043 PRAGMA user_version = 11;
1046 do_test pragma-8.2.10 {
1048 PRAGMA aux.user_version;
1051 do_test pragma-8.2.11 {
1053 PRAGMA main.user_version;
1056 do_test pragma-8.2.12 {
1059 PRAGMA aux.user_version;
1062 do_test pragma-8.2.13 {
1064 PRAGMA main.user_version;
1069 # Try a negative value for the user-version
1070 do_test pragma-8.2.14 {
1072 PRAGMA user_version = -450;
1075 do_test pragma-8.2.15 {
1077 PRAGMA user_version;
1080 } ; # ifcapable schema_version
1082 # Check to see if TEMP_STORE is memory or disk. Return strings
1083 # "memory" or "disk" as appropriate.
1085 proc check_temp_store {} {
1087 PRAGMA temp.cache_size = 1;
1088 CREATE TEMP TABLE IF NOT EXISTS a(b);
1090 INSERT INTO a VALUES(randomblob(1000));
1091 INSERT INTO a SELECT * FROM a;
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;
1100 db eval {PRAGMA database_list} {
1101 if {$name=="temp"} {
1102 set bt [btree_from_db db 1]
1103 if {[btree_ismemdb $bt]} {
1114 do_test pragma-8.3.1 {
1116 PRAGMA application_id;
1119 do_test pragma-8.3.2 {
1120 execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1123 # Test temp_store and temp_store_directory pragmas
1125 ifcapable pager_pragmas {
1126 do_test pragma-9.1 {
1133 if {$TEMP_STORE<=1} {
1134 do_test pragma-9.1.1 {
1138 do_test pragma-9.1.1 {
1143 do_test pragma-9.2 {
1147 PRAGMA temp_store=file;
1151 if {$TEMP_STORE==3} {
1152 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1153 do_test pragma-9.2.1 {
1157 do_test pragma-9.2.1 {
1162 do_test pragma-9.3 {
1166 PRAGMA temp_store=memory;
1170 if {$TEMP_STORE==0} {
1171 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1172 do_test pragma-9.3.1 {
1176 do_test pragma-9.3.1 {
1181 do_test pragma-9.4 {
1183 PRAGMA temp_store_directory;
1187 do_test pragma-9.5 {
1188 set pwd [string map {' ''} [file nativename [get_pwd]]]
1190 PRAGMA temp_store_directory='$pwd';
1193 do_test pragma-9.6 {
1195 PRAGMA temp_store_directory;
1197 } [list [file nativename [get_pwd]]]
1198 do_test pragma-9.7 {
1200 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1202 } {1 {not a writable directory}}
1203 do_test pragma-9.8 {
1205 PRAGMA temp_store_directory='';
1208 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1210 do_test pragma-9.9 {
1212 PRAGMA temp_store_directory;
1213 PRAGMA temp_store=FILE;
1214 CREATE TEMP TABLE temp_store_directory_test(a integer);
1215 INSERT INTO temp_store_directory_test values (2);
1216 SELECT * FROM temp_store_directory_test;
1219 do_test pragma-9.10 {
1221 PRAGMA temp_store_directory='$pwd';
1222 SELECT * FROM temp_store_directory_test;
1224 } {1 {no such table: temp_store_directory_test}}
1228 do_test pragma-9.11 {
1230 PRAGMA temp_store = 0;
1234 do_test pragma-9.12 {
1236 PRAGMA temp_store = 1;
1240 do_test pragma-9.13 {
1242 PRAGMA temp_store = 2;
1246 do_test pragma-9.14 {
1248 PRAGMA temp_store = 3;
1252 do_test pragma-9.15 {
1255 CREATE TEMP TABLE temp_table(t);
1256 INSERT INTO temp_table VALUES('valuable data');
1257 PRAGMA temp_store = 1;
1259 } {1 {temporary storage cannot be changed from within a transaction}}
1260 do_test pragma-9.16 {
1262 SELECT * FROM temp_table;
1267 do_test pragma-9.17 {
1269 INSERT INTO temp_table VALUES('valuable data II');
1270 SELECT * FROM temp_table;
1272 } {{valuable data} {valuable data II}}
1274 do_test pragma-9.18 {
1276 db eval {SELECT t FROM temp_table} {
1277 execsql {pragma temp_store = 1}
1281 } {1 {temporary storage cannot be changed from within a transaction}}
1283 } ;# ifcapable pager_pragmas
1287 do_test pragma-10.0 {
1292 PRAGMA count_changes = 1;
1294 CREATE TABLE t1(a PRIMARY KEY);
1295 CREATE TABLE t1_mirror(a);
1296 CREATE TABLE t1_mirror2(a);
1297 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1298 INSERT INTO t1_mirror VALUES(new.a);
1300 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1301 INSERT INTO t1_mirror2 VALUES(new.a);
1303 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1304 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1306 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1307 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1309 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1310 DELETE FROM t1_mirror WHERE a = old.a;
1312 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1313 DELETE FROM t1_mirror2 WHERE a = old.a;
1318 do_test pragma-10.1 {
1320 INSERT INTO t1 VALUES(randstr(10,10));
1323 do_test pragma-10.2 {
1325 UPDATE t1 SET a = randstr(10,10);
1328 do_test pragma-10.3 {
1334 } ;# ifcapable trigger
1336 ifcapable schema_pragmas {
1337 do_test pragma-11.1 {
1339 pragma collation_list;
1341 } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
1342 do_test pragma-11.2 {
1343 db collate New_Collation blah...
1345 pragma collation_list;
1347 } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
1350 ifcapable schema_pragmas&&tempdb {
1351 do_test pragma-12.1 {
1354 PRAGMA temp.table_info('abc');
1359 do_test pragma-12.2 {
1362 PRAGMA temp.default_cache_size = 200;
1363 PRAGMA temp.default_cache_size;
1368 do_test pragma-12.3 {
1371 PRAGMA temp.cache_size = 400;
1372 PRAGMA temp.cache_size;
1380 do_test pragma-13.1 {
1382 DROP TABLE IF EXISTS t4;
1383 PRAGMA vdbe_trace=on;
1384 PRAGMA vdbe_listing=on;
1385 PRAGMA sql_trace=on;
1386 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1387 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1388 INSERT INTO t4(b) VALUES(randstr(30,30));
1389 INSERT INTO t4(b) VALUES(1.23456);
1390 INSERT INTO t4(b) VALUES(NULL);
1391 INSERT INTO t4(b) VALUES(0);
1392 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1396 PRAGMA vdbe_trace=off;
1397 PRAGMA vdbe_listing=off;
1398 PRAGMA sql_trace=off;
1402 } ;# ifcapable bloblit
1404 ifcapable pager_pragmas {
1409 # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1410 # number of pages in the database file.
1412 do_test pragma-14.1 {
1413 execsql { pragma auto_vacuum = 0 }
1414 execsql { pragma page_count; pragma main.page_count }
1417 do_test pragma-14.2 {
1419 CREATE TABLE abc(a, b, c);
1421 PRAGMA main.page_count;
1422 PRAGMA temp.page_count;
1425 do_test pragma-14.2uc {
1426 execsql {pragma PAGE_COUNT}
1429 do_test pragma-14.3 {
1432 CREATE TABLE def(a, b, c);
1436 do_test pragma-14.3uc {
1437 execsql {pragma PAGE_COUNT}
1440 do_test pragma-14.4 {
1441 set page_size [db one {pragma page_size}]
1442 expr [file size test.db] / $page_size
1445 do_test pragma-14.5 {
1452 do_test pragma-14.6 {
1453 forcedelete test2.db
1454 sqlite3 db2 test2.db
1456 PRAGMA auto_vacuum = 0;
1457 CREATE TABLE t1(a, b, c);
1458 CREATE TABLE t2(a, b, c);
1459 CREATE TABLE t3(a, b, c);
1460 CREATE TABLE t4(a, b, c);
1464 ATTACH 'test2.db' AS aux;
1465 PRAGMA aux.page_count;
1468 do_test pragma-14.6uc {
1469 execsql {pragma AUX.PAGE_COUNT}
1473 # Test that the value set using the cache_size pragma is not reset when the
1474 # schema is reloaded.
1476 ifcapable pager_pragmas {
1479 do_test pragma-15.1 {
1481 PRAGMA cache_size=59;
1485 do_test pragma-15.2 {
1488 CREATE TABLE newtable(a, b, c);
1492 do_test pragma-15.3 {
1493 # Evaluating this statement will cause the schema to be reloaded (because
1494 # the schema was changed by another connection in pragma-15.2). At one
1495 # point there was a bug that reset the cache_size to its default value
1496 # when this happened.
1497 execsql { SELECT * FROM sqlite_master }
1498 execsql { PRAGMA cache_size }
1502 # Reset the sqlite3_temp_directory variable for the next run of tests:
1503 sqlite3 dbX :memory:
1504 dbX eval {PRAGMA temp_store_directory = ""}
1507 ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1508 set sqlite_hostid_num 1
1511 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1512 set using_proxy $value
1515 # Test the lock_proxy_file pragmas.
1518 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1521 do_test pragma-16.1 {
1523 PRAGMA lock_proxy_file="mylittleproxy";
1524 select * from sqlite_master;
1527 PRAGMA lock_proxy_file;
1531 do_test pragma-16.2 {
1534 PRAGMA lock_proxy_file="mylittleproxy";
1539 do_test pragma-16.2.1 {
1542 PRAGMA lock_proxy_file=":auto:";
1543 select * from sqlite_master;
1546 PRAGMA lock_proxy_file;
1551 do_test pragma-16.3 {
1554 PRAGMA lock_proxy_file="myotherproxy";
1557 select * from sqlite_master;
1559 } {1 {database is locked}}
1561 do_test pragma-16.4 {
1566 PRAGMA lock_proxy_file="myoriginalproxy";
1567 PRAGMA lock_proxy_file="myotherproxy";
1568 PRAGMA lock_proxy_file;
1573 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1574 do_test pragma-16.5 {
1577 PRAGMA lock_proxy_file=":auto:";
1578 PRAGMA lock_proxy_file;
1582 do_test pragma-16.6 {
1584 sqlite3 db2 test2.db
1585 set lockpath [execsql {
1586 PRAGMA lock_proxy_file=":auto:";
1587 PRAGMA lock_proxy_file;
1589 string match "*test2.db:auto:" $lockpath
1592 set sqlite_hostid_num 2
1593 do_test pragma-16.7 {
1597 PRAGMA lock_proxy_file=":auto:";
1598 select * from sqlite_master;
1601 } {1 {database is locked}}
1604 do_test pragma-16.8 {
1607 execsql { select * from sqlite_master }
1609 } {1 {database is locked}}
1612 do_test pragma-16.8.1 {
1614 PRAGMA lock_proxy_file="yetanotherproxy";
1615 PRAGMA lock_proxy_file;
1618 do_test pragma-16.8.2 {
1620 create table mine(x);
1625 do_test pragma-16.9 {
1626 sqlite3 db proxytest.db
1627 set lockpath2 [execsql {
1628 PRAGMA lock_proxy_file=":auto:";
1629 PRAGMA lock_proxy_file;
1631 string match "*proxytest.db:auto:" $lockpath2
1634 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1635 set sqlite_hostid_num 0
1638 # Parsing of auto_vacuum settings.
1640 foreach {autovac_setting val} {
1656 do_test pragma-17.1.$autovac_setting {
1660 PRAGMA auto_vacuum=$::autovac_setting;
1666 # Parsing of temp_store settings.
1668 foreach {temp_setting val} {
1681 do_test pragma-18.1.$temp_setting {
1685 PRAGMA temp_store=$::temp_setting;
1686 PRAGMA temp_store=$::temp_setting;
1692 # The SQLITE_FCNTL_PRAGMA logic, with error handling.
1696 sqlite3 db test.db -vfs tvfs
1697 do_test pragma-19.1 {
1698 catchsql {PRAGMA error}
1699 } {1 {SQL logic error}}
1700 do_test pragma-19.2 {
1701 catchsql {PRAGMA error='This is the error message'}
1702 } {1 {This is the error message}}
1703 do_test pragma-19.3 {
1704 catchsql {PRAGMA error='7 This is the error message'}
1705 } {1 {This is the error message}}
1706 do_test pragma-19.4 {
1707 catchsql {PRAGMA error=7}
1708 } {1 {out of memory}}
1709 do_test pragma-19.5 {
1710 file tail [lindex [execsql {PRAGMA filename}] 0]
1713 if {$tcl_platform(platform)=="windows"} {
1714 # Test data_store_directory pragma
1719 do_test pragma-20.1 {
1720 catchsql {PRAGMA data_store_directory}
1722 do_test pragma-20.2 {
1723 set pwd [string map {' ''} [file nativename [get_pwd]]]
1724 catchsql "PRAGMA data_store_directory='$pwd';"
1726 do_test pragma-20.3 {
1727 catchsql {PRAGMA data_store_directory}
1728 } [list 0 [list [file nativename [get_pwd]]]]
1729 do_test pragma-20.4 {
1730 set pwd [string map {' ''} [file nativename \
1731 [file join [get_pwd] data_dir]]]
1732 catchsql "PRAGMA data_store_directory='$pwd';"
1734 do_test pragma-20.5 {
1735 sqlite3 db2 test2.db
1736 catchsql "PRAGMA database_list;" db2
1737 } [list 0 [list 0 main [file nativename \
1738 [file join [get_pwd] data_dir test2.db]]]]
1740 do_test pragma-20.6 {
1741 sqlite3 db2 [file join [get_pwd] test2.db]
1742 catchsql "PRAGMA database_list;" db2
1743 } [list 0 [list 0 main [file nativename \
1744 [file join [get_pwd] test2.db]]]]
1746 do_test pragma-20.7 {
1747 catchsql "PRAGMA data_store_directory='';"
1749 do_test pragma-20.8 {
1750 catchsql {PRAGMA data_store_directory}
1753 forcedelete data_dir
1756 database_may_be_corrupt
1757 if {![nonzero_reserved_bytes]} {
1760 # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1766 PRAGMA page_size = 1024;
1767 PRAGMA auto_vacuum = 0;
1768 CREATE TABLE t1(a PRIMARY KEY, b);
1769 INSERT INTO t1 VALUES(1, 1);
1771 for {set i 0} {$i < 10} {incr i} {
1772 execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1775 forcecopy test.db testerr.db
1776 hexio_write testerr.db 15000 [string repeat 55 100]
1779 set mainerr {*** in database main ***
1780 Multiple uses for byte 672 of page 15}
1781 set auxerr {*** in database aux ***
1782 Multiple uses for byte 672 of page 15}
1784 set mainerr {/{\*\*\* in database main \*\*\*
1785 Multiple uses for byte 672 of page 15}.*/}
1786 set auxerr {/{\*\*\* in database aux \*\*\*
1787 Multiple uses for byte 672 of page 15}.*/}
1791 sqlite3 db testerr.db
1792 execsql { PRAGMA integrity_check }
1799 ATTACH 'testerr.db' AS 'aux';
1800 PRAGMA integrity_check;
1804 execsql { PRAGMA main.integrity_check; }
1807 execsql { PRAGMA aux.integrity_check; }
1812 sqlite3 db testerr.db
1814 ATTACH 'test.db' AS 'aux';
1815 PRAGMA integrity_check;
1819 execsql { PRAGMA main.integrity_check; }
1822 execsql { PRAGMA aux.integrity_check; }
1827 forcedelete test.db test.db-wal test.db-journal
1832 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1833 CREATE INDEX i1 ON t1(b,c);
1834 CREATE INDEX i2 ON t1(c,d);
1835 CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
1836 CREATE TABLE t2(x INTEGER REFERENCES t1);
1838 db2 eval {SELECT name FROM sqlite_master}
1843 CREATE INDEX i2 ON t1(c,d,b);
1845 capture_pragma db2 out {PRAGMA index_info(i2)}
1846 db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1847 } {2 c | 3 d | 1 b |}
1849 # EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1850 # pragma returns information about every column in an index.
1852 # EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1853 # returns information about every column in the index, not just the key
1857 capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1858 db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1859 } {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
1861 # (The first column of output from PRAGMA index_xinfo is...)
1862 # EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1863 # means left-most. Key columns come before auxiliary columns.)
1865 # (The second column of output from PRAGMA index_xinfo is...)
1866 # EVIDENCE-OF: R-40889-06838 The rank of the column within the table
1867 # being indexed, or -1 if the index-column is the rowid of the table
1870 # (The third column of output from PRAGMA index_xinfo is...)
1871 # EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
1872 # NULL if the index-column is the rowid of the table being indexed.
1874 # (The fourth column of output from PRAGMA index_xinfo is...)
1875 # EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1876 # (DESC) order by the index and 0 otherwise.
1878 # (The fifth column of output from PRAGMA index_xinfo is...)
1879 # EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1880 # compare values in the index-column.
1882 # (The sixth column of output from PRAGMA index_xinfo is...)
1883 # EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1884 # if the index-column is an auxiliary column.
1887 db2 eval {PRAGMA index_xinfo(i2)}
1888 } {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1890 db2 eval {PRAGMA index_xinfo(i2x)}
1891 } {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
1893 # EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1894 # pragma returns one row for each index associated with the given table.
1896 # (The first column of output from PRAGMA index_list is...)
1897 # EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1898 # for internal tracking purposes.
1900 # (The second column of output from PRAGMA index_list is...)
1901 # EVIDENCE-OF: R-35496-03635 The name of the index.
1903 # (The third column of output from PRAGMA index_list is...)
1904 # EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1906 # (The fourth column of output from PRAGMA index_list is...)
1907 # EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1908 # INDEX statement, "u" if the index was created by a UNIQUE constraint,
1909 # or "pk" if the index was created by a PRIMARY KEY constraint.
1913 CREATE INDEX i3 ON t1(d,b,c);
1915 capture_pragma db2 out {PRAGMA index_list(t1)}
1916 db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1917 } {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
1920 ALTER TABLE t1 ADD COLUMN e;
1923 PRAGMA table_info(t1);
1929 CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1932 PRAGMA foreign_key_list(t2);
1934 } {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1937 ifcapable !has_codec {
1939 do_execsql_test 24.0 {
1940 PRAGMA page_size = 1024;
1941 CREATE TABLE t1(a, b, c);
1942 CREATE INDEX i1 ON t1(b);
1943 INSERT INTO t1 VALUES('a', 'b', 'c');
1944 PRAGMA integrity_check;
1947 set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
1949 hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
1952 do_catchsql_test 24.1 {
1954 } {1 {database disk image is malformed}}
1955 do_catchsql_test 24.2 {
1956 PRAGMA integrity_check;
1957 } {0 {{database disk image is malformed}}}
1959 database_never_corrupt