xref: /sqlite-3.40.0/test/pragma.test (revision efeaec36)
1cd61c281Sdrh# 2002 March 6
2cd61c281Sdrh#
3cd61c281Sdrh# The author disclaims copyright to this source code.  In place of
4cd61c281Sdrh# a legal notice, here is a blessing:
5cd61c281Sdrh#
6cd61c281Sdrh#    May you do good and not evil.
7cd61c281Sdrh#    May you find forgiveness for yourself and forgive others.
8cd61c281Sdrh#    May you share freely, never taking more than you give.
9cd61c281Sdrh#
10cd61c281Sdrh#***********************************************************************
11cd61c281Sdrh# This file implements regression tests for SQLite library.
12cd61c281Sdrh#
13cd61c281Sdrh# This file implements tests for the PRAGMA command.
14cd61c281Sdrh#
15838cce43Sdanielk1977# $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $
16cd61c281Sdrh
17cd61c281Sdrhset testdir [file dirname $argv0]
18cd61c281Sdrhsource $testdir/tester.tcl
195885e762Sdanset testprefix pragma
20cd61c281Sdrh
2168928b6cSdan# Do not use a codec for tests in this file, as the database file is
2268928b6cSdan# manipulated directly using tcl scripts (using the [hexio_write] command).
2368928b6cSdan#
2468928b6cSdando_not_use_codec
2568928b6cSdan
2691cf71b0Sdanielk1977# Test organization:
2791cf71b0Sdanielk1977#
2891cf71b0Sdanielk1977# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
2991cf71b0Sdanielk1977# pragma-2.*: Test synchronous on attached db.
3091cf71b0Sdanielk1977# pragma-3.*: Test detection of table/index inconsistency by integrity_check.
3191cf71b0Sdanielk1977# pragma-4.*: Test cache_size and default_cache_size on attached db.
3291cf71b0Sdanielk1977# pragma-5.*: Test that pragma synchronous may not be used inside of a
3391cf71b0Sdanielk1977#             transaction.
34cc6bd383Sdanielk1977# pragma-6.*: Test schema-query pragmas.
35cc6bd383Sdanielk1977# pragma-7.*: Miscellaneous tests.
36cc6bd383Sdanielk1977# pragma-8.*: Test user_version and schema_version pragmas.
379a09a3caStpoindex# pragma-9.*: Test temp_store and temp_store_directory.
38cc6bd383Sdanielk1977# pragma-10.*: Test the count_changes pragma in the presence of triggers.
3948af65aeSdanielk1977# pragma-11.*: Test the collation_list pragma.
4059a93791Sdanielk1977# pragma-14.*: Test the page_count pragma.
418cf6c554Sdanielk1977# pragma-15.*: Test that the value set using the cache_size pragma is not
428cf6c554Sdanielk1977#              reset when the schema is reloaded.
43aebf413dSaswift# pragma-16.*: Test proxy locking
44a112d140Smistachkin# pragma-20.*: Test data_store_directory.
455885e762Sdan# pragma-22.*: Test that "PRAGMA [db].integrity_check" respects the "db"
465885e762Sdan#              directive - if it is present.
4791cf71b0Sdanielk1977#
4891cf71b0Sdanielk1977
4905a82983Sdrhifcapable !pragma {
5005a82983Sdrh  finish_test
5105a82983Sdrh  return
5205a82983Sdrh}
5305a82983Sdrh
54c228be5bSdrh# Capture the output of a pragma in a TEMP table.
55c228be5bSdrh#
56c228be5bSdrhproc capture_pragma {db tabname sql} {
57c228be5bSdrh  $db eval "DROP TABLE IF EXISTS temp.$tabname"
58c228be5bSdrh  set once 1
59c228be5bSdrh  $db eval $sql x {
60c228be5bSdrh    if {$once} {
61c228be5bSdrh      set once 0
62c228be5bSdrh      set ins "INSERT INTO $tabname VALUES"
63c228be5bSdrh      set crtab "CREATE TEMP TABLE $tabname "
64c228be5bSdrh      set sep "("
65c228be5bSdrh      foreach col $x(*) {
66c228be5bSdrh        append ins ${sep}\$x($col)
67c228be5bSdrh        append crtab ${sep}\"$col\"
68c228be5bSdrh        set sep ,
69c228be5bSdrh      }
70c228be5bSdrh      append ins )
71c228be5bSdrh      append crtab )
72c228be5bSdrh      $db eval $crtab
73c228be5bSdrh    }
74c228be5bSdrh    $db eval $ins
75c228be5bSdrh  }
76c228be5bSdrh}
77c228be5bSdrh
78cd61c281Sdrh# Delete the preexisting database to avoid the special setup
79cd61c281Sdrh# that the "all.test" script does.
80cd61c281Sdrh#
81cd61c281Sdrhdb close
82fda06befSmistachkindelete_file test.db test.db-journal
83fda06befSmistachkindelete_file test3.db test3.db-journal
84dddca286Sdrhsqlite3 db test.db; set DB [sqlite3_connection_pointer db]
85cd61c281Sdrh
86b3366b99Sdrh# EVIDENCE-OF: R-13861-56665 PRAGMA schema.cache_size; PRAGMA
87b3366b99Sdrh# schema.cache_size = pages; PRAGMA schema.cache_size = -kibibytes;
889d356fbeSdrh# Query or change the suggested maximum number of database disk pages
899d356fbeSdrh# that SQLite will hold in memory at once per open database file.
909d356fbeSdrh#
91c7b4a44cSdanielk1977ifcapable pager_pragmas {
921e9daa6aSdrhset DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
931e9daa6aSdrhset TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
94cd61c281Sdrhdo_test pragma-1.1 {
95cd61c281Sdrh  execsql {
96cd61c281Sdrh    PRAGMA cache_size;
97cd61c281Sdrh    PRAGMA default_cache_size;
98cd61c281Sdrh    PRAGMA synchronous;
99cd61c281Sdrh  }
1001e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
101cd61c281Sdrhdo_test pragma-1.2 {
1029d356fbeSdrh  # EVIDENCE-OF: R-42059-47211 If the argument N is positive then the
1039d356fbeSdrh  # suggested cache size is set to N.
104cd61c281Sdrh  execsql {
105eb43e5c7Sdrh    PRAGMA synchronous=OFF;
106cd61c281Sdrh    PRAGMA cache_size=1234;
107cd61c281Sdrh    PRAGMA cache_size;
108cd61c281Sdrh    PRAGMA default_cache_size;
109cd61c281Sdrh    PRAGMA synchronous;
110cd61c281Sdrh  }
1111e9daa6aSdrh} [list 1234 $DFLT_CACHE_SZ 0]
112cd61c281Sdrhdo_test pragma-1.3 {
113cd61c281Sdrh  db close
114ef4ac8f9Sdrh  sqlite3 db test.db
115cd61c281Sdrh  execsql {
116cd61c281Sdrh    PRAGMA cache_size;
117cd61c281Sdrh    PRAGMA default_cache_size;
118cd61c281Sdrh    PRAGMA synchronous;
119cd61c281Sdrh  }
1201e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
121cd61c281Sdrhdo_test pragma-1.4 {
122cd61c281Sdrh  execsql {
123cd61c281Sdrh    PRAGMA synchronous=OFF;
124cd61c281Sdrh    PRAGMA cache_size;
125cd61c281Sdrh    PRAGMA default_cache_size;
126cd61c281Sdrh    PRAGMA synchronous;
127cd61c281Sdrh  }
1281e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
129cd61c281Sdrhdo_test pragma-1.5 {
130cd61c281Sdrh  execsql {
131d2cb50b7Sdrh    PRAGMA cache_size=-4321;
132cd61c281Sdrh    PRAGMA cache_size;
133cd61c281Sdrh    PRAGMA default_cache_size;
134cd61c281Sdrh    PRAGMA synchronous;
135cd61c281Sdrh  }
1363b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 0]
137cd61c281Sdrhdo_test pragma-1.6 {
138cd61c281Sdrh  execsql {
139cd61c281Sdrh    PRAGMA synchronous=ON;
140cd61c281Sdrh    PRAGMA cache_size;
141cd61c281Sdrh    PRAGMA default_cache_size;
142cd61c281Sdrh    PRAGMA synchronous;
143cd61c281Sdrh  }
1443b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 1]
145cd61c281Sdrhdo_test pragma-1.7 {
146cd61c281Sdrh  db close
147ef4ac8f9Sdrh  sqlite3 db test.db
148cd61c281Sdrh  execsql {
149cd61c281Sdrh    PRAGMA cache_size;
150cd61c281Sdrh    PRAGMA default_cache_size;
151cd61c281Sdrh    PRAGMA synchronous;
152cd61c281Sdrh  }
1531e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
154cd61c281Sdrhdo_test pragma-1.8 {
155cd61c281Sdrh  execsql {
156d2cb50b7Sdrh    PRAGMA default_cache_size=-123;
157cd61c281Sdrh    PRAGMA cache_size;
158cd61c281Sdrh    PRAGMA default_cache_size;
159cd61c281Sdrh    PRAGMA synchronous;
160cd61c281Sdrh  }
16191cf71b0Sdanielk1977} {123 123 2}
162802d69a7Sdrhdo_test pragma-1.9.1 {
163cd61c281Sdrh  db close
164dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
165cd61c281Sdrh  execsql {
166cd61c281Sdrh    PRAGMA cache_size;
167cd61c281Sdrh    PRAGMA default_cache_size;
168cd61c281Sdrh    PRAGMA synchronous;
169cd61c281Sdrh  }
17091cf71b0Sdanielk1977} {123 123 2}
171802d69a7Sdrhifcapable vacuum {
172802d69a7Sdrh  do_test pragma-1.9.2 {
173802d69a7Sdrh    execsql {
174802d69a7Sdrh      VACUUM;
175802d69a7Sdrh      PRAGMA cache_size;
176802d69a7Sdrh      PRAGMA default_cache_size;
177802d69a7Sdrh      PRAGMA synchronous;
178802d69a7Sdrh    }
179802d69a7Sdrh  } {123 123 2}
180802d69a7Sdrh}
18191cf71b0Sdanielk1977do_test pragma-1.10 {
1825a38705eSdrh  execsql {
1834303feedSdrh    PRAGMA synchronous=NORMAL;
1844303feedSdrh    PRAGMA cache_size;
1854303feedSdrh    PRAGMA default_cache_size;
1864303feedSdrh    PRAGMA synchronous;
1874303feedSdrh  }
18891cf71b0Sdanielk1977} {123 123 1}
1896841b1cbSdrhdo_test pragma-1.11.1 {
1906841b1cbSdrh  execsql {
1916841b1cbSdrh    PRAGMA synchronous=EXTRA;
1926841b1cbSdrh    PRAGMA cache_size;
1936841b1cbSdrh    PRAGMA default_cache_size;
1946841b1cbSdrh    PRAGMA synchronous;
1956841b1cbSdrh  }
1966841b1cbSdrh} {123 123 3}
1976841b1cbSdrhdo_test pragma-1.11.2 {
1984303feedSdrh  execsql {
1994303feedSdrh    PRAGMA synchronous=FULL;
2004303feedSdrh    PRAGMA cache_size;
2014303feedSdrh    PRAGMA default_cache_size;
2024303feedSdrh    PRAGMA synchronous;
2034303feedSdrh  }
20491cf71b0Sdanielk1977} {123 123 2}
20591cf71b0Sdanielk1977do_test pragma-1.12 {
2064303feedSdrh  db close
207dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
2084303feedSdrh  execsql {
2094303feedSdrh    PRAGMA cache_size;
2104303feedSdrh    PRAGMA default_cache_size;
2114303feedSdrh    PRAGMA synchronous;
2124303feedSdrh  }
21391cf71b0Sdanielk1977} {123 123 2}
21491cf71b0Sdanielk1977
2155260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition
2165260f7e9Sdrh# to keywords like "off" and "full".
2175260f7e9Sdrh#
2185260f7e9Sdrhdo_test pragma-1.13 {
2195260f7e9Sdrh  execsql {
2205260f7e9Sdrh    PRAGMA synchronous=0;
2215260f7e9Sdrh    PRAGMA synchronous;
2225260f7e9Sdrh  }
2235260f7e9Sdrh} {0}
2245260f7e9Sdrhdo_test pragma-1.14 {
2255260f7e9Sdrh  execsql {
2265260f7e9Sdrh    PRAGMA synchronous=2;
2275260f7e9Sdrh    PRAGMA synchronous;
2285260f7e9Sdrh  }
2295260f7e9Sdrh} {2}
23059ac655dSdrhdo_test pragma-1.14.1 {
23159ac655dSdrh  execsql {
23259ac655dSdrh    PRAGMA synchronous=4;
23359ac655dSdrh    PRAGMA synchronous;
23459ac655dSdrh  }
2356841b1cbSdrh} {4}
23659ac655dSdrhdo_test pragma-1.14.2 {
23759ac655dSdrh  execsql {
238d99d2836Sdrh    PRAGMA synchronous=3;
239d99d2836Sdrh    PRAGMA synchronous;
240d99d2836Sdrh  }
2416841b1cbSdrh} {3}
242d99d2836Sdrhdo_test pragma-1.14.3 {
243d99d2836Sdrh  execsql {
2446841b1cbSdrh    PRAGMA synchronous=8;
2456841b1cbSdrh    PRAGMA synchronous;
2466841b1cbSdrh  }
2476841b1cbSdrh} {0}
2486841b1cbSdrhdo_test pragma-1.14.4 {
2496841b1cbSdrh  execsql {
25059ac655dSdrh    PRAGMA synchronous=10;
25159ac655dSdrh    PRAGMA synchronous;
25259ac655dSdrh  }
25359ac655dSdrh} {2}
254c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
2555260f7e9Sdrh
2565260f7e9Sdrh# Test turning "flag" pragmas on and off.
2575260f7e9Sdrh#
2586338c762Sdanielk1977ifcapable debug {
2596338c762Sdanielk1977  # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
2606338c762Sdanielk1977  #
2615260f7e9Sdrh  do_test pragma-1.15 {
2625260f7e9Sdrh    execsql {
2635260f7e9Sdrh      PRAGMA vdbe_listing=YES;
2645260f7e9Sdrh      PRAGMA vdbe_listing;
2655260f7e9Sdrh    }
2665260f7e9Sdrh  } {1}
2675260f7e9Sdrh  do_test pragma-1.16 {
2685260f7e9Sdrh    execsql {
2695260f7e9Sdrh      PRAGMA vdbe_listing=NO;
2705260f7e9Sdrh      PRAGMA vdbe_listing;
2715260f7e9Sdrh    }
2725260f7e9Sdrh  } {0}
2736338c762Sdanielk1977}
2746338c762Sdanielk1977
2755260f7e9Sdrhdo_test pragma-1.17 {
2765260f7e9Sdrh  execsql {
2775260f7e9Sdrh    PRAGMA parser_trace=ON;
2785260f7e9Sdrh    PRAGMA parser_trace=OFF;
2795260f7e9Sdrh  }
2805260f7e9Sdrh} {}
2815260f7e9Sdrhdo_test pragma-1.18 {
2825260f7e9Sdrh  execsql {
2835260f7e9Sdrh    PRAGMA bogus = -1234;  -- Parsing of negative values
2845260f7e9Sdrh  }
2855260f7e9Sdrh} {}
2865260f7e9Sdrh
28791cf71b0Sdanielk1977# Test modifying the safety_level of an attached database.
2885a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
28991cf71b0Sdanielk1977  do_test pragma-2.1 {
290fda06befSmistachkin    forcedelete test2.db
291fda06befSmistachkin    forcedelete test2.db-journal
2924303feedSdrh    execsql {
29391cf71b0Sdanielk1977      ATTACH 'test2.db' AS aux;
2944303feedSdrh    }
29591cf71b0Sdanielk1977  } {}
29691cf71b0Sdanielk1977  do_test pragma-2.2 {
2974303feedSdrh    execsql {
29891cf71b0Sdanielk1977      pragma aux.synchronous;
2994303feedSdrh    }
30091cf71b0Sdanielk1977  } {2}
30191cf71b0Sdanielk1977  do_test pragma-2.3 {
30291cf71b0Sdanielk1977    execsql {
30391cf71b0Sdanielk1977      pragma aux.synchronous = OFF;
30491cf71b0Sdanielk1977      pragma aux.synchronous;
30591cf71b0Sdanielk1977      pragma synchronous;
30691cf71b0Sdanielk1977    }
30791cf71b0Sdanielk1977  } {0 2}
30891cf71b0Sdanielk1977  do_test pragma-2.4 {
30991cf71b0Sdanielk1977    execsql {
31091cf71b0Sdanielk1977      pragma aux.synchronous = ON;
31191cf71b0Sdanielk1977      pragma synchronous;
31291cf71b0Sdanielk1977      pragma aux.synchronous;
31391cf71b0Sdanielk1977    }
31491cf71b0Sdanielk1977  } {2 1}
315c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
3164303feedSdrh
317ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check
318ed717fe3Sdrh# pragma finds it.
319ed717fe3Sdrh#
32025d6543dSdrh# These tests won't work if the database is encrypted
32125d6543dSdrh#
322ed717fe3Sdrhdo_test pragma-3.1 {
3231e9daa6aSdrh  db close
324fda06befSmistachkin  forcedelete test.db test.db-journal
3251e9daa6aSdrh  sqlite3 db test.db
326ed717fe3Sdrh  execsql {
3271e9daa6aSdrh    PRAGMA auto_vacuum=OFF;
328ed717fe3Sdrh    BEGIN;
329ed717fe3Sdrh    CREATE TABLE t2(a,b,c);
330ed717fe3Sdrh    CREATE INDEX i2 ON t2(a);
331ed717fe3Sdrh    INSERT INTO t2 VALUES(11,2,3);
332ed717fe3Sdrh    INSERT INTO t2 VALUES(22,3,4);
333ed717fe3Sdrh    COMMIT;
334ed717fe3Sdrh    SELECT rowid, * from t2;
335ed717fe3Sdrh  }
336ed717fe3Sdrh} {1 11 2 3 2 22 3 4}
3375a8f9374Sdanielk1977ifcapable attach {
33840e016e4Sdrh  if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
339ed717fe3Sdrh    do_test pragma-3.2 {
340bb8a279eSdrh      db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
341bb8a279eSdrh      set pgsz [db eval {PRAGMA page_size}]
342bb8a279eSdrh      # overwrite the header on the rootpage of the index in order to
343bb8a279eSdrh      # make the index appear to be empty.
344bb8a279eSdrh      #
345bb8a279eSdrh      set offset [expr {$pgsz*($rootpage-1)}]
346bb8a279eSdrh      hexio_write test.db $offset 0a00000000040000000000
347bb8a279eSdrh      db close
348bb8a279eSdrh      sqlite3 db test.db
349ed717fe3Sdrh      execsql {PRAGMA integrity_check}
3506fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
35191cf71b0Sdanielk1977    do_test pragma-3.3 {
3521dcdbc06Sdrh      execsql {PRAGMA integrity_check=1}
3536fbe41acSdrh    } {{row 1 missing from index i2}}
3541dcdbc06Sdrh    do_test pragma-3.4 {
35591cf71b0Sdanielk1977      execsql {
3561dcdbc06Sdrh        ATTACH DATABASE 'test.db' AS t2;
3571dcdbc06Sdrh        PRAGMA integrity_check
35891cf71b0Sdanielk1977      }
3596fbe41acSdrh    } {{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}}
3601dcdbc06Sdrh    do_test pragma-3.5 {
3611dcdbc06Sdrh      execsql {
362bb8a279eSdrh        PRAGMA integrity_check=4
3631dcdbc06Sdrh      }
3646fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
3651dcdbc06Sdrh    do_test pragma-3.6 {
3661dcdbc06Sdrh      execsql {
3671dcdbc06Sdrh        PRAGMA integrity_check=xyz
3681dcdbc06Sdrh      }
3696fbe41acSdrh    } {{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}}
3701dcdbc06Sdrh    do_test pragma-3.7 {
3711dcdbc06Sdrh      execsql {
3721dcdbc06Sdrh        PRAGMA integrity_check=0
3731dcdbc06Sdrh      }
3746fbe41acSdrh    } {{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}}
3751dcdbc06Sdrh
3761dcdbc06Sdrh    # Add additional corruption by appending unused pages to the end of
3771dcdbc06Sdrh    # the database file testerr.db
3781dcdbc06Sdrh    #
3791dcdbc06Sdrh    do_test pragma-3.8 {
3801dcdbc06Sdrh      execsql {DETACH t2}
381fda06befSmistachkin      forcedelete testerr.db testerr.db-journal
382e3d559adSdrh      set out [open testerr.db w]
383e3d559adSdrh      fconfigure $out -translation binary
384e3d559adSdrh      set in [open test.db r]
385e3d559adSdrh      fconfigure $in -translation binary
3861dcdbc06Sdrh      puts -nonewline $out [read $in]
3871dcdbc06Sdrh      seek $in 0
3881dcdbc06Sdrh      puts -nonewline $out [read $in]
3891dcdbc06Sdrh      close $in
3901dcdbc06Sdrh      close $out
391dd3cd977Sdrh      hexio_write testerr.db 28 00000000
3921dcdbc06Sdrh      execsql {REINDEX t2}
3931dcdbc06Sdrh      execsql {PRAGMA integrity_check}
3941dcdbc06Sdrh    } {ok}
39541c58b78Sdanielk1977    do_test pragma-3.8.1 {
39641c58b78Sdanielk1977      execsql {PRAGMA quick_check}
39741c58b78Sdanielk1977    } {ok}
3985d16a9a6Sdrh    do_test pragma-3.8.2 {
3995d16a9a6Sdrh      execsql {PRAGMA QUICK_CHECK}
4005d16a9a6Sdrh    } {ok}
4011dcdbc06Sdrh    do_test pragma-3.9 {
4021dcdbc06Sdrh      execsql {
4031dcdbc06Sdrh        ATTACH 'testerr.db' AS t2;
4041dcdbc06Sdrh        PRAGMA integrity_check
4051dcdbc06Sdrh      }
4061dcdbc06Sdrh    } {{*** in database t2 ***
4071dcdbc06SdrhPage 4 is never used
4081dcdbc06SdrhPage 5 is never used
4096fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
4101dcdbc06Sdrh    do_test pragma-3.10 {
4111dcdbc06Sdrh      execsql {
4121dcdbc06Sdrh        PRAGMA integrity_check=1
4131dcdbc06Sdrh      }
4141dcdbc06Sdrh    } {{*** in database t2 ***
4151dcdbc06SdrhPage 4 is never used}}
4161dcdbc06Sdrh    do_test pragma-3.11 {
4171dcdbc06Sdrh      execsql {
4181dcdbc06Sdrh        PRAGMA integrity_check=5
4191dcdbc06Sdrh      }
4201dcdbc06Sdrh    } {{*** in database t2 ***
4211dcdbc06SdrhPage 4 is never used
4221dcdbc06SdrhPage 5 is never used
4236fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
4241dcdbc06Sdrh    do_test pragma-3.12 {
4251dcdbc06Sdrh      execsql {
4261dcdbc06Sdrh        PRAGMA integrity_check=4
4271dcdbc06Sdrh      }
4281dcdbc06Sdrh    } {{*** in database t2 ***
4291dcdbc06SdrhPage 4 is never used
4301dcdbc06SdrhPage 5 is never used
4316fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
4321dcdbc06Sdrh    do_test pragma-3.13 {
4331dcdbc06Sdrh      execsql {
4341dcdbc06Sdrh        PRAGMA integrity_check=3
4351dcdbc06Sdrh      }
4361dcdbc06Sdrh    } {{*** in database t2 ***
4371dcdbc06SdrhPage 4 is never used
4381dcdbc06SdrhPage 5 is never used
4391dcdbc06SdrhPage 6 is never used}}
4401dcdbc06Sdrh    do_test pragma-3.14 {
4411dcdbc06Sdrh      execsql {
4421dcdbc06Sdrh        PRAGMA integrity_check(2)
4431dcdbc06Sdrh      }
4441dcdbc06Sdrh    } {{*** in database t2 ***
4451dcdbc06SdrhPage 4 is never used
4461dcdbc06SdrhPage 5 is never used}}
4471dcdbc06Sdrh    do_test pragma-3.15 {
4481dcdbc06Sdrh      execsql {
4491dcdbc06Sdrh        ATTACH 'testerr.db' AS t3;
4501dcdbc06Sdrh        PRAGMA integrity_check
4511dcdbc06Sdrh      }
4521dcdbc06Sdrh    } {{*** in database t2 ***
4531dcdbc06SdrhPage 4 is never used
4541dcdbc06SdrhPage 5 is never used
4556fbe41acSdrhPage 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 ***
4561dcdbc06SdrhPage 4 is never used
4571dcdbc06SdrhPage 5 is never used
4586fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
4591dcdbc06Sdrh    do_test pragma-3.16 {
4601dcdbc06Sdrh      execsql {
461bb8a279eSdrh        PRAGMA integrity_check(10)
4621dcdbc06Sdrh      }
4631dcdbc06Sdrh    } {{*** in database t2 ***
4641dcdbc06SdrhPage 4 is never used
4651dcdbc06SdrhPage 5 is never used
4666fbe41acSdrhPage 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 ***
4671dcdbc06SdrhPage 4 is never used
4681dcdbc06SdrhPage 5 is never used
4696fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
4701dcdbc06Sdrh    do_test pragma-3.17 {
4711dcdbc06Sdrh      execsql {
472bb8a279eSdrh        PRAGMA integrity_check=8
4731dcdbc06Sdrh      }
4741dcdbc06Sdrh    } {{*** in database t2 ***
4751dcdbc06SdrhPage 4 is never used
4761dcdbc06SdrhPage 5 is never used
4776fbe41acSdrhPage 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 ***
4781dcdbc06SdrhPage 4 is never used
4791dcdbc06SdrhPage 5 is never used}}
4801dcdbc06Sdrh    do_test pragma-3.18 {
4811dcdbc06Sdrh      execsql {
4821dcdbc06Sdrh        PRAGMA integrity_check=4
4831dcdbc06Sdrh      }
4841dcdbc06Sdrh    } {{*** in database t2 ***
4851dcdbc06SdrhPage 4 is never used
4861dcdbc06SdrhPage 5 is never used
4876fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
4881dcdbc06Sdrh  }
489d2cb50b7Sdrh  do_test pragma-3.19 {
490d2cb50b7Sdrh    catch {db close}
491fda06befSmistachkin    forcedelete test.db test.db-journal
492d2cb50b7Sdrh    sqlite3 db test.db
493d2cb50b7Sdrh    db eval {PRAGMA integrity_check}
494d2cb50b7Sdrh  } {ok}
4955a8f9374Sdanielk1977}
496cefc87fcSdrh
497cefc87fcSdrh# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
498cefc87fcSdrh# constraint violations.
499cefc87fcSdrh#
500cefc87fcSdrhdo_execsql_test pragma-3.20 {
501cefc87fcSdrh  CREATE TABLE t1(a,b);
502cefc87fcSdrh  CREATE INDEX t1a ON t1(a);
503cefc87fcSdrh  INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
504cefc87fcSdrh  PRAGMA writable_schema=ON;
505cefc87fcSdrh  UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
506cefc87fcSdrh   WHERE name='t1a';
507cefc87fcSdrh  UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
508cefc87fcSdrh   WHERE name='t1';
509cefc87fcSdrh  PRAGMA writable_schema=OFF;
510cefc87fcSdrh  ALTER TABLE t1 RENAME TO t1x;
511cefc87fcSdrh  PRAGMA integrity_check;
512cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
513cefc87fcSdrhdo_execsql_test pragma-3.21 {
514cefc87fcSdrh  PRAGMA integrity_check(3);
515cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
516cefc87fcSdrhdo_execsql_test pragma-3.22 {
517cefc87fcSdrh  PRAGMA integrity_check(2);
518cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a}}
5197efa426eSdrhdo_execsql_test pragma-3.23 {
520cefc87fcSdrh  PRAGMA integrity_check(1);
521cefc87fcSdrh} {{non-unique entry in index t1a}}
52291cf71b0Sdanielk1977
5237efa426eSdrh# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
5247efa426eSdrh# interface) used to leave index cursors in an inconsistent state
5257efa426eSdrh# which could result in an assertion fault in sqlite3BtreeKey()
5267efa426eSdrh# called from saveCursorPosition() if content is removed from the
5277efa426eSdrh# index while the integrity_check is still running.  This test verifies
5287efa426eSdrh# that problem has been fixed.
5297efa426eSdrh#
5307efa426eSdrhdo_test pragma-3.30 {
5317efa426eSdrh  db close
5327efa426eSdrh  delete_file test.db
5337efa426eSdrh  sqlite3 db test.db
5347efa426eSdrh  db eval {
5357efa426eSdrh    CREATE TABLE t1(a,b,c);
5367efa426eSdrh    WITH RECURSIVE
5377efa426eSdrh      c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
5387efa426eSdrh    INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
5397efa426eSdrh    CREATE INDEX t1a ON t1(a);
5407efa426eSdrh    CREATE INDEX t1bc ON t1(b,c);
5417efa426eSdrh  }
5427efa426eSdrh  db eval {PRAGMA integrity_check} {
5437efa426eSdrh     db eval {DELETE FROM t1}
5447efa426eSdrh  }
5457efa426eSdrh} {}
5467efa426eSdrh
54791cf71b0Sdanielk1977# Test modifying the cache_size of an attached database.
5485a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
54991cf71b0Sdanielk1977do_test pragma-4.1 {
55091cf71b0Sdanielk1977  execsql {
5511e9daa6aSdrh    ATTACH 'test2.db' AS aux;
55291cf71b0Sdanielk1977    pragma aux.cache_size;
55391cf71b0Sdanielk1977    pragma aux.default_cache_size;
55491cf71b0Sdanielk1977  }
5551e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5561bdd9b57Sdrhdo_test pragma-4.2 {
5571bdd9b57Sdrh  execsql {
55891cf71b0Sdanielk1977    pragma aux.cache_size = 50;
55991cf71b0Sdanielk1977    pragma aux.cache_size;
56091cf71b0Sdanielk1977    pragma aux.default_cache_size;
5611bdd9b57Sdrh  }
5621e9daa6aSdrh} [list 50 $DFLT_CACHE_SZ]
5631bdd9b57Sdrhdo_test pragma-4.3 {
5641bdd9b57Sdrh  execsql {
56591cf71b0Sdanielk1977    pragma aux.default_cache_size = 456;
56691cf71b0Sdanielk1977    pragma aux.cache_size;
56791cf71b0Sdanielk1977    pragma aux.default_cache_size;
5681bdd9b57Sdrh  }
56991cf71b0Sdanielk1977} {456 456}
5701bdd9b57Sdrhdo_test pragma-4.4 {
5711bdd9b57Sdrh  execsql {
57291cf71b0Sdanielk1977    pragma cache_size;
57391cf71b0Sdanielk1977    pragma default_cache_size;
5741bdd9b57Sdrh  }
5751e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5761bdd9b57Sdrhdo_test pragma-4.5 {
5771bdd9b57Sdrh  execsql {
57891cf71b0Sdanielk1977    DETACH aux;
57991cf71b0Sdanielk1977    ATTACH 'test3.db' AS aux;
58091cf71b0Sdanielk1977    pragma aux.cache_size;
58191cf71b0Sdanielk1977    pragma aux.default_cache_size;
5821bdd9b57Sdrh  }
5831e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5841bdd9b57Sdrhdo_test pragma-4.6 {
5851bdd9b57Sdrh  execsql {
58691cf71b0Sdanielk1977    DETACH aux;
58791cf71b0Sdanielk1977    ATTACH 'test2.db' AS aux;
58891cf71b0Sdanielk1977    pragma aux.cache_size;
58991cf71b0Sdanielk1977    pragma aux.default_cache_size;
5901bdd9b57Sdrh  }
59191cf71b0Sdanielk1977} {456 456}
592c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
5931bdd9b57Sdrh
59491cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is
59591cf71b0Sdanielk1977# disallowed.
596c7b4a44cSdanielk1977ifcapable pager_pragmas {
59791cf71b0Sdanielk1977do_test pragma-5.0 {
5981bdd9b57Sdrh  execsql {
59991cf71b0Sdanielk1977    pragma synchronous;
6001bdd9b57Sdrh  }
60191cf71b0Sdanielk1977} {2}
60291cf71b0Sdanielk1977do_test pragma-5.1 {
6031bdd9b57Sdrh  catchsql {
6041bdd9b57Sdrh    BEGIN;
60591cf71b0Sdanielk1977    pragma synchronous = OFF;
6061bdd9b57Sdrh  }
60791cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}}
60891cf71b0Sdanielk1977do_test pragma-5.2 {
60991cf71b0Sdanielk1977  execsql {
61091cf71b0Sdanielk1977    pragma synchronous;
61191cf71b0Sdanielk1977  }
61291cf71b0Sdanielk1977} {2}
6135260f7e9Sdrhcatchsql {COMMIT;}
614c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
6155260f7e9Sdrh
6165260f7e9Sdrh# Test schema-query pragmas
6175260f7e9Sdrh#
61827188fb5Sdanielk1977ifcapable schema_pragmas {
6195a8f9374Sdanielk1977ifcapable tempdb&&attach {
6205260f7e9Sdrh  do_test pragma-6.1 {
621956bc92cSdrh    set res {}
62240e016e4Sdrh    execsql {SELECT * FROM sqlite_temp_master}
6235260f7e9Sdrh    foreach {idx name file} [execsql {pragma database_list}] {
6245260f7e9Sdrh      lappend res $idx $name
6255260f7e9Sdrh    }
6265260f7e9Sdrh    set res
627a71aa001Sdrh  } {0 main 1 temp 2 aux}
62853c0f748Sdanielk1977}
6295260f7e9Sdrhdo_test pragma-6.2 {
6305260f7e9Sdrh  execsql {
631a6dddd9bSdrh    CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
6325260f7e9Sdrh    pragma table_info(t2)
6335260f7e9Sdrh  }
634a6dddd9bSdrh} {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
635d2cb50b7Sdrhdo_test pragma-6.2.1 {
636d2cb50b7Sdrh  execsql {
637d2cb50b7Sdrh    pragma table_info;
638d2cb50b7Sdrh  }
639d2cb50b7Sdrh} {}
640736c7d4bSdrhdb nullvalue <<NULL>>
641417ec638Sdrhdo_test pragma-6.2.2 {
642417ec638Sdrh  execsql {
643736c7d4bSdrh    CREATE TABLE t5(
644736c7d4bSdrh      a TEXT DEFAULT CURRENT_TIMESTAMP,
645736c7d4bSdrh      b DEFAULT (5+3),
646736c7d4bSdrh      c TEXT,
647736c7d4bSdrh      d INTEGER DEFAULT NULL,
648384b7fe2Sdrh      e TEXT DEFAULT '',
649384b7fe2Sdrh      UNIQUE(b,c,d),
650384b7fe2Sdrh      PRIMARY KEY(e,b,c)
651736c7d4bSdrh    );
652417ec638Sdrh    PRAGMA table_info(t5);
653417ec638Sdrh  }
654384b7fe2Sdrh} {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}
655736c7d4bSdrhdb nullvalue {}
656384b7fe2Sdrhdo_test pragma-6.2.3 {
657384b7fe2Sdrh  execsql {
658384b7fe2Sdrh    CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
659384b7fe2Sdrh    pragma table_info(t2_3)
660384b7fe2Sdrh  }
661384b7fe2Sdrh} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
6626bf89570Sdrhifcapable {foreignkey} {
663d2cb50b7Sdrh  do_test pragma-6.3.1 {
6645260f7e9Sdrh    execsql {
6655260f7e9Sdrh      CREATE TABLE t3(a int references t2(b), b UNIQUE);
6665260f7e9Sdrh      pragma foreign_key_list(t3);
6675260f7e9Sdrh    }
6681da40a38Sdan  } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
669d2cb50b7Sdrh  do_test pragma-6.3.2 {
670d2cb50b7Sdrh    execsql {
671d2cb50b7Sdrh      pragma foreign_key_list;
672d2cb50b7Sdrh    }
673d2cb50b7Sdrh  } {}
674d2cb50b7Sdrh  do_test pragma-6.3.3 {
675d2cb50b7Sdrh    execsql {
676d2cb50b7Sdrh      pragma foreign_key_list(t3_bogus);
677d2cb50b7Sdrh    }
678d2cb50b7Sdrh  } {}
679d2cb50b7Sdrh  do_test pragma-6.3.4 {
680d2cb50b7Sdrh    execsql {
681d2cb50b7Sdrh      pragma foreign_key_list(t5);
682d2cb50b7Sdrh    }
683d2cb50b7Sdrh  } {}
6845260f7e9Sdrh  do_test pragma-6.4 {
685c228be5bSdrh    capture_pragma db out {
6865260f7e9Sdrh      pragma index_list(t3);
6875260f7e9Sdrh    }
688c228be5bSdrh    db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
6893ef26156Sdrh  } {0 sqlite_autoindex_t3_1 1}
6906bf89570Sdrh}
6916bf89570Sdrhifcapable {!foreignkey} {
6926bf89570Sdrh  execsql {CREATE TABLE t3(a,b UNIQUE)}
6936bf89570Sdrh}
694d2cb50b7Sdrhdo_test pragma-6.5.1 {
6955260f7e9Sdrh  execsql {
6965260f7e9Sdrh    CREATE INDEX t3i1 ON t3(a,b);
697c228be5bSdrh  }
698c228be5bSdrh  capture_pragma db out {
6995260f7e9Sdrh    pragma index_info(t3i1);
7005260f7e9Sdrh  }
701c228be5bSdrh  db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
7025260f7e9Sdrh} {0 0 a 1 1 b}
7037be0fd9cSdrh
7047be0fd9cSdrh# EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
7057be0fd9cSdrh# by the index_info pragma, but they are listed by the index_xinfo
7067be0fd9cSdrh# pragma.
7077be0fd9cSdrh#
7087be0fd9cSdrhdo_test pragma-6.5.1b {
7097be0fd9cSdrh  capture_pragma db out {PRAGMA index_xinfo(t3i1)}
7107be0fd9cSdrh  db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
7117be0fd9cSdrh} {0 0 a 1 1 b 2 -1 {}}
7127be0fd9cSdrh
7137be0fd9cSdrh
714b3366b99Sdrh# EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
715b3366b99Sdrh# pragma returns one row for each key column in the named index.
7167be0fd9cSdrh#
7177be0fd9cSdrh# (The first column of output from PRAGMA index_info is...)
7187be0fd9cSdrh# EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
7197be0fd9cSdrh# means left-most.)
7207be0fd9cSdrh#
7217be0fd9cSdrh# (The second column of output from PRAGMA index_info is...)
7227be0fd9cSdrh# EVIDENCE-OF: R-65019-08383 The rank of the column within the table
7237be0fd9cSdrh# being indexed.
7247be0fd9cSdrh#
7257be0fd9cSdrh# (The third column of output from PRAGMA index_info is...)
7267be0fd9cSdrh# EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
7277be0fd9cSdrh#
7287be0fd9cSdrhdo_execsql_test pragma-6.5.1c {
7297be0fd9cSdrh  CREATE INDEX t3i2 ON t3(b,a);
7307be0fd9cSdrh  PRAGMA index_info='t3i2';
7317be0fd9cSdrh  DROP INDEX t3i2;
7327be0fd9cSdrh} {0 1 b 1 0 a}
7337be0fd9cSdrh
734d2cb50b7Sdrhdo_test pragma-6.5.2 {
735d2cb50b7Sdrh  execsql {
736d2cb50b7Sdrh    pragma index_info(t3i1_bogus);
737d2cb50b7Sdrh  }
738d2cb50b7Sdrh} {}
739260d8a6aSdanielk1977
740260d8a6aSdanielk1977ifcapable tempdb {
741260d8a6aSdanielk1977  # Test for ticket #3320. When a temp table of the same name exists, make
742260d8a6aSdanielk1977  # sure the schema of the main table can still be queried using
743260d8a6aSdanielk1977  # "pragma table_info":
744260d8a6aSdanielk1977  do_test pragma-6.6.1 {
745260d8a6aSdanielk1977    execsql {
746260d8a6aSdanielk1977      CREATE TABLE trial(col_main);
747260d8a6aSdanielk1977      CREATE TEMP TABLE trial(col_temp);
748260d8a6aSdanielk1977    }
749260d8a6aSdanielk1977  } {}
750260d8a6aSdanielk1977  do_test pragma-6.6.2 {
751260d8a6aSdanielk1977    execsql {
752260d8a6aSdanielk1977      PRAGMA table_info(trial);
753260d8a6aSdanielk1977    }
754260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
755260d8a6aSdanielk1977  do_test pragma-6.6.3 {
756260d8a6aSdanielk1977    execsql {
757260d8a6aSdanielk1977      PRAGMA temp.table_info(trial);
758260d8a6aSdanielk1977    }
759260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
760260d8a6aSdanielk1977  do_test pragma-6.6.4 {
761260d8a6aSdanielk1977    execsql {
762260d8a6aSdanielk1977      PRAGMA main.table_info(trial);
763260d8a6aSdanielk1977    }
764260d8a6aSdanielk1977  } {0 col_main {} 0 {} 0}
765260d8a6aSdanielk1977}
766f96a3778Sdanielk1977
767f96a3778Sdanielk1977do_test pragma-6.7 {
768f96a3778Sdanielk1977  execsql {
769f96a3778Sdanielk1977    CREATE TABLE test_table(
770f96a3778Sdanielk1977      one INT NOT NULL DEFAULT -1,
771f96a3778Sdanielk1977      two text,
772f96a3778Sdanielk1977      three VARCHAR(45, 65) DEFAULT 'abcde',
773f96a3778Sdanielk1977      four REAL DEFAULT X'abcdef',
774f96a3778Sdanielk1977      five DEFAULT CURRENT_TIME
775f96a3778Sdanielk1977    );
776f96a3778Sdanielk1977  }
777c228be5bSdrh  capture_pragma db out {PRAGMA table_info(test_table)}
778c228be5bSdrh  db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
779c228be5bSdrh            ORDER BY cid}
780f96a3778Sdanielk1977} [concat \
781f96a3778Sdanielk1977  {0 one INT 1 -1 0} \
782f96a3778Sdanielk1977  {1 two text 0 {} 0} \
783f96a3778Sdanielk1977  {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
784f96a3778Sdanielk1977  {3 four REAL 0 X'abcdef' 0} \
785f96a3778Sdanielk1977  {4 five {} 0 CURRENT_TIME 0} \
786f96a3778Sdanielk1977]
7871b678969Sdrhdo_test pragma-6.8 {
7881b678969Sdrh  execsql {
7891b678969Sdrh    CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
7901b678969Sdrh    PRAGMA table_info(t68);
7911b678969Sdrh  }
7921b678969Sdrh} [concat \
7931b678969Sdrh  {0 a {} 0 {} 1} \
7941b678969Sdrh  {1 b {} 0 {} 2} \
7951b678969Sdrh  {2 c {} 0 {} 4} \
7961b678969Sdrh]
79727188fb5Sdanielk1977} ;# ifcapable schema_pragmas
7985260f7e9Sdrh# Miscellaneous tests
7995260f7e9Sdrh#
80027188fb5Sdanielk1977ifcapable schema_pragmas {
801b3366b99Sdrh# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
802b3366b99Sdrh# pragma returns one row for each index associated with the given table.
803b3366b99Sdrh#
804d2cb50b7Sdrhdo_test pragma-7.1.1 {
8055260f7e9Sdrh  # Make sure a pragma knows to read the schema if it needs to
8065260f7e9Sdrh  db close
8075260f7e9Sdrh  sqlite3 db test.db
808c228be5bSdrh  capture_pragma db out "PRAGMA index_list(t3)"
809c228be5bSdrh  db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
810c228be5bSdrh} {t3i1 c sqlite_autoindex_t3_1 u}
811d2cb50b7Sdrhdo_test pragma-7.1.2 {
812d2cb50b7Sdrh  execsql {
813d2cb50b7Sdrh    pragma index_list(t3_bogus);
814d2cb50b7Sdrh  }
815d2cb50b7Sdrh} {}
81627188fb5Sdanielk1977} ;# ifcapable schema_pragmas
8176c62608fSdrhifcapable {utf16} {
818cb354603Sdan  if {[permutation] == ""} {
8195260f7e9Sdrh    do_test pragma-7.2 {
8205260f7e9Sdrh      db close
8215260f7e9Sdrh      sqlite3 db test.db
8225260f7e9Sdrh      catchsql {
8235260f7e9Sdrh        pragma encoding=bogus;
8245260f7e9Sdrh      }
8255260f7e9Sdrh    } {1 {unsupported encoding: bogus}}
8266c62608fSdrh  }
827cb354603Sdan}
82853c0f748Sdanielk1977ifcapable tempdb {
8295260f7e9Sdrh  do_test pragma-7.3 {
8305260f7e9Sdrh    db close
8315260f7e9Sdrh    sqlite3 db test.db
8325260f7e9Sdrh    execsql {
8335260f7e9Sdrh      pragma lock_status;
8345260f7e9Sdrh    }
8355260f7e9Sdrh  } {main unlocked temp closed}
83653c0f748Sdanielk1977} else {
83753c0f748Sdanielk1977  do_test pragma-7.3 {
83853c0f748Sdanielk1977    db close
83953c0f748Sdanielk1977    sqlite3 db test.db
84053c0f748Sdanielk1977    execsql {
84153c0f748Sdanielk1977      pragma lock_status;
84253c0f748Sdanielk1977    }
84353c0f748Sdanielk1977  } {main unlocked}
84453c0f748Sdanielk1977}
8455260f7e9Sdrh
8465260f7e9Sdrh
847dae2495bSdanielk1977#----------------------------------------------------------------------
848b92b70bbSdanielk1977# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
849b92b70bbSdanielk1977# user_version" statements.
850dae2495bSdanielk1977#
851b92b70bbSdanielk1977# pragma-8.1: PRAGMA schema_version
852b92b70bbSdanielk1977# pragma-8.2: PRAGMA user_version
853dae2495bSdanielk1977#
854dae2495bSdanielk1977
85511cf9fbcSdanielk1977ifcapable schema_version {
85611cf9fbcSdanielk1977
857b92b70bbSdanielk1977# First check that we can set the schema version and then retrieve the
858dae2495bSdanielk1977# same value.
859dae2495bSdanielk1977do_test pragma-8.1.1 {
860dae2495bSdanielk1977  execsql {
861b92b70bbSdanielk1977    PRAGMA schema_version = 105;
862dae2495bSdanielk1977  }
863dae2495bSdanielk1977} {}
864dae2495bSdanielk1977do_test pragma-8.1.2 {
86525403650Sdrh  execsql2 {
866b92b70bbSdanielk1977    PRAGMA schema_version;
867dae2495bSdanielk1977  }
86825403650Sdrh} {schema_version 105}
869dae2495bSdanielk1977do_test pragma-8.1.3 {
870dae2495bSdanielk1977  execsql {
871b92b70bbSdanielk1977    PRAGMA schema_version = 106;
872dae2495bSdanielk1977  }
873dae2495bSdanielk1977} {}
874dae2495bSdanielk1977do_test pragma-8.1.4 {
875dae2495bSdanielk1977  execsql {
876b92b70bbSdanielk1977    PRAGMA schema_version;
877dae2495bSdanielk1977  }
878dae2495bSdanielk1977} 106
879dae2495bSdanielk1977
880b92b70bbSdanielk1977# Check that creating a table modifies the schema-version (this is really
881b92b70bbSdanielk1977# to verify that the value being read is in fact the schema version).
882dae2495bSdanielk1977do_test pragma-8.1.5 {
883dae2495bSdanielk1977  execsql {
884dae2495bSdanielk1977    CREATE TABLE t4(a, b, c);
885dae2495bSdanielk1977    INSERT INTO t4 VALUES(1, 2, 3);
886dae2495bSdanielk1977    SELECT * FROM t4;
887dae2495bSdanielk1977  }
888dae2495bSdanielk1977} {1 2 3}
889dae2495bSdanielk1977do_test pragma-8.1.6 {
890dae2495bSdanielk1977  execsql {
891b92b70bbSdanielk1977    PRAGMA schema_version;
892dae2495bSdanielk1977  }
893dae2495bSdanielk1977} 107
894dae2495bSdanielk1977
895dae2495bSdanielk1977# Now open a second connection to the database. Ensure that changing the
896b92b70bbSdanielk1977# schema-version using the first connection forces the second connection
897dae2495bSdanielk1977# to reload the schema. This has to be done using the C-API test functions,
898dae2495bSdanielk1977# because the TCL API accounts for SCHEMA_ERROR and retries the query.
899dae2495bSdanielk1977do_test pragma-8.1.7 {
900dddca286Sdrh  sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
901dae2495bSdanielk1977  execsql {
902dae2495bSdanielk1977    SELECT * FROM t4;
903dae2495bSdanielk1977  } db2
904dae2495bSdanielk1977} {1 2 3}
905dae2495bSdanielk1977do_test pragma-8.1.8 {
906dae2495bSdanielk1977  execsql {
907b92b70bbSdanielk1977    PRAGMA schema_version = 108;
908dae2495bSdanielk1977  }
909dae2495bSdanielk1977} {}
910dae2495bSdanielk1977do_test pragma-8.1.9 {
911dae2495bSdanielk1977  set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
912dae2495bSdanielk1977  sqlite3_step $::STMT
913dae2495bSdanielk1977} SQLITE_ERROR
914dae2495bSdanielk1977do_test pragma-8.1.10 {
915dae2495bSdanielk1977  sqlite3_finalize $::STMT
916dae2495bSdanielk1977} SQLITE_SCHEMA
917dae2495bSdanielk1977
918b92b70bbSdanielk1977# Make sure the schema-version can be manipulated in an attached database.
919fda06befSmistachkinforcedelete test2.db
920fda06befSmistachkinforcedelete test2.db-journal
9215a8f9374Sdanielk1977ifcapable attach {
922dae2495bSdanielk1977  do_test pragma-8.1.11 {
923dae2495bSdanielk1977    execsql {
924dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
925dae2495bSdanielk1977      CREATE TABLE aux.t1(a, b, c);
926b92b70bbSdanielk1977      PRAGMA aux.schema_version = 205;
927dae2495bSdanielk1977    }
928dae2495bSdanielk1977  } {}
929dae2495bSdanielk1977  do_test pragma-8.1.12 {
930dae2495bSdanielk1977    execsql {
931b92b70bbSdanielk1977      PRAGMA aux.schema_version;
932dae2495bSdanielk1977    }
933dae2495bSdanielk1977  } 205
9345a8f9374Sdanielk1977}
935dae2495bSdanielk1977do_test pragma-8.1.13 {
936dae2495bSdanielk1977  execsql {
937b92b70bbSdanielk1977    PRAGMA schema_version;
938dae2495bSdanielk1977  }
939dae2495bSdanielk1977} 108
940dae2495bSdanielk1977
941b92b70bbSdanielk1977# And check that modifying the schema-version in an attached database
942dae2495bSdanielk1977# forces the second connection to reload the schema.
9435a8f9374Sdanielk1977ifcapable attach {
944dae2495bSdanielk1977  do_test pragma-8.1.14 {
945dddca286Sdrh    sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
946dae2495bSdanielk1977    execsql {
947dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
948dae2495bSdanielk1977      SELECT * FROM aux.t1;
949dae2495bSdanielk1977    } db2
950dae2495bSdanielk1977  } {}
951dae2495bSdanielk1977  do_test pragma-8.1.15 {
952dae2495bSdanielk1977    execsql {
953b92b70bbSdanielk1977      PRAGMA aux.schema_version = 206;
954dae2495bSdanielk1977    }
955dae2495bSdanielk1977  } {}
956dae2495bSdanielk1977  do_test pragma-8.1.16 {
957dae2495bSdanielk1977    set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
958dae2495bSdanielk1977    sqlite3_step $::STMT
959dae2495bSdanielk1977  } SQLITE_ERROR
960dae2495bSdanielk1977  do_test pragma-8.1.17 {
961dae2495bSdanielk1977    sqlite3_finalize $::STMT
962dae2495bSdanielk1977  } SQLITE_SCHEMA
963dae2495bSdanielk1977  do_test pragma-8.1.18 {
964dae2495bSdanielk1977    db2 close
965dae2495bSdanielk1977  } {}
9665a8f9374Sdanielk1977}
967dae2495bSdanielk1977
968b92b70bbSdanielk1977# Now test that the user-version can be read and written (and that we aren't
969b92b70bbSdanielk1977# accidentally manipulating the schema-version instead).
970dae2495bSdanielk1977do_test pragma-8.2.1 {
97125403650Sdrh  execsql2 {
972b92b70bbSdanielk1977    PRAGMA user_version;
973dae2495bSdanielk1977  }
97425403650Sdrh} {user_version 0}
975dae2495bSdanielk1977do_test pragma-8.2.2 {
976dae2495bSdanielk1977  execsql {
977b92b70bbSdanielk1977    PRAGMA user_version = 2;
978dae2495bSdanielk1977  }
979dae2495bSdanielk1977} {}
980802d69a7Sdrhdo_test pragma-8.2.3.1 {
98125403650Sdrh  execsql2 {
982b92b70bbSdanielk1977    PRAGMA user_version;
983dae2495bSdanielk1977  }
98425403650Sdrh} {user_version 2}
985802d69a7Sdrhdo_test pragma-8.2.3.2 {
986802d69a7Sdrh  db close
987802d69a7Sdrh  sqlite3 db test.db
988802d69a7Sdrh  execsql {
989802d69a7Sdrh    PRAGMA user_version;
990802d69a7Sdrh  }
991802d69a7Sdrh} {2}
992802d69a7Sdrhdo_test pragma-8.2.4.1 {
993dae2495bSdanielk1977  execsql {
994b92b70bbSdanielk1977    PRAGMA schema_version;
995dae2495bSdanielk1977  }
996dae2495bSdanielk1977} {108}
997802d69a7Sdrhifcapable vacuum {
998802d69a7Sdrh  do_test pragma-8.2.4.2 {
999802d69a7Sdrh    execsql {
1000802d69a7Sdrh      VACUUM;
1001802d69a7Sdrh      PRAGMA user_version;
1002802d69a7Sdrh    }
1003802d69a7Sdrh  } {2}
1004802d69a7Sdrh  do_test pragma-8.2.4.3 {
1005802d69a7Sdrh    execsql {
1006802d69a7Sdrh      PRAGMA schema_version;
1007802d69a7Sdrh    }
1008802d69a7Sdrh  } {109}
1009802d69a7Sdrh}
10105a8f9374Sdanielk1977
10115a8f9374Sdanielk1977ifcapable attach {
1012802d69a7Sdrh  db eval {ATTACH 'test2.db' AS aux}
1013dae2495bSdanielk1977
1014b92b70bbSdanielk1977  # Check that the user-version in the auxilary database can be manipulated (
1015dae2495bSdanielk1977  # and that we aren't accidentally manipulating the same in the main db).
1016dae2495bSdanielk1977  do_test pragma-8.2.5 {
1017dae2495bSdanielk1977    execsql {
1018b92b70bbSdanielk1977      PRAGMA aux.user_version;
1019dae2495bSdanielk1977    }
1020dae2495bSdanielk1977  } {0}
1021dae2495bSdanielk1977  do_test pragma-8.2.6 {
1022dae2495bSdanielk1977    execsql {
1023b92b70bbSdanielk1977      PRAGMA aux.user_version = 3;
1024dae2495bSdanielk1977    }
1025dae2495bSdanielk1977  } {}
1026dae2495bSdanielk1977  do_test pragma-8.2.7 {
1027dae2495bSdanielk1977    execsql {
1028b92b70bbSdanielk1977      PRAGMA aux.user_version;
1029dae2495bSdanielk1977    }
1030dae2495bSdanielk1977  } {3}
1031dae2495bSdanielk1977  do_test pragma-8.2.8 {
1032dae2495bSdanielk1977    execsql {
1033b92b70bbSdanielk1977      PRAGMA main.user_version;
1034dae2495bSdanielk1977    }
1035dae2495bSdanielk1977  } {2}
1036dae2495bSdanielk1977
1037b92b70bbSdanielk1977  # Now check that a ROLLBACK resets the user-version if it has been modified
1038dae2495bSdanielk1977  # within a transaction.
1039dae2495bSdanielk1977  do_test pragma-8.2.9 {
1040dae2495bSdanielk1977    execsql {
1041dae2495bSdanielk1977      BEGIN;
1042b92b70bbSdanielk1977      PRAGMA aux.user_version = 10;
1043b92b70bbSdanielk1977      PRAGMA user_version = 11;
1044dae2495bSdanielk1977    }
1045dae2495bSdanielk1977  } {}
1046dae2495bSdanielk1977  do_test pragma-8.2.10 {
1047dae2495bSdanielk1977    execsql {
1048b92b70bbSdanielk1977      PRAGMA aux.user_version;
1049dae2495bSdanielk1977    }
1050dae2495bSdanielk1977  } {10}
1051dae2495bSdanielk1977  do_test pragma-8.2.11 {
1052dae2495bSdanielk1977    execsql {
1053b92b70bbSdanielk1977      PRAGMA main.user_version;
1054dae2495bSdanielk1977    }
1055dae2495bSdanielk1977  } {11}
1056dae2495bSdanielk1977  do_test pragma-8.2.12 {
1057dae2495bSdanielk1977    execsql {
1058dae2495bSdanielk1977      ROLLBACK;
1059b92b70bbSdanielk1977      PRAGMA aux.user_version;
1060dae2495bSdanielk1977    }
1061dae2495bSdanielk1977  } {3}
1062dae2495bSdanielk1977  do_test pragma-8.2.13 {
1063dae2495bSdanielk1977    execsql {
1064b92b70bbSdanielk1977      PRAGMA main.user_version;
1065dae2495bSdanielk1977    }
1066dae2495bSdanielk1977  } {2}
10675a8f9374Sdanielk1977}
1068dae2495bSdanielk1977
1069b92b70bbSdanielk1977# Try a negative value for the user-version
1070dae2495bSdanielk1977do_test pragma-8.2.14 {
1071dae2495bSdanielk1977  execsql {
1072b92b70bbSdanielk1977    PRAGMA user_version = -450;
1073dae2495bSdanielk1977  }
1074dae2495bSdanielk1977} {}
1075dae2495bSdanielk1977do_test pragma-8.2.15 {
1076dae2495bSdanielk1977  execsql {
1077b92b70bbSdanielk1977    PRAGMA user_version;
1078dae2495bSdanielk1977  }
1079dae2495bSdanielk1977} {-450}
1080d9c847dcSdanielk1977} ; # ifcapable schema_version
1081d9c847dcSdanielk1977
1082d19744f3Sdrh# Check to see if TEMP_STORE is memory or disk.  Return strings
1083d19744f3Sdrh# "memory" or "disk" as appropriate.
1084d19744f3Sdrh#
1085d19744f3Sdrhproc check_temp_store {} {
10869131ab93Sdan  db eval {
10879131ab93Sdan    PRAGMA temp.cache_size = 1;
10889131ab93Sdan    CREATE TEMP TABLE IF NOT EXISTS a(b);
10899131ab93Sdan    DELETE FROM a;
10909131ab93Sdan    INSERT INTO a VALUES(randomblob(1000));
10919131ab93Sdan    INSERT INTO a SELECT * FROM a;
10929131ab93Sdan    INSERT INTO a SELECT * FROM a;
10939131ab93Sdan    INSERT INTO a SELECT * FROM a;
10949131ab93Sdan    INSERT INTO a SELECT * FROM a;
10959131ab93Sdan    INSERT INTO a SELECT * FROM a;
10969131ab93Sdan    INSERT INTO a SELECT * FROM a;
10979131ab93Sdan    INSERT INTO a SELECT * FROM a;
10989131ab93Sdan    INSERT INTO a SELECT * FROM a;
10999131ab93Sdan  }
1100d19744f3Sdrh  db eval {PRAGMA database_list} {
1101d19744f3Sdrh    if {$name=="temp"} {
110217b90b53Sdanielk1977      set bt [btree_from_db db 1]
110317b90b53Sdanielk1977      if {[btree_ismemdb $bt]} {
1104d19744f3Sdrh        return "memory"
1105d19744f3Sdrh      }
110617b90b53Sdanielk1977      return "disk"
1107d19744f3Sdrh    }
1108d19744f3Sdrh  }
1109d19744f3Sdrh  return "unknown"
1110d19744f3Sdrh}
1111d19744f3Sdrh
11124ee09b4bSdrh# Application_ID
11134ee09b4bSdrh#
11144ee09b4bSdrhdo_test pragma-8.3.1 {
11154ee09b4bSdrh  execsql {
11164ee09b4bSdrh    PRAGMA application_id;
11174ee09b4bSdrh  }
11184ee09b4bSdrh} {0}
11194ee09b4bSdrhdo_test pragma-8.3.2 {
11204ee09b4bSdrh  execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
11214ee09b4bSdrh} {12345}
11229a09a3caStpoindex
11239a09a3caStpoindex# Test temp_store and temp_store_directory pragmas
11249a09a3caStpoindex#
1125268283bcSdrhifcapable pager_pragmas {
11269a09a3caStpoindexdo_test pragma-9.1 {
11279a09a3caStpoindex  db close
11289a09a3caStpoindex  sqlite3 db test.db
11299a09a3caStpoindex  execsql {
11309a09a3caStpoindex    PRAGMA temp_store;
11319a09a3caStpoindex  }
11329a09a3caStpoindex} {0}
1133d19744f3Sdrhif {$TEMP_STORE<=1} {
1134d19744f3Sdrh  do_test pragma-9.1.1 {
1135d19744f3Sdrh    check_temp_store
1136d19744f3Sdrh  } {disk}
1137d19744f3Sdrh} else {
1138d19744f3Sdrh  do_test pragma-9.1.1 {
1139d19744f3Sdrh    check_temp_store
1140d19744f3Sdrh  } {memory}
1141d19744f3Sdrh}
1142d19744f3Sdrh
11439a09a3caStpoindexdo_test pragma-9.2 {
1144d19744f3Sdrh  db close
1145d19744f3Sdrh  sqlite3 db test.db
11469a09a3caStpoindex  execsql {
11479a09a3caStpoindex    PRAGMA temp_store=file;
11489a09a3caStpoindex    PRAGMA temp_store;
11499a09a3caStpoindex  }
11509a09a3caStpoindex} {1}
1151d19744f3Sdrhif {$TEMP_STORE==3} {
1152d19744f3Sdrh  # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1153d19744f3Sdrh  do_test pragma-9.2.1 {
1154d19744f3Sdrh    check_temp_store
1155d19744f3Sdrh  } {memory}
1156d19744f3Sdrh} else {
1157d19744f3Sdrh  do_test pragma-9.2.1 {
1158d19744f3Sdrh    check_temp_store
1159d19744f3Sdrh  } {disk}
1160d19744f3Sdrh}
1161d19744f3Sdrh
11629a09a3caStpoindexdo_test pragma-9.3 {
1163d19744f3Sdrh  db close
1164d19744f3Sdrh  sqlite3 db test.db
11659a09a3caStpoindex  execsql {
11669a09a3caStpoindex    PRAGMA temp_store=memory;
11679a09a3caStpoindex    PRAGMA temp_store;
11689a09a3caStpoindex  }
11699a09a3caStpoindex} {2}
1170d19744f3Sdrhif {$TEMP_STORE==0} {
1171d19744f3Sdrh  # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1172d19744f3Sdrh  do_test pragma-9.3.1 {
1173d19744f3Sdrh    check_temp_store
1174d19744f3Sdrh  } {disk}
1175d19744f3Sdrh} else {
1176d19744f3Sdrh  do_test pragma-9.3.1 {
1177d19744f3Sdrh    check_temp_store
1178d19744f3Sdrh  } {memory}
1179d19744f3Sdrh}
1180d19744f3Sdrh
11819a09a3caStpoindexdo_test pragma-9.4 {
11829a09a3caStpoindex  execsql {
11839a09a3caStpoindex    PRAGMA temp_store_directory;
11849a09a3caStpoindex  }
11859a09a3caStpoindex} {}
118678f82d1eSdrhifcapable wsd {
11879a09a3caStpoindex  do_test pragma-9.5 {
1188f8a78464Smistachkin    set pwd [string map {' ''} [file nativename [get_pwd]]]
1189268283bcSdrh    execsql "
1190268283bcSdrh      PRAGMA temp_store_directory='$pwd';
11919a09a3caStpoindex    "
11929a09a3caStpoindex  } {}
11939a09a3caStpoindex  do_test pragma-9.6 {
11949a09a3caStpoindex    execsql {
11959a09a3caStpoindex      PRAGMA temp_store_directory;
11969a09a3caStpoindex    }
1197f8a78464Smistachkin  } [list [file nativename [get_pwd]]]
11989a09a3caStpoindex  do_test pragma-9.7 {
1199268283bcSdrh    catchsql {
12009a09a3caStpoindex      PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
12019a09a3caStpoindex    }
1202268283bcSdrh  } {1 {not a writable directory}}
12039a09a3caStpoindex  do_test pragma-9.8 {
12049a09a3caStpoindex    execsql {
12059a09a3caStpoindex      PRAGMA temp_store_directory='';
12069a09a3caStpoindex    }
12079a09a3caStpoindex  } {}
12081e9daa6aSdrh  if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
120953c0f748Sdanielk1977    ifcapable tempdb {
12109a09a3caStpoindex      do_test pragma-9.9 {
12119a09a3caStpoindex        execsql {
12129a09a3caStpoindex          PRAGMA temp_store_directory;
12139a09a3caStpoindex          PRAGMA temp_store=FILE;
12149a09a3caStpoindex          CREATE TEMP TABLE temp_store_directory_test(a integer);
12159a09a3caStpoindex          INSERT INTO temp_store_directory_test values (2);
12169a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
12179a09a3caStpoindex        }
12189a09a3caStpoindex      } {2}
12199a09a3caStpoindex      do_test pragma-9.10 {
1220268283bcSdrh        catchsql "
1221268283bcSdrh          PRAGMA temp_store_directory='$pwd';
12229a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
12239a09a3caStpoindex        "
1224268283bcSdrh      } {1 {no such table: temp_store_directory_test}}
12251e9daa6aSdrh    }
12261e9daa6aSdrh  }
122778f82d1eSdrh}
122895b289b6Sdanielk1977do_test pragma-9.11 {
122995b289b6Sdanielk1977  execsql {
123095b289b6Sdanielk1977    PRAGMA temp_store = 0;
123195b289b6Sdanielk1977    PRAGMA temp_store;
123295b289b6Sdanielk1977  }
123395b289b6Sdanielk1977} {0}
123495b289b6Sdanielk1977do_test pragma-9.12 {
123595b289b6Sdanielk1977  execsql {
123695b289b6Sdanielk1977    PRAGMA temp_store = 1;
123795b289b6Sdanielk1977    PRAGMA temp_store;
123895b289b6Sdanielk1977  }
123995b289b6Sdanielk1977} {1}
124095b289b6Sdanielk1977do_test pragma-9.13 {
124195b289b6Sdanielk1977  execsql {
124295b289b6Sdanielk1977    PRAGMA temp_store = 2;
124395b289b6Sdanielk1977    PRAGMA temp_store;
124495b289b6Sdanielk1977  }
124595b289b6Sdanielk1977} {2}
124695b289b6Sdanielk1977do_test pragma-9.14 {
124795b289b6Sdanielk1977  execsql {
124895b289b6Sdanielk1977    PRAGMA temp_store = 3;
124995b289b6Sdanielk1977    PRAGMA temp_store;
125095b289b6Sdanielk1977  }
125195b289b6Sdanielk1977} {0}
125295b289b6Sdanielk1977do_test pragma-9.15 {
125395b289b6Sdanielk1977  catchsql {
125495b289b6Sdanielk1977    BEGIN EXCLUSIVE;
125595b289b6Sdanielk1977    CREATE TEMP TABLE temp_table(t);
125695b289b6Sdanielk1977    INSERT INTO temp_table VALUES('valuable data');
125795b289b6Sdanielk1977    PRAGMA temp_store = 1;
125895b289b6Sdanielk1977  }
125995b289b6Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
126095b289b6Sdanielk1977do_test pragma-9.16 {
126195b289b6Sdanielk1977  execsql {
126295b289b6Sdanielk1977    SELECT * FROM temp_table;
126395b289b6Sdanielk1977    COMMIT;
126495b289b6Sdanielk1977  }
126595b289b6Sdanielk1977} {{valuable data}}
1266983e2309Sdanielk1977
1267983e2309Sdanielk1977do_test pragma-9.17 {
1268983e2309Sdanielk1977  execsql {
1269983e2309Sdanielk1977    INSERT INTO temp_table VALUES('valuable data II');
1270983e2309Sdanielk1977    SELECT * FROM temp_table;
1271983e2309Sdanielk1977  }
1272983e2309Sdanielk1977} {{valuable data} {valuable data II}}
1273983e2309Sdanielk1977
1274983e2309Sdanielk1977do_test pragma-9.18 {
1275983e2309Sdanielk1977  set rc [catch {
1276983e2309Sdanielk1977    db eval {SELECT t FROM temp_table} {
1277983e2309Sdanielk1977      execsql {pragma temp_store = 1}
1278983e2309Sdanielk1977    }
1279983e2309Sdanielk1977  } msg]
1280983e2309Sdanielk1977  list $rc $msg
1281983e2309Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
1282983e2309Sdanielk1977
1283268283bcSdrh} ;# ifcapable pager_pragmas
12849a09a3caStpoindex
1285cc6bd383Sdanielk1977ifcapable trigger {
1286cc6bd383Sdanielk1977
1287cc6bd383Sdanielk1977do_test pragma-10.0 {
1288cc6bd383Sdanielk1977  catchsql {
1289cc6bd383Sdanielk1977    DROP TABLE main.t1;
1290cc6bd383Sdanielk1977  }
1291cc6bd383Sdanielk1977  execsql {
1292cc6bd383Sdanielk1977    PRAGMA count_changes = 1;
1293cc6bd383Sdanielk1977
1294cc6bd383Sdanielk1977    CREATE TABLE t1(a PRIMARY KEY);
1295cc6bd383Sdanielk1977    CREATE TABLE t1_mirror(a);
1296cc6bd383Sdanielk1977    CREATE TABLE t1_mirror2(a);
1297cc6bd383Sdanielk1977    CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1298cc6bd383Sdanielk1977      INSERT INTO t1_mirror VALUES(new.a);
1299cc6bd383Sdanielk1977    END;
1300cc6bd383Sdanielk1977    CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1301cc6bd383Sdanielk1977      INSERT INTO t1_mirror2 VALUES(new.a);
1302cc6bd383Sdanielk1977    END;
1303cc6bd383Sdanielk1977    CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1304cc6bd383Sdanielk1977      UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1305cc6bd383Sdanielk1977    END;
1306cc6bd383Sdanielk1977    CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1307cc6bd383Sdanielk1977      UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1308cc6bd383Sdanielk1977    END;
1309cc6bd383Sdanielk1977    CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1310cc6bd383Sdanielk1977      DELETE FROM t1_mirror WHERE a = old.a;
1311cc6bd383Sdanielk1977    END;
1312cc6bd383Sdanielk1977    CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1313cc6bd383Sdanielk1977      DELETE FROM t1_mirror2 WHERE a = old.a;
1314cc6bd383Sdanielk1977    END;
1315cc6bd383Sdanielk1977  }
1316cc6bd383Sdanielk1977} {}
1317cc6bd383Sdanielk1977
1318cc6bd383Sdanielk1977do_test pragma-10.1 {
1319cc6bd383Sdanielk1977  execsql {
1320cc6bd383Sdanielk1977    INSERT INTO t1 VALUES(randstr(10,10));
1321cc6bd383Sdanielk1977  }
1322cc6bd383Sdanielk1977} {1}
1323cc6bd383Sdanielk1977do_test pragma-10.2 {
1324cc6bd383Sdanielk1977  execsql {
1325cc6bd383Sdanielk1977    UPDATE t1 SET a = randstr(10,10);
1326cc6bd383Sdanielk1977  }
1327cc6bd383Sdanielk1977} {1}
1328cc6bd383Sdanielk1977do_test pragma-10.3 {
1329cc6bd383Sdanielk1977  execsql {
1330cc6bd383Sdanielk1977    DELETE FROM t1;
1331cc6bd383Sdanielk1977  }
1332cc6bd383Sdanielk1977} {1}
1333cc6bd383Sdanielk1977
1334cc6bd383Sdanielk1977} ;# ifcapable trigger
1335cc6bd383Sdanielk1977
133648af65aeSdanielk1977ifcapable schema_pragmas {
133748af65aeSdanielk1977  do_test pragma-11.1 {
133848af65aeSdanielk1977    execsql2 {
133948af65aeSdanielk1977      pragma collation_list;
134048af65aeSdanielk1977    }
13412d823315Sdrh  } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
134248af65aeSdanielk1977  do_test pragma-11.2 {
134348af65aeSdanielk1977    db collate New_Collation blah...
134448af65aeSdanielk1977    execsql {
134548af65aeSdanielk1977      pragma collation_list;
134648af65aeSdanielk1977    }
13472d823315Sdrh  } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
134848af65aeSdanielk1977}
134948af65aeSdanielk1977
1350ddfb2f03Sdanielk1977ifcapable schema_pragmas&&tempdb {
1351ddfb2f03Sdanielk1977  do_test pragma-12.1 {
1352ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1353ddfb2f03Sdanielk1977    execsql {
1354ddfb2f03Sdanielk1977      PRAGMA temp.table_info('abc');
1355ddfb2f03Sdanielk1977    } db2
1356ddfb2f03Sdanielk1977  } {}
1357ddfb2f03Sdanielk1977  db2 close
1358ddfb2f03Sdanielk1977
1359ddfb2f03Sdanielk1977  do_test pragma-12.2 {
1360ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1361ddfb2f03Sdanielk1977    execsql {
1362ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size = 200;
1363ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size;
1364ddfb2f03Sdanielk1977    } db2
1365ddfb2f03Sdanielk1977  } {200}
1366ddfb2f03Sdanielk1977  db2 close
1367ddfb2f03Sdanielk1977
1368ddfb2f03Sdanielk1977  do_test pragma-12.3 {
1369ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1370ddfb2f03Sdanielk1977    execsql {
1371ddfb2f03Sdanielk1977      PRAGMA temp.cache_size = 400;
1372ddfb2f03Sdanielk1977      PRAGMA temp.cache_size;
1373ddfb2f03Sdanielk1977    } db2
1374ddfb2f03Sdanielk1977  } {400}
1375ddfb2f03Sdanielk1977  db2 close
1376ddfb2f03Sdanielk1977}
1377ddfb2f03Sdanielk1977
13784b2688abSdanielk1977ifcapable bloblit {
13794b2688abSdanielk1977
138005a82983Sdrhdo_test pragma-13.1 {
138105a82983Sdrh  execsql {
138205a82983Sdrh    DROP TABLE IF EXISTS t4;
138305a82983Sdrh    PRAGMA vdbe_trace=on;
138405a82983Sdrh    PRAGMA vdbe_listing=on;
138505a82983Sdrh    PRAGMA sql_trace=on;
138605a82983Sdrh    CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
138705a82983Sdrh    INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
138805a82983Sdrh    INSERT INTO t4(b) VALUES(randstr(30,30));
138905a82983Sdrh    INSERT INTO t4(b) VALUES(1.23456);
139005a82983Sdrh    INSERT INTO t4(b) VALUES(NULL);
139105a82983Sdrh    INSERT INTO t4(b) VALUES(0);
139205a82983Sdrh    INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
139305a82983Sdrh    SELECT * FROM t4;
139405a82983Sdrh  }
139505a82983Sdrh  execsql {
139605a82983Sdrh    PRAGMA vdbe_trace=off;
139705a82983Sdrh    PRAGMA vdbe_listing=off;
139805a82983Sdrh    PRAGMA sql_trace=off;
139905a82983Sdrh  }
140005a82983Sdrh} {}
140105a82983Sdrh
14024b2688abSdanielk1977} ;# ifcapable bloblit
14034b2688abSdanielk1977
140459a93791Sdanielk1977ifcapable pager_pragmas {
140559a93791Sdanielk1977  db close
1406fda06befSmistachkin  forcedelete test.db
140759a93791Sdanielk1977  sqlite3 db test.db
140859a93791Sdanielk1977
1409b3366b99Sdrh  # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1410b3366b99Sdrh  # number of pages in the database file.
141151a74d4cSdrh  #
141259a93791Sdanielk1977  do_test pragma-14.1 {
141359a93791Sdanielk1977    execsql { pragma auto_vacuum = 0 }
141451a74d4cSdrh    execsql { pragma page_count; pragma main.page_count }
141551a74d4cSdrh  } {0 0}
141659a93791Sdanielk1977
141759a93791Sdanielk1977  do_test pragma-14.2 {
141859a93791Sdanielk1977    execsql {
141959a93791Sdanielk1977      CREATE TABLE abc(a, b, c);
142059a93791Sdanielk1977      PRAGMA page_count;
142151a74d4cSdrh      PRAGMA main.page_count;
142251a74d4cSdrh      PRAGMA temp.page_count;
142359a93791Sdanielk1977    }
142451a74d4cSdrh  } {2 2 0}
14255d16a9a6Sdrh  do_test pragma-14.2uc {
14265d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
14275d16a9a6Sdrh  } {2}
142859a93791Sdanielk1977
142959a93791Sdanielk1977  do_test pragma-14.3 {
143059a93791Sdanielk1977    execsql {
143159a93791Sdanielk1977      BEGIN;
143259a93791Sdanielk1977      CREATE TABLE def(a, b, c);
143359a93791Sdanielk1977      PRAGMA page_count;
143459a93791Sdanielk1977    }
143559a93791Sdanielk1977  } {3}
14365d16a9a6Sdrh  do_test pragma-14.3uc {
14375d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
14385d16a9a6Sdrh  } {3}
143959a93791Sdanielk1977
144059a93791Sdanielk1977  do_test pragma-14.4 {
144159a93791Sdanielk1977    set page_size [db one {pragma page_size}]
144259a93791Sdanielk1977    expr [file size test.db] / $page_size
144359a93791Sdanielk1977  } {2}
144459a93791Sdanielk1977
144559a93791Sdanielk1977  do_test pragma-14.5 {
144659a93791Sdanielk1977    execsql {
144759a93791Sdanielk1977      ROLLBACK;
144859a93791Sdanielk1977      PRAGMA page_count;
144959a93791Sdanielk1977    }
145059a93791Sdanielk1977  } {2}
145159a93791Sdanielk1977
145259a93791Sdanielk1977  do_test pragma-14.6 {
1453fda06befSmistachkin    forcedelete test2.db
145459a93791Sdanielk1977    sqlite3 db2 test2.db
145559a93791Sdanielk1977    execsql {
145659a93791Sdanielk1977      PRAGMA auto_vacuum = 0;
145759a93791Sdanielk1977      CREATE TABLE t1(a, b, c);
145859a93791Sdanielk1977      CREATE TABLE t2(a, b, c);
145959a93791Sdanielk1977      CREATE TABLE t3(a, b, c);
146059a93791Sdanielk1977      CREATE TABLE t4(a, b, c);
146159a93791Sdanielk1977    } db2
146259a93791Sdanielk1977    db2 close
146359a93791Sdanielk1977    execsql {
146459a93791Sdanielk1977      ATTACH 'test2.db' AS aux;
146559a93791Sdanielk1977      PRAGMA aux.page_count;
146659a93791Sdanielk1977    }
146759a93791Sdanielk1977  } {5}
14685d16a9a6Sdrh  do_test pragma-14.6uc {
14695d16a9a6Sdrh    execsql {pragma AUX.PAGE_COUNT}
14705d16a9a6Sdrh  } {5}
147159a93791Sdanielk1977}
147259a93791Sdanielk1977
14738cf6c554Sdanielk1977# Test that the value set using the cache_size pragma is not reset when the
14748cf6c554Sdanielk1977# schema is reloaded.
14758cf6c554Sdanielk1977#
14768cf6c554Sdanielk1977ifcapable pager_pragmas {
14778cf6c554Sdanielk1977  db close
14788cf6c554Sdanielk1977  sqlite3 db test.db
14798cf6c554Sdanielk1977  do_test pragma-15.1 {
14808cf6c554Sdanielk1977    execsql {
14818cf6c554Sdanielk1977      PRAGMA cache_size=59;
14828cf6c554Sdanielk1977      PRAGMA cache_size;
14838cf6c554Sdanielk1977    }
14848cf6c554Sdanielk1977  } {59}
14858cf6c554Sdanielk1977  do_test pragma-15.2 {
14868cf6c554Sdanielk1977    sqlite3 db2 test.db
14878cf6c554Sdanielk1977    execsql {
14888cf6c554Sdanielk1977      CREATE TABLE newtable(a, b, c);
14898cf6c554Sdanielk1977    } db2
14908cf6c554Sdanielk1977    db2 close
14918cf6c554Sdanielk1977  } {}
14928cf6c554Sdanielk1977  do_test pragma-15.3 {
14938cf6c554Sdanielk1977    # Evaluating this statement will cause the schema to be reloaded (because
14948cf6c554Sdanielk1977    # the schema was changed by another connection in pragma-15.2). At one
14958cf6c554Sdanielk1977    # point there was a bug that reset the cache_size to its default value
14968cf6c554Sdanielk1977    # when this happened.
14978cf6c554Sdanielk1977    execsql { SELECT * FROM sqlite_master }
14988cf6c554Sdanielk1977    execsql { PRAGMA cache_size }
14998cf6c554Sdanielk1977  } {59}
15008cf6c554Sdanielk1977}
15018cf6c554Sdanielk1977
15025558a8a6Sdanielk1977# Reset the sqlite3_temp_directory variable for the next run of tests:
15035558a8a6Sdanielk1977sqlite3 dbX :memory:
15045558a8a6Sdanielk1977dbX eval {PRAGMA temp_store_directory = ""}
15055558a8a6Sdanielk1977dbX close
15065558a8a6Sdanielk1977
1507838cce43Sdanielk1977ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1508aebf413dSaswift  set sqlite_hostid_num 1
1509aebf413dSaswift
1510aebf413dSaswift  set using_proxy 0
1511aebf413dSaswift  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1512aebf413dSaswift    set using_proxy $value
1513aebf413dSaswift  }
1514aebf413dSaswift
1515aebf413dSaswift  # Test the lock_proxy_file pragmas.
1516aebf413dSaswift  #
1517aebf413dSaswift  db close
1518aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1519aebf413dSaswift
1520aebf413dSaswift  sqlite3 db test.db
1521aebf413dSaswift  do_test pragma-16.1 {
1522aebf413dSaswift    execsql {
1523aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1524aebf413dSaswift      select * from sqlite_master;
1525aebf413dSaswift    }
1526aebf413dSaswift    execsql {
1527aebf413dSaswift      PRAGMA lock_proxy_file;
1528aebf413dSaswift    }
1529aebf413dSaswift  } {mylittleproxy}
1530aebf413dSaswift
1531aebf413dSaswift  do_test pragma-16.2 {
1532aebf413dSaswift    sqlite3 db2 test.db
1533aebf413dSaswift    execsql {
1534aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1535aebf413dSaswift    } db2
1536aebf413dSaswift  } {}
1537aebf413dSaswift
1538aebf413dSaswift  db2 close
1539aebf413dSaswift  do_test pragma-16.2.1 {
1540aebf413dSaswift    sqlite3 db2 test.db
1541aebf413dSaswift    execsql {
1542aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1543aebf413dSaswift      select * from sqlite_master;
1544aebf413dSaswift    } db2
1545aebf413dSaswift    execsql {
1546aebf413dSaswift      PRAGMA lock_proxy_file;
1547aebf413dSaswift    } db2
1548aebf413dSaswift  } {mylittleproxy}
1549aebf413dSaswift
1550aebf413dSaswift  db2 close
1551aebf413dSaswift  do_test pragma-16.3 {
1552aebf413dSaswift    sqlite3 db2 test.db
1553aebf413dSaswift    execsql {
1554aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1555aebf413dSaswift    } db2
1556aebf413dSaswift    catchsql {
1557aebf413dSaswift      select * from sqlite_master;
1558aebf413dSaswift    } db2
1559aebf413dSaswift  } {1 {database is locked}}
1560aebf413dSaswift
1561aebf413dSaswift  do_test pragma-16.4 {
1562aebf413dSaswift    db2 close
1563aebf413dSaswift    db close
1564aebf413dSaswift    sqlite3 db2 test.db
1565aebf413dSaswift    execsql {
1566aebf413dSaswift      PRAGMA lock_proxy_file="myoriginalproxy";
1567aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1568aebf413dSaswift      PRAGMA lock_proxy_file;
1569aebf413dSaswift    } db2
1570aebf413dSaswift  } {myotherproxy}
1571aebf413dSaswift
1572aebf413dSaswift  db2 close
1573aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1574aebf413dSaswift  do_test pragma-16.5 {
1575aebf413dSaswift    sqlite3 db2 test.db
1576aebf413dSaswift    execsql {
1577aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1578aebf413dSaswift      PRAGMA lock_proxy_file;
1579aebf413dSaswift    } db2
1580aebf413dSaswift  } {myotherproxy}
1581aebf413dSaswift
1582aebf413dSaswift  do_test pragma-16.6 {
1583aebf413dSaswift    db2 close
1584aebf413dSaswift    sqlite3 db2 test2.db
1585aebf413dSaswift    set lockpath [execsql {
1586aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1587aebf413dSaswift      PRAGMA lock_proxy_file;
1588aebf413dSaswift    } db2]
1589aebf413dSaswift    string match "*test2.db:auto:" $lockpath
1590aebf413dSaswift  } {1}
1591aebf413dSaswift
1592aebf413dSaswift  set sqlite_hostid_num 2
1593aebf413dSaswift  do_test pragma-16.7 {
159414d14607Sdan    list [catch {
1595aebf413dSaswift      sqlite3 db test2.db
1596aebf413dSaswift      execsql {
1597aebf413dSaswift        PRAGMA lock_proxy_file=":auto:";
1598aebf413dSaswift        select * from sqlite_master;
1599aebf413dSaswift      }
160014d14607Sdan    } msg] $msg
1601aebf413dSaswift  } {1 {database is locked}}
1602aebf413dSaswift  db close
1603aebf413dSaswift
1604aebf413dSaswift  do_test pragma-16.8 {
160514d14607Sdan    list [catch {
1606aebf413dSaswift      sqlite3 db test2.db
160714d14607Sdan      execsql { select * from sqlite_master }
160814d14607Sdan    } msg] $msg
1609aebf413dSaswift  } {1 {database is locked}}
1610aebf413dSaswift
1611aebf413dSaswift  db2 close
1612aebf413dSaswift  do_test pragma-16.8.1 {
1613aebf413dSaswift    execsql {
1614aebf413dSaswift      PRAGMA lock_proxy_file="yetanotherproxy";
1615aebf413dSaswift      PRAGMA lock_proxy_file;
1616aebf413dSaswift    }
1617aebf413dSaswift  } {yetanotherproxy}
1618aebf413dSaswift  do_test pragma-16.8.2 {
1619aebf413dSaswift    execsql {
1620aebf413dSaswift      create table mine(x);
1621aebf413dSaswift    }
1622aebf413dSaswift  } {}
1623aebf413dSaswift
1624aebf413dSaswift  db close
1625aebf413dSaswift  do_test pragma-16.9 {
1626aebf413dSaswift    sqlite3 db proxytest.db
1627aebf413dSaswift    set lockpath2 [execsql {
1628aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1629aebf413dSaswift      PRAGMA lock_proxy_file;
1630aebf413dSaswift    } db]
1631aebf413dSaswift    string match "*proxytest.db:auto:" $lockpath2
1632aebf413dSaswift  } {1}
1633aebf413dSaswift
1634aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1635aebf413dSaswift  set sqlite_hostid_num 0
1636aebf413dSaswift}
1637d2cb50b7Sdrh
1638d2cb50b7Sdrh# Parsing of auto_vacuum settings.
1639d2cb50b7Sdrh#
1640d2cb50b7Sdrhforeach {autovac_setting val} {
1641d2cb50b7Sdrh  0 0
1642d2cb50b7Sdrh  1 1
1643d2cb50b7Sdrh  2 2
1644d2cb50b7Sdrh  3 0
1645d2cb50b7Sdrh  -1 0
1646d2cb50b7Sdrh  none 0
1647d2cb50b7Sdrh  NONE 0
1648d2cb50b7Sdrh  NoNe 0
1649d2cb50b7Sdrh  full 1
1650d2cb50b7Sdrh  FULL 1
1651d2cb50b7Sdrh  incremental 2
1652d2cb50b7Sdrh  INCREMENTAL 2
1653d2cb50b7Sdrh  -1234 0
1654d2cb50b7Sdrh  1234 0
1655d2cb50b7Sdrh} {
1656d2cb50b7Sdrh  do_test pragma-17.1.$autovac_setting {
1657d2cb50b7Sdrh    catch {db close}
1658d2cb50b7Sdrh    sqlite3 db :memory:
1659d2cb50b7Sdrh    execsql "
1660d2cb50b7Sdrh      PRAGMA auto_vacuum=$::autovac_setting;
1661d2cb50b7Sdrh      PRAGMA auto_vacuum;
1662d2cb50b7Sdrh    "
1663d2cb50b7Sdrh  } $val
1664d2cb50b7Sdrh}
1665d2cb50b7Sdrh
1666d2cb50b7Sdrh# Parsing of temp_store settings.
1667d2cb50b7Sdrh#
1668d2cb50b7Sdrhforeach {temp_setting val} {
1669d2cb50b7Sdrh  0 0
1670d2cb50b7Sdrh  1 1
1671d2cb50b7Sdrh  2 2
1672d2cb50b7Sdrh  3 0
1673d2cb50b7Sdrh  -1 0
1674d2cb50b7Sdrh  file 1
1675d2cb50b7Sdrh  FILE 1
1676d2cb50b7Sdrh  fIlE 1
1677d2cb50b7Sdrh  memory 2
1678d2cb50b7Sdrh  MEMORY 2
1679d2cb50b7Sdrh  MeMoRy 2
1680d2cb50b7Sdrh} {
1681d2cb50b7Sdrh  do_test pragma-18.1.$temp_setting {
1682d2cb50b7Sdrh    catch {db close}
1683d2cb50b7Sdrh    sqlite3 db :memory:
1684d2cb50b7Sdrh    execsql "
1685d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1686d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1687d2cb50b7Sdrh      PRAGMA temp_store;
1688d2cb50b7Sdrh    "
1689d2cb50b7Sdrh  } $val
1690d2cb50b7Sdrh}
1691d2cb50b7Sdrh
169292c700dbSdrh# The SQLITE_FCNTL_PRAGMA logic, with error handling.
169392c700dbSdrh#
169492c700dbSdrhdb close
169592c700dbSdrhtestvfs tvfs
169692c700dbSdrhsqlite3 db test.db -vfs tvfs
169792c700dbSdrhdo_test pragma-19.1 {
169892c700dbSdrh  catchsql {PRAGMA error}
1699a690ff36Sdrh} {1 {SQL logic error}}
170092c700dbSdrhdo_test pragma-19.2 {
170192c700dbSdrh  catchsql {PRAGMA error='This is the error message'}
170292c700dbSdrh} {1 {This is the error message}}
170392c700dbSdrhdo_test pragma-19.3 {
170492c700dbSdrh  catchsql {PRAGMA error='7 This is the error message'}
170592c700dbSdrh} {1 {This is the error message}}
170692c700dbSdrhdo_test pragma-19.4 {
170792c700dbSdrh  catchsql {PRAGMA error=7}
170892c700dbSdrh} {1 {out of memory}}
1709c8517f61Sdrhdo_test pragma-19.5 {
17105b044545Smistachkin  file tail [lindex [execsql {PRAGMA filename}] 0]
1711c8517f61Sdrh} {test.db}
171292c700dbSdrh
1713cc71645bSdrhif {$tcl_platform(platform)=="windows"} {
1714a112d140Smistachkin# Test data_store_directory pragma
1715a112d140Smistachkin#
1716a112d140Smistachkindb close
1717a112d140Smistachkinsqlite3 db test.db
1718a112d140Smistachkinfile mkdir data_dir
1719a112d140Smistachkindo_test pragma-20.1 {
1720a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1721a112d140Smistachkin} {0 {}}
1722a112d140Smistachkindo_test pragma-20.2 {
1723a112d140Smistachkin  set pwd [string map {' ''} [file nativename [get_pwd]]]
1724a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1725a112d140Smistachkin} {0 {}}
1726a112d140Smistachkindo_test pragma-20.3 {
1727a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1728a112d140Smistachkin} [list 0 [list [file nativename [get_pwd]]]]
1729a112d140Smistachkindo_test pragma-20.4 {
1730a112d140Smistachkin  set pwd [string map {' ''} [file nativename \
1731a112d140Smistachkin    [file join [get_pwd] data_dir]]]
1732a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1733a112d140Smistachkin} {0 {}}
1734a112d140Smistachkindo_test pragma-20.5 {
1735a112d140Smistachkin  sqlite3 db2 test2.db
1736a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1737a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1738a112d140Smistachkin    [file join [get_pwd] data_dir test2.db]]]]
1739a112d140Smistachkincatch {db2 close}
1740a112d140Smistachkindo_test pragma-20.6 {
1741a112d140Smistachkin  sqlite3 db2 [file join [get_pwd] test2.db]
1742a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1743a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1744a112d140Smistachkin    [file join [get_pwd] test2.db]]]]
1745a112d140Smistachkincatch {db2 close}
1746a112d140Smistachkindo_test pragma-20.7 {
1747a112d140Smistachkin  catchsql "PRAGMA data_store_directory='';"
1748a112d140Smistachkin} {0 {}}
1749a112d140Smistachkindo_test pragma-20.8 {
1750a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1751a112d140Smistachkin} {0 {}}
175292c700dbSdrh
1753a112d140Smistachkinforcedelete data_dir
1754cc71645bSdrh} ;# endif windows
1755cd61c281Sdrh
17561fed5dabSdandatabase_may_be_corrupt
17577da56b4fSdrhif {![nonzero_reserved_bytes]} {
17581fed5dabSdan
17595885e762Sdan  do_test 21.1 {
17605885e762Sdan    # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
17615885e762Sdan    #
17625885e762Sdan    db close
17635885e762Sdan    forcedelete test.db
17645885e762Sdan    sqlite3 db test.db
17655885e762Sdan    execsql {
17665885e762Sdan      PRAGMA page_size = 1024;
17675885e762Sdan      PRAGMA auto_vacuum = 0;
17685885e762Sdan      CREATE TABLE t1(a PRIMARY KEY, b);
17695885e762Sdan      INSERT INTO t1 VALUES(1, 1);
17705885e762Sdan    }
17715885e762Sdan    for {set i 0} {$i < 10} {incr i} {
17725885e762Sdan      execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
17735885e762Sdan    }
17745885e762Sdan    db close
17755885e762Sdan    forcecopy test.db testerr.db
17765885e762Sdan    hexio_write testerr.db 15000 [string repeat 55 100]
17775885e762Sdan  } {100}
17785885e762Sdan
17795885e762Sdan  set mainerr {*** in database main ***
17805885e762SdanMultiple uses for byte 672 of page 15}
17815885e762Sdan  set auxerr {*** in database aux ***
17825885e762SdanMultiple uses for byte 672 of page 15}
17835885e762Sdan
1784597515d7Sdan  set mainerr {/{\*\*\* in database main \*\*\*
1785597515d7SdanMultiple uses for byte 672 of page 15}.*/}
1786597515d7Sdan  set auxerr {/{\*\*\* in database aux \*\*\*
1787597515d7SdanMultiple uses for byte 672 of page 15}.*/}
1788597515d7Sdan
17895885e762Sdan  do_test 22.2 {
17905885e762Sdan    catch { db close }
17915885e762Sdan    sqlite3 db testerr.db
17925885e762Sdan    execsql { PRAGMA integrity_check }
1793597515d7Sdan  } $mainerr
17945885e762Sdan
17955885e762Sdan  do_test 22.3.1 {
17965885e762Sdan    catch { db close }
17975885e762Sdan    sqlite3 db test.db
17985885e762Sdan    execsql {
17995885e762Sdan      ATTACH 'testerr.db' AS 'aux';
18005885e762Sdan      PRAGMA integrity_check;
18015885e762Sdan    }
1802597515d7Sdan  } $auxerr
18035885e762Sdan  do_test 22.3.2 {
18045885e762Sdan    execsql { PRAGMA main.integrity_check; }
18055885e762Sdan  } {ok}
18065885e762Sdan  do_test 22.3.3 {
18075885e762Sdan    execsql { PRAGMA aux.integrity_check; }
1808597515d7Sdan  } $auxerr
18095885e762Sdan
18105885e762Sdan  do_test 22.4.1 {
18115885e762Sdan    catch { db close }
18125885e762Sdan    sqlite3 db testerr.db
18135885e762Sdan    execsql {
18145885e762Sdan      ATTACH 'test.db' AS 'aux';
18155885e762Sdan      PRAGMA integrity_check;
18165885e762Sdan    }
1817597515d7Sdan  } $mainerr
18185885e762Sdan  do_test 22.4.2 {
18195885e762Sdan    execsql { PRAGMA main.integrity_check; }
1820597515d7Sdan  } $mainerr
18215885e762Sdan  do_test 22.4.3 {
18225885e762Sdan    execsql { PRAGMA aux.integrity_check; }
18235885e762Sdan  } {ok}
18247da56b4fSdrh}
18255885e762Sdan
1826c95e01daSdrhdb close
1827c95e01daSdrhforcedelete test.db test.db-wal test.db-journal
1828c95e01daSdrhsqlite3 db test.db
1829c95e01daSdrhsqlite3 db2 test.db
1830c95e01daSdrhdo_test 23.1 {
1831c95e01daSdrh  db eval {
1832c95e01daSdrh    CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1833c95e01daSdrh    CREATE INDEX i1 ON t1(b,c);
1834c95e01daSdrh    CREATE INDEX i2 ON t1(c,d);
18357be0fd9cSdrh    CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
1836c95e01daSdrh    CREATE TABLE t2(x INTEGER REFERENCES t1);
1837c95e01daSdrh  }
1838c95e01daSdrh  db2 eval {SELECT name FROM sqlite_master}
18397be0fd9cSdrh} {t1 i1 i2 i2x t2}
1840c228be5bSdrhdo_test 23.2a {
1841c95e01daSdrh  db eval {
1842c95e01daSdrh    DROP INDEX i2;
1843c95e01daSdrh    CREATE INDEX i2 ON t1(c,d,b);
1844c95e01daSdrh  }
1845c228be5bSdrh  capture_pragma db2 out {PRAGMA index_info(i2)}
18465e7028c2Sdrh  db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
18475e7028c2Sdrh} {2 c | 3 d | 1 b |}
18487be0fd9cSdrh
1849b3366b99Sdrh# EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1850b3366b99Sdrh# pragma returns information about every column in an index.
18517be0fd9cSdrh#
18527be0fd9cSdrh# EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
18537be0fd9cSdrh# returns information about every column in the index, not just the key
18547be0fd9cSdrh# columns.
18557be0fd9cSdrh#
1856c228be5bSdrhdo_test 23.2b {
1857c228be5bSdrh  capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1858c228be5bSdrh  db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1859c228be5bSdrh} {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
18607be0fd9cSdrh
18617be0fd9cSdrh# (The first column of output from PRAGMA index_xinfo is...)
18627be0fd9cSdrh# EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
18637be0fd9cSdrh# means left-most. Key columns come before auxiliary columns.)
18647be0fd9cSdrh#
18657be0fd9cSdrh# (The second column of output from PRAGMA index_xinfo is...)
18667be0fd9cSdrh# EVIDENCE-OF: R-40889-06838 The rank of the column within the table
18677be0fd9cSdrh# being indexed, or -1 if the index-column is the rowid of the table
18687be0fd9cSdrh# being indexed.
18697be0fd9cSdrh#
18707be0fd9cSdrh# (The third column of output from PRAGMA index_xinfo is...)
18717be0fd9cSdrh# EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
18727be0fd9cSdrh# NULL if the index-column is the rowid of the table being indexed.
18737be0fd9cSdrh#
18747be0fd9cSdrh# (The fourth column of output from PRAGMA index_xinfo is...)
18757be0fd9cSdrh# EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
18767be0fd9cSdrh# (DESC) order by the index and 0 otherwise.
18777be0fd9cSdrh#
18787be0fd9cSdrh# (The fifth column of output from PRAGMA index_xinfo is...)
18797be0fd9cSdrh# EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
18807be0fd9cSdrh# compare values in the index-column.
18817be0fd9cSdrh#
18827be0fd9cSdrh# (The sixth column of output from PRAGMA index_xinfo is...)
18837be0fd9cSdrh# EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
18847be0fd9cSdrh# if the index-column is an auxiliary column.
18857be0fd9cSdrh#
18867be0fd9cSdrhdo_test 23.2c {
18877be0fd9cSdrh  db2 eval {PRAGMA index_xinfo(i2)}
18887be0fd9cSdrh} {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
18897be0fd9cSdrhdo_test 23.2d {
18907be0fd9cSdrh  db2 eval {PRAGMA index_xinfo(i2x)}
18917be0fd9cSdrh} {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
18927be0fd9cSdrh
1893b3366b99Sdrh# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1894b3366b99Sdrh# pragma returns one row for each index associated with the given table.
18957be0fd9cSdrh#
18967be0fd9cSdrh# (The first column of output from PRAGMA index_list is...)
18977be0fd9cSdrh# EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
18987be0fd9cSdrh# for internal tracking purposes.
18997be0fd9cSdrh#
19007be0fd9cSdrh# (The second column of output from PRAGMA index_list is...)
19017be0fd9cSdrh# EVIDENCE-OF: R-35496-03635 The name of the index.
19027be0fd9cSdrh#
19037be0fd9cSdrh# (The third column of output from PRAGMA index_list is...)
19047be0fd9cSdrh# EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
19057be0fd9cSdrh#
19067be0fd9cSdrh# (The fourth column of output from PRAGMA index_list is...)
19077be0fd9cSdrh# EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
19087be0fd9cSdrh# INDEX statement, "u" if the index was created by a UNIQUE constraint,
19097be0fd9cSdrh# or "pk" if the index was created by a PRIMARY KEY constraint.
19107be0fd9cSdrh#
1911c95e01daSdrhdo_test 23.3 {
1912c95e01daSdrh  db eval {
1913c95e01daSdrh    CREATE INDEX i3 ON t1(d,b,c);
1914c95e01daSdrh  }
1915c228be5bSdrh  capture_pragma db2 out {PRAGMA index_list(t1)}
19167be0fd9cSdrh  db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
19177be0fd9cSdrh} {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
1918c95e01daSdrhdo_test 23.4 {
1919c95e01daSdrh  db eval {
1920c95e01daSdrh    ALTER TABLE t1 ADD COLUMN e;
1921c95e01daSdrh  }
1922c95e01daSdrh  db2 eval {
1923c95e01daSdrh    PRAGMA table_info(t1);
1924c95e01daSdrh  }
1925c95e01daSdrh} {/4 e {} 0 {} 0/}
1926c95e01daSdrhdo_test 23.5 {
1927c95e01daSdrh  db eval {
1928c95e01daSdrh    DROP TABLE t2;
1929c95e01daSdrh    CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1930c95e01daSdrh  }
1931c95e01daSdrh  db2 eval {
1932c95e01daSdrh    PRAGMA foreign_key_list(t2);
1933c95e01daSdrh  }
1934c95e01daSdrh} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
19353e1e14d8Sdrhdb2 close
1936c95e01daSdrh
1937*efeaec36Sdrhifcapable !has_codec {
193838413723Sdan  reset_db
193938413723Sdan  do_execsql_test 24.0 {
194038413723Sdan    PRAGMA page_size = 1024;
194138413723Sdan    CREATE TABLE t1(a, b, c);
194238413723Sdan    CREATE INDEX i1 ON t1(b);
194338413723Sdan    INSERT INTO t1 VALUES('a', 'b', 'c');
194438413723Sdan    PRAGMA integrity_check;
194538413723Sdan  } {ok}
194638413723Sdan
194738413723Sdan  set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
194838413723Sdan  db close
194938413723Sdan  hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
195038413723Sdan
195138413723Sdan  sqlite3 db test.db
195238413723Sdan  do_catchsql_test 24.1 {
195338413723Sdan    SELECT * FROM t1;
195438413723Sdan  } {1 {database disk image is malformed}}
195538413723Sdan  do_catchsql_test 24.2 {
195638413723Sdan    PRAGMA integrity_check;
195774588cebSdrh  } {0 {{database disk image is malformed}}}
1958*efeaec36Sdrh}
19591fed5dabSdandatabase_never_corrupt
1960cd61c281Sdrhfinish_test
1961