xref: /sqlite-3.40.0/test/pragma.test (revision f8a78464)
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
19cd61c281Sdrh
2068928b6cSdan# Do not use a codec for tests in this file, as the database file is
2168928b6cSdan# manipulated directly using tcl scripts (using the [hexio_write] command).
2268928b6cSdan#
2368928b6cSdando_not_use_codec
2468928b6cSdan
2591cf71b0Sdanielk1977# Test organization:
2691cf71b0Sdanielk1977#
2791cf71b0Sdanielk1977# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
2891cf71b0Sdanielk1977# pragma-2.*: Test synchronous on attached db.
2991cf71b0Sdanielk1977# pragma-3.*: Test detection of table/index inconsistency by integrity_check.
3091cf71b0Sdanielk1977# pragma-4.*: Test cache_size and default_cache_size on attached db.
3191cf71b0Sdanielk1977# pragma-5.*: Test that pragma synchronous may not be used inside of a
3291cf71b0Sdanielk1977#             transaction.
33cc6bd383Sdanielk1977# pragma-6.*: Test schema-query pragmas.
34cc6bd383Sdanielk1977# pragma-7.*: Miscellaneous tests.
35cc6bd383Sdanielk1977# pragma-8.*: Test user_version and schema_version pragmas.
369a09a3caStpoindex# pragma-9.*: Test temp_store and temp_store_directory.
37cc6bd383Sdanielk1977# pragma-10.*: Test the count_changes pragma in the presence of triggers.
3848af65aeSdanielk1977# pragma-11.*: Test the collation_list pragma.
3959a93791Sdanielk1977# pragma-14.*: Test the page_count pragma.
408cf6c554Sdanielk1977# pragma-15.*: Test that the value set using the cache_size pragma is not
418cf6c554Sdanielk1977#              reset when the schema is reloaded.
42aebf413dSaswift# pragma-16.*: Test proxy locking
4391cf71b0Sdanielk1977#
4491cf71b0Sdanielk1977
4505a82983Sdrhifcapable !pragma {
4605a82983Sdrh  finish_test
4705a82983Sdrh  return
4805a82983Sdrh}
4905a82983Sdrh
50cd61c281Sdrh# Delete the preexisting database to avoid the special setup
51cd61c281Sdrh# that the "all.test" script does.
52cd61c281Sdrh#
53cd61c281Sdrhdb close
54fda06befSmistachkindelete_file test.db test.db-journal
55fda06befSmistachkindelete_file test3.db test3.db-journal
56dddca286Sdrhsqlite3 db test.db; set DB [sqlite3_connection_pointer db]
57cd61c281Sdrh
581e9daa6aSdrh
59c7b4a44cSdanielk1977ifcapable pager_pragmas {
601e9daa6aSdrhset DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
611e9daa6aSdrhset TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
62cd61c281Sdrhdo_test pragma-1.1 {
63cd61c281Sdrh  execsql {
64cd61c281Sdrh    PRAGMA cache_size;
65cd61c281Sdrh    PRAGMA default_cache_size;
66cd61c281Sdrh    PRAGMA synchronous;
67cd61c281Sdrh  }
681e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
69cd61c281Sdrhdo_test pragma-1.2 {
70cd61c281Sdrh  execsql {
71eb43e5c7Sdrh    PRAGMA synchronous=OFF;
72cd61c281Sdrh    PRAGMA cache_size=1234;
73cd61c281Sdrh    PRAGMA cache_size;
74cd61c281Sdrh    PRAGMA default_cache_size;
75cd61c281Sdrh    PRAGMA synchronous;
76cd61c281Sdrh  }
771e9daa6aSdrh} [list 1234 $DFLT_CACHE_SZ 0]
78cd61c281Sdrhdo_test pragma-1.3 {
79cd61c281Sdrh  db close
80ef4ac8f9Sdrh  sqlite3 db test.db
81cd61c281Sdrh  execsql {
82cd61c281Sdrh    PRAGMA cache_size;
83cd61c281Sdrh    PRAGMA default_cache_size;
84cd61c281Sdrh    PRAGMA synchronous;
85cd61c281Sdrh  }
861e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
87cd61c281Sdrhdo_test pragma-1.4 {
88cd61c281Sdrh  execsql {
89cd61c281Sdrh    PRAGMA synchronous=OFF;
90cd61c281Sdrh    PRAGMA cache_size;
91cd61c281Sdrh    PRAGMA default_cache_size;
92cd61c281Sdrh    PRAGMA synchronous;
93cd61c281Sdrh  }
941e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
95cd61c281Sdrhdo_test pragma-1.5 {
96cd61c281Sdrh  execsql {
97d2cb50b7Sdrh    PRAGMA cache_size=-4321;
98cd61c281Sdrh    PRAGMA cache_size;
99cd61c281Sdrh    PRAGMA default_cache_size;
100cd61c281Sdrh    PRAGMA synchronous;
101cd61c281Sdrh  }
1023b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 0]
103cd61c281Sdrhdo_test pragma-1.6 {
104cd61c281Sdrh  execsql {
105cd61c281Sdrh    PRAGMA synchronous=ON;
106cd61c281Sdrh    PRAGMA cache_size;
107cd61c281Sdrh    PRAGMA default_cache_size;
108cd61c281Sdrh    PRAGMA synchronous;
109cd61c281Sdrh  }
1103b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 1]
111cd61c281Sdrhdo_test pragma-1.7 {
112cd61c281Sdrh  db close
113ef4ac8f9Sdrh  sqlite3 db test.db
114cd61c281Sdrh  execsql {
115cd61c281Sdrh    PRAGMA cache_size;
116cd61c281Sdrh    PRAGMA default_cache_size;
117cd61c281Sdrh    PRAGMA synchronous;
118cd61c281Sdrh  }
1191e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
120cd61c281Sdrhdo_test pragma-1.8 {
121cd61c281Sdrh  execsql {
122d2cb50b7Sdrh    PRAGMA default_cache_size=-123;
123cd61c281Sdrh    PRAGMA cache_size;
124cd61c281Sdrh    PRAGMA default_cache_size;
125cd61c281Sdrh    PRAGMA synchronous;
126cd61c281Sdrh  }
12791cf71b0Sdanielk1977} {123 123 2}
128802d69a7Sdrhdo_test pragma-1.9.1 {
129cd61c281Sdrh  db close
130dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
131cd61c281Sdrh  execsql {
132cd61c281Sdrh    PRAGMA cache_size;
133cd61c281Sdrh    PRAGMA default_cache_size;
134cd61c281Sdrh    PRAGMA synchronous;
135cd61c281Sdrh  }
13691cf71b0Sdanielk1977} {123 123 2}
137802d69a7Sdrhifcapable vacuum {
138802d69a7Sdrh  do_test pragma-1.9.2 {
139802d69a7Sdrh    execsql {
140802d69a7Sdrh      VACUUM;
141802d69a7Sdrh      PRAGMA cache_size;
142802d69a7Sdrh      PRAGMA default_cache_size;
143802d69a7Sdrh      PRAGMA synchronous;
144802d69a7Sdrh    }
145802d69a7Sdrh  } {123 123 2}
146802d69a7Sdrh}
14791cf71b0Sdanielk1977do_test pragma-1.10 {
1485a38705eSdrh  execsql {
1494303feedSdrh    PRAGMA synchronous=NORMAL;
1504303feedSdrh    PRAGMA cache_size;
1514303feedSdrh    PRAGMA default_cache_size;
1524303feedSdrh    PRAGMA synchronous;
1534303feedSdrh  }
15491cf71b0Sdanielk1977} {123 123 1}
15591cf71b0Sdanielk1977do_test pragma-1.11 {
1564303feedSdrh  execsql {
1574303feedSdrh    PRAGMA synchronous=FULL;
1584303feedSdrh    PRAGMA cache_size;
1594303feedSdrh    PRAGMA default_cache_size;
1604303feedSdrh    PRAGMA synchronous;
1614303feedSdrh  }
16291cf71b0Sdanielk1977} {123 123 2}
16391cf71b0Sdanielk1977do_test pragma-1.12 {
1644303feedSdrh  db close
165dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
1664303feedSdrh  execsql {
1674303feedSdrh    PRAGMA cache_size;
1684303feedSdrh    PRAGMA default_cache_size;
1694303feedSdrh    PRAGMA synchronous;
1704303feedSdrh  }
17191cf71b0Sdanielk1977} {123 123 2}
17291cf71b0Sdanielk1977
1735260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition
1745260f7e9Sdrh# to keywords like "off" and "full".
1755260f7e9Sdrh#
1765260f7e9Sdrhdo_test pragma-1.13 {
1775260f7e9Sdrh  execsql {
1785260f7e9Sdrh    PRAGMA synchronous=0;
1795260f7e9Sdrh    PRAGMA synchronous;
1805260f7e9Sdrh  }
1815260f7e9Sdrh} {0}
1825260f7e9Sdrhdo_test pragma-1.14 {
1835260f7e9Sdrh  execsql {
1845260f7e9Sdrh    PRAGMA synchronous=2;
1855260f7e9Sdrh    PRAGMA synchronous;
1865260f7e9Sdrh  }
1875260f7e9Sdrh} {2}
188c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
1895260f7e9Sdrh
1905260f7e9Sdrh# Test turning "flag" pragmas on and off.
1915260f7e9Sdrh#
1926338c762Sdanielk1977ifcapable debug {
1936338c762Sdanielk1977  # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
1946338c762Sdanielk1977  #
1955260f7e9Sdrh  do_test pragma-1.15 {
1965260f7e9Sdrh    execsql {
1975260f7e9Sdrh      PRAGMA vdbe_listing=YES;
1985260f7e9Sdrh      PRAGMA vdbe_listing;
1995260f7e9Sdrh    }
2005260f7e9Sdrh  } {1}
2015260f7e9Sdrh  do_test pragma-1.16 {
2025260f7e9Sdrh    execsql {
2035260f7e9Sdrh      PRAGMA vdbe_listing=NO;
2045260f7e9Sdrh      PRAGMA vdbe_listing;
2055260f7e9Sdrh    }
2065260f7e9Sdrh  } {0}
2076338c762Sdanielk1977}
2086338c762Sdanielk1977
2095260f7e9Sdrhdo_test pragma-1.17 {
2105260f7e9Sdrh  execsql {
2115260f7e9Sdrh    PRAGMA parser_trace=ON;
2125260f7e9Sdrh    PRAGMA parser_trace=OFF;
2135260f7e9Sdrh  }
2145260f7e9Sdrh} {}
2155260f7e9Sdrhdo_test pragma-1.18 {
2165260f7e9Sdrh  execsql {
2175260f7e9Sdrh    PRAGMA bogus = -1234;  -- Parsing of negative values
2185260f7e9Sdrh  }
2195260f7e9Sdrh} {}
2205260f7e9Sdrh
22191cf71b0Sdanielk1977# Test modifying the safety_level of an attached database.
2225a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
22391cf71b0Sdanielk1977  do_test pragma-2.1 {
224fda06befSmistachkin    forcedelete test2.db
225fda06befSmistachkin    forcedelete test2.db-journal
2264303feedSdrh    execsql {
22791cf71b0Sdanielk1977      ATTACH 'test2.db' AS aux;
2284303feedSdrh    }
22991cf71b0Sdanielk1977  } {}
23091cf71b0Sdanielk1977  do_test pragma-2.2 {
2314303feedSdrh    execsql {
23291cf71b0Sdanielk1977      pragma aux.synchronous;
2334303feedSdrh    }
23491cf71b0Sdanielk1977  } {2}
23591cf71b0Sdanielk1977  do_test pragma-2.3 {
23691cf71b0Sdanielk1977    execsql {
23791cf71b0Sdanielk1977      pragma aux.synchronous = OFF;
23891cf71b0Sdanielk1977      pragma aux.synchronous;
23991cf71b0Sdanielk1977      pragma synchronous;
24091cf71b0Sdanielk1977    }
24191cf71b0Sdanielk1977  } {0 2}
24291cf71b0Sdanielk1977  do_test pragma-2.4 {
24391cf71b0Sdanielk1977    execsql {
24491cf71b0Sdanielk1977      pragma aux.synchronous = ON;
24591cf71b0Sdanielk1977      pragma synchronous;
24691cf71b0Sdanielk1977      pragma aux.synchronous;
24791cf71b0Sdanielk1977    }
24891cf71b0Sdanielk1977  } {2 1}
249c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
2504303feedSdrh
251ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check
252ed717fe3Sdrh# pragma finds it.
253ed717fe3Sdrh#
25425d6543dSdrh# These tests won't work if the database is encrypted
25525d6543dSdrh#
256ed717fe3Sdrhdo_test pragma-3.1 {
2571e9daa6aSdrh  db close
258fda06befSmistachkin  forcedelete test.db test.db-journal
2591e9daa6aSdrh  sqlite3 db test.db
260ed717fe3Sdrh  execsql {
2611e9daa6aSdrh    PRAGMA auto_vacuum=OFF;
262ed717fe3Sdrh    BEGIN;
263ed717fe3Sdrh    CREATE TABLE t2(a,b,c);
264ed717fe3Sdrh    CREATE INDEX i2 ON t2(a);
265ed717fe3Sdrh    INSERT INTO t2 VALUES(11,2,3);
266ed717fe3Sdrh    INSERT INTO t2 VALUES(22,3,4);
267ed717fe3Sdrh    COMMIT;
268ed717fe3Sdrh    SELECT rowid, * from t2;
269ed717fe3Sdrh  }
270ed717fe3Sdrh} {1 11 2 3 2 22 3 4}
2715a8f9374Sdanielk1977ifcapable attach {
27240e016e4Sdrh  if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
273ed717fe3Sdrh    do_test pragma-3.2 {
274bb8a279eSdrh      db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
275bb8a279eSdrh      set pgsz [db eval {PRAGMA page_size}]
276bb8a279eSdrh      # overwrite the header on the rootpage of the index in order to
277bb8a279eSdrh      # make the index appear to be empty.
278bb8a279eSdrh      #
279bb8a279eSdrh      set offset [expr {$pgsz*($rootpage-1)}]
280bb8a279eSdrh      hexio_write test.db $offset 0a00000000040000000000
281bb8a279eSdrh      db close
282bb8a279eSdrh      sqlite3 db test.db
283ed717fe3Sdrh      execsql {PRAGMA integrity_check}
284bb8a279eSdrh    } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
28591cf71b0Sdanielk1977    do_test pragma-3.3 {
2861dcdbc06Sdrh      execsql {PRAGMA integrity_check=1}
2871dcdbc06Sdrh    } {{rowid 1 missing from index i2}}
2881dcdbc06Sdrh    do_test pragma-3.4 {
28991cf71b0Sdanielk1977      execsql {
2901dcdbc06Sdrh        ATTACH DATABASE 'test.db' AS t2;
2911dcdbc06Sdrh        PRAGMA integrity_check
29291cf71b0Sdanielk1977      }
293bb8a279eSdrh    } {{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}}
2941dcdbc06Sdrh    do_test pragma-3.5 {
2951dcdbc06Sdrh      execsql {
296bb8a279eSdrh        PRAGMA integrity_check=4
2971dcdbc06Sdrh      }
298bb8a279eSdrh    } {{rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2} {rowid 1 missing from index i2}}
2991dcdbc06Sdrh    do_test pragma-3.6 {
3001dcdbc06Sdrh      execsql {
3011dcdbc06Sdrh        PRAGMA integrity_check=xyz
3021dcdbc06Sdrh      }
303bb8a279eSdrh    } {{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}}
3041dcdbc06Sdrh    do_test pragma-3.7 {
3051dcdbc06Sdrh      execsql {
3061dcdbc06Sdrh        PRAGMA integrity_check=0
3071dcdbc06Sdrh      }
308bb8a279eSdrh    } {{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}}
3091dcdbc06Sdrh
3101dcdbc06Sdrh    # Add additional corruption by appending unused pages to the end of
3111dcdbc06Sdrh    # the database file testerr.db
3121dcdbc06Sdrh    #
3131dcdbc06Sdrh    do_test pragma-3.8 {
3141dcdbc06Sdrh      execsql {DETACH t2}
315fda06befSmistachkin      forcedelete testerr.db testerr.db-journal
316e3d559adSdrh      set out [open testerr.db w]
317e3d559adSdrh      fconfigure $out -translation binary
318e3d559adSdrh      set in [open test.db r]
319e3d559adSdrh      fconfigure $in -translation binary
3201dcdbc06Sdrh      puts -nonewline $out [read $in]
3211dcdbc06Sdrh      seek $in 0
3221dcdbc06Sdrh      puts -nonewline $out [read $in]
3231dcdbc06Sdrh      close $in
3241dcdbc06Sdrh      close $out
325dd3cd977Sdrh      hexio_write testerr.db 28 00000000
3261dcdbc06Sdrh      execsql {REINDEX t2}
3271dcdbc06Sdrh      execsql {PRAGMA integrity_check}
3281dcdbc06Sdrh    } {ok}
32941c58b78Sdanielk1977    do_test pragma-3.8.1 {
33041c58b78Sdanielk1977      execsql {PRAGMA quick_check}
33141c58b78Sdanielk1977    } {ok}
3325d16a9a6Sdrh    do_test pragma-3.8.2 {
3335d16a9a6Sdrh      execsql {PRAGMA QUICK_CHECK}
3345d16a9a6Sdrh    } {ok}
3351dcdbc06Sdrh    do_test pragma-3.9 {
3361dcdbc06Sdrh      execsql {
3371dcdbc06Sdrh        ATTACH 'testerr.db' AS t2;
3381dcdbc06Sdrh        PRAGMA integrity_check
3391dcdbc06Sdrh      }
3401dcdbc06Sdrh    } {{*** in database t2 ***
3411dcdbc06SdrhPage 4 is never used
3421dcdbc06SdrhPage 5 is never used
343bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
3441dcdbc06Sdrh    do_test pragma-3.10 {
3451dcdbc06Sdrh      execsql {
3461dcdbc06Sdrh        PRAGMA integrity_check=1
3471dcdbc06Sdrh      }
3481dcdbc06Sdrh    } {{*** in database t2 ***
3491dcdbc06SdrhPage 4 is never used}}
3501dcdbc06Sdrh    do_test pragma-3.11 {
3511dcdbc06Sdrh      execsql {
3521dcdbc06Sdrh        PRAGMA integrity_check=5
3531dcdbc06Sdrh      }
3541dcdbc06Sdrh    } {{*** in database t2 ***
3551dcdbc06SdrhPage 4 is never used
3561dcdbc06SdrhPage 5 is never used
357bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2}}
3581dcdbc06Sdrh    do_test pragma-3.12 {
3591dcdbc06Sdrh      execsql {
3601dcdbc06Sdrh        PRAGMA integrity_check=4
3611dcdbc06Sdrh      }
3621dcdbc06Sdrh    } {{*** in database t2 ***
3631dcdbc06SdrhPage 4 is never used
3641dcdbc06SdrhPage 5 is never used
3651dcdbc06SdrhPage 6 is never used} {rowid 1 missing from index i2}}
3661dcdbc06Sdrh    do_test pragma-3.13 {
3671dcdbc06Sdrh      execsql {
3681dcdbc06Sdrh        PRAGMA integrity_check=3
3691dcdbc06Sdrh      }
3701dcdbc06Sdrh    } {{*** in database t2 ***
3711dcdbc06SdrhPage 4 is never used
3721dcdbc06SdrhPage 5 is never used
3731dcdbc06SdrhPage 6 is never used}}
3741dcdbc06Sdrh    do_test pragma-3.14 {
3751dcdbc06Sdrh      execsql {
3761dcdbc06Sdrh        PRAGMA integrity_check(2)
3771dcdbc06Sdrh      }
3781dcdbc06Sdrh    } {{*** in database t2 ***
3791dcdbc06SdrhPage 4 is never used
3801dcdbc06SdrhPage 5 is never used}}
3811dcdbc06Sdrh    do_test pragma-3.15 {
3821dcdbc06Sdrh      execsql {
3831dcdbc06Sdrh        ATTACH 'testerr.db' AS t3;
3841dcdbc06Sdrh        PRAGMA integrity_check
3851dcdbc06Sdrh      }
3861dcdbc06Sdrh    } {{*** in database t2 ***
3871dcdbc06SdrhPage 4 is never used
3881dcdbc06SdrhPage 5 is never used
389bb8a279eSdrhPage 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 ***
3901dcdbc06SdrhPage 4 is never used
3911dcdbc06SdrhPage 5 is never used
392bb8a279eSdrhPage 6 is never used} {rowid 1 missing from index i2} {rowid 2 missing from index i2} {wrong # of entries in index i2}}
3931dcdbc06Sdrh    do_test pragma-3.16 {
3941dcdbc06Sdrh      execsql {
395bb8a279eSdrh        PRAGMA integrity_check(10)
3961dcdbc06Sdrh      }
3971dcdbc06Sdrh    } {{*** in database t2 ***
3981dcdbc06SdrhPage 4 is never used
3991dcdbc06SdrhPage 5 is never used
400bb8a279eSdrhPage 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 ***
4011dcdbc06SdrhPage 4 is never used
4021dcdbc06SdrhPage 5 is never used
4031dcdbc06SdrhPage 6 is never used} {rowid 1 missing from index i2}}
4041dcdbc06Sdrh    do_test pragma-3.17 {
4051dcdbc06Sdrh      execsql {
406bb8a279eSdrh        PRAGMA integrity_check=8
4071dcdbc06Sdrh      }
4081dcdbc06Sdrh    } {{*** in database t2 ***
4091dcdbc06SdrhPage 4 is never used
4101dcdbc06SdrhPage 5 is never used
411bb8a279eSdrhPage 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 ***
4121dcdbc06SdrhPage 4 is never used
4131dcdbc06SdrhPage 5 is never used}}
4141dcdbc06Sdrh    do_test pragma-3.18 {
4151dcdbc06Sdrh      execsql {
4161dcdbc06Sdrh        PRAGMA integrity_check=4
4171dcdbc06Sdrh      }
4181dcdbc06Sdrh    } {{*** in database t2 ***
4191dcdbc06SdrhPage 4 is never used
4201dcdbc06SdrhPage 5 is never used
4211dcdbc06SdrhPage 6 is never used} {rowid 1 missing from index i2}}
4221dcdbc06Sdrh  }
423d2cb50b7Sdrh  do_test pragma-3.19 {
424d2cb50b7Sdrh    catch {db close}
425fda06befSmistachkin    forcedelete test.db test.db-journal
426d2cb50b7Sdrh    sqlite3 db test.db
427d2cb50b7Sdrh    db eval {PRAGMA integrity_check}
428d2cb50b7Sdrh  } {ok}
4295a8f9374Sdanielk1977}
430d2cb50b7Sdrh#exit
43191cf71b0Sdanielk1977
43291cf71b0Sdanielk1977# Test modifying the cache_size of an attached database.
4335a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
43491cf71b0Sdanielk1977do_test pragma-4.1 {
43591cf71b0Sdanielk1977  execsql {
4361e9daa6aSdrh    ATTACH 'test2.db' AS aux;
43791cf71b0Sdanielk1977    pragma aux.cache_size;
43891cf71b0Sdanielk1977    pragma aux.default_cache_size;
43991cf71b0Sdanielk1977  }
4401e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
4411bdd9b57Sdrhdo_test pragma-4.2 {
4421bdd9b57Sdrh  execsql {
44391cf71b0Sdanielk1977    pragma aux.cache_size = 50;
44491cf71b0Sdanielk1977    pragma aux.cache_size;
44591cf71b0Sdanielk1977    pragma aux.default_cache_size;
4461bdd9b57Sdrh  }
4471e9daa6aSdrh} [list 50 $DFLT_CACHE_SZ]
4481bdd9b57Sdrhdo_test pragma-4.3 {
4491bdd9b57Sdrh  execsql {
45091cf71b0Sdanielk1977    pragma aux.default_cache_size = 456;
45191cf71b0Sdanielk1977    pragma aux.cache_size;
45291cf71b0Sdanielk1977    pragma aux.default_cache_size;
4531bdd9b57Sdrh  }
45491cf71b0Sdanielk1977} {456 456}
4551bdd9b57Sdrhdo_test pragma-4.4 {
4561bdd9b57Sdrh  execsql {
45791cf71b0Sdanielk1977    pragma cache_size;
45891cf71b0Sdanielk1977    pragma default_cache_size;
4591bdd9b57Sdrh  }
4601e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
4611bdd9b57Sdrhdo_test pragma-4.5 {
4621bdd9b57Sdrh  execsql {
46391cf71b0Sdanielk1977    DETACH aux;
46491cf71b0Sdanielk1977    ATTACH 'test3.db' AS aux;
46591cf71b0Sdanielk1977    pragma aux.cache_size;
46691cf71b0Sdanielk1977    pragma aux.default_cache_size;
4671bdd9b57Sdrh  }
4681e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
4691bdd9b57Sdrhdo_test pragma-4.6 {
4701bdd9b57Sdrh  execsql {
47191cf71b0Sdanielk1977    DETACH aux;
47291cf71b0Sdanielk1977    ATTACH 'test2.db' AS aux;
47391cf71b0Sdanielk1977    pragma aux.cache_size;
47491cf71b0Sdanielk1977    pragma aux.default_cache_size;
4751bdd9b57Sdrh  }
47691cf71b0Sdanielk1977} {456 456}
477c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
4781bdd9b57Sdrh
47991cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is
48091cf71b0Sdanielk1977# disallowed.
481c7b4a44cSdanielk1977ifcapable pager_pragmas {
48291cf71b0Sdanielk1977do_test pragma-5.0 {
4831bdd9b57Sdrh  execsql {
48491cf71b0Sdanielk1977    pragma synchronous;
4851bdd9b57Sdrh  }
48691cf71b0Sdanielk1977} {2}
48791cf71b0Sdanielk1977do_test pragma-5.1 {
4881bdd9b57Sdrh  catchsql {
4891bdd9b57Sdrh    BEGIN;
49091cf71b0Sdanielk1977    pragma synchronous = OFF;
4911bdd9b57Sdrh  }
49291cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}}
49391cf71b0Sdanielk1977do_test pragma-5.2 {
49491cf71b0Sdanielk1977  execsql {
49591cf71b0Sdanielk1977    pragma synchronous;
49691cf71b0Sdanielk1977  }
49791cf71b0Sdanielk1977} {2}
4985260f7e9Sdrhcatchsql {COMMIT;}
499c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
5005260f7e9Sdrh
5015260f7e9Sdrh# Test schema-query pragmas
5025260f7e9Sdrh#
50327188fb5Sdanielk1977ifcapable schema_pragmas {
5045a8f9374Sdanielk1977ifcapable tempdb&&attach {
5055260f7e9Sdrh  do_test pragma-6.1 {
506956bc92cSdrh    set res {}
50740e016e4Sdrh    execsql {SELECT * FROM sqlite_temp_master}
5085260f7e9Sdrh    foreach {idx name file} [execsql {pragma database_list}] {
5095260f7e9Sdrh      lappend res $idx $name
5105260f7e9Sdrh    }
5115260f7e9Sdrh    set res
512a71aa001Sdrh  } {0 main 1 temp 2 aux}
51353c0f748Sdanielk1977}
5145260f7e9Sdrhdo_test pragma-6.2 {
5155260f7e9Sdrh  execsql {
516d2cb50b7Sdrh    CREATE TABLE t2(a,b,c);
5175260f7e9Sdrh    pragma table_info(t2)
5185260f7e9Sdrh  }
519bfa8b102Sdrh} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
520d2cb50b7Sdrhdo_test pragma-6.2.1 {
521d2cb50b7Sdrh  execsql {
522d2cb50b7Sdrh    pragma table_info;
523d2cb50b7Sdrh  }
524d2cb50b7Sdrh} {}
525736c7d4bSdrhdb nullvalue <<NULL>>
526417ec638Sdrhdo_test pragma-6.2.2 {
527417ec638Sdrh  execsql {
528736c7d4bSdrh    CREATE TABLE t5(
529736c7d4bSdrh      a TEXT DEFAULT CURRENT_TIMESTAMP,
530736c7d4bSdrh      b DEFAULT (5+3),
531736c7d4bSdrh      c TEXT,
532736c7d4bSdrh      d INTEGER DEFAULT NULL,
533736c7d4bSdrh      e TEXT DEFAULT ''
534736c7d4bSdrh    );
535417ec638Sdrh    PRAGMA table_info(t5);
536417ec638Sdrh  }
537736c7d4bSdrh} {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 0 2 c TEXT 0 <<NULL>> 0 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 0}
538736c7d4bSdrhdb nullvalue {}
5396bf89570Sdrhifcapable {foreignkey} {
540d2cb50b7Sdrh  do_test pragma-6.3.1 {
5415260f7e9Sdrh    execsql {
5425260f7e9Sdrh      CREATE TABLE t3(a int references t2(b), b UNIQUE);
5435260f7e9Sdrh      pragma foreign_key_list(t3);
5445260f7e9Sdrh    }
5451da40a38Sdan  } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
546d2cb50b7Sdrh  do_test pragma-6.3.2 {
547d2cb50b7Sdrh    execsql {
548d2cb50b7Sdrh      pragma foreign_key_list;
549d2cb50b7Sdrh    }
550d2cb50b7Sdrh  } {}
551d2cb50b7Sdrh  do_test pragma-6.3.3 {
552d2cb50b7Sdrh    execsql {
553d2cb50b7Sdrh      pragma foreign_key_list(t3_bogus);
554d2cb50b7Sdrh    }
555d2cb50b7Sdrh  } {}
556d2cb50b7Sdrh  do_test pragma-6.3.4 {
557d2cb50b7Sdrh    execsql {
558d2cb50b7Sdrh      pragma foreign_key_list(t5);
559d2cb50b7Sdrh    }
560d2cb50b7Sdrh  } {}
5615260f7e9Sdrh  do_test pragma-6.4 {
5625260f7e9Sdrh    execsql {
5635260f7e9Sdrh      pragma index_list(t3);
5645260f7e9Sdrh    }
5655260f7e9Sdrh  } {0 sqlite_autoindex_t3_1 1}
5666bf89570Sdrh}
5676bf89570Sdrhifcapable {!foreignkey} {
5686bf89570Sdrh  execsql {CREATE TABLE t3(a,b UNIQUE)}
5696bf89570Sdrh}
570d2cb50b7Sdrhdo_test pragma-6.5.1 {
5715260f7e9Sdrh  execsql {
5725260f7e9Sdrh    CREATE INDEX t3i1 ON t3(a,b);
5735260f7e9Sdrh    pragma index_info(t3i1);
5745260f7e9Sdrh  }
5755260f7e9Sdrh} {0 0 a 1 1 b}
576d2cb50b7Sdrhdo_test pragma-6.5.2 {
577d2cb50b7Sdrh  execsql {
578d2cb50b7Sdrh    pragma index_info(t3i1_bogus);
579d2cb50b7Sdrh  }
580d2cb50b7Sdrh} {}
581260d8a6aSdanielk1977
582260d8a6aSdanielk1977ifcapable tempdb {
583260d8a6aSdanielk1977  # Test for ticket #3320. When a temp table of the same name exists, make
584260d8a6aSdanielk1977  # sure the schema of the main table can still be queried using
585260d8a6aSdanielk1977  # "pragma table_info":
586260d8a6aSdanielk1977  do_test pragma-6.6.1 {
587260d8a6aSdanielk1977    execsql {
588260d8a6aSdanielk1977      CREATE TABLE trial(col_main);
589260d8a6aSdanielk1977      CREATE TEMP TABLE trial(col_temp);
590260d8a6aSdanielk1977    }
591260d8a6aSdanielk1977  } {}
592260d8a6aSdanielk1977  do_test pragma-6.6.2 {
593260d8a6aSdanielk1977    execsql {
594260d8a6aSdanielk1977      PRAGMA table_info(trial);
595260d8a6aSdanielk1977    }
596260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
597260d8a6aSdanielk1977  do_test pragma-6.6.3 {
598260d8a6aSdanielk1977    execsql {
599260d8a6aSdanielk1977      PRAGMA temp.table_info(trial);
600260d8a6aSdanielk1977    }
601260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
602260d8a6aSdanielk1977  do_test pragma-6.6.4 {
603260d8a6aSdanielk1977    execsql {
604260d8a6aSdanielk1977      PRAGMA main.table_info(trial);
605260d8a6aSdanielk1977    }
606260d8a6aSdanielk1977  } {0 col_main {} 0 {} 0}
607260d8a6aSdanielk1977}
608f96a3778Sdanielk1977
609f96a3778Sdanielk1977do_test pragma-6.7 {
610f96a3778Sdanielk1977  execsql {
611f96a3778Sdanielk1977    CREATE TABLE test_table(
612f96a3778Sdanielk1977      one INT NOT NULL DEFAULT -1,
613f96a3778Sdanielk1977      two text,
614f96a3778Sdanielk1977      three VARCHAR(45, 65) DEFAULT 'abcde',
615f96a3778Sdanielk1977      four REAL DEFAULT X'abcdef',
616f96a3778Sdanielk1977      five DEFAULT CURRENT_TIME
617f96a3778Sdanielk1977    );
618f96a3778Sdanielk1977    PRAGMA table_info(test_table);
619f96a3778Sdanielk1977  }
620f96a3778Sdanielk1977} [concat \
621f96a3778Sdanielk1977  {0 one INT 1 -1 0} \
622f96a3778Sdanielk1977  {1 two text 0 {} 0} \
623f96a3778Sdanielk1977  {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
624f96a3778Sdanielk1977  {3 four REAL 0 X'abcdef' 0} \
625f96a3778Sdanielk1977  {4 five {} 0 CURRENT_TIME 0} \
626f96a3778Sdanielk1977]
62727188fb5Sdanielk1977} ;# ifcapable schema_pragmas
6285260f7e9Sdrh# Miscellaneous tests
6295260f7e9Sdrh#
63027188fb5Sdanielk1977ifcapable schema_pragmas {
631d2cb50b7Sdrhdo_test pragma-7.1.1 {
6325260f7e9Sdrh  # Make sure a pragma knows to read the schema if it needs to
6335260f7e9Sdrh  db close
6345260f7e9Sdrh  sqlite3 db test.db
6355260f7e9Sdrh  execsql {
6365260f7e9Sdrh    pragma index_list(t3);
6375260f7e9Sdrh  }
6385260f7e9Sdrh} {0 t3i1 0 1 sqlite_autoindex_t3_1 1}
639d2cb50b7Sdrhdo_test pragma-7.1.2 {
640d2cb50b7Sdrh  execsql {
641d2cb50b7Sdrh    pragma index_list(t3_bogus);
642d2cb50b7Sdrh  }
643d2cb50b7Sdrh} {}
64427188fb5Sdanielk1977} ;# ifcapable schema_pragmas
6456c62608fSdrhifcapable {utf16} {
646cb354603Sdan  if {[permutation] == ""} {
6475260f7e9Sdrh    do_test pragma-7.2 {
6485260f7e9Sdrh      db close
6495260f7e9Sdrh      sqlite3 db test.db
6505260f7e9Sdrh      catchsql {
6515260f7e9Sdrh        pragma encoding=bogus;
6525260f7e9Sdrh      }
6535260f7e9Sdrh    } {1 {unsupported encoding: bogus}}
6546c62608fSdrh  }
655cb354603Sdan}
65653c0f748Sdanielk1977ifcapable tempdb {
6575260f7e9Sdrh  do_test pragma-7.3 {
6585260f7e9Sdrh    db close
6595260f7e9Sdrh    sqlite3 db test.db
6605260f7e9Sdrh    execsql {
6615260f7e9Sdrh      pragma lock_status;
6625260f7e9Sdrh    }
6635260f7e9Sdrh  } {main unlocked temp closed}
66453c0f748Sdanielk1977} else {
66553c0f748Sdanielk1977  do_test pragma-7.3 {
66653c0f748Sdanielk1977    db close
66753c0f748Sdanielk1977    sqlite3 db test.db
66853c0f748Sdanielk1977    execsql {
66953c0f748Sdanielk1977      pragma lock_status;
67053c0f748Sdanielk1977    }
67153c0f748Sdanielk1977  } {main unlocked}
67253c0f748Sdanielk1977}
6735260f7e9Sdrh
6745260f7e9Sdrh
675dae2495bSdanielk1977#----------------------------------------------------------------------
676b92b70bbSdanielk1977# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
677b92b70bbSdanielk1977# user_version" statements.
678dae2495bSdanielk1977#
679b92b70bbSdanielk1977# pragma-8.1: PRAGMA schema_version
680b92b70bbSdanielk1977# pragma-8.2: PRAGMA user_version
681dae2495bSdanielk1977#
682dae2495bSdanielk1977
68311cf9fbcSdanielk1977ifcapable schema_version {
68411cf9fbcSdanielk1977
685b92b70bbSdanielk1977# First check that we can set the schema version and then retrieve the
686dae2495bSdanielk1977# same value.
687dae2495bSdanielk1977do_test pragma-8.1.1 {
688dae2495bSdanielk1977  execsql {
689b92b70bbSdanielk1977    PRAGMA schema_version = 105;
690dae2495bSdanielk1977  }
691dae2495bSdanielk1977} {}
692dae2495bSdanielk1977do_test pragma-8.1.2 {
69325403650Sdrh  execsql2 {
694b92b70bbSdanielk1977    PRAGMA schema_version;
695dae2495bSdanielk1977  }
69625403650Sdrh} {schema_version 105}
697dae2495bSdanielk1977do_test pragma-8.1.3 {
698dae2495bSdanielk1977  execsql {
699b92b70bbSdanielk1977    PRAGMA schema_version = 106;
700dae2495bSdanielk1977  }
701dae2495bSdanielk1977} {}
702dae2495bSdanielk1977do_test pragma-8.1.4 {
703dae2495bSdanielk1977  execsql {
704b92b70bbSdanielk1977    PRAGMA schema_version;
705dae2495bSdanielk1977  }
706dae2495bSdanielk1977} 106
707dae2495bSdanielk1977
708b92b70bbSdanielk1977# Check that creating a table modifies the schema-version (this is really
709b92b70bbSdanielk1977# to verify that the value being read is in fact the schema version).
710dae2495bSdanielk1977do_test pragma-8.1.5 {
711dae2495bSdanielk1977  execsql {
712dae2495bSdanielk1977    CREATE TABLE t4(a, b, c);
713dae2495bSdanielk1977    INSERT INTO t4 VALUES(1, 2, 3);
714dae2495bSdanielk1977    SELECT * FROM t4;
715dae2495bSdanielk1977  }
716dae2495bSdanielk1977} {1 2 3}
717dae2495bSdanielk1977do_test pragma-8.1.6 {
718dae2495bSdanielk1977  execsql {
719b92b70bbSdanielk1977    PRAGMA schema_version;
720dae2495bSdanielk1977  }
721dae2495bSdanielk1977} 107
722dae2495bSdanielk1977
723dae2495bSdanielk1977# Now open a second connection to the database. Ensure that changing the
724b92b70bbSdanielk1977# schema-version using the first connection forces the second connection
725dae2495bSdanielk1977# to reload the schema. This has to be done using the C-API test functions,
726dae2495bSdanielk1977# because the TCL API accounts for SCHEMA_ERROR and retries the query.
727dae2495bSdanielk1977do_test pragma-8.1.7 {
728dddca286Sdrh  sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
729dae2495bSdanielk1977  execsql {
730dae2495bSdanielk1977    SELECT * FROM t4;
731dae2495bSdanielk1977  } db2
732dae2495bSdanielk1977} {1 2 3}
733dae2495bSdanielk1977do_test pragma-8.1.8 {
734dae2495bSdanielk1977  execsql {
735b92b70bbSdanielk1977    PRAGMA schema_version = 108;
736dae2495bSdanielk1977  }
737dae2495bSdanielk1977} {}
738dae2495bSdanielk1977do_test pragma-8.1.9 {
739dae2495bSdanielk1977  set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
740dae2495bSdanielk1977  sqlite3_step $::STMT
741dae2495bSdanielk1977} SQLITE_ERROR
742dae2495bSdanielk1977do_test pragma-8.1.10 {
743dae2495bSdanielk1977  sqlite3_finalize $::STMT
744dae2495bSdanielk1977} SQLITE_SCHEMA
745dae2495bSdanielk1977
746b92b70bbSdanielk1977# Make sure the schema-version can be manipulated in an attached database.
747fda06befSmistachkinforcedelete test2.db
748fda06befSmistachkinforcedelete test2.db-journal
7495a8f9374Sdanielk1977ifcapable attach {
750dae2495bSdanielk1977  do_test pragma-8.1.11 {
751dae2495bSdanielk1977    execsql {
752dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
753dae2495bSdanielk1977      CREATE TABLE aux.t1(a, b, c);
754b92b70bbSdanielk1977      PRAGMA aux.schema_version = 205;
755dae2495bSdanielk1977    }
756dae2495bSdanielk1977  } {}
757dae2495bSdanielk1977  do_test pragma-8.1.12 {
758dae2495bSdanielk1977    execsql {
759b92b70bbSdanielk1977      PRAGMA aux.schema_version;
760dae2495bSdanielk1977    }
761dae2495bSdanielk1977  } 205
7625a8f9374Sdanielk1977}
763dae2495bSdanielk1977do_test pragma-8.1.13 {
764dae2495bSdanielk1977  execsql {
765b92b70bbSdanielk1977    PRAGMA schema_version;
766dae2495bSdanielk1977  }
767dae2495bSdanielk1977} 108
768dae2495bSdanielk1977
769b92b70bbSdanielk1977# And check that modifying the schema-version in an attached database
770dae2495bSdanielk1977# forces the second connection to reload the schema.
7715a8f9374Sdanielk1977ifcapable attach {
772dae2495bSdanielk1977  do_test pragma-8.1.14 {
773dddca286Sdrh    sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
774dae2495bSdanielk1977    execsql {
775dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
776dae2495bSdanielk1977      SELECT * FROM aux.t1;
777dae2495bSdanielk1977    } db2
778dae2495bSdanielk1977  } {}
779dae2495bSdanielk1977  do_test pragma-8.1.15 {
780dae2495bSdanielk1977    execsql {
781b92b70bbSdanielk1977      PRAGMA aux.schema_version = 206;
782dae2495bSdanielk1977    }
783dae2495bSdanielk1977  } {}
784dae2495bSdanielk1977  do_test pragma-8.1.16 {
785dae2495bSdanielk1977    set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
786dae2495bSdanielk1977    sqlite3_step $::STMT
787dae2495bSdanielk1977  } SQLITE_ERROR
788dae2495bSdanielk1977  do_test pragma-8.1.17 {
789dae2495bSdanielk1977    sqlite3_finalize $::STMT
790dae2495bSdanielk1977  } SQLITE_SCHEMA
791dae2495bSdanielk1977  do_test pragma-8.1.18 {
792dae2495bSdanielk1977    db2 close
793dae2495bSdanielk1977  } {}
7945a8f9374Sdanielk1977}
795dae2495bSdanielk1977
796b92b70bbSdanielk1977# Now test that the user-version can be read and written (and that we aren't
797b92b70bbSdanielk1977# accidentally manipulating the schema-version instead).
798dae2495bSdanielk1977do_test pragma-8.2.1 {
79925403650Sdrh  execsql2 {
800b92b70bbSdanielk1977    PRAGMA user_version;
801dae2495bSdanielk1977  }
80225403650Sdrh} {user_version 0}
803dae2495bSdanielk1977do_test pragma-8.2.2 {
804dae2495bSdanielk1977  execsql {
805b92b70bbSdanielk1977    PRAGMA user_version = 2;
806dae2495bSdanielk1977  }
807dae2495bSdanielk1977} {}
808802d69a7Sdrhdo_test pragma-8.2.3.1 {
80925403650Sdrh  execsql2 {
810b92b70bbSdanielk1977    PRAGMA user_version;
811dae2495bSdanielk1977  }
81225403650Sdrh} {user_version 2}
813802d69a7Sdrhdo_test pragma-8.2.3.2 {
814802d69a7Sdrh  db close
815802d69a7Sdrh  sqlite3 db test.db
816802d69a7Sdrh  execsql {
817802d69a7Sdrh    PRAGMA user_version;
818802d69a7Sdrh  }
819802d69a7Sdrh} {2}
820802d69a7Sdrhdo_test pragma-8.2.4.1 {
821dae2495bSdanielk1977  execsql {
822b92b70bbSdanielk1977    PRAGMA schema_version;
823dae2495bSdanielk1977  }
824dae2495bSdanielk1977} {108}
825802d69a7Sdrhifcapable vacuum {
826802d69a7Sdrh  do_test pragma-8.2.4.2 {
827802d69a7Sdrh    execsql {
828802d69a7Sdrh      VACUUM;
829802d69a7Sdrh      PRAGMA user_version;
830802d69a7Sdrh    }
831802d69a7Sdrh  } {2}
832802d69a7Sdrh  do_test pragma-8.2.4.3 {
833802d69a7Sdrh    execsql {
834802d69a7Sdrh      PRAGMA schema_version;
835802d69a7Sdrh    }
836802d69a7Sdrh  } {109}
837802d69a7Sdrh}
8385a8f9374Sdanielk1977
8395a8f9374Sdanielk1977ifcapable attach {
840802d69a7Sdrh  db eval {ATTACH 'test2.db' AS aux}
841dae2495bSdanielk1977
842b92b70bbSdanielk1977  # Check that the user-version in the auxilary database can be manipulated (
843dae2495bSdanielk1977  # and that we aren't accidentally manipulating the same in the main db).
844dae2495bSdanielk1977  do_test pragma-8.2.5 {
845dae2495bSdanielk1977    execsql {
846b92b70bbSdanielk1977      PRAGMA aux.user_version;
847dae2495bSdanielk1977    }
848dae2495bSdanielk1977  } {0}
849dae2495bSdanielk1977  do_test pragma-8.2.6 {
850dae2495bSdanielk1977    execsql {
851b92b70bbSdanielk1977      PRAGMA aux.user_version = 3;
852dae2495bSdanielk1977    }
853dae2495bSdanielk1977  } {}
854dae2495bSdanielk1977  do_test pragma-8.2.7 {
855dae2495bSdanielk1977    execsql {
856b92b70bbSdanielk1977      PRAGMA aux.user_version;
857dae2495bSdanielk1977    }
858dae2495bSdanielk1977  } {3}
859dae2495bSdanielk1977  do_test pragma-8.2.8 {
860dae2495bSdanielk1977    execsql {
861b92b70bbSdanielk1977      PRAGMA main.user_version;
862dae2495bSdanielk1977    }
863dae2495bSdanielk1977  } {2}
864dae2495bSdanielk1977
865b92b70bbSdanielk1977  # Now check that a ROLLBACK resets the user-version if it has been modified
866dae2495bSdanielk1977  # within a transaction.
867dae2495bSdanielk1977  do_test pragma-8.2.9 {
868dae2495bSdanielk1977    execsql {
869dae2495bSdanielk1977      BEGIN;
870b92b70bbSdanielk1977      PRAGMA aux.user_version = 10;
871b92b70bbSdanielk1977      PRAGMA user_version = 11;
872dae2495bSdanielk1977    }
873dae2495bSdanielk1977  } {}
874dae2495bSdanielk1977  do_test pragma-8.2.10 {
875dae2495bSdanielk1977    execsql {
876b92b70bbSdanielk1977      PRAGMA aux.user_version;
877dae2495bSdanielk1977    }
878dae2495bSdanielk1977  } {10}
879dae2495bSdanielk1977  do_test pragma-8.2.11 {
880dae2495bSdanielk1977    execsql {
881b92b70bbSdanielk1977      PRAGMA main.user_version;
882dae2495bSdanielk1977    }
883dae2495bSdanielk1977  } {11}
884dae2495bSdanielk1977  do_test pragma-8.2.12 {
885dae2495bSdanielk1977    execsql {
886dae2495bSdanielk1977      ROLLBACK;
887b92b70bbSdanielk1977      PRAGMA aux.user_version;
888dae2495bSdanielk1977    }
889dae2495bSdanielk1977  } {3}
890dae2495bSdanielk1977  do_test pragma-8.2.13 {
891dae2495bSdanielk1977    execsql {
892b92b70bbSdanielk1977      PRAGMA main.user_version;
893dae2495bSdanielk1977    }
894dae2495bSdanielk1977  } {2}
8955a8f9374Sdanielk1977}
896dae2495bSdanielk1977
897b92b70bbSdanielk1977# Try a negative value for the user-version
898dae2495bSdanielk1977do_test pragma-8.2.14 {
899dae2495bSdanielk1977  execsql {
900b92b70bbSdanielk1977    PRAGMA user_version = -450;
901dae2495bSdanielk1977  }
902dae2495bSdanielk1977} {}
903dae2495bSdanielk1977do_test pragma-8.2.15 {
904dae2495bSdanielk1977  execsql {
905b92b70bbSdanielk1977    PRAGMA user_version;
906dae2495bSdanielk1977  }
907dae2495bSdanielk1977} {-450}
908d9c847dcSdanielk1977} ; # ifcapable schema_version
909d9c847dcSdanielk1977
910d19744f3Sdrh# Check to see if TEMP_STORE is memory or disk.  Return strings
911d19744f3Sdrh# "memory" or "disk" as appropriate.
912d19744f3Sdrh#
913d19744f3Sdrhproc check_temp_store {} {
914d19744f3Sdrh  db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
915d19744f3Sdrh  db eval {PRAGMA database_list} {
916d19744f3Sdrh    if {$name=="temp"} {
91717b90b53Sdanielk1977      set bt [btree_from_db db 1]
91817b90b53Sdanielk1977      if {[btree_ismemdb $bt]} {
919d19744f3Sdrh        return "memory"
920d19744f3Sdrh      }
92117b90b53Sdanielk1977      return "disk"
922d19744f3Sdrh    }
923d19744f3Sdrh  }
924d19744f3Sdrh  return "unknown"
925d19744f3Sdrh}
926d19744f3Sdrh
9279a09a3caStpoindex
9289a09a3caStpoindex# Test temp_store and temp_store_directory pragmas
9299a09a3caStpoindex#
930268283bcSdrhifcapable pager_pragmas {
9319a09a3caStpoindexdo_test pragma-9.1 {
9329a09a3caStpoindex  db close
9339a09a3caStpoindex  sqlite3 db test.db
9349a09a3caStpoindex  execsql {
9359a09a3caStpoindex    PRAGMA temp_store;
9369a09a3caStpoindex  }
9379a09a3caStpoindex} {0}
938d19744f3Sdrhif {$TEMP_STORE<=1} {
939d19744f3Sdrh  do_test pragma-9.1.1 {
940d19744f3Sdrh    check_temp_store
941d19744f3Sdrh  } {disk}
942d19744f3Sdrh} else {
943d19744f3Sdrh  do_test pragma-9.1.1 {
944d19744f3Sdrh    check_temp_store
945d19744f3Sdrh  } {memory}
946d19744f3Sdrh}
947d19744f3Sdrh
9489a09a3caStpoindexdo_test pragma-9.2 {
949d19744f3Sdrh  db close
950d19744f3Sdrh  sqlite3 db test.db
9519a09a3caStpoindex  execsql {
9529a09a3caStpoindex    PRAGMA temp_store=file;
9539a09a3caStpoindex    PRAGMA temp_store;
9549a09a3caStpoindex  }
9559a09a3caStpoindex} {1}
956d19744f3Sdrhif {$TEMP_STORE==3} {
957d19744f3Sdrh  # When TEMP_STORE is 3, always use memory regardless of pragma settings.
958d19744f3Sdrh  do_test pragma-9.2.1 {
959d19744f3Sdrh    check_temp_store
960d19744f3Sdrh  } {memory}
961d19744f3Sdrh} else {
962d19744f3Sdrh  do_test pragma-9.2.1 {
963d19744f3Sdrh    check_temp_store
964d19744f3Sdrh  } {disk}
965d19744f3Sdrh}
966d19744f3Sdrh
9679a09a3caStpoindexdo_test pragma-9.3 {
968d19744f3Sdrh  db close
969d19744f3Sdrh  sqlite3 db test.db
9709a09a3caStpoindex  execsql {
9719a09a3caStpoindex    PRAGMA temp_store=memory;
9729a09a3caStpoindex    PRAGMA temp_store;
9739a09a3caStpoindex  }
9749a09a3caStpoindex} {2}
975d19744f3Sdrhif {$TEMP_STORE==0} {
976d19744f3Sdrh  # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
977d19744f3Sdrh  do_test pragma-9.3.1 {
978d19744f3Sdrh    check_temp_store
979d19744f3Sdrh  } {disk}
980d19744f3Sdrh} else {
981d19744f3Sdrh  do_test pragma-9.3.1 {
982d19744f3Sdrh    check_temp_store
983d19744f3Sdrh  } {memory}
984d19744f3Sdrh}
985d19744f3Sdrh
9869a09a3caStpoindexdo_test pragma-9.4 {
9879a09a3caStpoindex  execsql {
9889a09a3caStpoindex    PRAGMA temp_store_directory;
9899a09a3caStpoindex  }
9909a09a3caStpoindex} {}
99178f82d1eSdrhifcapable wsd {
9929a09a3caStpoindex  do_test pragma-9.5 {
993*f8a78464Smistachkin    set pwd [string map {' ''} [file nativename [get_pwd]]]
994268283bcSdrh    execsql "
995268283bcSdrh      PRAGMA temp_store_directory='$pwd';
9969a09a3caStpoindex    "
9979a09a3caStpoindex  } {}
9989a09a3caStpoindex  do_test pragma-9.6 {
9999a09a3caStpoindex    execsql {
10009a09a3caStpoindex      PRAGMA temp_store_directory;
10019a09a3caStpoindex    }
1002*f8a78464Smistachkin  } [list [file nativename [get_pwd]]]
10039a09a3caStpoindex  do_test pragma-9.7 {
1004268283bcSdrh    catchsql {
10059a09a3caStpoindex      PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
10069a09a3caStpoindex    }
1007268283bcSdrh  } {1 {not a writable directory}}
10089a09a3caStpoindex  do_test pragma-9.8 {
10099a09a3caStpoindex    execsql {
10109a09a3caStpoindex      PRAGMA temp_store_directory='';
10119a09a3caStpoindex    }
10129a09a3caStpoindex  } {}
10131e9daa6aSdrh  if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
101453c0f748Sdanielk1977    ifcapable tempdb {
10159a09a3caStpoindex      do_test pragma-9.9 {
10169a09a3caStpoindex        execsql {
10179a09a3caStpoindex          PRAGMA temp_store_directory;
10189a09a3caStpoindex          PRAGMA temp_store=FILE;
10199a09a3caStpoindex          CREATE TEMP TABLE temp_store_directory_test(a integer);
10209a09a3caStpoindex          INSERT INTO temp_store_directory_test values (2);
10219a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
10229a09a3caStpoindex        }
10239a09a3caStpoindex      } {2}
10249a09a3caStpoindex      do_test pragma-9.10 {
1025268283bcSdrh        catchsql "
1026268283bcSdrh          PRAGMA temp_store_directory='$pwd';
10279a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
10289a09a3caStpoindex        "
1029268283bcSdrh      } {1 {no such table: temp_store_directory_test}}
10301e9daa6aSdrh    }
10311e9daa6aSdrh  }
103278f82d1eSdrh}
103395b289b6Sdanielk1977do_test pragma-9.11 {
103495b289b6Sdanielk1977  execsql {
103595b289b6Sdanielk1977    PRAGMA temp_store = 0;
103695b289b6Sdanielk1977    PRAGMA temp_store;
103795b289b6Sdanielk1977  }
103895b289b6Sdanielk1977} {0}
103995b289b6Sdanielk1977do_test pragma-9.12 {
104095b289b6Sdanielk1977  execsql {
104195b289b6Sdanielk1977    PRAGMA temp_store = 1;
104295b289b6Sdanielk1977    PRAGMA temp_store;
104395b289b6Sdanielk1977  }
104495b289b6Sdanielk1977} {1}
104595b289b6Sdanielk1977do_test pragma-9.13 {
104695b289b6Sdanielk1977  execsql {
104795b289b6Sdanielk1977    PRAGMA temp_store = 2;
104895b289b6Sdanielk1977    PRAGMA temp_store;
104995b289b6Sdanielk1977  }
105095b289b6Sdanielk1977} {2}
105195b289b6Sdanielk1977do_test pragma-9.14 {
105295b289b6Sdanielk1977  execsql {
105395b289b6Sdanielk1977    PRAGMA temp_store = 3;
105495b289b6Sdanielk1977    PRAGMA temp_store;
105595b289b6Sdanielk1977  }
105695b289b6Sdanielk1977} {0}
105795b289b6Sdanielk1977do_test pragma-9.15 {
105895b289b6Sdanielk1977  catchsql {
105995b289b6Sdanielk1977    BEGIN EXCLUSIVE;
106095b289b6Sdanielk1977    CREATE TEMP TABLE temp_table(t);
106195b289b6Sdanielk1977    INSERT INTO temp_table VALUES('valuable data');
106295b289b6Sdanielk1977    PRAGMA temp_store = 1;
106395b289b6Sdanielk1977  }
106495b289b6Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
106595b289b6Sdanielk1977do_test pragma-9.16 {
106695b289b6Sdanielk1977  execsql {
106795b289b6Sdanielk1977    SELECT * FROM temp_table;
106895b289b6Sdanielk1977    COMMIT;
106995b289b6Sdanielk1977  }
107095b289b6Sdanielk1977} {{valuable data}}
1071983e2309Sdanielk1977
1072983e2309Sdanielk1977do_test pragma-9.17 {
1073983e2309Sdanielk1977  execsql {
1074983e2309Sdanielk1977    INSERT INTO temp_table VALUES('valuable data II');
1075983e2309Sdanielk1977    SELECT * FROM temp_table;
1076983e2309Sdanielk1977  }
1077983e2309Sdanielk1977} {{valuable data} {valuable data II}}
1078983e2309Sdanielk1977
1079983e2309Sdanielk1977do_test pragma-9.18 {
1080983e2309Sdanielk1977  set rc [catch {
1081983e2309Sdanielk1977    db eval {SELECT t FROM temp_table} {
1082983e2309Sdanielk1977      execsql {pragma temp_store = 1}
1083983e2309Sdanielk1977    }
1084983e2309Sdanielk1977  } msg]
1085983e2309Sdanielk1977  list $rc $msg
1086983e2309Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
1087983e2309Sdanielk1977
1088268283bcSdrh} ;# ifcapable pager_pragmas
10899a09a3caStpoindex
1090cc6bd383Sdanielk1977ifcapable trigger {
1091cc6bd383Sdanielk1977
1092cc6bd383Sdanielk1977do_test pragma-10.0 {
1093cc6bd383Sdanielk1977  catchsql {
1094cc6bd383Sdanielk1977    DROP TABLE main.t1;
1095cc6bd383Sdanielk1977  }
1096cc6bd383Sdanielk1977  execsql {
1097cc6bd383Sdanielk1977    PRAGMA count_changes = 1;
1098cc6bd383Sdanielk1977
1099cc6bd383Sdanielk1977    CREATE TABLE t1(a PRIMARY KEY);
1100cc6bd383Sdanielk1977    CREATE TABLE t1_mirror(a);
1101cc6bd383Sdanielk1977    CREATE TABLE t1_mirror2(a);
1102cc6bd383Sdanielk1977    CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1103cc6bd383Sdanielk1977      INSERT INTO t1_mirror VALUES(new.a);
1104cc6bd383Sdanielk1977    END;
1105cc6bd383Sdanielk1977    CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1106cc6bd383Sdanielk1977      INSERT INTO t1_mirror2 VALUES(new.a);
1107cc6bd383Sdanielk1977    END;
1108cc6bd383Sdanielk1977    CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1109cc6bd383Sdanielk1977      UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1110cc6bd383Sdanielk1977    END;
1111cc6bd383Sdanielk1977    CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1112cc6bd383Sdanielk1977      UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1113cc6bd383Sdanielk1977    END;
1114cc6bd383Sdanielk1977    CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1115cc6bd383Sdanielk1977      DELETE FROM t1_mirror WHERE a = old.a;
1116cc6bd383Sdanielk1977    END;
1117cc6bd383Sdanielk1977    CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1118cc6bd383Sdanielk1977      DELETE FROM t1_mirror2 WHERE a = old.a;
1119cc6bd383Sdanielk1977    END;
1120cc6bd383Sdanielk1977  }
1121cc6bd383Sdanielk1977} {}
1122cc6bd383Sdanielk1977
1123cc6bd383Sdanielk1977do_test pragma-10.1 {
1124cc6bd383Sdanielk1977  execsql {
1125cc6bd383Sdanielk1977    INSERT INTO t1 VALUES(randstr(10,10));
1126cc6bd383Sdanielk1977  }
1127cc6bd383Sdanielk1977} {1}
1128cc6bd383Sdanielk1977do_test pragma-10.2 {
1129cc6bd383Sdanielk1977  execsql {
1130cc6bd383Sdanielk1977    UPDATE t1 SET a = randstr(10,10);
1131cc6bd383Sdanielk1977  }
1132cc6bd383Sdanielk1977} {1}
1133cc6bd383Sdanielk1977do_test pragma-10.3 {
1134cc6bd383Sdanielk1977  execsql {
1135cc6bd383Sdanielk1977    DELETE FROM t1;
1136cc6bd383Sdanielk1977  }
1137cc6bd383Sdanielk1977} {1}
1138cc6bd383Sdanielk1977
1139cc6bd383Sdanielk1977} ;# ifcapable trigger
1140cc6bd383Sdanielk1977
114148af65aeSdanielk1977ifcapable schema_pragmas {
114248af65aeSdanielk1977  do_test pragma-11.1 {
114348af65aeSdanielk1977    execsql2 {
114448af65aeSdanielk1977      pragma collation_list;
114548af65aeSdanielk1977    }
11469b5adfa2Sdrh  } {seq 0 name NOCASE seq 1 name RTRIM seq 2 name BINARY}
114748af65aeSdanielk1977  do_test pragma-11.2 {
114848af65aeSdanielk1977    db collate New_Collation blah...
114948af65aeSdanielk1977    execsql {
115048af65aeSdanielk1977      pragma collation_list;
115148af65aeSdanielk1977    }
11529b5adfa2Sdrh  } {0 New_Collation 1 NOCASE 2 RTRIM 3 BINARY}
115348af65aeSdanielk1977}
115448af65aeSdanielk1977
1155ddfb2f03Sdanielk1977ifcapable schema_pragmas&&tempdb {
1156ddfb2f03Sdanielk1977  do_test pragma-12.1 {
1157ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1158ddfb2f03Sdanielk1977    execsql {
1159ddfb2f03Sdanielk1977      PRAGMA temp.table_info('abc');
1160ddfb2f03Sdanielk1977    } db2
1161ddfb2f03Sdanielk1977  } {}
1162ddfb2f03Sdanielk1977  db2 close
1163ddfb2f03Sdanielk1977
1164ddfb2f03Sdanielk1977  do_test pragma-12.2 {
1165ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1166ddfb2f03Sdanielk1977    execsql {
1167ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size = 200;
1168ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size;
1169ddfb2f03Sdanielk1977    } db2
1170ddfb2f03Sdanielk1977  } {200}
1171ddfb2f03Sdanielk1977  db2 close
1172ddfb2f03Sdanielk1977
1173ddfb2f03Sdanielk1977  do_test pragma-12.3 {
1174ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1175ddfb2f03Sdanielk1977    execsql {
1176ddfb2f03Sdanielk1977      PRAGMA temp.cache_size = 400;
1177ddfb2f03Sdanielk1977      PRAGMA temp.cache_size;
1178ddfb2f03Sdanielk1977    } db2
1179ddfb2f03Sdanielk1977  } {400}
1180ddfb2f03Sdanielk1977  db2 close
1181ddfb2f03Sdanielk1977}
1182ddfb2f03Sdanielk1977
11834b2688abSdanielk1977ifcapable bloblit {
11844b2688abSdanielk1977
118505a82983Sdrhdo_test pragma-13.1 {
118605a82983Sdrh  execsql {
118705a82983Sdrh    DROP TABLE IF EXISTS t4;
118805a82983Sdrh    PRAGMA vdbe_trace=on;
118905a82983Sdrh    PRAGMA vdbe_listing=on;
119005a82983Sdrh    PRAGMA sql_trace=on;
119105a82983Sdrh    CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
119205a82983Sdrh    INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
119305a82983Sdrh    INSERT INTO t4(b) VALUES(randstr(30,30));
119405a82983Sdrh    INSERT INTO t4(b) VALUES(1.23456);
119505a82983Sdrh    INSERT INTO t4(b) VALUES(NULL);
119605a82983Sdrh    INSERT INTO t4(b) VALUES(0);
119705a82983Sdrh    INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
119805a82983Sdrh    SELECT * FROM t4;
119905a82983Sdrh  }
120005a82983Sdrh  execsql {
120105a82983Sdrh    PRAGMA vdbe_trace=off;
120205a82983Sdrh    PRAGMA vdbe_listing=off;
120305a82983Sdrh    PRAGMA sql_trace=off;
120405a82983Sdrh  }
120505a82983Sdrh} {}
120605a82983Sdrh
12074b2688abSdanielk1977} ;# ifcapable bloblit
12084b2688abSdanielk1977
120959a93791Sdanielk1977ifcapable pager_pragmas {
121059a93791Sdanielk1977  db close
1211fda06befSmistachkin  forcedelete test.db
121259a93791Sdanielk1977  sqlite3 db test.db
121359a93791Sdanielk1977
121459a93791Sdanielk1977  do_test pragma-14.1 {
121559a93791Sdanielk1977    execsql { pragma auto_vacuum = 0 }
121659a93791Sdanielk1977    execsql { pragma page_count }
121759a93791Sdanielk1977  } {0}
121859a93791Sdanielk1977
121959a93791Sdanielk1977  do_test pragma-14.2 {
122059a93791Sdanielk1977    execsql {
122159a93791Sdanielk1977      CREATE TABLE abc(a, b, c);
122259a93791Sdanielk1977      PRAGMA page_count;
122359a93791Sdanielk1977    }
122459a93791Sdanielk1977  } {2}
12255d16a9a6Sdrh  do_test pragma-14.2uc {
12265d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
12275d16a9a6Sdrh  } {2}
122859a93791Sdanielk1977
122959a93791Sdanielk1977  do_test pragma-14.3 {
123059a93791Sdanielk1977    execsql {
123159a93791Sdanielk1977      BEGIN;
123259a93791Sdanielk1977      CREATE TABLE def(a, b, c);
123359a93791Sdanielk1977      PRAGMA page_count;
123459a93791Sdanielk1977    }
123559a93791Sdanielk1977  } {3}
12365d16a9a6Sdrh  do_test pragma-14.3uc {
12375d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
12385d16a9a6Sdrh  } {3}
123959a93791Sdanielk1977
124059a93791Sdanielk1977  do_test pragma-14.4 {
124159a93791Sdanielk1977    set page_size [db one {pragma page_size}]
124259a93791Sdanielk1977    expr [file size test.db] / $page_size
124359a93791Sdanielk1977  } {2}
124459a93791Sdanielk1977
124559a93791Sdanielk1977  do_test pragma-14.5 {
124659a93791Sdanielk1977    execsql {
124759a93791Sdanielk1977      ROLLBACK;
124859a93791Sdanielk1977      PRAGMA page_count;
124959a93791Sdanielk1977    }
125059a93791Sdanielk1977  } {2}
125159a93791Sdanielk1977
125259a93791Sdanielk1977  do_test pragma-14.6 {
1253fda06befSmistachkin    forcedelete test2.db
125459a93791Sdanielk1977    sqlite3 db2 test2.db
125559a93791Sdanielk1977    execsql {
125659a93791Sdanielk1977      PRAGMA auto_vacuum = 0;
125759a93791Sdanielk1977      CREATE TABLE t1(a, b, c);
125859a93791Sdanielk1977      CREATE TABLE t2(a, b, c);
125959a93791Sdanielk1977      CREATE TABLE t3(a, b, c);
126059a93791Sdanielk1977      CREATE TABLE t4(a, b, c);
126159a93791Sdanielk1977    } db2
126259a93791Sdanielk1977    db2 close
126359a93791Sdanielk1977    execsql {
126459a93791Sdanielk1977      ATTACH 'test2.db' AS aux;
126559a93791Sdanielk1977      PRAGMA aux.page_count;
126659a93791Sdanielk1977    }
126759a93791Sdanielk1977  } {5}
12685d16a9a6Sdrh  do_test pragma-14.6uc {
12695d16a9a6Sdrh    execsql {pragma AUX.PAGE_COUNT}
12705d16a9a6Sdrh  } {5}
127159a93791Sdanielk1977}
127259a93791Sdanielk1977
12738cf6c554Sdanielk1977# Test that the value set using the cache_size pragma is not reset when the
12748cf6c554Sdanielk1977# schema is reloaded.
12758cf6c554Sdanielk1977#
12768cf6c554Sdanielk1977ifcapable pager_pragmas {
12778cf6c554Sdanielk1977  db close
12788cf6c554Sdanielk1977  sqlite3 db test.db
12798cf6c554Sdanielk1977  do_test pragma-15.1 {
12808cf6c554Sdanielk1977    execsql {
12818cf6c554Sdanielk1977      PRAGMA cache_size=59;
12828cf6c554Sdanielk1977      PRAGMA cache_size;
12838cf6c554Sdanielk1977    }
12848cf6c554Sdanielk1977  } {59}
12858cf6c554Sdanielk1977  do_test pragma-15.2 {
12868cf6c554Sdanielk1977    sqlite3 db2 test.db
12878cf6c554Sdanielk1977    execsql {
12888cf6c554Sdanielk1977      CREATE TABLE newtable(a, b, c);
12898cf6c554Sdanielk1977    } db2
12908cf6c554Sdanielk1977    db2 close
12918cf6c554Sdanielk1977  } {}
12928cf6c554Sdanielk1977  do_test pragma-15.3 {
12938cf6c554Sdanielk1977    # Evaluating this statement will cause the schema to be reloaded (because
12948cf6c554Sdanielk1977    # the schema was changed by another connection in pragma-15.2). At one
12958cf6c554Sdanielk1977    # point there was a bug that reset the cache_size to its default value
12968cf6c554Sdanielk1977    # when this happened.
12978cf6c554Sdanielk1977    execsql { SELECT * FROM sqlite_master }
12988cf6c554Sdanielk1977    execsql { PRAGMA cache_size }
12998cf6c554Sdanielk1977  } {59}
13008cf6c554Sdanielk1977}
13018cf6c554Sdanielk1977
13025558a8a6Sdanielk1977# Reset the sqlite3_temp_directory variable for the next run of tests:
13035558a8a6Sdanielk1977sqlite3 dbX :memory:
13045558a8a6Sdanielk1977dbX eval {PRAGMA temp_store_directory = ""}
13055558a8a6Sdanielk1977dbX close
13065558a8a6Sdanielk1977
1307838cce43Sdanielk1977ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1308aebf413dSaswift  set sqlite_hostid_num 1
1309aebf413dSaswift
1310aebf413dSaswift  set using_proxy 0
1311aebf413dSaswift  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1312aebf413dSaswift    set using_proxy $value
1313aebf413dSaswift  }
1314aebf413dSaswift
1315aebf413dSaswift  # Test the lock_proxy_file pragmas.
1316aebf413dSaswift  #
1317aebf413dSaswift  db close
1318aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1319aebf413dSaswift
1320aebf413dSaswift  sqlite3 db test.db
1321aebf413dSaswift  do_test pragma-16.1 {
1322aebf413dSaswift    execsql {
1323aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1324aebf413dSaswift      select * from sqlite_master;
1325aebf413dSaswift    }
1326aebf413dSaswift    execsql {
1327aebf413dSaswift      PRAGMA lock_proxy_file;
1328aebf413dSaswift    }
1329aebf413dSaswift  } {mylittleproxy}
1330aebf413dSaswift
1331aebf413dSaswift  do_test pragma-16.2 {
1332aebf413dSaswift    sqlite3 db2 test.db
1333aebf413dSaswift    execsql {
1334aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1335aebf413dSaswift    } db2
1336aebf413dSaswift  } {}
1337aebf413dSaswift
1338aebf413dSaswift  db2 close
1339aebf413dSaswift  do_test pragma-16.2.1 {
1340aebf413dSaswift    sqlite3 db2 test.db
1341aebf413dSaswift    execsql {
1342aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1343aebf413dSaswift      select * from sqlite_master;
1344aebf413dSaswift    } db2
1345aebf413dSaswift    execsql {
1346aebf413dSaswift      PRAGMA lock_proxy_file;
1347aebf413dSaswift    } db2
1348aebf413dSaswift  } {mylittleproxy}
1349aebf413dSaswift
1350aebf413dSaswift  db2 close
1351aebf413dSaswift  do_test pragma-16.3 {
1352aebf413dSaswift    sqlite3 db2 test.db
1353aebf413dSaswift    execsql {
1354aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1355aebf413dSaswift    } db2
1356aebf413dSaswift    catchsql {
1357aebf413dSaswift      select * from sqlite_master;
1358aebf413dSaswift    } db2
1359aebf413dSaswift  } {1 {database is locked}}
1360aebf413dSaswift
1361aebf413dSaswift  do_test pragma-16.4 {
1362aebf413dSaswift    db2 close
1363aebf413dSaswift    db close
1364aebf413dSaswift    sqlite3 db2 test.db
1365aebf413dSaswift    execsql {
1366aebf413dSaswift      PRAGMA lock_proxy_file="myoriginalproxy";
1367aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1368aebf413dSaswift      PRAGMA lock_proxy_file;
1369aebf413dSaswift    } db2
1370aebf413dSaswift  } {myotherproxy}
1371aebf413dSaswift
1372aebf413dSaswift  db2 close
1373aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1374aebf413dSaswift  do_test pragma-16.5 {
1375aebf413dSaswift    sqlite3 db2 test.db
1376aebf413dSaswift    execsql {
1377aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1378aebf413dSaswift      PRAGMA lock_proxy_file;
1379aebf413dSaswift    } db2
1380aebf413dSaswift  } {myotherproxy}
1381aebf413dSaswift
1382aebf413dSaswift  do_test pragma-16.6 {
1383aebf413dSaswift    db2 close
1384aebf413dSaswift    sqlite3 db2 test2.db
1385aebf413dSaswift    set lockpath [execsql {
1386aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1387aebf413dSaswift      PRAGMA lock_proxy_file;
1388aebf413dSaswift    } db2]
1389aebf413dSaswift    string match "*test2.db:auto:" $lockpath
1390aebf413dSaswift  } {1}
1391aebf413dSaswift
1392aebf413dSaswift  set sqlite_hostid_num 2
1393aebf413dSaswift  do_test pragma-16.7 {
139414d14607Sdan    list [catch {
1395aebf413dSaswift      sqlite3 db test2.db
1396aebf413dSaswift      execsql {
1397aebf413dSaswift        PRAGMA lock_proxy_file=":auto:";
1398aebf413dSaswift        select * from sqlite_master;
1399aebf413dSaswift      }
140014d14607Sdan    } msg] $msg
1401aebf413dSaswift  } {1 {database is locked}}
1402aebf413dSaswift  db close
1403aebf413dSaswift
1404aebf413dSaswift  do_test pragma-16.8 {
140514d14607Sdan    list [catch {
1406aebf413dSaswift      sqlite3 db test2.db
140714d14607Sdan      execsql { select * from sqlite_master }
140814d14607Sdan    } msg] $msg
1409aebf413dSaswift  } {1 {database is locked}}
1410aebf413dSaswift
1411aebf413dSaswift  db2 close
1412aebf413dSaswift  do_test pragma-16.8.1 {
1413aebf413dSaswift    execsql {
1414aebf413dSaswift      PRAGMA lock_proxy_file="yetanotherproxy";
1415aebf413dSaswift      PRAGMA lock_proxy_file;
1416aebf413dSaswift    }
1417aebf413dSaswift  } {yetanotherproxy}
1418aebf413dSaswift  do_test pragma-16.8.2 {
1419aebf413dSaswift    execsql {
1420aebf413dSaswift      create table mine(x);
1421aebf413dSaswift    }
1422aebf413dSaswift  } {}
1423aebf413dSaswift
1424aebf413dSaswift  db close
1425aebf413dSaswift  do_test pragma-16.9 {
1426aebf413dSaswift    sqlite3 db proxytest.db
1427aebf413dSaswift    set lockpath2 [execsql {
1428aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1429aebf413dSaswift      PRAGMA lock_proxy_file;
1430aebf413dSaswift    } db]
1431aebf413dSaswift    string match "*proxytest.db:auto:" $lockpath2
1432aebf413dSaswift  } {1}
1433aebf413dSaswift
1434aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1435aebf413dSaswift  set sqlite_hostid_num 0
1436aebf413dSaswift}
1437d2cb50b7Sdrh
1438d2cb50b7Sdrh# Parsing of auto_vacuum settings.
1439d2cb50b7Sdrh#
1440d2cb50b7Sdrhforeach {autovac_setting val} {
1441d2cb50b7Sdrh  0 0
1442d2cb50b7Sdrh  1 1
1443d2cb50b7Sdrh  2 2
1444d2cb50b7Sdrh  3 0
1445d2cb50b7Sdrh  -1 0
1446d2cb50b7Sdrh  none 0
1447d2cb50b7Sdrh  NONE 0
1448d2cb50b7Sdrh  NoNe 0
1449d2cb50b7Sdrh  full 1
1450d2cb50b7Sdrh  FULL 1
1451d2cb50b7Sdrh  incremental 2
1452d2cb50b7Sdrh  INCREMENTAL 2
1453d2cb50b7Sdrh  -1234 0
1454d2cb50b7Sdrh  1234 0
1455d2cb50b7Sdrh} {
1456d2cb50b7Sdrh  do_test pragma-17.1.$autovac_setting {
1457d2cb50b7Sdrh    catch {db close}
1458d2cb50b7Sdrh    sqlite3 db :memory:
1459d2cb50b7Sdrh    execsql "
1460d2cb50b7Sdrh      PRAGMA auto_vacuum=$::autovac_setting;
1461d2cb50b7Sdrh      PRAGMA auto_vacuum;
1462d2cb50b7Sdrh    "
1463d2cb50b7Sdrh  } $val
1464d2cb50b7Sdrh}
1465d2cb50b7Sdrh
1466d2cb50b7Sdrh# Parsing of temp_store settings.
1467d2cb50b7Sdrh#
1468d2cb50b7Sdrhforeach {temp_setting val} {
1469d2cb50b7Sdrh  0 0
1470d2cb50b7Sdrh  1 1
1471d2cb50b7Sdrh  2 2
1472d2cb50b7Sdrh  3 0
1473d2cb50b7Sdrh  -1 0
1474d2cb50b7Sdrh  file 1
1475d2cb50b7Sdrh  FILE 1
1476d2cb50b7Sdrh  fIlE 1
1477d2cb50b7Sdrh  memory 2
1478d2cb50b7Sdrh  MEMORY 2
1479d2cb50b7Sdrh  MeMoRy 2
1480d2cb50b7Sdrh} {
1481d2cb50b7Sdrh  do_test pragma-18.1.$temp_setting {
1482d2cb50b7Sdrh    catch {db close}
1483d2cb50b7Sdrh    sqlite3 db :memory:
1484d2cb50b7Sdrh    execsql "
1485d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1486d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1487d2cb50b7Sdrh      PRAGMA temp_store;
1488d2cb50b7Sdrh    "
1489d2cb50b7Sdrh  } $val
1490d2cb50b7Sdrh}
1491d2cb50b7Sdrh
149292c700dbSdrh# The SQLITE_FCNTL_PRAGMA logic, with error handling.
149392c700dbSdrh#
149492c700dbSdrhdb close
149592c700dbSdrhtestvfs tvfs
149692c700dbSdrhsqlite3 db test.db -vfs tvfs
149792c700dbSdrhdo_test pragma-19.1 {
149892c700dbSdrh  catchsql {PRAGMA error}
149992c700dbSdrh} {1 {SQL logic error or missing database}}
150092c700dbSdrhdo_test pragma-19.2 {
150192c700dbSdrh  catchsql {PRAGMA error='This is the error message'}
150292c700dbSdrh} {1 {This is the error message}}
150392c700dbSdrhdo_test pragma-19.3 {
150492c700dbSdrh  catchsql {PRAGMA error='7 This is the error message'}
150592c700dbSdrh} {1 {This is the error message}}
150692c700dbSdrhdo_test pragma-19.4 {
150792c700dbSdrh  catchsql {PRAGMA error=7}
150892c700dbSdrh} {1 {out of memory}}
1509c8517f61Sdrhdo_test pragma-19.5 {
15105b044545Smistachkin  file tail [lindex [execsql {PRAGMA filename}] 0]
1511c8517f61Sdrh} {test.db}
151292c700dbSdrh
151392c700dbSdrh
1514cd61c281Sdrhfinish_test
1515