xref: /sqlite-3.40.0/test/pragma.test (revision dfe4e6bb)
1# 2002 March 6
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library.
12#
13# This file implements tests for the PRAGMA command.
14#
15# $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19set testprefix pragma
20
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).
23#
24do_not_use_codec
25
26# Test organization:
27#
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.
47#
48
49ifcapable !pragma {
50  finish_test
51  return
52}
53
54# Capture the output of a pragma in a TEMP table.
55#
56proc 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  }
76}
77
78# Delete the preexisting database to avoid the special setup
79# that the "all.test" script does.
80#
81db close
82delete_file test.db test.db-journal
83delete_file test3.db test3.db-journal
84sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
85
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.
90#
91ifcapable pager_pragmas {
92set DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
93set TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
94do_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]
101do_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]
112do_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]
121do_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]
129do_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]
137do_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]
145do_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]
154do_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}
162do_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}
171ifcapable 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}
180}
181do_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}
189do_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}
197do_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}
205do_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}
214
215# Make sure the pragma handler understands numeric values in addition
216# to keywords like "off" and "full".
217#
218do_test pragma-1.13 {
219  execsql {
220    PRAGMA synchronous=0;
221    PRAGMA synchronous;
222  }
223} {0}
224do_test pragma-1.14 {
225  execsql {
226    PRAGMA synchronous=2;
227    PRAGMA synchronous;
228  }
229} {2}
230do_test pragma-1.14.1 {
231  execsql {
232    PRAGMA synchronous=4;
233    PRAGMA synchronous;
234  }
235} {4}
236do_test pragma-1.14.2 {
237  execsql {
238    PRAGMA synchronous=3;
239    PRAGMA synchronous;
240  }
241} {3}
242do_test pragma-1.14.3 {
243  execsql {
244    PRAGMA synchronous=8;
245    PRAGMA synchronous;
246  }
247} {0}
248do_test pragma-1.14.4 {
249  execsql {
250    PRAGMA synchronous=10;
251    PRAGMA synchronous;
252  }
253} {2}
254} ;# ifcapable pager_pragmas
255
256# Test turning "flag" pragmas on and off.
257#
258ifcapable debug {
259  # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
260  #
261  do_test pragma-1.15 {
262    execsql {
263      PRAGMA vdbe_listing=YES;
264      PRAGMA vdbe_listing;
265    }
266  } {1}
267  do_test pragma-1.16 {
268    execsql {
269      PRAGMA vdbe_listing=NO;
270      PRAGMA vdbe_listing;
271    }
272  } {0}
273}
274
275do_test pragma-1.17 {
276  execsql {
277    PRAGMA parser_trace=ON;
278    PRAGMA parser_trace=OFF;
279  }
280} {}
281do_test pragma-1.18 {
282  execsql {
283    PRAGMA bogus = -1234;  -- Parsing of negative values
284  }
285} {}
286
287# Test modifying the safety_level of an attached database.
288ifcapable pager_pragmas&&attach {
289  do_test pragma-2.1 {
290    forcedelete test2.db
291    forcedelete test2.db-journal
292    execsql {
293      ATTACH 'test2.db' AS aux;
294    }
295  } {}
296  do_test pragma-2.2 {
297    execsql {
298      pragma aux.synchronous;
299    }
300  } {2}
301  do_test pragma-2.3 {
302    execsql {
303      pragma aux.synchronous = OFF;
304      pragma aux.synchronous;
305      pragma synchronous;
306    }
307  } {0 2}
308  do_test pragma-2.4 {
309    execsql {
310      pragma aux.synchronous = ON;
311      pragma synchronous;
312      pragma aux.synchronous;
313    }
314  } {2 1}
315} ;# ifcapable pager_pragmas
316
317# Construct a corrupted index and make sure the integrity_check
318# pragma finds it.
319#
320# These tests won't work if the database is encrypted
321#
322do_test pragma-3.1 {
323  db close
324  forcedelete test.db test.db-journal
325  sqlite3 db test.db
326  execsql {
327    PRAGMA auto_vacuum=OFF;
328    BEGIN;
329    CREATE TABLE t2(a,b,c);
330    CREATE INDEX i2 ON t2(a);
331    INSERT INTO t2 VALUES(11,2,3);
332    INSERT INTO t2 VALUES(22,3,4);
333    COMMIT;
334    SELECT rowid, * from t2;
335  }
336} {1 11 2 3 2 22 3 4}
337ifcapable attach {
338  if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
339    do_test pragma-3.2 {
340      db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
341      set pgsz [db eval {PRAGMA page_size}]
342      # overwrite the header on the rootpage of the index in order to
343      # make the index appear to be empty.
344      #
345      set offset [expr {$pgsz*($rootpage-1)}]
346      hexio_write test.db $offset 0a00000000040000000000
347      db close
348      sqlite3 db test.db
349      execsql {PRAGMA integrity_check}
350    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
351    do_test pragma-3.3 {
352      execsql {PRAGMA integrity_check=1}
353    } {{row 1 missing from index i2}}
354    do_test pragma-3.4 {
355      execsql {
356        ATTACH DATABASE 'test.db' AS t2;
357        PRAGMA integrity_check
358      }
359    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
360    do_test pragma-3.5 {
361      execsql {
362        PRAGMA integrity_check=4
363      }
364    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
365    do_test pragma-3.6 {
366      execsql {
367        PRAGMA integrity_check=xyz
368      }
369    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
370    do_test pragma-3.7 {
371      execsql {
372        PRAGMA integrity_check=0
373      }
374    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
375
376    # Add additional corruption by appending unused pages to the end of
377    # the database file testerr.db
378    #
379    do_test pragma-3.8 {
380      execsql {DETACH t2}
381      forcedelete testerr.db testerr.db-journal
382      set out [open testerr.db w]
383      fconfigure $out -translation binary
384      set in [open test.db r]
385      fconfigure $in -translation binary
386      puts -nonewline $out [read $in]
387      seek $in 0
388      puts -nonewline $out [read $in]
389      close $in
390      close $out
391      hexio_write testerr.db 28 00000000
392      execsql {REINDEX t2}
393      execsql {PRAGMA integrity_check}
394    } {ok}
395    do_test pragma-3.8.1 {
396      execsql {PRAGMA quick_check}
397    } {ok}
398    do_test pragma-3.8.2 {
399      execsql {PRAGMA QUICK_CHECK}
400    } {ok}
401    do_test pragma-3.9 {
402      execsql {
403        ATTACH 'testerr.db' AS t2;
404        PRAGMA integrity_check
405      }
406    } {{*** in database t2 ***
407Page 4 is never used
408Page 5 is never used
409Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
410    do_test pragma-3.10 {
411      execsql {
412        PRAGMA integrity_check=1
413      }
414    } {{*** in database t2 ***
415Page 4 is never used}}
416    do_test pragma-3.11 {
417      execsql {
418        PRAGMA integrity_check=5
419      }
420    } {{*** in database t2 ***
421Page 4 is never used
422Page 5 is never used
423Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
424    do_test pragma-3.12 {
425      execsql {
426        PRAGMA integrity_check=4
427      }
428    } {{*** in database t2 ***
429Page 4 is never used
430Page 5 is never used
431Page 6 is never used} {row 1 missing from index i2}}
432    do_test pragma-3.13 {
433      execsql {
434        PRAGMA integrity_check=3
435      }
436    } {{*** in database t2 ***
437Page 4 is never used
438Page 5 is never used
439Page 6 is never used}}
440    do_test pragma-3.14 {
441      execsql {
442        PRAGMA integrity_check(2)
443      }
444    } {{*** in database t2 ***
445Page 4 is never used
446Page 5 is never used}}
447    do_test pragma-3.15 {
448      execsql {
449        ATTACH 'testerr.db' AS t3;
450        PRAGMA integrity_check
451      }
452    } {{*** in database t2 ***
453Page 4 is never used
454Page 5 is never used
455Page 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 ***
456Page 4 is never used
457Page 5 is never used
458Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
459    do_test pragma-3.16 {
460      execsql {
461        PRAGMA integrity_check(10)
462      }
463    } {{*** in database t2 ***
464Page 4 is never used
465Page 5 is never used
466Page 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 ***
467Page 4 is never used
468Page 5 is never used
469Page 6 is never used} {row 1 missing from index i2}}
470    do_test pragma-3.17 {
471      execsql {
472        PRAGMA integrity_check=8
473      }
474    } {{*** in database t2 ***
475Page 4 is never used
476Page 5 is never used
477Page 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 ***
478Page 4 is never used
479Page 5 is never used}}
480    do_test pragma-3.18 {
481      execsql {
482        PRAGMA integrity_check=4
483      }
484    } {{*** in database t2 ***
485Page 4 is never used
486Page 5 is never used
487Page 6 is never used} {row 1 missing from index i2}}
488  }
489  do_test pragma-3.19 {
490    catch {db close}
491    forcedelete test.db test.db-journal
492    sqlite3 db test.db
493    db eval {PRAGMA integrity_check}
494  } {ok}
495}
496
497# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
498# constraint violations.
499#
500do_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)'
506   WHERE name='t1a';
507  UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
508   WHERE name='t1';
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}}
513do_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}}
516do_execsql_test pragma-3.22 {
517  PRAGMA integrity_check(2);
518} {{non-unique entry in index t1a} {NULL value in t1x.a}}
519do_execsql_test pragma-3.23 {
520  PRAGMA integrity_check(1);
521} {{non-unique entry in index t1a}}
522
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.
529#
530do_test pragma-3.30 {
531  db close
532  delete_file test.db
533  sqlite3 db test.db
534  db eval {
535    CREATE TABLE t1(a,b,c);
536    WITH RECURSIVE
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);
541  }
542  db eval {PRAGMA integrity_check} {
543     db eval {DELETE FROM t1}
544  }
545} {}
546
547# Test modifying the cache_size of an attached database.
548ifcapable pager_pragmas&&attach {
549do_test pragma-4.1 {
550  execsql {
551    ATTACH 'test2.db' AS aux;
552    pragma aux.cache_size;
553    pragma aux.default_cache_size;
554  }
555} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
556do_test pragma-4.2 {
557  execsql {
558    pragma aux.cache_size = 50;
559    pragma aux.cache_size;
560    pragma aux.default_cache_size;
561  }
562} [list 50 $DFLT_CACHE_SZ]
563do_test pragma-4.3 {
564  execsql {
565    pragma aux.default_cache_size = 456;
566    pragma aux.cache_size;
567    pragma aux.default_cache_size;
568  }
569} {456 456}
570do_test pragma-4.4 {
571  execsql {
572    pragma cache_size;
573    pragma default_cache_size;
574  }
575} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
576do_test pragma-4.5 {
577  execsql {
578    DETACH aux;
579    ATTACH 'test3.db' AS aux;
580    pragma aux.cache_size;
581    pragma aux.default_cache_size;
582  }
583} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
584do_test pragma-4.6 {
585  execsql {
586    DETACH aux;
587    ATTACH 'test2.db' AS aux;
588    pragma aux.cache_size;
589    pragma aux.default_cache_size;
590  }
591} {456 456}
592} ;# ifcapable pager_pragmas
593
594# Test that modifying the sync-level in the middle of a transaction is
595# disallowed.
596ifcapable pager_pragmas {
597do_test pragma-5.0 {
598  execsql {
599    pragma synchronous;
600  }
601} {2}
602do_test pragma-5.1 {
603  catchsql {
604    BEGIN;
605    pragma synchronous = OFF;
606  }
607} {1 {Safety level may not be changed inside a transaction}}
608do_test pragma-5.2 {
609  execsql {
610    pragma synchronous;
611  }
612} {2}
613catchsql {COMMIT;}
614} ;# ifcapable pager_pragmas
615
616# Test schema-query pragmas
617#
618ifcapable schema_pragmas {
619ifcapable tempdb&&attach {
620  do_test pragma-6.1 {
621    set res {}
622    execsql {SELECT * FROM sqlite_temp_master}
623    foreach {idx name file} [execsql {pragma database_list}] {
624      lappend res $idx $name
625    }
626    set res
627  } {0 main 1 temp 2 aux}
628}
629do_test pragma-6.2 {
630  execsql {
631    CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
632    pragma table_info(t2)
633  }
634} {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
635do_test pragma-6.2.1 {
636  execsql {
637    pragma table_info;
638  }
639} {}
640db nullvalue <<NULL>>
641do_test pragma-6.2.2 {
642  execsql {
643    CREATE TABLE t5(
644      a TEXT DEFAULT CURRENT_TIMESTAMP,
645      b DEFAULT (5+3),
646      c TEXT,
647      d INTEGER DEFAULT NULL,
648      e TEXT DEFAULT '',
649      UNIQUE(b,c,d),
650      PRIMARY KEY(e,b,c)
651    );
652    PRAGMA table_info(t5);
653  }
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}
655db nullvalue {}
656do_test pragma-6.2.3 {
657  execsql {
658    CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
659    pragma table_info(t2_3)
660  }
661} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
662ifcapable {foreignkey} {
663  do_test pragma-6.3.1 {
664    execsql {
665      CREATE TABLE t3(a int references t2(b), b UNIQUE);
666      pragma foreign_key_list(t3);
667    }
668  } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
669  do_test pragma-6.3.2 {
670    execsql {
671      pragma foreign_key_list;
672    }
673  } {}
674  do_test pragma-6.3.3 {
675    execsql {
676      pragma foreign_key_list(t3_bogus);
677    }
678  } {}
679  do_test pragma-6.3.4 {
680    execsql {
681      pragma foreign_key_list(t5);
682    }
683  } {}
684  do_test pragma-6.4 {
685    capture_pragma db out {
686      pragma index_list(t3);
687    }
688    db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
689  } {0 sqlite_autoindex_t3_1 1}
690}
691ifcapable {!foreignkey} {
692  execsql {CREATE TABLE t3(a,b UNIQUE)}
693}
694do_test pragma-6.5.1 {
695  execsql {
696    CREATE INDEX t3i1 ON t3(a,b);
697  }
698  capture_pragma db out {
699    pragma index_info(t3i1);
700  }
701  db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
702} {0 0 a 1 1 b}
703
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
706# pragma.
707#
708do_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 {}}
712
713
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.
716#
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
719# means left-most.)
720#
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
723# being indexed.
724#
725# (The third column of output from PRAGMA index_info is...)
726# EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
727#
728do_execsql_test pragma-6.5.1c {
729  CREATE INDEX t3i2 ON t3(b,a);
730  PRAGMA index_info='t3i2';
731  DROP INDEX t3i2;
732} {0 1 b 1 0 a}
733
734do_test pragma-6.5.2 {
735  execsql {
736    pragma index_info(t3i1_bogus);
737  }
738} {}
739
740ifcapable tempdb {
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 {
745    execsql {
746      CREATE TABLE trial(col_main);
747      CREATE TEMP TABLE trial(col_temp);
748    }
749  } {}
750  do_test pragma-6.6.2 {
751    execsql {
752      PRAGMA table_info(trial);
753    }
754  } {0 col_temp {} 0 {} 0}
755  do_test pragma-6.6.3 {
756    execsql {
757      PRAGMA temp.table_info(trial);
758    }
759  } {0 col_temp {} 0 {} 0}
760  do_test pragma-6.6.4 {
761    execsql {
762      PRAGMA main.table_info(trial);
763    }
764  } {0 col_main {} 0 {} 0}
765}
766
767do_test pragma-6.7 {
768  execsql {
769    CREATE TABLE test_table(
770      one INT NOT NULL DEFAULT -1,
771      two text,
772      three VARCHAR(45, 65) DEFAULT 'abcde',
773      four REAL DEFAULT X'abcdef',
774      five DEFAULT CURRENT_TIME
775    );
776  }
777  capture_pragma db out {PRAGMA table_info(test_table)}
778  db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
779            ORDER BY cid}
780} [concat \
781  {0 one INT 1 -1 0} \
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} \
786]
787do_test pragma-6.8 {
788  execsql {
789    CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
790    PRAGMA table_info(t68);
791  }
792} [concat \
793  {0 a {} 0 {} 1} \
794  {1 b {} 0 {} 2} \
795  {2 c {} 0 {} 4} \
796]
797} ;# ifcapable schema_pragmas
798# Miscellaneous tests
799#
800ifcapable 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.
803#
804do_test pragma-7.1.1 {
805  # Make sure a pragma knows to read the schema if it needs to
806  db close
807  sqlite3 db test.db
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}
811do_test pragma-7.1.2 {
812  execsql {
813    pragma index_list(t3_bogus);
814  }
815} {}
816} ;# ifcapable schema_pragmas
817ifcapable {utf16} {
818  if {[permutation] == ""} {
819    do_test pragma-7.2 {
820      db close
821      sqlite3 db test.db
822      catchsql {
823        pragma encoding=bogus;
824      }
825    } {1 {unsupported encoding: bogus}}
826  }
827}
828ifcapable tempdb {
829  do_test pragma-7.3 {
830    db close
831    sqlite3 db test.db
832    execsql {
833      pragma lock_status;
834    }
835  } {main unlocked temp closed}
836} else {
837  do_test pragma-7.3 {
838    db close
839    sqlite3 db test.db
840    execsql {
841      pragma lock_status;
842    }
843  } {main unlocked}
844}
845
846
847#----------------------------------------------------------------------
848# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
849# user_version" statements.
850#
851# pragma-8.1: PRAGMA schema_version
852# pragma-8.2: PRAGMA user_version
853#
854
855ifcapable schema_version {
856
857# First check that we can set the schema version and then retrieve the
858# same value.
859do_test pragma-8.1.1 {
860  execsql {
861    PRAGMA schema_version = 105;
862  }
863} {}
864do_test pragma-8.1.2 {
865  execsql2 {
866    PRAGMA schema_version;
867  }
868} {schema_version 105}
869do_test pragma-8.1.3 {
870  execsql {
871    PRAGMA schema_version = 106;
872  }
873} {}
874do_test pragma-8.1.4 {
875  execsql {
876    PRAGMA schema_version;
877  }
878} 106
879
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).
882do_test pragma-8.1.5 {
883  execsql {
884    CREATE TABLE t4(a, b, c);
885    INSERT INTO t4 VALUES(1, 2, 3);
886    SELECT * FROM t4;
887  }
888} {1 2 3}
889do_test pragma-8.1.6 {
890  execsql {
891    PRAGMA schema_version;
892  }
893} 107
894
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.
899do_test pragma-8.1.7 {
900  sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
901  execsql {
902    SELECT * FROM t4;
903  } db2
904} {1 2 3}
905do_test pragma-8.1.8 {
906  execsql {
907    PRAGMA schema_version = 108;
908  }
909} {}
910do_test pragma-8.1.9 {
911  set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
912  sqlite3_step $::STMT
913} SQLITE_ERROR
914do_test pragma-8.1.10 {
915  sqlite3_finalize $::STMT
916} SQLITE_SCHEMA
917
918# Make sure the schema-version can be manipulated in an attached database.
919forcedelete test2.db
920forcedelete test2.db-journal
921ifcapable attach {
922  do_test pragma-8.1.11 {
923    execsql {
924      ATTACH 'test2.db' AS aux;
925      CREATE TABLE aux.t1(a, b, c);
926      PRAGMA aux.schema_version = 205;
927    }
928  } {}
929  do_test pragma-8.1.12 {
930    execsql {
931      PRAGMA aux.schema_version;
932    }
933  } 205
934}
935do_test pragma-8.1.13 {
936  execsql {
937    PRAGMA schema_version;
938  }
939} 108
940
941# And check that modifying the schema-version in an attached database
942# forces the second connection to reload the schema.
943ifcapable attach {
944  do_test pragma-8.1.14 {
945    sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
946    execsql {
947      ATTACH 'test2.db' AS aux;
948      SELECT * FROM aux.t1;
949    } db2
950  } {}
951  do_test pragma-8.1.15 {
952    execsql {
953      PRAGMA aux.schema_version = 206;
954    }
955  } {}
956  do_test pragma-8.1.16 {
957    set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
958    sqlite3_step $::STMT
959  } SQLITE_ERROR
960  do_test pragma-8.1.17 {
961    sqlite3_finalize $::STMT
962  } SQLITE_SCHEMA
963  do_test pragma-8.1.18 {
964    db2 close
965  } {}
966}
967
968# Now test that the user-version can be read and written (and that we aren't
969# accidentally manipulating the schema-version instead).
970do_test pragma-8.2.1 {
971  execsql2 {
972    PRAGMA user_version;
973  }
974} {user_version 0}
975do_test pragma-8.2.2 {
976  execsql {
977    PRAGMA user_version = 2;
978  }
979} {}
980do_test pragma-8.2.3.1 {
981  execsql2 {
982    PRAGMA user_version;
983  }
984} {user_version 2}
985do_test pragma-8.2.3.2 {
986  db close
987  sqlite3 db test.db
988  execsql {
989    PRAGMA user_version;
990  }
991} {2}
992do_test pragma-8.2.4.1 {
993  execsql {
994    PRAGMA schema_version;
995  }
996} {108}
997ifcapable vacuum {
998  do_test pragma-8.2.4.2 {
999    execsql {
1000      VACUUM;
1001      PRAGMA user_version;
1002    }
1003  } {2}
1004  do_test pragma-8.2.4.3 {
1005    execsql {
1006      PRAGMA schema_version;
1007    }
1008  } {109}
1009}
1010
1011ifcapable attach {
1012  db eval {ATTACH 'test2.db' AS aux}
1013
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 {
1017    execsql {
1018      PRAGMA aux.user_version;
1019    }
1020  } {0}
1021  do_test pragma-8.2.6 {
1022    execsql {
1023      PRAGMA aux.user_version = 3;
1024    }
1025  } {}
1026  do_test pragma-8.2.7 {
1027    execsql {
1028      PRAGMA aux.user_version;
1029    }
1030  } {3}
1031  do_test pragma-8.2.8 {
1032    execsql {
1033      PRAGMA main.user_version;
1034    }
1035  } {2}
1036
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 {
1040    execsql {
1041      BEGIN;
1042      PRAGMA aux.user_version = 10;
1043      PRAGMA user_version = 11;
1044    }
1045  } {}
1046  do_test pragma-8.2.10 {
1047    execsql {
1048      PRAGMA aux.user_version;
1049    }
1050  } {10}
1051  do_test pragma-8.2.11 {
1052    execsql {
1053      PRAGMA main.user_version;
1054    }
1055  } {11}
1056  do_test pragma-8.2.12 {
1057    execsql {
1058      ROLLBACK;
1059      PRAGMA aux.user_version;
1060    }
1061  } {3}
1062  do_test pragma-8.2.13 {
1063    execsql {
1064      PRAGMA main.user_version;
1065    }
1066  } {2}
1067}
1068
1069# Try a negative value for the user-version
1070do_test pragma-8.2.14 {
1071  execsql {
1072    PRAGMA user_version = -450;
1073  }
1074} {}
1075do_test pragma-8.2.15 {
1076  execsql {
1077    PRAGMA user_version;
1078  }
1079} {-450}
1080} ; # ifcapable schema_version
1081
1082# Check to see if TEMP_STORE is memory or disk.  Return strings
1083# "memory" or "disk" as appropriate.
1084#
1085proc check_temp_store {} {
1086  db eval {
1087    PRAGMA temp.cache_size = 1;
1088    CREATE TEMP TABLE IF NOT EXISTS a(b);
1089    DELETE FROM a;
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;
1099  }
1100  db eval {PRAGMA database_list} {
1101    if {$name=="temp"} {
1102      set bt [btree_from_db db 1]
1103      if {[btree_ismemdb $bt]} {
1104        return "memory"
1105      }
1106      return "disk"
1107    }
1108  }
1109  return "unknown"
1110}
1111
1112# Application_ID
1113#
1114do_test pragma-8.3.1 {
1115  execsql {
1116    PRAGMA application_id;
1117  }
1118} {0}
1119do_test pragma-8.3.2 {
1120  execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1121} {12345}
1122
1123# Test temp_store and temp_store_directory pragmas
1124#
1125ifcapable pager_pragmas {
1126do_test pragma-9.1 {
1127  db close
1128  sqlite3 db test.db
1129  execsql {
1130    PRAGMA temp_store;
1131  }
1132} {0}
1133if {$TEMP_STORE<=1} {
1134  do_test pragma-9.1.1 {
1135    check_temp_store
1136  } {disk}
1137} else {
1138  do_test pragma-9.1.1 {
1139    check_temp_store
1140  } {memory}
1141}
1142
1143do_test pragma-9.2 {
1144  db close
1145  sqlite3 db test.db
1146  execsql {
1147    PRAGMA temp_store=file;
1148    PRAGMA temp_store;
1149  }
1150} {1}
1151if {$TEMP_STORE==3} {
1152  # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1153  do_test pragma-9.2.1 {
1154    check_temp_store
1155  } {memory}
1156} else {
1157  do_test pragma-9.2.1 {
1158    check_temp_store
1159  } {disk}
1160}
1161
1162do_test pragma-9.3 {
1163  db close
1164  sqlite3 db test.db
1165  execsql {
1166    PRAGMA temp_store=memory;
1167    PRAGMA temp_store;
1168  }
1169} {2}
1170if {$TEMP_STORE==0} {
1171  # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1172  do_test pragma-9.3.1 {
1173    check_temp_store
1174  } {disk}
1175} else {
1176  do_test pragma-9.3.1 {
1177    check_temp_store
1178  } {memory}
1179}
1180
1181do_test pragma-9.4 {
1182  execsql {
1183    PRAGMA temp_store_directory;
1184  }
1185} {}
1186ifcapable wsd {
1187  do_test pragma-9.5 {
1188    set pwd [string map {' ''} [file nativename [get_pwd]]]
1189    execsql "
1190      PRAGMA temp_store_directory='$pwd';
1191    "
1192  } {}
1193  do_test pragma-9.6 {
1194    execsql {
1195      PRAGMA temp_store_directory;
1196    }
1197  } [list [file nativename [get_pwd]]]
1198  do_test pragma-9.7 {
1199    catchsql {
1200      PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1201    }
1202  } {1 {not a writable directory}}
1203  do_test pragma-9.8 {
1204    execsql {
1205      PRAGMA temp_store_directory='';
1206    }
1207  } {}
1208  if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1209    ifcapable tempdb {
1210      do_test pragma-9.9 {
1211        execsql {
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;
1217        }
1218      } {2}
1219      do_test pragma-9.10 {
1220        catchsql "
1221          PRAGMA temp_store_directory='$pwd';
1222          SELECT * FROM temp_store_directory_test;
1223        "
1224      } {1 {no such table: temp_store_directory_test}}
1225    }
1226  }
1227}
1228do_test pragma-9.11 {
1229  execsql {
1230    PRAGMA temp_store = 0;
1231    PRAGMA temp_store;
1232  }
1233} {0}
1234do_test pragma-9.12 {
1235  execsql {
1236    PRAGMA temp_store = 1;
1237    PRAGMA temp_store;
1238  }
1239} {1}
1240do_test pragma-9.13 {
1241  execsql {
1242    PRAGMA temp_store = 2;
1243    PRAGMA temp_store;
1244  }
1245} {2}
1246do_test pragma-9.14 {
1247  execsql {
1248    PRAGMA temp_store = 3;
1249    PRAGMA temp_store;
1250  }
1251} {0}
1252do_test pragma-9.15 {
1253  catchsql {
1254    BEGIN EXCLUSIVE;
1255    CREATE TEMP TABLE temp_table(t);
1256    INSERT INTO temp_table VALUES('valuable data');
1257    PRAGMA temp_store = 1;
1258  }
1259} {1 {temporary storage cannot be changed from within a transaction}}
1260do_test pragma-9.16 {
1261  execsql {
1262    SELECT * FROM temp_table;
1263    COMMIT;
1264  }
1265} {{valuable data}}
1266
1267do_test pragma-9.17 {
1268  execsql {
1269    INSERT INTO temp_table VALUES('valuable data II');
1270    SELECT * FROM temp_table;
1271  }
1272} {{valuable data} {valuable data II}}
1273
1274do_test pragma-9.18 {
1275  set rc [catch {
1276    db eval {SELECT t FROM temp_table} {
1277      execsql {pragma temp_store = 1}
1278    }
1279  } msg]
1280  list $rc $msg
1281} {1 {temporary storage cannot be changed from within a transaction}}
1282
1283} ;# ifcapable pager_pragmas
1284
1285ifcapable trigger {
1286
1287do_test pragma-10.0 {
1288  catchsql {
1289    DROP TABLE main.t1;
1290  }
1291  execsql {
1292    PRAGMA count_changes = 1;
1293
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);
1299    END;
1300    CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1301      INSERT INTO t1_mirror2 VALUES(new.a);
1302    END;
1303    CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1304      UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1305    END;
1306    CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1307      UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1308    END;
1309    CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1310      DELETE FROM t1_mirror WHERE a = old.a;
1311    END;
1312    CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1313      DELETE FROM t1_mirror2 WHERE a = old.a;
1314    END;
1315  }
1316} {}
1317
1318do_test pragma-10.1 {
1319  execsql {
1320    INSERT INTO t1 VALUES(randstr(10,10));
1321  }
1322} {1}
1323do_test pragma-10.2 {
1324  execsql {
1325    UPDATE t1 SET a = randstr(10,10);
1326  }
1327} {1}
1328do_test pragma-10.3 {
1329  execsql {
1330    DELETE FROM t1;
1331  }
1332} {1}
1333
1334} ;# ifcapable trigger
1335
1336ifcapable schema_pragmas {
1337  do_test pragma-11.1 {
1338    execsql2 {
1339      pragma collation_list;
1340    }
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...
1344    execsql {
1345      pragma collation_list;
1346    }
1347  } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
1348}
1349
1350ifcapable schema_pragmas&&tempdb {
1351  do_test pragma-12.1 {
1352    sqlite3 db2 test.db
1353    execsql {
1354      PRAGMA temp.table_info('abc');
1355    } db2
1356  } {}
1357  db2 close
1358
1359  do_test pragma-12.2 {
1360    sqlite3 db2 test.db
1361    execsql {
1362      PRAGMA temp.default_cache_size = 200;
1363      PRAGMA temp.default_cache_size;
1364    } db2
1365  } {200}
1366  db2 close
1367
1368  do_test pragma-12.3 {
1369    sqlite3 db2 test.db
1370    execsql {
1371      PRAGMA temp.cache_size = 400;
1372      PRAGMA temp.cache_size;
1373    } db2
1374  } {400}
1375  db2 close
1376}
1377
1378ifcapable bloblit {
1379
1380do_test pragma-13.1 {
1381  execsql {
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;
1393    SELECT * FROM t4;
1394  }
1395  execsql {
1396    PRAGMA vdbe_trace=off;
1397    PRAGMA vdbe_listing=off;
1398    PRAGMA sql_trace=off;
1399  }
1400} {}
1401
1402} ;# ifcapable bloblit
1403
1404ifcapable pager_pragmas {
1405  db close
1406  forcedelete test.db
1407  sqlite3 db test.db
1408
1409  # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1410  # number of pages in the database file.
1411  #
1412  do_test pragma-14.1 {
1413    execsql { pragma auto_vacuum = 0 }
1414    execsql { pragma page_count; pragma main.page_count }
1415  } {0 0}
1416
1417  do_test pragma-14.2 {
1418    execsql {
1419      CREATE TABLE abc(a, b, c);
1420      PRAGMA page_count;
1421      PRAGMA main.page_count;
1422      PRAGMA temp.page_count;
1423    }
1424  } {2 2 0}
1425  do_test pragma-14.2uc {
1426    execsql {pragma PAGE_COUNT}
1427  } {2}
1428
1429  do_test pragma-14.3 {
1430    execsql {
1431      BEGIN;
1432      CREATE TABLE def(a, b, c);
1433      PRAGMA page_count;
1434    }
1435  } {3}
1436  do_test pragma-14.3uc {
1437    execsql {pragma PAGE_COUNT}
1438  } {3}
1439
1440  do_test pragma-14.4 {
1441    set page_size [db one {pragma page_size}]
1442    expr [file size test.db] / $page_size
1443  } {2}
1444
1445  do_test pragma-14.5 {
1446    execsql {
1447      ROLLBACK;
1448      PRAGMA page_count;
1449    }
1450  } {2}
1451
1452  do_test pragma-14.6 {
1453    forcedelete test2.db
1454    sqlite3 db2 test2.db
1455    execsql {
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);
1461    } db2
1462    db2 close
1463    execsql {
1464      ATTACH 'test2.db' AS aux;
1465      PRAGMA aux.page_count;
1466    }
1467  } {5}
1468  do_test pragma-14.6uc {
1469    execsql {pragma AUX.PAGE_COUNT}
1470  } {5}
1471}
1472
1473# Test that the value set using the cache_size pragma is not reset when the
1474# schema is reloaded.
1475#
1476ifcapable pager_pragmas {
1477  db close
1478  sqlite3 db test.db
1479  do_test pragma-15.1 {
1480    execsql {
1481      PRAGMA cache_size=59;
1482      PRAGMA cache_size;
1483    }
1484  } {59}
1485  do_test pragma-15.2 {
1486    sqlite3 db2 test.db
1487    execsql {
1488      CREATE TABLE newtable(a, b, c);
1489    } db2
1490    db2 close
1491  } {}
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 }
1499  } {59}
1500}
1501
1502# Reset the sqlite3_temp_directory variable for the next run of tests:
1503sqlite3 dbX :memory:
1504dbX eval {PRAGMA temp_store_directory = ""}
1505dbX close
1506
1507ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1508  set sqlite_hostid_num 1
1509
1510  set using_proxy 0
1511  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1512    set using_proxy $value
1513  }
1514
1515  # Test the lock_proxy_file pragmas.
1516  #
1517  db close
1518  set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1519
1520  sqlite3 db test.db
1521  do_test pragma-16.1 {
1522    execsql {
1523      PRAGMA lock_proxy_file="mylittleproxy";
1524      select * from sqlite_master;
1525    }
1526    execsql {
1527      PRAGMA lock_proxy_file;
1528    }
1529  } {mylittleproxy}
1530
1531  do_test pragma-16.2 {
1532    sqlite3 db2 test.db
1533    execsql {
1534      PRAGMA lock_proxy_file="mylittleproxy";
1535    } db2
1536  } {}
1537
1538  db2 close
1539  do_test pragma-16.2.1 {
1540    sqlite3 db2 test.db
1541    execsql {
1542      PRAGMA lock_proxy_file=":auto:";
1543      select * from sqlite_master;
1544    } db2
1545    execsql {
1546      PRAGMA lock_proxy_file;
1547    } db2
1548  } {mylittleproxy}
1549
1550  db2 close
1551  do_test pragma-16.3 {
1552    sqlite3 db2 test.db
1553    execsql {
1554      PRAGMA lock_proxy_file="myotherproxy";
1555    } db2
1556    catchsql {
1557      select * from sqlite_master;
1558    } db2
1559  } {1 {database is locked}}
1560
1561  do_test pragma-16.4 {
1562    db2 close
1563    db close
1564    sqlite3 db2 test.db
1565    execsql {
1566      PRAGMA lock_proxy_file="myoriginalproxy";
1567      PRAGMA lock_proxy_file="myotherproxy";
1568      PRAGMA lock_proxy_file;
1569    } db2
1570  } {myotherproxy}
1571
1572  db2 close
1573  set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1574  do_test pragma-16.5 {
1575    sqlite3 db2 test.db
1576    execsql {
1577      PRAGMA lock_proxy_file=":auto:";
1578      PRAGMA lock_proxy_file;
1579    } db2
1580  } {myotherproxy}
1581
1582  do_test pragma-16.6 {
1583    db2 close
1584    sqlite3 db2 test2.db
1585    set lockpath [execsql {
1586      PRAGMA lock_proxy_file=":auto:";
1587      PRAGMA lock_proxy_file;
1588    } db2]
1589    string match "*test2.db:auto:" $lockpath
1590  } {1}
1591
1592  set sqlite_hostid_num 2
1593  do_test pragma-16.7 {
1594    list [catch {
1595      sqlite3 db test2.db
1596      execsql {
1597        PRAGMA lock_proxy_file=":auto:";
1598        select * from sqlite_master;
1599      }
1600    } msg] $msg
1601  } {1 {database is locked}}
1602  db close
1603
1604  do_test pragma-16.8 {
1605    list [catch {
1606      sqlite3 db test2.db
1607      execsql { select * from sqlite_master }
1608    } msg] $msg
1609  } {1 {database is locked}}
1610
1611  db2 close
1612  do_test pragma-16.8.1 {
1613    execsql {
1614      PRAGMA lock_proxy_file="yetanotherproxy";
1615      PRAGMA lock_proxy_file;
1616    }
1617  } {yetanotherproxy}
1618  do_test pragma-16.8.2 {
1619    execsql {
1620      create table mine(x);
1621    }
1622  } {}
1623
1624  db close
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;
1630    } db]
1631    string match "*proxytest.db:auto:" $lockpath2
1632  } {1}
1633
1634  set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1635  set sqlite_hostid_num 0
1636}
1637
1638# Parsing of auto_vacuum settings.
1639#
1640foreach {autovac_setting val} {
1641  0 0
1642  1 1
1643  2 2
1644  3 0
1645  -1 0
1646  none 0
1647  NONE 0
1648  NoNe 0
1649  full 1
1650  FULL 1
1651  incremental 2
1652  INCREMENTAL 2
1653  -1234 0
1654  1234 0
1655} {
1656  do_test pragma-17.1.$autovac_setting {
1657    catch {db close}
1658    sqlite3 db :memory:
1659    execsql "
1660      PRAGMA auto_vacuum=$::autovac_setting;
1661      PRAGMA auto_vacuum;
1662    "
1663  } $val
1664}
1665
1666# Parsing of temp_store settings.
1667#
1668foreach {temp_setting val} {
1669  0 0
1670  1 1
1671  2 2
1672  3 0
1673  -1 0
1674  file 1
1675  FILE 1
1676  fIlE 1
1677  memory 2
1678  MEMORY 2
1679  MeMoRy 2
1680} {
1681  do_test pragma-18.1.$temp_setting {
1682    catch {db close}
1683    sqlite3 db :memory:
1684    execsql "
1685      PRAGMA temp_store=$::temp_setting;
1686      PRAGMA temp_store=$::temp_setting;
1687      PRAGMA temp_store;
1688    "
1689  } $val
1690}
1691
1692# The SQLITE_FCNTL_PRAGMA logic, with error handling.
1693#
1694db close
1695testvfs tvfs
1696sqlite3 db test.db -vfs tvfs
1697do_test pragma-19.1 {
1698  catchsql {PRAGMA error}
1699} {1 {SQL logic error or missing database}}
1700do_test pragma-19.2 {
1701  catchsql {PRAGMA error='This is the error message'}
1702} {1 {This is the error message}}
1703do_test pragma-19.3 {
1704  catchsql {PRAGMA error='7 This is the error message'}
1705} {1 {This is the error message}}
1706do_test pragma-19.4 {
1707  catchsql {PRAGMA error=7}
1708} {1 {out of memory}}
1709do_test pragma-19.5 {
1710  file tail [lindex [execsql {PRAGMA filename}] 0]
1711} {test.db}
1712
1713if {$tcl_platform(platform)=="windows"} {
1714# Test data_store_directory pragma
1715#
1716db close
1717sqlite3 db test.db
1718file mkdir data_dir
1719do_test pragma-20.1 {
1720  catchsql {PRAGMA data_store_directory}
1721} {0 {}}
1722do_test pragma-20.2 {
1723  set pwd [string map {' ''} [file nativename [get_pwd]]]
1724  catchsql "PRAGMA data_store_directory='$pwd';"
1725} {0 {}}
1726do_test pragma-20.3 {
1727  catchsql {PRAGMA data_store_directory}
1728} [list 0 [list [file nativename [get_pwd]]]]
1729do_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';"
1733} {0 {}}
1734do_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]]]]
1739catch {db2 close}
1740do_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]]]]
1745catch {db2 close}
1746do_test pragma-20.7 {
1747  catchsql "PRAGMA data_store_directory='';"
1748} {0 {}}
1749do_test pragma-20.8 {
1750  catchsql {PRAGMA data_store_directory}
1751} {0 {}}
1752
1753forcedelete data_dir
1754} ;# endif windows
1755
1756database_may_be_corrupt
1757if {![nonzero_reserved_bytes]} {
1758
1759  do_test 21.1 {
1760    # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1761    #
1762    db close
1763    forcedelete test.db
1764    sqlite3 db test.db
1765    execsql {
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);
1770    }
1771    for {set i 0} {$i < 10} {incr i} {
1772      execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1773    }
1774    db close
1775    forcecopy test.db testerr.db
1776    hexio_write testerr.db 15000 [string repeat 55 100]
1777  } {100}
1778
1779  set mainerr {*** in database main ***
1780Multiple uses for byte 672 of page 15}
1781  set auxerr {*** in database aux ***
1782Multiple uses for byte 672 of page 15}
1783
1784  set mainerr {/{\*\*\* in database main \*\*\*
1785Multiple uses for byte 672 of page 15}.*/}
1786  set auxerr {/{\*\*\* in database aux \*\*\*
1787Multiple uses for byte 672 of page 15}.*/}
1788
1789  do_test 22.2 {
1790    catch { db close }
1791    sqlite3 db testerr.db
1792    execsql { PRAGMA integrity_check }
1793  } $mainerr
1794
1795  do_test 22.3.1 {
1796    catch { db close }
1797    sqlite3 db test.db
1798    execsql {
1799      ATTACH 'testerr.db' AS 'aux';
1800      PRAGMA integrity_check;
1801    }
1802  } $auxerr
1803  do_test 22.3.2 {
1804    execsql { PRAGMA main.integrity_check; }
1805  } {ok}
1806  do_test 22.3.3 {
1807    execsql { PRAGMA aux.integrity_check; }
1808  } $auxerr
1809
1810  do_test 22.4.1 {
1811    catch { db close }
1812    sqlite3 db testerr.db
1813    execsql {
1814      ATTACH 'test.db' AS 'aux';
1815      PRAGMA integrity_check;
1816    }
1817  } $mainerr
1818  do_test 22.4.2 {
1819    execsql { PRAGMA main.integrity_check; }
1820  } $mainerr
1821  do_test 22.4.3 {
1822    execsql { PRAGMA aux.integrity_check; }
1823  } {ok}
1824}
1825
1826db close
1827forcedelete test.db test.db-wal test.db-journal
1828sqlite3 db test.db
1829sqlite3 db2 test.db
1830do_test 23.1 {
1831  db eval {
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);
1837  }
1838  db2 eval {SELECT name FROM sqlite_master}
1839} {t1 i1 i2 i2x t2}
1840do_test 23.2a {
1841  db eval {
1842    DROP INDEX i2;
1843    CREATE INDEX i2 ON t1(c,d,b);
1844  }
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 |}
1848
1849# EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1850# pragma returns information about every column in an index.
1851#
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
1854# columns.
1855#
1856do_test 23.2b {
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 |}
1860
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.)
1864#
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
1868# being indexed.
1869#
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.
1873#
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.
1877#
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.
1881#
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.
1885#
1886do_test 23.2c {
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}
1889do_test 23.2d {
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}
1892
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.
1895#
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.
1899#
1900# (The second column of output from PRAGMA index_list is...)
1901# EVIDENCE-OF: R-35496-03635 The name of the index.
1902#
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.
1905#
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.
1910#
1911do_test 23.3 {
1912  db eval {
1913    CREATE INDEX i3 ON t1(d,b,c);
1914  }
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 |}
1918do_test 23.4 {
1919  db eval {
1920    ALTER TABLE t1 ADD COLUMN e;
1921  }
1922  db2 eval {
1923    PRAGMA table_info(t1);
1924  }
1925} {/4 e {} 0 {} 0/}
1926do_test 23.5 {
1927  db eval {
1928    DROP TABLE t2;
1929    CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1930  }
1931  db2 eval {
1932    PRAGMA foreign_key_list(t2);
1933  }
1934} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1935
1936database_never_corrupt
1937finish_test
1938