xref: /sqlite-3.40.0/test/pragma.test (revision c95e01da)
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
54cd61c281Sdrh# Delete the preexisting database to avoid the special setup
55cd61c281Sdrh# that the "all.test" script does.
56cd61c281Sdrh#
57cd61c281Sdrhdb close
58fda06befSmistachkindelete_file test.db test.db-journal
59fda06befSmistachkindelete_file test3.db test3.db-journal
60dddca286Sdrhsqlite3 db test.db; set DB [sqlite3_connection_pointer db]
61cd61c281Sdrh
621e9daa6aSdrh
63c7b4a44cSdanielk1977ifcapable pager_pragmas {
641e9daa6aSdrhset DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
651e9daa6aSdrhset TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
66cd61c281Sdrhdo_test pragma-1.1 {
67cd61c281Sdrh  execsql {
68cd61c281Sdrh    PRAGMA cache_size;
69cd61c281Sdrh    PRAGMA default_cache_size;
70cd61c281Sdrh    PRAGMA synchronous;
71cd61c281Sdrh  }
721e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
73cd61c281Sdrhdo_test pragma-1.2 {
74cd61c281Sdrh  execsql {
75eb43e5c7Sdrh    PRAGMA synchronous=OFF;
76cd61c281Sdrh    PRAGMA cache_size=1234;
77cd61c281Sdrh    PRAGMA cache_size;
78cd61c281Sdrh    PRAGMA default_cache_size;
79cd61c281Sdrh    PRAGMA synchronous;
80cd61c281Sdrh  }
811e9daa6aSdrh} [list 1234 $DFLT_CACHE_SZ 0]
82cd61c281Sdrhdo_test pragma-1.3 {
83cd61c281Sdrh  db close
84ef4ac8f9Sdrh  sqlite3 db test.db
85cd61c281Sdrh  execsql {
86cd61c281Sdrh    PRAGMA cache_size;
87cd61c281Sdrh    PRAGMA default_cache_size;
88cd61c281Sdrh    PRAGMA synchronous;
89cd61c281Sdrh  }
901e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
91cd61c281Sdrhdo_test pragma-1.4 {
92cd61c281Sdrh  execsql {
93cd61c281Sdrh    PRAGMA synchronous=OFF;
94cd61c281Sdrh    PRAGMA cache_size;
95cd61c281Sdrh    PRAGMA default_cache_size;
96cd61c281Sdrh    PRAGMA synchronous;
97cd61c281Sdrh  }
981e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
99cd61c281Sdrhdo_test pragma-1.5 {
100cd61c281Sdrh  execsql {
101d2cb50b7Sdrh    PRAGMA cache_size=-4321;
102cd61c281Sdrh    PRAGMA cache_size;
103cd61c281Sdrh    PRAGMA default_cache_size;
104cd61c281Sdrh    PRAGMA synchronous;
105cd61c281Sdrh  }
1063b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 0]
107cd61c281Sdrhdo_test pragma-1.6 {
108cd61c281Sdrh  execsql {
109cd61c281Sdrh    PRAGMA synchronous=ON;
110cd61c281Sdrh    PRAGMA cache_size;
111cd61c281Sdrh    PRAGMA default_cache_size;
112cd61c281Sdrh    PRAGMA synchronous;
113cd61c281Sdrh  }
1143b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 1]
115cd61c281Sdrhdo_test pragma-1.7 {
116cd61c281Sdrh  db close
117ef4ac8f9Sdrh  sqlite3 db test.db
118cd61c281Sdrh  execsql {
119cd61c281Sdrh    PRAGMA cache_size;
120cd61c281Sdrh    PRAGMA default_cache_size;
121cd61c281Sdrh    PRAGMA synchronous;
122cd61c281Sdrh  }
1231e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
124cd61c281Sdrhdo_test pragma-1.8 {
125cd61c281Sdrh  execsql {
126d2cb50b7Sdrh    PRAGMA default_cache_size=-123;
127cd61c281Sdrh    PRAGMA cache_size;
128cd61c281Sdrh    PRAGMA default_cache_size;
129cd61c281Sdrh    PRAGMA synchronous;
130cd61c281Sdrh  }
13191cf71b0Sdanielk1977} {123 123 2}
132802d69a7Sdrhdo_test pragma-1.9.1 {
133cd61c281Sdrh  db close
134dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
135cd61c281Sdrh  execsql {
136cd61c281Sdrh    PRAGMA cache_size;
137cd61c281Sdrh    PRAGMA default_cache_size;
138cd61c281Sdrh    PRAGMA synchronous;
139cd61c281Sdrh  }
14091cf71b0Sdanielk1977} {123 123 2}
141802d69a7Sdrhifcapable vacuum {
142802d69a7Sdrh  do_test pragma-1.9.2 {
143802d69a7Sdrh    execsql {
144802d69a7Sdrh      VACUUM;
145802d69a7Sdrh      PRAGMA cache_size;
146802d69a7Sdrh      PRAGMA default_cache_size;
147802d69a7Sdrh      PRAGMA synchronous;
148802d69a7Sdrh    }
149802d69a7Sdrh  } {123 123 2}
150802d69a7Sdrh}
15191cf71b0Sdanielk1977do_test pragma-1.10 {
1525a38705eSdrh  execsql {
1534303feedSdrh    PRAGMA synchronous=NORMAL;
1544303feedSdrh    PRAGMA cache_size;
1554303feedSdrh    PRAGMA default_cache_size;
1564303feedSdrh    PRAGMA synchronous;
1574303feedSdrh  }
15891cf71b0Sdanielk1977} {123 123 1}
15991cf71b0Sdanielk1977do_test pragma-1.11 {
1604303feedSdrh  execsql {
1614303feedSdrh    PRAGMA synchronous=FULL;
1624303feedSdrh    PRAGMA cache_size;
1634303feedSdrh    PRAGMA default_cache_size;
1644303feedSdrh    PRAGMA synchronous;
1654303feedSdrh  }
16691cf71b0Sdanielk1977} {123 123 2}
16791cf71b0Sdanielk1977do_test pragma-1.12 {
1684303feedSdrh  db close
169dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
1704303feedSdrh  execsql {
1714303feedSdrh    PRAGMA cache_size;
1724303feedSdrh    PRAGMA default_cache_size;
1734303feedSdrh    PRAGMA synchronous;
1744303feedSdrh  }
17591cf71b0Sdanielk1977} {123 123 2}
17691cf71b0Sdanielk1977
1775260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition
1785260f7e9Sdrh# to keywords like "off" and "full".
1795260f7e9Sdrh#
1805260f7e9Sdrhdo_test pragma-1.13 {
1815260f7e9Sdrh  execsql {
1825260f7e9Sdrh    PRAGMA synchronous=0;
1835260f7e9Sdrh    PRAGMA synchronous;
1845260f7e9Sdrh  }
1855260f7e9Sdrh} {0}
1865260f7e9Sdrhdo_test pragma-1.14 {
1875260f7e9Sdrh  execsql {
1885260f7e9Sdrh    PRAGMA synchronous=2;
1895260f7e9Sdrh    PRAGMA synchronous;
1905260f7e9Sdrh  }
1915260f7e9Sdrh} {2}
192c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
1935260f7e9Sdrh
1945260f7e9Sdrh# Test turning "flag" pragmas on and off.
1955260f7e9Sdrh#
1966338c762Sdanielk1977ifcapable debug {
1976338c762Sdanielk1977  # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
1986338c762Sdanielk1977  #
1995260f7e9Sdrh  do_test pragma-1.15 {
2005260f7e9Sdrh    execsql {
2015260f7e9Sdrh      PRAGMA vdbe_listing=YES;
2025260f7e9Sdrh      PRAGMA vdbe_listing;
2035260f7e9Sdrh    }
2045260f7e9Sdrh  } {1}
2055260f7e9Sdrh  do_test pragma-1.16 {
2065260f7e9Sdrh    execsql {
2075260f7e9Sdrh      PRAGMA vdbe_listing=NO;
2085260f7e9Sdrh      PRAGMA vdbe_listing;
2095260f7e9Sdrh    }
2105260f7e9Sdrh  } {0}
2116338c762Sdanielk1977}
2126338c762Sdanielk1977
2135260f7e9Sdrhdo_test pragma-1.17 {
2145260f7e9Sdrh  execsql {
2155260f7e9Sdrh    PRAGMA parser_trace=ON;
2165260f7e9Sdrh    PRAGMA parser_trace=OFF;
2175260f7e9Sdrh  }
2185260f7e9Sdrh} {}
2195260f7e9Sdrhdo_test pragma-1.18 {
2205260f7e9Sdrh  execsql {
2215260f7e9Sdrh    PRAGMA bogus = -1234;  -- Parsing of negative values
2225260f7e9Sdrh  }
2235260f7e9Sdrh} {}
2245260f7e9Sdrh
22591cf71b0Sdanielk1977# Test modifying the safety_level of an attached database.
2265a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
22791cf71b0Sdanielk1977  do_test pragma-2.1 {
228fda06befSmistachkin    forcedelete test2.db
229fda06befSmistachkin    forcedelete test2.db-journal
2304303feedSdrh    execsql {
23191cf71b0Sdanielk1977      ATTACH 'test2.db' AS aux;
2324303feedSdrh    }
23391cf71b0Sdanielk1977  } {}
23491cf71b0Sdanielk1977  do_test pragma-2.2 {
2354303feedSdrh    execsql {
23691cf71b0Sdanielk1977      pragma aux.synchronous;
2374303feedSdrh    }
23891cf71b0Sdanielk1977  } {2}
23991cf71b0Sdanielk1977  do_test pragma-2.3 {
24091cf71b0Sdanielk1977    execsql {
24191cf71b0Sdanielk1977      pragma aux.synchronous = OFF;
24291cf71b0Sdanielk1977      pragma aux.synchronous;
24391cf71b0Sdanielk1977      pragma synchronous;
24491cf71b0Sdanielk1977    }
24591cf71b0Sdanielk1977  } {0 2}
24691cf71b0Sdanielk1977  do_test pragma-2.4 {
24791cf71b0Sdanielk1977    execsql {
24891cf71b0Sdanielk1977      pragma aux.synchronous = ON;
24991cf71b0Sdanielk1977      pragma synchronous;
25091cf71b0Sdanielk1977      pragma aux.synchronous;
25191cf71b0Sdanielk1977    }
25291cf71b0Sdanielk1977  } {2 1}
253c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
2544303feedSdrh
255ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check
256ed717fe3Sdrh# pragma finds it.
257ed717fe3Sdrh#
25825d6543dSdrh# These tests won't work if the database is encrypted
25925d6543dSdrh#
260ed717fe3Sdrhdo_test pragma-3.1 {
2611e9daa6aSdrh  db close
262fda06befSmistachkin  forcedelete test.db test.db-journal
2631e9daa6aSdrh  sqlite3 db test.db
264ed717fe3Sdrh  execsql {
2651e9daa6aSdrh    PRAGMA auto_vacuum=OFF;
266ed717fe3Sdrh    BEGIN;
267ed717fe3Sdrh    CREATE TABLE t2(a,b,c);
268ed717fe3Sdrh    CREATE INDEX i2 ON t2(a);
269ed717fe3Sdrh    INSERT INTO t2 VALUES(11,2,3);
270ed717fe3Sdrh    INSERT INTO t2 VALUES(22,3,4);
271ed717fe3Sdrh    COMMIT;
272ed717fe3Sdrh    SELECT rowid, * from t2;
273ed717fe3Sdrh  }
274ed717fe3Sdrh} {1 11 2 3 2 22 3 4}
2755a8f9374Sdanielk1977ifcapable attach {
27640e016e4Sdrh  if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
277ed717fe3Sdrh    do_test pragma-3.2 {
278bb8a279eSdrh      db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
279bb8a279eSdrh      set pgsz [db eval {PRAGMA page_size}]
280bb8a279eSdrh      # overwrite the header on the rootpage of the index in order to
281bb8a279eSdrh      # make the index appear to be empty.
282bb8a279eSdrh      #
283bb8a279eSdrh      set offset [expr {$pgsz*($rootpage-1)}]
284bb8a279eSdrh      hexio_write test.db $offset 0a00000000040000000000
285bb8a279eSdrh      db close
286bb8a279eSdrh      sqlite3 db test.db
287ed717fe3Sdrh      execsql {PRAGMA integrity_check}
288bb8a279eSdrh    } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
28991cf71b0Sdanielk1977    do_test pragma-3.3 {
2901dcdbc06Sdrh      execsql {PRAGMA integrity_check=1}
2911dcdbc06Sdrh    } {{rowid 1 missing from index i2}}
2921dcdbc06Sdrh    do_test pragma-3.4 {
29391cf71b0Sdanielk1977      execsql {
2941dcdbc06Sdrh        ATTACH DATABASE 'test.db' AS t2;
2951dcdbc06Sdrh        PRAGMA integrity_check
29691cf71b0Sdanielk1977      }
297bb8a279eSdrh    } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
2981dcdbc06Sdrh    do_test pragma-3.5 {
2991dcdbc06Sdrh      execsql {
300bb8a279eSdrh        PRAGMA integrity_check=4
3011dcdbc06Sdrh      }
302bb8a279eSdrh    } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2}}
3031dcdbc06Sdrh    do_test pragma-3.6 {
3041dcdbc06Sdrh      execsql {
3051dcdbc06Sdrh        PRAGMA integrity_check=xyz
3061dcdbc06Sdrh      }
307bb8a279eSdrh    } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
3081dcdbc06Sdrh    do_test pragma-3.7 {
3091dcdbc06Sdrh      execsql {
3101dcdbc06Sdrh        PRAGMA integrity_check=0
3111dcdbc06Sdrh      }
312bb8a279eSdrh    } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
3131dcdbc06Sdrh
3141dcdbc06Sdrh    # Add additional corruption by appending unused pages to the end of
3151dcdbc06Sdrh    # the database file testerr.db
3161dcdbc06Sdrh    #
3171dcdbc06Sdrh    do_test pragma-3.8 {
3181dcdbc06Sdrh      execsql {DETACH t2}
319fda06befSmistachkin      forcedelete testerr.db testerr.db-journal
320e3d559adSdrh      set out [open testerr.db w]
321e3d559adSdrh      fconfigure $out -translation binary
322e3d559adSdrh      set in [open test.db r]
323e3d559adSdrh      fconfigure $in -translation binary
3241dcdbc06Sdrh      puts -nonewline $out [read $in]
3251dcdbc06Sdrh      seek $in 0
3261dcdbc06Sdrh      puts -nonewline $out [read $in]
3271dcdbc06Sdrh      close $in
3281dcdbc06Sdrh      close $out
329dd3cd977Sdrh      hexio_write testerr.db 28 00000000
3301dcdbc06Sdrh      execsql {REINDEX t2}
3311dcdbc06Sdrh      execsql {PRAGMA integrity_check}
3321dcdbc06Sdrh    } {ok}
33341c58b78Sdanielk1977    do_test pragma-3.8.1 {
33441c58b78Sdanielk1977      execsql {PRAGMA quick_check}
33541c58b78Sdanielk1977    } {ok}
3365d16a9a6Sdrh    do_test pragma-3.8.2 {
3375d16a9a6Sdrh      execsql {PRAGMA QUICK_CHECK}
3385d16a9a6Sdrh    } {ok}
3391dcdbc06Sdrh    do_test pragma-3.9 {
3401dcdbc06Sdrh      execsql {
3411dcdbc06Sdrh        ATTACH 'testerr.db' AS t2;
3421dcdbc06Sdrh        PRAGMA integrity_check
3431dcdbc06Sdrh      }
3441dcdbc06Sdrh    } {{*** in database t2 ***
3451dcdbc06SdrhPage 4 is never used
3461dcdbc06SdrhPage 5 is never used
347bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
3481dcdbc06Sdrh    do_test pragma-3.10 {
3491dcdbc06Sdrh      execsql {
3501dcdbc06Sdrh        PRAGMA integrity_check=1
3511dcdbc06Sdrh      }
3521dcdbc06Sdrh    } {{*** in database t2 ***
3531dcdbc06SdrhPage 4 is never used}}
3541dcdbc06Sdrh    do_test pragma-3.11 {
3551dcdbc06Sdrh      execsql {
3561dcdbc06Sdrh        PRAGMA integrity_check=5
3571dcdbc06Sdrh      }
3581dcdbc06Sdrh    } {{*** in database t2 ***
3591dcdbc06SdrhPage 4 is never used
3601dcdbc06SdrhPage 5 is never used
361bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2}}
3621dcdbc06Sdrh    do_test pragma-3.12 {
3631dcdbc06Sdrh      execsql {
3641dcdbc06Sdrh        PRAGMA integrity_check=4
3651dcdbc06Sdrh      }
3661dcdbc06Sdrh    } {{*** in database t2 ***
3671dcdbc06SdrhPage 4 is never used
3681dcdbc06SdrhPage 5 is never used
3691dcdbc06SdrhPage 6 is never used} {rowid 1 missing from index i2}}
3701dcdbc06Sdrh    do_test pragma-3.13 {
3711dcdbc06Sdrh      execsql {
3721dcdbc06Sdrh        PRAGMA integrity_check=3
3731dcdbc06Sdrh      }
3741dcdbc06Sdrh    } {{*** in database t2 ***
3751dcdbc06SdrhPage 4 is never used
3761dcdbc06SdrhPage 5 is never used
3771dcdbc06SdrhPage 6 is never used}}
3781dcdbc06Sdrh    do_test pragma-3.14 {
3791dcdbc06Sdrh      execsql {
3801dcdbc06Sdrh        PRAGMA integrity_check(2)
3811dcdbc06Sdrh      }
3821dcdbc06Sdrh    } {{*** in database t2 ***
3831dcdbc06SdrhPage 4 is never used
3841dcdbc06SdrhPage 5 is never used}}
3851dcdbc06Sdrh    do_test pragma-3.15 {
3861dcdbc06Sdrh      execsql {
3871dcdbc06Sdrh        ATTACH 'testerr.db' AS t3;
3881dcdbc06Sdrh        PRAGMA integrity_check
3891dcdbc06Sdrh      }
3901dcdbc06Sdrh    } {{*** in database t2 ***
3911dcdbc06SdrhPage 4 is never used
3921dcdbc06SdrhPage 5 is never used
393bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
3941dcdbc06SdrhPage 4 is never used
3951dcdbc06SdrhPage 5 is never used
396bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
3971dcdbc06Sdrh    do_test pragma-3.16 {
3981dcdbc06Sdrh      execsql {
399bb8a279eSdrh        PRAGMA integrity_check(10)
4001dcdbc06Sdrh      }
4011dcdbc06Sdrh    } {{*** in database t2 ***
4021dcdbc06SdrhPage 4 is never used
4031dcdbc06SdrhPage 5 is never used
404bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
4051dcdbc06SdrhPage 4 is never used
4061dcdbc06SdrhPage 5 is never used
4071dcdbc06SdrhPage 6 is never used} {rowid 1 missing from index i2}}
4081dcdbc06Sdrh    do_test pragma-3.17 {
4091dcdbc06Sdrh      execsql {
410bb8a279eSdrh        PRAGMA integrity_check=8
4111dcdbc06Sdrh      }
4121dcdbc06Sdrh    } {{*** in database t2 ***
4131dcdbc06SdrhPage 4 is never used
4141dcdbc06SdrhPage 5 is never used
415bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
4161dcdbc06SdrhPage 4 is never used
4171dcdbc06SdrhPage 5 is never used}}
4181dcdbc06Sdrh    do_test pragma-3.18 {
4191dcdbc06Sdrh      execsql {
4201dcdbc06Sdrh        PRAGMA integrity_check=4
4211dcdbc06Sdrh      }
4221dcdbc06Sdrh    } {{*** in database t2 ***
4231dcdbc06SdrhPage 4 is never used
4241dcdbc06SdrhPage 5 is never used
4251dcdbc06SdrhPage 6 is never used} {rowid 1 missing from index i2}}
4261dcdbc06Sdrh  }
427d2cb50b7Sdrh  do_test pragma-3.19 {
428d2cb50b7Sdrh    catch {db close}
429fda06befSmistachkin    forcedelete test.db test.db-journal
430d2cb50b7Sdrh    sqlite3 db test.db
431d2cb50b7Sdrh    db eval {PRAGMA integrity_check}
432d2cb50b7Sdrh  } {ok}
4335a8f9374Sdanielk1977}
434d2cb50b7Sdrh#exit
43591cf71b0Sdanielk1977
43691cf71b0Sdanielk1977# Test modifying the cache_size of an attached database.
4375a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
43891cf71b0Sdanielk1977do_test pragma-4.1 {
43991cf71b0Sdanielk1977  execsql {
4401e9daa6aSdrh    ATTACH 'test2.db' AS aux;
44191cf71b0Sdanielk1977    pragma aux.cache_size;
44291cf71b0Sdanielk1977    pragma aux.default_cache_size;
44391cf71b0Sdanielk1977  }
4441e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
4451bdd9b57Sdrhdo_test pragma-4.2 {
4461bdd9b57Sdrh  execsql {
44791cf71b0Sdanielk1977    pragma aux.cache_size = 50;
44891cf71b0Sdanielk1977    pragma aux.cache_size;
44991cf71b0Sdanielk1977    pragma aux.default_cache_size;
4501bdd9b57Sdrh  }
4511e9daa6aSdrh} [list 50 $DFLT_CACHE_SZ]
4521bdd9b57Sdrhdo_test pragma-4.3 {
4531bdd9b57Sdrh  execsql {
45491cf71b0Sdanielk1977    pragma aux.default_cache_size = 456;
45591cf71b0Sdanielk1977    pragma aux.cache_size;
45691cf71b0Sdanielk1977    pragma aux.default_cache_size;
4571bdd9b57Sdrh  }
45891cf71b0Sdanielk1977} {456 456}
4591bdd9b57Sdrhdo_test pragma-4.4 {
4601bdd9b57Sdrh  execsql {
46191cf71b0Sdanielk1977    pragma cache_size;
46291cf71b0Sdanielk1977    pragma default_cache_size;
4631bdd9b57Sdrh  }
4641e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
4651bdd9b57Sdrhdo_test pragma-4.5 {
4661bdd9b57Sdrh  execsql {
46791cf71b0Sdanielk1977    DETACH aux;
46891cf71b0Sdanielk1977    ATTACH 'test3.db' AS aux;
46991cf71b0Sdanielk1977    pragma aux.cache_size;
47091cf71b0Sdanielk1977    pragma aux.default_cache_size;
4711bdd9b57Sdrh  }
4721e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
4731bdd9b57Sdrhdo_test pragma-4.6 {
4741bdd9b57Sdrh  execsql {
47591cf71b0Sdanielk1977    DETACH aux;
47691cf71b0Sdanielk1977    ATTACH 'test2.db' AS aux;
47791cf71b0Sdanielk1977    pragma aux.cache_size;
47891cf71b0Sdanielk1977    pragma aux.default_cache_size;
4791bdd9b57Sdrh  }
48091cf71b0Sdanielk1977} {456 456}
481c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
4821bdd9b57Sdrh
48391cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is
48491cf71b0Sdanielk1977# disallowed.
485c7b4a44cSdanielk1977ifcapable pager_pragmas {
48691cf71b0Sdanielk1977do_test pragma-5.0 {
4871bdd9b57Sdrh  execsql {
48891cf71b0Sdanielk1977    pragma synchronous;
4891bdd9b57Sdrh  }
49091cf71b0Sdanielk1977} {2}
49191cf71b0Sdanielk1977do_test pragma-5.1 {
4921bdd9b57Sdrh  catchsql {
4931bdd9b57Sdrh    BEGIN;
49491cf71b0Sdanielk1977    pragma synchronous = OFF;
4951bdd9b57Sdrh  }
49691cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}}
49791cf71b0Sdanielk1977do_test pragma-5.2 {
49891cf71b0Sdanielk1977  execsql {
49991cf71b0Sdanielk1977    pragma synchronous;
50091cf71b0Sdanielk1977  }
50191cf71b0Sdanielk1977} {2}
5025260f7e9Sdrhcatchsql {COMMIT;}
503c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
5045260f7e9Sdrh
5055260f7e9Sdrh# Test schema-query pragmas
5065260f7e9Sdrh#
50727188fb5Sdanielk1977ifcapable schema_pragmas {
5085a8f9374Sdanielk1977ifcapable tempdb&&attach {
5095260f7e9Sdrh  do_test pragma-6.1 {
510956bc92cSdrh    set res {}
51140e016e4Sdrh    execsql {SELECT * FROM sqlite_temp_master}
5125260f7e9Sdrh    foreach {idx name file} [execsql {pragma database_list}] {
5135260f7e9Sdrh      lappend res $idx $name
5145260f7e9Sdrh    }
5155260f7e9Sdrh    set res
516a71aa001Sdrh  } {0 main 1 temp 2 aux}
51753c0f748Sdanielk1977}
5185260f7e9Sdrhdo_test pragma-6.2 {
5195260f7e9Sdrh  execsql {
520d2cb50b7Sdrh    CREATE TABLE t2(a,b,c);
5215260f7e9Sdrh    pragma table_info(t2)
5225260f7e9Sdrh  }
523bfa8b102Sdrh} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
524d2cb50b7Sdrhdo_test pragma-6.2.1 {
525d2cb50b7Sdrh  execsql {
526d2cb50b7Sdrh    pragma table_info;
527d2cb50b7Sdrh  }
528d2cb50b7Sdrh} {}
529736c7d4bSdrhdb nullvalue <<NULL>>
530417ec638Sdrhdo_test pragma-6.2.2 {
531417ec638Sdrh  execsql {
532736c7d4bSdrh    CREATE TABLE t5(
533736c7d4bSdrh      a TEXT DEFAULT CURRENT_TIMESTAMP,
534736c7d4bSdrh      b DEFAULT (5+3),
535736c7d4bSdrh      c TEXT,
536736c7d4bSdrh      d INTEGER DEFAULT NULL,
537384b7fe2Sdrh      e TEXT DEFAULT '',
538384b7fe2Sdrh      UNIQUE(b,c,d),
539384b7fe2Sdrh      PRIMARY KEY(e,b,c)
540736c7d4bSdrh    );
541417ec638Sdrh    PRAGMA table_info(t5);
542417ec638Sdrh  }
543384b7fe2Sdrh} {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}
544736c7d4bSdrhdb nullvalue {}
545384b7fe2Sdrhdo_test pragma-6.2.3 {
546384b7fe2Sdrh  execsql {
547384b7fe2Sdrh    CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
548384b7fe2Sdrh    pragma table_info(t2_3)
549384b7fe2Sdrh  }
550384b7fe2Sdrh} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
5516bf89570Sdrhifcapable {foreignkey} {
552d2cb50b7Sdrh  do_test pragma-6.3.1 {
5535260f7e9Sdrh    execsql {
5545260f7e9Sdrh      CREATE TABLE t3(a int references t2(b), b UNIQUE);
5555260f7e9Sdrh      pragma foreign_key_list(t3);
5565260f7e9Sdrh    }
5571da40a38Sdan  } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
558d2cb50b7Sdrh  do_test pragma-6.3.2 {
559d2cb50b7Sdrh    execsql {
560d2cb50b7Sdrh      pragma foreign_key_list;
561d2cb50b7Sdrh    }
562d2cb50b7Sdrh  } {}
563d2cb50b7Sdrh  do_test pragma-6.3.3 {
564d2cb50b7Sdrh    execsql {
565d2cb50b7Sdrh      pragma foreign_key_list(t3_bogus);
566d2cb50b7Sdrh    }
567d2cb50b7Sdrh  } {}
568d2cb50b7Sdrh  do_test pragma-6.3.4 {
569d2cb50b7Sdrh    execsql {
570d2cb50b7Sdrh      pragma foreign_key_list(t5);
571d2cb50b7Sdrh    }
572d2cb50b7Sdrh  } {}
5735260f7e9Sdrh  do_test pragma-6.4 {
5745260f7e9Sdrh    execsql {
5755260f7e9Sdrh      pragma index_list(t3);
5765260f7e9Sdrh    }
5775260f7e9Sdrh  } {0 sqlite_autoindex_t3_1 1}
5786bf89570Sdrh}
5796bf89570Sdrhifcapable {!foreignkey} {
5806bf89570Sdrh  execsql {CREATE TABLE t3(a,b UNIQUE)}
5816bf89570Sdrh}
582d2cb50b7Sdrhdo_test pragma-6.5.1 {
5835260f7e9Sdrh  execsql {
5845260f7e9Sdrh    CREATE INDEX t3i1 ON t3(a,b);
5855260f7e9Sdrh    pragma index_info(t3i1);
5865260f7e9Sdrh  }
5875260f7e9Sdrh} {0 0 a 1 1 b}
588d2cb50b7Sdrhdo_test pragma-6.5.2 {
589d2cb50b7Sdrh  execsql {
590d2cb50b7Sdrh    pragma index_info(t3i1_bogus);
591d2cb50b7Sdrh  }
592d2cb50b7Sdrh} {}
593260d8a6aSdanielk1977
594260d8a6aSdanielk1977ifcapable tempdb {
595260d8a6aSdanielk1977  # Test for ticket #3320. When a temp table of the same name exists, make
596260d8a6aSdanielk1977  # sure the schema of the main table can still be queried using
597260d8a6aSdanielk1977  # "pragma table_info":
598260d8a6aSdanielk1977  do_test pragma-6.6.1 {
599260d8a6aSdanielk1977    execsql {
600260d8a6aSdanielk1977      CREATE TABLE trial(col_main);
601260d8a6aSdanielk1977      CREATE TEMP TABLE trial(col_temp);
602260d8a6aSdanielk1977    }
603260d8a6aSdanielk1977  } {}
604260d8a6aSdanielk1977  do_test pragma-6.6.2 {
605260d8a6aSdanielk1977    execsql {
606260d8a6aSdanielk1977      PRAGMA table_info(trial);
607260d8a6aSdanielk1977    }
608260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
609260d8a6aSdanielk1977  do_test pragma-6.6.3 {
610260d8a6aSdanielk1977    execsql {
611260d8a6aSdanielk1977      PRAGMA temp.table_info(trial);
612260d8a6aSdanielk1977    }
613260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
614260d8a6aSdanielk1977  do_test pragma-6.6.4 {
615260d8a6aSdanielk1977    execsql {
616260d8a6aSdanielk1977      PRAGMA main.table_info(trial);
617260d8a6aSdanielk1977    }
618260d8a6aSdanielk1977  } {0 col_main {} 0 {} 0}
619260d8a6aSdanielk1977}
620f96a3778Sdanielk1977
621f96a3778Sdanielk1977do_test pragma-6.7 {
622f96a3778Sdanielk1977  execsql {
623f96a3778Sdanielk1977    CREATE TABLE test_table(
624f96a3778Sdanielk1977      one INT NOT NULL DEFAULT -1,
625f96a3778Sdanielk1977      two text,
626f96a3778Sdanielk1977      three VARCHAR(45, 65) DEFAULT 'abcde',
627f96a3778Sdanielk1977      four REAL DEFAULT X'abcdef',
628f96a3778Sdanielk1977      five DEFAULT CURRENT_TIME
629f96a3778Sdanielk1977    );
630f96a3778Sdanielk1977    PRAGMA table_info(test_table);
631f96a3778Sdanielk1977  }
632f96a3778Sdanielk1977} [concat \
633f96a3778Sdanielk1977  {0 one INT 1 -1 0} \
634f96a3778Sdanielk1977  {1 two text 0 {} 0} \
635f96a3778Sdanielk1977  {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
636f96a3778Sdanielk1977  {3 four REAL 0 X'abcdef' 0} \
637f96a3778Sdanielk1977  {4 five {} 0 CURRENT_TIME 0} \
638f96a3778Sdanielk1977]
63927188fb5Sdanielk1977} ;# ifcapable schema_pragmas
6405260f7e9Sdrh# Miscellaneous tests
6415260f7e9Sdrh#
64227188fb5Sdanielk1977ifcapable schema_pragmas {
643d2cb50b7Sdrhdo_test pragma-7.1.1 {
6445260f7e9Sdrh  # Make sure a pragma knows to read the schema if it needs to
6455260f7e9Sdrh  db close
6465260f7e9Sdrh  sqlite3 db test.db
6475260f7e9Sdrh  execsql {
6485260f7e9Sdrh    pragma index_list(t3);
6495260f7e9Sdrh  }
6505260f7e9Sdrh} {0 t3i1 0 1 sqlite_autoindex_t3_1 1}
651d2cb50b7Sdrhdo_test pragma-7.1.2 {
652d2cb50b7Sdrh  execsql {
653d2cb50b7Sdrh    pragma index_list(t3_bogus);
654d2cb50b7Sdrh  }
655d2cb50b7Sdrh} {}
65627188fb5Sdanielk1977} ;# ifcapable schema_pragmas
6576c62608fSdrhifcapable {utf16} {
658cb354603Sdan  if {[permutation] == ""} {
6595260f7e9Sdrh    do_test pragma-7.2 {
6605260f7e9Sdrh      db close
6615260f7e9Sdrh      sqlite3 db test.db
6625260f7e9Sdrh      catchsql {
6635260f7e9Sdrh        pragma encoding=bogus;
6645260f7e9Sdrh      }
6655260f7e9Sdrh    } {1 {unsupported encoding: bogus}}
6666c62608fSdrh  }
667cb354603Sdan}
66853c0f748Sdanielk1977ifcapable tempdb {
6695260f7e9Sdrh  do_test pragma-7.3 {
6705260f7e9Sdrh    db close
6715260f7e9Sdrh    sqlite3 db test.db
6725260f7e9Sdrh    execsql {
6735260f7e9Sdrh      pragma lock_status;
6745260f7e9Sdrh    }
6755260f7e9Sdrh  } {main unlocked temp closed}
67653c0f748Sdanielk1977} else {
67753c0f748Sdanielk1977  do_test pragma-7.3 {
67853c0f748Sdanielk1977    db close
67953c0f748Sdanielk1977    sqlite3 db test.db
68053c0f748Sdanielk1977    execsql {
68153c0f748Sdanielk1977      pragma lock_status;
68253c0f748Sdanielk1977    }
68353c0f748Sdanielk1977  } {main unlocked}
68453c0f748Sdanielk1977}
6855260f7e9Sdrh
6865260f7e9Sdrh
687dae2495bSdanielk1977#----------------------------------------------------------------------
688b92b70bbSdanielk1977# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
689b92b70bbSdanielk1977# user_version" statements.
690dae2495bSdanielk1977#
691b92b70bbSdanielk1977# pragma-8.1: PRAGMA schema_version
692b92b70bbSdanielk1977# pragma-8.2: PRAGMA user_version
693dae2495bSdanielk1977#
694dae2495bSdanielk1977
69511cf9fbcSdanielk1977ifcapable schema_version {
69611cf9fbcSdanielk1977
697b92b70bbSdanielk1977# First check that we can set the schema version and then retrieve the
698dae2495bSdanielk1977# same value.
699dae2495bSdanielk1977do_test pragma-8.1.1 {
700dae2495bSdanielk1977  execsql {
701b92b70bbSdanielk1977    PRAGMA schema_version = 105;
702dae2495bSdanielk1977  }
703dae2495bSdanielk1977} {}
704dae2495bSdanielk1977do_test pragma-8.1.2 {
70525403650Sdrh  execsql2 {
706b92b70bbSdanielk1977    PRAGMA schema_version;
707dae2495bSdanielk1977  }
70825403650Sdrh} {schema_version 105}
709dae2495bSdanielk1977do_test pragma-8.1.3 {
710dae2495bSdanielk1977  execsql {
711b92b70bbSdanielk1977    PRAGMA schema_version = 106;
712dae2495bSdanielk1977  }
713dae2495bSdanielk1977} {}
714dae2495bSdanielk1977do_test pragma-8.1.4 {
715dae2495bSdanielk1977  execsql {
716b92b70bbSdanielk1977    PRAGMA schema_version;
717dae2495bSdanielk1977  }
718dae2495bSdanielk1977} 106
719dae2495bSdanielk1977
720b92b70bbSdanielk1977# Check that creating a table modifies the schema-version (this is really
721b92b70bbSdanielk1977# to verify that the value being read is in fact the schema version).
722dae2495bSdanielk1977do_test pragma-8.1.5 {
723dae2495bSdanielk1977  execsql {
724dae2495bSdanielk1977    CREATE TABLE t4(a, b, c);
725dae2495bSdanielk1977    INSERT INTO t4 VALUES(1, 2, 3);
726dae2495bSdanielk1977    SELECT * FROM t4;
727dae2495bSdanielk1977  }
728dae2495bSdanielk1977} {1 2 3}
729dae2495bSdanielk1977do_test pragma-8.1.6 {
730dae2495bSdanielk1977  execsql {
731b92b70bbSdanielk1977    PRAGMA schema_version;
732dae2495bSdanielk1977  }
733dae2495bSdanielk1977} 107
734dae2495bSdanielk1977
735dae2495bSdanielk1977# Now open a second connection to the database. Ensure that changing the
736b92b70bbSdanielk1977# schema-version using the first connection forces the second connection
737dae2495bSdanielk1977# to reload the schema. This has to be done using the C-API test functions,
738dae2495bSdanielk1977# because the TCL API accounts for SCHEMA_ERROR and retries the query.
739dae2495bSdanielk1977do_test pragma-8.1.7 {
740dddca286Sdrh  sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
741dae2495bSdanielk1977  execsql {
742dae2495bSdanielk1977    SELECT * FROM t4;
743dae2495bSdanielk1977  } db2
744dae2495bSdanielk1977} {1 2 3}
745dae2495bSdanielk1977do_test pragma-8.1.8 {
746dae2495bSdanielk1977  execsql {
747b92b70bbSdanielk1977    PRAGMA schema_version = 108;
748dae2495bSdanielk1977  }
749dae2495bSdanielk1977} {}
750dae2495bSdanielk1977do_test pragma-8.1.9 {
751dae2495bSdanielk1977  set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
752dae2495bSdanielk1977  sqlite3_step $::STMT
753dae2495bSdanielk1977} SQLITE_ERROR
754dae2495bSdanielk1977do_test pragma-8.1.10 {
755dae2495bSdanielk1977  sqlite3_finalize $::STMT
756dae2495bSdanielk1977} SQLITE_SCHEMA
757dae2495bSdanielk1977
758b92b70bbSdanielk1977# Make sure the schema-version can be manipulated in an attached database.
759fda06befSmistachkinforcedelete test2.db
760fda06befSmistachkinforcedelete test2.db-journal
7615a8f9374Sdanielk1977ifcapable attach {
762dae2495bSdanielk1977  do_test pragma-8.1.11 {
763dae2495bSdanielk1977    execsql {
764dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
765dae2495bSdanielk1977      CREATE TABLE aux.t1(a, b, c);
766b92b70bbSdanielk1977      PRAGMA aux.schema_version = 205;
767dae2495bSdanielk1977    }
768dae2495bSdanielk1977  } {}
769dae2495bSdanielk1977  do_test pragma-8.1.12 {
770dae2495bSdanielk1977    execsql {
771b92b70bbSdanielk1977      PRAGMA aux.schema_version;
772dae2495bSdanielk1977    }
773dae2495bSdanielk1977  } 205
7745a8f9374Sdanielk1977}
775dae2495bSdanielk1977do_test pragma-8.1.13 {
776dae2495bSdanielk1977  execsql {
777b92b70bbSdanielk1977    PRAGMA schema_version;
778dae2495bSdanielk1977  }
779dae2495bSdanielk1977} 108
780dae2495bSdanielk1977
781b92b70bbSdanielk1977# And check that modifying the schema-version in an attached database
782dae2495bSdanielk1977# forces the second connection to reload the schema.
7835a8f9374Sdanielk1977ifcapable attach {
784dae2495bSdanielk1977  do_test pragma-8.1.14 {
785dddca286Sdrh    sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
786dae2495bSdanielk1977    execsql {
787dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
788dae2495bSdanielk1977      SELECT * FROM aux.t1;
789dae2495bSdanielk1977    } db2
790dae2495bSdanielk1977  } {}
791dae2495bSdanielk1977  do_test pragma-8.1.15 {
792dae2495bSdanielk1977    execsql {
793b92b70bbSdanielk1977      PRAGMA aux.schema_version = 206;
794dae2495bSdanielk1977    }
795dae2495bSdanielk1977  } {}
796dae2495bSdanielk1977  do_test pragma-8.1.16 {
797dae2495bSdanielk1977    set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
798dae2495bSdanielk1977    sqlite3_step $::STMT
799dae2495bSdanielk1977  } SQLITE_ERROR
800dae2495bSdanielk1977  do_test pragma-8.1.17 {
801dae2495bSdanielk1977    sqlite3_finalize $::STMT
802dae2495bSdanielk1977  } SQLITE_SCHEMA
803dae2495bSdanielk1977  do_test pragma-8.1.18 {
804dae2495bSdanielk1977    db2 close
805dae2495bSdanielk1977  } {}
8065a8f9374Sdanielk1977}
807dae2495bSdanielk1977
808b92b70bbSdanielk1977# Now test that the user-version can be read and written (and that we aren't
809b92b70bbSdanielk1977# accidentally manipulating the schema-version instead).
810dae2495bSdanielk1977do_test pragma-8.2.1 {
81125403650Sdrh  execsql2 {
812b92b70bbSdanielk1977    PRAGMA user_version;
813dae2495bSdanielk1977  }
81425403650Sdrh} {user_version 0}
815dae2495bSdanielk1977do_test pragma-8.2.2 {
816dae2495bSdanielk1977  execsql {
817b92b70bbSdanielk1977    PRAGMA user_version = 2;
818dae2495bSdanielk1977  }
819dae2495bSdanielk1977} {}
820802d69a7Sdrhdo_test pragma-8.2.3.1 {
82125403650Sdrh  execsql2 {
822b92b70bbSdanielk1977    PRAGMA user_version;
823dae2495bSdanielk1977  }
82425403650Sdrh} {user_version 2}
825802d69a7Sdrhdo_test pragma-8.2.3.2 {
826802d69a7Sdrh  db close
827802d69a7Sdrh  sqlite3 db test.db
828802d69a7Sdrh  execsql {
829802d69a7Sdrh    PRAGMA user_version;
830802d69a7Sdrh  }
831802d69a7Sdrh} {2}
832802d69a7Sdrhdo_test pragma-8.2.4.1 {
833dae2495bSdanielk1977  execsql {
834b92b70bbSdanielk1977    PRAGMA schema_version;
835dae2495bSdanielk1977  }
836dae2495bSdanielk1977} {108}
837802d69a7Sdrhifcapable vacuum {
838802d69a7Sdrh  do_test pragma-8.2.4.2 {
839802d69a7Sdrh    execsql {
840802d69a7Sdrh      VACUUM;
841802d69a7Sdrh      PRAGMA user_version;
842802d69a7Sdrh    }
843802d69a7Sdrh  } {2}
844802d69a7Sdrh  do_test pragma-8.2.4.3 {
845802d69a7Sdrh    execsql {
846802d69a7Sdrh      PRAGMA schema_version;
847802d69a7Sdrh    }
848802d69a7Sdrh  } {109}
849802d69a7Sdrh}
8505a8f9374Sdanielk1977
8515a8f9374Sdanielk1977ifcapable attach {
852802d69a7Sdrh  db eval {ATTACH 'test2.db' AS aux}
853dae2495bSdanielk1977
854b92b70bbSdanielk1977  # Check that the user-version in the auxilary database can be manipulated (
855dae2495bSdanielk1977  # and that we aren't accidentally manipulating the same in the main db).
856dae2495bSdanielk1977  do_test pragma-8.2.5 {
857dae2495bSdanielk1977    execsql {
858b92b70bbSdanielk1977      PRAGMA aux.user_version;
859dae2495bSdanielk1977    }
860dae2495bSdanielk1977  } {0}
861dae2495bSdanielk1977  do_test pragma-8.2.6 {
862dae2495bSdanielk1977    execsql {
863b92b70bbSdanielk1977      PRAGMA aux.user_version = 3;
864dae2495bSdanielk1977    }
865dae2495bSdanielk1977  } {}
866dae2495bSdanielk1977  do_test pragma-8.2.7 {
867dae2495bSdanielk1977    execsql {
868b92b70bbSdanielk1977      PRAGMA aux.user_version;
869dae2495bSdanielk1977    }
870dae2495bSdanielk1977  } {3}
871dae2495bSdanielk1977  do_test pragma-8.2.8 {
872dae2495bSdanielk1977    execsql {
873b92b70bbSdanielk1977      PRAGMA main.user_version;
874dae2495bSdanielk1977    }
875dae2495bSdanielk1977  } {2}
876dae2495bSdanielk1977
877b92b70bbSdanielk1977  # Now check that a ROLLBACK resets the user-version if it has been modified
878dae2495bSdanielk1977  # within a transaction.
879dae2495bSdanielk1977  do_test pragma-8.2.9 {
880dae2495bSdanielk1977    execsql {
881dae2495bSdanielk1977      BEGIN;
882b92b70bbSdanielk1977      PRAGMA aux.user_version = 10;
883b92b70bbSdanielk1977      PRAGMA user_version = 11;
884dae2495bSdanielk1977    }
885dae2495bSdanielk1977  } {}
886dae2495bSdanielk1977  do_test pragma-8.2.10 {
887dae2495bSdanielk1977    execsql {
888b92b70bbSdanielk1977      PRAGMA aux.user_version;
889dae2495bSdanielk1977    }
890dae2495bSdanielk1977  } {10}
891dae2495bSdanielk1977  do_test pragma-8.2.11 {
892dae2495bSdanielk1977    execsql {
893b92b70bbSdanielk1977      PRAGMA main.user_version;
894dae2495bSdanielk1977    }
895dae2495bSdanielk1977  } {11}
896dae2495bSdanielk1977  do_test pragma-8.2.12 {
897dae2495bSdanielk1977    execsql {
898dae2495bSdanielk1977      ROLLBACK;
899b92b70bbSdanielk1977      PRAGMA aux.user_version;
900dae2495bSdanielk1977    }
901dae2495bSdanielk1977  } {3}
902dae2495bSdanielk1977  do_test pragma-8.2.13 {
903dae2495bSdanielk1977    execsql {
904b92b70bbSdanielk1977      PRAGMA main.user_version;
905dae2495bSdanielk1977    }
906dae2495bSdanielk1977  } {2}
9075a8f9374Sdanielk1977}
908dae2495bSdanielk1977
909b92b70bbSdanielk1977# Try a negative value for the user-version
910dae2495bSdanielk1977do_test pragma-8.2.14 {
911dae2495bSdanielk1977  execsql {
912b92b70bbSdanielk1977    PRAGMA user_version = -450;
913dae2495bSdanielk1977  }
914dae2495bSdanielk1977} {}
915dae2495bSdanielk1977do_test pragma-8.2.15 {
916dae2495bSdanielk1977  execsql {
917b92b70bbSdanielk1977    PRAGMA user_version;
918dae2495bSdanielk1977  }
919dae2495bSdanielk1977} {-450}
920d9c847dcSdanielk1977} ; # ifcapable schema_version
921d9c847dcSdanielk1977
922d19744f3Sdrh# Check to see if TEMP_STORE is memory or disk.  Return strings
923d19744f3Sdrh# "memory" or "disk" as appropriate.
924d19744f3Sdrh#
925d19744f3Sdrhproc check_temp_store {} {
926d19744f3Sdrh  db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
927d19744f3Sdrh  db eval {PRAGMA database_list} {
928d19744f3Sdrh    if {$name=="temp"} {
92917b90b53Sdanielk1977      set bt [btree_from_db db 1]
93017b90b53Sdanielk1977      if {[btree_ismemdb $bt]} {
931d19744f3Sdrh        return "memory"
932d19744f3Sdrh      }
93317b90b53Sdanielk1977      return "disk"
934d19744f3Sdrh    }
935d19744f3Sdrh  }
936d19744f3Sdrh  return "unknown"
937d19744f3Sdrh}
938d19744f3Sdrh
9399a09a3caStpoindex
9409a09a3caStpoindex# Test temp_store and temp_store_directory pragmas
9419a09a3caStpoindex#
942268283bcSdrhifcapable pager_pragmas {
9439a09a3caStpoindexdo_test pragma-9.1 {
9449a09a3caStpoindex  db close
9459a09a3caStpoindex  sqlite3 db test.db
9469a09a3caStpoindex  execsql {
9479a09a3caStpoindex    PRAGMA temp_store;
9489a09a3caStpoindex  }
9499a09a3caStpoindex} {0}
950d19744f3Sdrhif {$TEMP_STORE<=1} {
951d19744f3Sdrh  do_test pragma-9.1.1 {
952d19744f3Sdrh    check_temp_store
953d19744f3Sdrh  } {disk}
954d19744f3Sdrh} else {
955d19744f3Sdrh  do_test pragma-9.1.1 {
956d19744f3Sdrh    check_temp_store
957d19744f3Sdrh  } {memory}
958d19744f3Sdrh}
959d19744f3Sdrh
9609a09a3caStpoindexdo_test pragma-9.2 {
961d19744f3Sdrh  db close
962d19744f3Sdrh  sqlite3 db test.db
9639a09a3caStpoindex  execsql {
9649a09a3caStpoindex    PRAGMA temp_store=file;
9659a09a3caStpoindex    PRAGMA temp_store;
9669a09a3caStpoindex  }
9679a09a3caStpoindex} {1}
968d19744f3Sdrhif {$TEMP_STORE==3} {
969d19744f3Sdrh  # When TEMP_STORE is 3, always use memory regardless of pragma settings.
970d19744f3Sdrh  do_test pragma-9.2.1 {
971d19744f3Sdrh    check_temp_store
972d19744f3Sdrh  } {memory}
973d19744f3Sdrh} else {
974d19744f3Sdrh  do_test pragma-9.2.1 {
975d19744f3Sdrh    check_temp_store
976d19744f3Sdrh  } {disk}
977d19744f3Sdrh}
978d19744f3Sdrh
9799a09a3caStpoindexdo_test pragma-9.3 {
980d19744f3Sdrh  db close
981d19744f3Sdrh  sqlite3 db test.db
9829a09a3caStpoindex  execsql {
9839a09a3caStpoindex    PRAGMA temp_store=memory;
9849a09a3caStpoindex    PRAGMA temp_store;
9859a09a3caStpoindex  }
9869a09a3caStpoindex} {2}
987d19744f3Sdrhif {$TEMP_STORE==0} {
988d19744f3Sdrh  # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
989d19744f3Sdrh  do_test pragma-9.3.1 {
990d19744f3Sdrh    check_temp_store
991d19744f3Sdrh  } {disk}
992d19744f3Sdrh} else {
993d19744f3Sdrh  do_test pragma-9.3.1 {
994d19744f3Sdrh    check_temp_store
995d19744f3Sdrh  } {memory}
996d19744f3Sdrh}
997d19744f3Sdrh
9989a09a3caStpoindexdo_test pragma-9.4 {
9999a09a3caStpoindex  execsql {
10009a09a3caStpoindex    PRAGMA temp_store_directory;
10019a09a3caStpoindex  }
10029a09a3caStpoindex} {}
100378f82d1eSdrhifcapable wsd {
10049a09a3caStpoindex  do_test pragma-9.5 {
1005f8a78464Smistachkin    set pwd [string map {' ''} [file nativename [get_pwd]]]
1006268283bcSdrh    execsql "
1007268283bcSdrh      PRAGMA temp_store_directory='$pwd';
10089a09a3caStpoindex    "
10099a09a3caStpoindex  } {}
10109a09a3caStpoindex  do_test pragma-9.6 {
10119a09a3caStpoindex    execsql {
10129a09a3caStpoindex      PRAGMA temp_store_directory;
10139a09a3caStpoindex    }
1014f8a78464Smistachkin  } [list [file nativename [get_pwd]]]
10159a09a3caStpoindex  do_test pragma-9.7 {
1016268283bcSdrh    catchsql {
10179a09a3caStpoindex      PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
10189a09a3caStpoindex    }
1019268283bcSdrh  } {1 {not a writable directory}}
10209a09a3caStpoindex  do_test pragma-9.8 {
10219a09a3caStpoindex    execsql {
10229a09a3caStpoindex      PRAGMA temp_store_directory='';
10239a09a3caStpoindex    }
10249a09a3caStpoindex  } {}
10251e9daa6aSdrh  if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
102653c0f748Sdanielk1977    ifcapable tempdb {
10279a09a3caStpoindex      do_test pragma-9.9 {
10289a09a3caStpoindex        execsql {
10299a09a3caStpoindex          PRAGMA temp_store_directory;
10309a09a3caStpoindex          PRAGMA temp_store=FILE;
10319a09a3caStpoindex          CREATE TEMP TABLE temp_store_directory_test(a integer);
10329a09a3caStpoindex          INSERT INTO temp_store_directory_test values (2);
10339a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
10349a09a3caStpoindex        }
10359a09a3caStpoindex      } {2}
10369a09a3caStpoindex      do_test pragma-9.10 {
1037268283bcSdrh        catchsql "
1038268283bcSdrh          PRAGMA temp_store_directory='$pwd';
10399a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
10409a09a3caStpoindex        "
1041268283bcSdrh      } {1 {no such table: temp_store_directory_test}}
10421e9daa6aSdrh    }
10431e9daa6aSdrh  }
104478f82d1eSdrh}
104595b289b6Sdanielk1977do_test pragma-9.11 {
104695b289b6Sdanielk1977  execsql {
104795b289b6Sdanielk1977    PRAGMA temp_store = 0;
104895b289b6Sdanielk1977    PRAGMA temp_store;
104995b289b6Sdanielk1977  }
105095b289b6Sdanielk1977} {0}
105195b289b6Sdanielk1977do_test pragma-9.12 {
105295b289b6Sdanielk1977  execsql {
105395b289b6Sdanielk1977    PRAGMA temp_store = 1;
105495b289b6Sdanielk1977    PRAGMA temp_store;
105595b289b6Sdanielk1977  }
105695b289b6Sdanielk1977} {1}
105795b289b6Sdanielk1977do_test pragma-9.13 {
105895b289b6Sdanielk1977  execsql {
105995b289b6Sdanielk1977    PRAGMA temp_store = 2;
106095b289b6Sdanielk1977    PRAGMA temp_store;
106195b289b6Sdanielk1977  }
106295b289b6Sdanielk1977} {2}
106395b289b6Sdanielk1977do_test pragma-9.14 {
106495b289b6Sdanielk1977  execsql {
106595b289b6Sdanielk1977    PRAGMA temp_store = 3;
106695b289b6Sdanielk1977    PRAGMA temp_store;
106795b289b6Sdanielk1977  }
106895b289b6Sdanielk1977} {0}
106995b289b6Sdanielk1977do_test pragma-9.15 {
107095b289b6Sdanielk1977  catchsql {
107195b289b6Sdanielk1977    BEGIN EXCLUSIVE;
107295b289b6Sdanielk1977    CREATE TEMP TABLE temp_table(t);
107395b289b6Sdanielk1977    INSERT INTO temp_table VALUES('valuable data');
107495b289b6Sdanielk1977    PRAGMA temp_store = 1;
107595b289b6Sdanielk1977  }
107695b289b6Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
107795b289b6Sdanielk1977do_test pragma-9.16 {
107895b289b6Sdanielk1977  execsql {
107995b289b6Sdanielk1977    SELECT * FROM temp_table;
108095b289b6Sdanielk1977    COMMIT;
108195b289b6Sdanielk1977  }
108295b289b6Sdanielk1977} {{valuable data}}
1083983e2309Sdanielk1977
1084983e2309Sdanielk1977do_test pragma-9.17 {
1085983e2309Sdanielk1977  execsql {
1086983e2309Sdanielk1977    INSERT INTO temp_table VALUES('valuable data II');
1087983e2309Sdanielk1977    SELECT * FROM temp_table;
1088983e2309Sdanielk1977  }
1089983e2309Sdanielk1977} {{valuable data} {valuable data II}}
1090983e2309Sdanielk1977
1091983e2309Sdanielk1977do_test pragma-9.18 {
1092983e2309Sdanielk1977  set rc [catch {
1093983e2309Sdanielk1977    db eval {SELECT t FROM temp_table} {
1094983e2309Sdanielk1977      execsql {pragma temp_store = 1}
1095983e2309Sdanielk1977    }
1096983e2309Sdanielk1977  } msg]
1097983e2309Sdanielk1977  list $rc $msg
1098983e2309Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
1099983e2309Sdanielk1977
1100268283bcSdrh} ;# ifcapable pager_pragmas
11019a09a3caStpoindex
1102cc6bd383Sdanielk1977ifcapable trigger {
1103cc6bd383Sdanielk1977
1104cc6bd383Sdanielk1977do_test pragma-10.0 {
1105cc6bd383Sdanielk1977  catchsql {
1106cc6bd383Sdanielk1977    DROP TABLE main.t1;
1107cc6bd383Sdanielk1977  }
1108cc6bd383Sdanielk1977  execsql {
1109cc6bd383Sdanielk1977    PRAGMA count_changes = 1;
1110cc6bd383Sdanielk1977
1111cc6bd383Sdanielk1977    CREATE TABLE t1(a PRIMARY KEY);
1112cc6bd383Sdanielk1977    CREATE TABLE t1_mirror(a);
1113cc6bd383Sdanielk1977    CREATE TABLE t1_mirror2(a);
1114cc6bd383Sdanielk1977    CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1115cc6bd383Sdanielk1977      INSERT INTO t1_mirror VALUES(new.a);
1116cc6bd383Sdanielk1977    END;
1117cc6bd383Sdanielk1977    CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1118cc6bd383Sdanielk1977      INSERT INTO t1_mirror2 VALUES(new.a);
1119cc6bd383Sdanielk1977    END;
1120cc6bd383Sdanielk1977    CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1121cc6bd383Sdanielk1977      UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1122cc6bd383Sdanielk1977    END;
1123cc6bd383Sdanielk1977    CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1124cc6bd383Sdanielk1977      UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1125cc6bd383Sdanielk1977    END;
1126cc6bd383Sdanielk1977    CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1127cc6bd383Sdanielk1977      DELETE FROM t1_mirror WHERE a = old.a;
1128cc6bd383Sdanielk1977    END;
1129cc6bd383Sdanielk1977    CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1130cc6bd383Sdanielk1977      DELETE FROM t1_mirror2 WHERE a = old.a;
1131cc6bd383Sdanielk1977    END;
1132cc6bd383Sdanielk1977  }
1133cc6bd383Sdanielk1977} {}
1134cc6bd383Sdanielk1977
1135cc6bd383Sdanielk1977do_test pragma-10.1 {
1136cc6bd383Sdanielk1977  execsql {
1137cc6bd383Sdanielk1977    INSERT INTO t1 VALUES(randstr(10,10));
1138cc6bd383Sdanielk1977  }
1139cc6bd383Sdanielk1977} {1}
1140cc6bd383Sdanielk1977do_test pragma-10.2 {
1141cc6bd383Sdanielk1977  execsql {
1142cc6bd383Sdanielk1977    UPDATE t1 SET a = randstr(10,10);
1143cc6bd383Sdanielk1977  }
1144cc6bd383Sdanielk1977} {1}
1145cc6bd383Sdanielk1977do_test pragma-10.3 {
1146cc6bd383Sdanielk1977  execsql {
1147cc6bd383Sdanielk1977    DELETE FROM t1;
1148cc6bd383Sdanielk1977  }
1149cc6bd383Sdanielk1977} {1}
1150cc6bd383Sdanielk1977
1151cc6bd383Sdanielk1977} ;# ifcapable trigger
1152cc6bd383Sdanielk1977
115348af65aeSdanielk1977ifcapable schema_pragmas {
115448af65aeSdanielk1977  do_test pragma-11.1 {
115548af65aeSdanielk1977    execsql2 {
115648af65aeSdanielk1977      pragma collation_list;
115748af65aeSdanielk1977    }
11589b5adfa2Sdrh  } {seq 0 name NOCASE seq 1 name RTRIM seq 2 name BINARY}
115948af65aeSdanielk1977  do_test pragma-11.2 {
116048af65aeSdanielk1977    db collate New_Collation blah...
116148af65aeSdanielk1977    execsql {
116248af65aeSdanielk1977      pragma collation_list;
116348af65aeSdanielk1977    }
11649b5adfa2Sdrh  } {0 New_Collation 1 NOCASE 2 RTRIM 3 BINARY}
116548af65aeSdanielk1977}
116648af65aeSdanielk1977
1167ddfb2f03Sdanielk1977ifcapable schema_pragmas&&tempdb {
1168ddfb2f03Sdanielk1977  do_test pragma-12.1 {
1169ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1170ddfb2f03Sdanielk1977    execsql {
1171ddfb2f03Sdanielk1977      PRAGMA temp.table_info('abc');
1172ddfb2f03Sdanielk1977    } db2
1173ddfb2f03Sdanielk1977  } {}
1174ddfb2f03Sdanielk1977  db2 close
1175ddfb2f03Sdanielk1977
1176ddfb2f03Sdanielk1977  do_test pragma-12.2 {
1177ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1178ddfb2f03Sdanielk1977    execsql {
1179ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size = 200;
1180ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size;
1181ddfb2f03Sdanielk1977    } db2
1182ddfb2f03Sdanielk1977  } {200}
1183ddfb2f03Sdanielk1977  db2 close
1184ddfb2f03Sdanielk1977
1185ddfb2f03Sdanielk1977  do_test pragma-12.3 {
1186ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1187ddfb2f03Sdanielk1977    execsql {
1188ddfb2f03Sdanielk1977      PRAGMA temp.cache_size = 400;
1189ddfb2f03Sdanielk1977      PRAGMA temp.cache_size;
1190ddfb2f03Sdanielk1977    } db2
1191ddfb2f03Sdanielk1977  } {400}
1192ddfb2f03Sdanielk1977  db2 close
1193ddfb2f03Sdanielk1977}
1194ddfb2f03Sdanielk1977
11954b2688abSdanielk1977ifcapable bloblit {
11964b2688abSdanielk1977
119705a82983Sdrhdo_test pragma-13.1 {
119805a82983Sdrh  execsql {
119905a82983Sdrh    DROP TABLE IF EXISTS t4;
120005a82983Sdrh    PRAGMA vdbe_trace=on;
120105a82983Sdrh    PRAGMA vdbe_listing=on;
120205a82983Sdrh    PRAGMA sql_trace=on;
120305a82983Sdrh    CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
120405a82983Sdrh    INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
120505a82983Sdrh    INSERT INTO t4(b) VALUES(randstr(30,30));
120605a82983Sdrh    INSERT INTO t4(b) VALUES(1.23456);
120705a82983Sdrh    INSERT INTO t4(b) VALUES(NULL);
120805a82983Sdrh    INSERT INTO t4(b) VALUES(0);
120905a82983Sdrh    INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
121005a82983Sdrh    SELECT * FROM t4;
121105a82983Sdrh  }
121205a82983Sdrh  execsql {
121305a82983Sdrh    PRAGMA vdbe_trace=off;
121405a82983Sdrh    PRAGMA vdbe_listing=off;
121505a82983Sdrh    PRAGMA sql_trace=off;
121605a82983Sdrh  }
121705a82983Sdrh} {}
121805a82983Sdrh
12194b2688abSdanielk1977} ;# ifcapable bloblit
12204b2688abSdanielk1977
122159a93791Sdanielk1977ifcapable pager_pragmas {
122259a93791Sdanielk1977  db close
1223fda06befSmistachkin  forcedelete test.db
122459a93791Sdanielk1977  sqlite3 db test.db
122559a93791Sdanielk1977
122659a93791Sdanielk1977  do_test pragma-14.1 {
122759a93791Sdanielk1977    execsql { pragma auto_vacuum = 0 }
122859a93791Sdanielk1977    execsql { pragma page_count }
122959a93791Sdanielk1977  } {0}
123059a93791Sdanielk1977
123159a93791Sdanielk1977  do_test pragma-14.2 {
123259a93791Sdanielk1977    execsql {
123359a93791Sdanielk1977      CREATE TABLE abc(a, b, c);
123459a93791Sdanielk1977      PRAGMA page_count;
123559a93791Sdanielk1977    }
123659a93791Sdanielk1977  } {2}
12375d16a9a6Sdrh  do_test pragma-14.2uc {
12385d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
12395d16a9a6Sdrh  } {2}
124059a93791Sdanielk1977
124159a93791Sdanielk1977  do_test pragma-14.3 {
124259a93791Sdanielk1977    execsql {
124359a93791Sdanielk1977      BEGIN;
124459a93791Sdanielk1977      CREATE TABLE def(a, b, c);
124559a93791Sdanielk1977      PRAGMA page_count;
124659a93791Sdanielk1977    }
124759a93791Sdanielk1977  } {3}
12485d16a9a6Sdrh  do_test pragma-14.3uc {
12495d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
12505d16a9a6Sdrh  } {3}
125159a93791Sdanielk1977
125259a93791Sdanielk1977  do_test pragma-14.4 {
125359a93791Sdanielk1977    set page_size [db one {pragma page_size}]
125459a93791Sdanielk1977    expr [file size test.db] / $page_size
125559a93791Sdanielk1977  } {2}
125659a93791Sdanielk1977
125759a93791Sdanielk1977  do_test pragma-14.5 {
125859a93791Sdanielk1977    execsql {
125959a93791Sdanielk1977      ROLLBACK;
126059a93791Sdanielk1977      PRAGMA page_count;
126159a93791Sdanielk1977    }
126259a93791Sdanielk1977  } {2}
126359a93791Sdanielk1977
126459a93791Sdanielk1977  do_test pragma-14.6 {
1265fda06befSmistachkin    forcedelete test2.db
126659a93791Sdanielk1977    sqlite3 db2 test2.db
126759a93791Sdanielk1977    execsql {
126859a93791Sdanielk1977      PRAGMA auto_vacuum = 0;
126959a93791Sdanielk1977      CREATE TABLE t1(a, b, c);
127059a93791Sdanielk1977      CREATE TABLE t2(a, b, c);
127159a93791Sdanielk1977      CREATE TABLE t3(a, b, c);
127259a93791Sdanielk1977      CREATE TABLE t4(a, b, c);
127359a93791Sdanielk1977    } db2
127459a93791Sdanielk1977    db2 close
127559a93791Sdanielk1977    execsql {
127659a93791Sdanielk1977      ATTACH 'test2.db' AS aux;
127759a93791Sdanielk1977      PRAGMA aux.page_count;
127859a93791Sdanielk1977    }
127959a93791Sdanielk1977  } {5}
12805d16a9a6Sdrh  do_test pragma-14.6uc {
12815d16a9a6Sdrh    execsql {pragma AUX.PAGE_COUNT}
12825d16a9a6Sdrh  } {5}
128359a93791Sdanielk1977}
128459a93791Sdanielk1977
12858cf6c554Sdanielk1977# Test that the value set using the cache_size pragma is not reset when the
12868cf6c554Sdanielk1977# schema is reloaded.
12878cf6c554Sdanielk1977#
12888cf6c554Sdanielk1977ifcapable pager_pragmas {
12898cf6c554Sdanielk1977  db close
12908cf6c554Sdanielk1977  sqlite3 db test.db
12918cf6c554Sdanielk1977  do_test pragma-15.1 {
12928cf6c554Sdanielk1977    execsql {
12938cf6c554Sdanielk1977      PRAGMA cache_size=59;
12948cf6c554Sdanielk1977      PRAGMA cache_size;
12958cf6c554Sdanielk1977    }
12968cf6c554Sdanielk1977  } {59}
12978cf6c554Sdanielk1977  do_test pragma-15.2 {
12988cf6c554Sdanielk1977    sqlite3 db2 test.db
12998cf6c554Sdanielk1977    execsql {
13008cf6c554Sdanielk1977      CREATE TABLE newtable(a, b, c);
13018cf6c554Sdanielk1977    } db2
13028cf6c554Sdanielk1977    db2 close
13038cf6c554Sdanielk1977  } {}
13048cf6c554Sdanielk1977  do_test pragma-15.3 {
13058cf6c554Sdanielk1977    # Evaluating this statement will cause the schema to be reloaded (because
13068cf6c554Sdanielk1977    # the schema was changed by another connection in pragma-15.2). At one
13078cf6c554Sdanielk1977    # point there was a bug that reset the cache_size to its default value
13088cf6c554Sdanielk1977    # when this happened.
13098cf6c554Sdanielk1977    execsql { SELECT * FROM sqlite_master }
13108cf6c554Sdanielk1977    execsql { PRAGMA cache_size }
13118cf6c554Sdanielk1977  } {59}
13128cf6c554Sdanielk1977}
13138cf6c554Sdanielk1977
13145558a8a6Sdanielk1977# Reset the sqlite3_temp_directory variable for the next run of tests:
13155558a8a6Sdanielk1977sqlite3 dbX :memory:
13165558a8a6Sdanielk1977dbX eval {PRAGMA temp_store_directory = ""}
13175558a8a6Sdanielk1977dbX close
13185558a8a6Sdanielk1977
1319838cce43Sdanielk1977ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1320aebf413dSaswift  set sqlite_hostid_num 1
1321aebf413dSaswift
1322aebf413dSaswift  set using_proxy 0
1323aebf413dSaswift  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1324aebf413dSaswift    set using_proxy $value
1325aebf413dSaswift  }
1326aebf413dSaswift
1327aebf413dSaswift  # Test the lock_proxy_file pragmas.
1328aebf413dSaswift  #
1329aebf413dSaswift  db close
1330aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1331aebf413dSaswift
1332aebf413dSaswift  sqlite3 db test.db
1333aebf413dSaswift  do_test pragma-16.1 {
1334aebf413dSaswift    execsql {
1335aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1336aebf413dSaswift      select * from sqlite_master;
1337aebf413dSaswift    }
1338aebf413dSaswift    execsql {
1339aebf413dSaswift      PRAGMA lock_proxy_file;
1340aebf413dSaswift    }
1341aebf413dSaswift  } {mylittleproxy}
1342aebf413dSaswift
1343aebf413dSaswift  do_test pragma-16.2 {
1344aebf413dSaswift    sqlite3 db2 test.db
1345aebf413dSaswift    execsql {
1346aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1347aebf413dSaswift    } db2
1348aebf413dSaswift  } {}
1349aebf413dSaswift
1350aebf413dSaswift  db2 close
1351aebf413dSaswift  do_test pragma-16.2.1 {
1352aebf413dSaswift    sqlite3 db2 test.db
1353aebf413dSaswift    execsql {
1354aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1355aebf413dSaswift      select * from sqlite_master;
1356aebf413dSaswift    } db2
1357aebf413dSaswift    execsql {
1358aebf413dSaswift      PRAGMA lock_proxy_file;
1359aebf413dSaswift    } db2
1360aebf413dSaswift  } {mylittleproxy}
1361aebf413dSaswift
1362aebf413dSaswift  db2 close
1363aebf413dSaswift  do_test pragma-16.3 {
1364aebf413dSaswift    sqlite3 db2 test.db
1365aebf413dSaswift    execsql {
1366aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1367aebf413dSaswift    } db2
1368aebf413dSaswift    catchsql {
1369aebf413dSaswift      select * from sqlite_master;
1370aebf413dSaswift    } db2
1371aebf413dSaswift  } {1 {database is locked}}
1372aebf413dSaswift
1373aebf413dSaswift  do_test pragma-16.4 {
1374aebf413dSaswift    db2 close
1375aebf413dSaswift    db close
1376aebf413dSaswift    sqlite3 db2 test.db
1377aebf413dSaswift    execsql {
1378aebf413dSaswift      PRAGMA lock_proxy_file="myoriginalproxy";
1379aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1380aebf413dSaswift      PRAGMA lock_proxy_file;
1381aebf413dSaswift    } db2
1382aebf413dSaswift  } {myotherproxy}
1383aebf413dSaswift
1384aebf413dSaswift  db2 close
1385aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1386aebf413dSaswift  do_test pragma-16.5 {
1387aebf413dSaswift    sqlite3 db2 test.db
1388aebf413dSaswift    execsql {
1389aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1390aebf413dSaswift      PRAGMA lock_proxy_file;
1391aebf413dSaswift    } db2
1392aebf413dSaswift  } {myotherproxy}
1393aebf413dSaswift
1394aebf413dSaswift  do_test pragma-16.6 {
1395aebf413dSaswift    db2 close
1396aebf413dSaswift    sqlite3 db2 test2.db
1397aebf413dSaswift    set lockpath [execsql {
1398aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1399aebf413dSaswift      PRAGMA lock_proxy_file;
1400aebf413dSaswift    } db2]
1401aebf413dSaswift    string match "*test2.db:auto:" $lockpath
1402aebf413dSaswift  } {1}
1403aebf413dSaswift
1404aebf413dSaswift  set sqlite_hostid_num 2
1405aebf413dSaswift  do_test pragma-16.7 {
140614d14607Sdan    list [catch {
1407aebf413dSaswift      sqlite3 db test2.db
1408aebf413dSaswift      execsql {
1409aebf413dSaswift        PRAGMA lock_proxy_file=":auto:";
1410aebf413dSaswift        select * from sqlite_master;
1411aebf413dSaswift      }
141214d14607Sdan    } msg] $msg
1413aebf413dSaswift  } {1 {database is locked}}
1414aebf413dSaswift  db close
1415aebf413dSaswift
1416aebf413dSaswift  do_test pragma-16.8 {
141714d14607Sdan    list [catch {
1418aebf413dSaswift      sqlite3 db test2.db
141914d14607Sdan      execsql { select * from sqlite_master }
142014d14607Sdan    } msg] $msg
1421aebf413dSaswift  } {1 {database is locked}}
1422aebf413dSaswift
1423aebf413dSaswift  db2 close
1424aebf413dSaswift  do_test pragma-16.8.1 {
1425aebf413dSaswift    execsql {
1426aebf413dSaswift      PRAGMA lock_proxy_file="yetanotherproxy";
1427aebf413dSaswift      PRAGMA lock_proxy_file;
1428aebf413dSaswift    }
1429aebf413dSaswift  } {yetanotherproxy}
1430aebf413dSaswift  do_test pragma-16.8.2 {
1431aebf413dSaswift    execsql {
1432aebf413dSaswift      create table mine(x);
1433aebf413dSaswift    }
1434aebf413dSaswift  } {}
1435aebf413dSaswift
1436aebf413dSaswift  db close
1437aebf413dSaswift  do_test pragma-16.9 {
1438aebf413dSaswift    sqlite3 db proxytest.db
1439aebf413dSaswift    set lockpath2 [execsql {
1440aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1441aebf413dSaswift      PRAGMA lock_proxy_file;
1442aebf413dSaswift    } db]
1443aebf413dSaswift    string match "*proxytest.db:auto:" $lockpath2
1444aebf413dSaswift  } {1}
1445aebf413dSaswift
1446aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1447aebf413dSaswift  set sqlite_hostid_num 0
1448aebf413dSaswift}
1449d2cb50b7Sdrh
1450d2cb50b7Sdrh# Parsing of auto_vacuum settings.
1451d2cb50b7Sdrh#
1452d2cb50b7Sdrhforeach {autovac_setting val} {
1453d2cb50b7Sdrh  0 0
1454d2cb50b7Sdrh  1 1
1455d2cb50b7Sdrh  2 2
1456d2cb50b7Sdrh  3 0
1457d2cb50b7Sdrh  -1 0
1458d2cb50b7Sdrh  none 0
1459d2cb50b7Sdrh  NONE 0
1460d2cb50b7Sdrh  NoNe 0
1461d2cb50b7Sdrh  full 1
1462d2cb50b7Sdrh  FULL 1
1463d2cb50b7Sdrh  incremental 2
1464d2cb50b7Sdrh  INCREMENTAL 2
1465d2cb50b7Sdrh  -1234 0
1466d2cb50b7Sdrh  1234 0
1467d2cb50b7Sdrh} {
1468d2cb50b7Sdrh  do_test pragma-17.1.$autovac_setting {
1469d2cb50b7Sdrh    catch {db close}
1470d2cb50b7Sdrh    sqlite3 db :memory:
1471d2cb50b7Sdrh    execsql "
1472d2cb50b7Sdrh      PRAGMA auto_vacuum=$::autovac_setting;
1473d2cb50b7Sdrh      PRAGMA auto_vacuum;
1474d2cb50b7Sdrh    "
1475d2cb50b7Sdrh  } $val
1476d2cb50b7Sdrh}
1477d2cb50b7Sdrh
1478d2cb50b7Sdrh# Parsing of temp_store settings.
1479d2cb50b7Sdrh#
1480d2cb50b7Sdrhforeach {temp_setting val} {
1481d2cb50b7Sdrh  0 0
1482d2cb50b7Sdrh  1 1
1483d2cb50b7Sdrh  2 2
1484d2cb50b7Sdrh  3 0
1485d2cb50b7Sdrh  -1 0
1486d2cb50b7Sdrh  file 1
1487d2cb50b7Sdrh  FILE 1
1488d2cb50b7Sdrh  fIlE 1
1489d2cb50b7Sdrh  memory 2
1490d2cb50b7Sdrh  MEMORY 2
1491d2cb50b7Sdrh  MeMoRy 2
1492d2cb50b7Sdrh} {
1493d2cb50b7Sdrh  do_test pragma-18.1.$temp_setting {
1494d2cb50b7Sdrh    catch {db close}
1495d2cb50b7Sdrh    sqlite3 db :memory:
1496d2cb50b7Sdrh    execsql "
1497d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1498d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1499d2cb50b7Sdrh      PRAGMA temp_store;
1500d2cb50b7Sdrh    "
1501d2cb50b7Sdrh  } $val
1502d2cb50b7Sdrh}
1503d2cb50b7Sdrh
150492c700dbSdrh# The SQLITE_FCNTL_PRAGMA logic, with error handling.
150592c700dbSdrh#
150692c700dbSdrhdb close
150792c700dbSdrhtestvfs tvfs
150892c700dbSdrhsqlite3 db test.db -vfs tvfs
150992c700dbSdrhdo_test pragma-19.1 {
151092c700dbSdrh  catchsql {PRAGMA error}
151192c700dbSdrh} {1 {SQL logic error or missing database}}
151292c700dbSdrhdo_test pragma-19.2 {
151392c700dbSdrh  catchsql {PRAGMA error='This is the error message'}
151492c700dbSdrh} {1 {This is the error message}}
151592c700dbSdrhdo_test pragma-19.3 {
151692c700dbSdrh  catchsql {PRAGMA error='7 This is the error message'}
151792c700dbSdrh} {1 {This is the error message}}
151892c700dbSdrhdo_test pragma-19.4 {
151992c700dbSdrh  catchsql {PRAGMA error=7}
152092c700dbSdrh} {1 {out of memory}}
1521c8517f61Sdrhdo_test pragma-19.5 {
15225b044545Smistachkin  file tail [lindex [execsql {PRAGMA filename}] 0]
1523c8517f61Sdrh} {test.db}
152492c700dbSdrh
1525cc71645bSdrhif {$tcl_platform(platform)=="windows"} {
1526a112d140Smistachkin# Test data_store_directory pragma
1527a112d140Smistachkin#
1528a112d140Smistachkindb close
1529a112d140Smistachkinsqlite3 db test.db
1530a112d140Smistachkinfile mkdir data_dir
1531a112d140Smistachkindo_test pragma-20.1 {
1532a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1533a112d140Smistachkin} {0 {}}
1534a112d140Smistachkindo_test pragma-20.2 {
1535a112d140Smistachkin  set pwd [string map {' ''} [file nativename [get_pwd]]]
1536a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1537a112d140Smistachkin} {0 {}}
1538a112d140Smistachkindo_test pragma-20.3 {
1539a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1540a112d140Smistachkin} [list 0 [list [file nativename [get_pwd]]]]
1541a112d140Smistachkindo_test pragma-20.4 {
1542a112d140Smistachkin  set pwd [string map {' ''} [file nativename \
1543a112d140Smistachkin    [file join [get_pwd] data_dir]]]
1544a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1545a112d140Smistachkin} {0 {}}
1546a112d140Smistachkindo_test pragma-20.5 {
1547a112d140Smistachkin  sqlite3 db2 test2.db
1548a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1549a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1550a112d140Smistachkin    [file join [get_pwd] data_dir test2.db]]]]
1551a112d140Smistachkincatch {db2 close}
1552a112d140Smistachkindo_test pragma-20.6 {
1553a112d140Smistachkin  sqlite3 db2 [file join [get_pwd] test2.db]
1554a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1555a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1556a112d140Smistachkin    [file join [get_pwd] test2.db]]]]
1557a112d140Smistachkincatch {db2 close}
1558a112d140Smistachkindo_test pragma-20.7 {
1559a112d140Smistachkin  catchsql "PRAGMA data_store_directory='';"
1560a112d140Smistachkin} {0 {}}
1561a112d140Smistachkindo_test pragma-20.8 {
1562a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1563a112d140Smistachkin} {0 {}}
156492c700dbSdrh
1565a112d140Smistachkinforcedelete data_dir
1566cc71645bSdrh} ;# endif windows
1567cd61c281Sdrh
15685885e762Sdando_test 21.1 {
15695885e762Sdan  # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
15705885e762Sdan  #
15715885e762Sdan  db close
15725885e762Sdan  forcedelete test.db
15735885e762Sdan  sqlite3 db test.db
15745885e762Sdan  execsql {
15755885e762Sdan    PRAGMA page_size = 1024;
15765885e762Sdan    PRAGMA auto_vacuum = 0;
15775885e762Sdan    CREATE TABLE t1(a PRIMARY KEY, b);
15785885e762Sdan    INSERT INTO t1 VALUES(1, 1);
15795885e762Sdan  }
15805885e762Sdan  for {set i 0} {$i < 10} {incr i} {
15815885e762Sdan    execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
15825885e762Sdan  }
15835885e762Sdan  db close
15845885e762Sdan  forcecopy test.db testerr.db
15855885e762Sdan  hexio_write testerr.db 15000 [string repeat 55 100]
15865885e762Sdan} {100}
15875885e762Sdan
15885885e762Sdanset mainerr {*** in database main ***
15895885e762SdanMultiple uses for byte 672 of page 15}
15905885e762Sdanset auxerr {*** in database aux ***
15915885e762SdanMultiple uses for byte 672 of page 15}
15925885e762Sdan
15935885e762Sdando_test 22.2 {
15945885e762Sdan  catch { db close }
15955885e762Sdan  sqlite3 db testerr.db
15965885e762Sdan  execsql { PRAGMA integrity_check }
15975885e762Sdan} [list $mainerr]
15985885e762Sdan
15995885e762Sdando_test 22.3.1 {
16005885e762Sdan  catch { db close }
16015885e762Sdan  sqlite3 db test.db
16025885e762Sdan  execsql {
16035885e762Sdan    ATTACH 'testerr.db' AS 'aux';
16045885e762Sdan    PRAGMA integrity_check;
16055885e762Sdan  }
16065885e762Sdan} [list $auxerr]
16075885e762Sdando_test 22.3.2 {
16085885e762Sdan  execsql { PRAGMA main.integrity_check; }
16095885e762Sdan} {ok}
16105885e762Sdando_test 22.3.3 {
16115885e762Sdan  execsql { PRAGMA aux.integrity_check; }
16125885e762Sdan} [list $auxerr]
16135885e762Sdan
16145885e762Sdando_test 22.4.1 {
16155885e762Sdan  catch { db close }
16165885e762Sdan  sqlite3 db testerr.db
16175885e762Sdan  execsql {
16185885e762Sdan    ATTACH 'test.db' AS 'aux';
16195885e762Sdan    PRAGMA integrity_check;
16205885e762Sdan  }
16215885e762Sdan} [list $mainerr]
16225885e762Sdando_test 22.4.2 {
16235885e762Sdan  execsql { PRAGMA main.integrity_check; }
16245885e762Sdan} [list $mainerr]
16255885e762Sdando_test 22.4.3 {
16265885e762Sdan  execsql { PRAGMA aux.integrity_check; }
16275885e762Sdan} {ok}
16285885e762Sdan
1629*c95e01daSdrhdb close
1630*c95e01daSdrhforcedelete test.db test.db-wal test.db-journal
1631*c95e01daSdrhsqlite3 db test.db
1632*c95e01daSdrhsqlite3 db2 test.db
1633*c95e01daSdrhdo_test 23.1 {
1634*c95e01daSdrh  db eval {
1635*c95e01daSdrh    CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1636*c95e01daSdrh    CREATE INDEX i1 ON t1(b,c);
1637*c95e01daSdrh    CREATE INDEX i2 ON t1(c,d);
1638*c95e01daSdrh    CREATE TABLE t2(x INTEGER REFERENCES t1);
1639*c95e01daSdrh  }
1640*c95e01daSdrh  db2 eval {SELECT name FROM sqlite_master}
1641*c95e01daSdrh} {t1 i1 i2 t2}
1642*c95e01daSdrhdo_test 23.2 {
1643*c95e01daSdrh  db eval {
1644*c95e01daSdrh    DROP INDEX i2;
1645*c95e01daSdrh    CREATE INDEX i2 ON t1(c,d,b);
1646*c95e01daSdrh  }
1647*c95e01daSdrh  db2 eval {PRAGMA index_info(i2)}
1648*c95e01daSdrh} {0 2 c 1 3 d 2 1 b}
1649*c95e01daSdrhdo_test 23.3 {
1650*c95e01daSdrh  db eval {
1651*c95e01daSdrh    CREATE INDEX i3 ON t1(d,b,c);
1652*c95e01daSdrh  }
1653*c95e01daSdrh  db2 eval {PRAGMA index_list(t1)}
1654*c95e01daSdrh} {0 i3 0 1 i2 0 2 i1 0}
1655*c95e01daSdrhdo_test 23.4 {
1656*c95e01daSdrh  db eval {
1657*c95e01daSdrh    ALTER TABLE t1 ADD COLUMN e;
1658*c95e01daSdrh  }
1659*c95e01daSdrh  db2 eval {
1660*c95e01daSdrh    PRAGMA table_info(t1);
1661*c95e01daSdrh  }
1662*c95e01daSdrh} {/4 e {} 0 {} 0/}
1663*c95e01daSdrhdo_test 23.5 {
1664*c95e01daSdrh  db eval {
1665*c95e01daSdrh    DROP TABLE t2;
1666*c95e01daSdrh    CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1667*c95e01daSdrh  }
1668*c95e01daSdrh  db2 eval {
1669*c95e01daSdrh    PRAGMA foreign_key_list(t2);
1670*c95e01daSdrh  }
1671*c95e01daSdrh} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1672*c95e01daSdrh
1673cd61c281Sdrhfinish_test
1674