xref: /sqlite-3.40.0/test/tempdb2.test (revision 175b8f06)
1c88ae52dSdan# 2016 March 3
2c88ae52dSdan#
3c88ae52dSdan# The author disclaims copyright to this source code.  In place of
4c88ae52dSdan# a legal notice, here is a blessing:
5c88ae52dSdan#
6c88ae52dSdan#    May you do good and not evil.
7c88ae52dSdan#    May you find forgiveness for yourself and forgive others.
8c88ae52dSdan#    May you share freely, never taking more than you give.
9c88ae52dSdan#
10c88ae52dSdan#***********************************************************************
11c88ae52dSdan
12c88ae52dSdanset testdir [file dirname $argv0]
13c88ae52dSdansource $testdir/tester.tcl
14c88ae52dSdanset testprefix tempdb2
15c88ae52dSdan
16c88ae52dSdandb close
17c88ae52dSdansqlite3 db ""
18c88ae52dSdan
1922f60b84Sdanset unlocked unlocked
2022f60b84Sdanif {$::TEMP_STORE>=2} { set unlocked unknown }
2122f60b84Sdan
22c88ae52dSdanproc int2str {i} { string range [string repeat "$i." 450] 0 899 }
23c88ae52dSdandb func int2str int2str
24c88ae52dSdan
25c88ae52dSdan#-------------------------------------------------------------------------
26c88ae52dSdan#
27c88ae52dSdan#  1.1: Write a big transaction to the db. One so large that it forces
28c88ae52dSdan#       the file to be created and the cache flushed to disk on COMMIT.
29c88ae52dSdan#
30c88ae52dSdan#  1.2: Write a small transaction - one small enough that it remains in
31c88ae52dSdan#       memory on COMMIT. All the pages of table [t1] are now dirty.
32c88ae52dSdan#
33c88ae52dSdan#  1.3: Delete the contents of [t1]. This moves all of its leaves to the
34c88ae52dSdan#       free-list and causes the btree layer to call PagerDontWrite() on
35c88ae52dSdan#       each of them.
36c88ae52dSdan#
37c88ae52dSdan#       Then do a big update on table [t2]. So big that the former leaves
38c88ae52dSdan#       of [t1] are forced out of the cache. Then roll back the transaction.
39c88ae52dSdan#       If the PagerDontWrite() calls are honoured and the data is not written
40c88ae52dSdan#       to disk, the update made in test 1.2 will be lost at this point. Or, if
41c88ae52dSdan#       they are ignored (as they should be for temp databases), the update
42c88ae52dSdan#       will be safely written out to disk before the cache entries are
43c88ae52dSdan#       discarded.
44c88ae52dSdan#
45c88ae52dSdando_execsql_test 1.1 {
46c88ae52dSdan  PRAGMA page_size=1024;
47c88ae52dSdan  PRAGMA cache_size=50;
48c88ae52dSdan
49c88ae52dSdan  BEGIN;
50c88ae52dSdan    CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
51c88ae52dSdan    INSERT INTO t1 VALUES(1, int2str(1));
52c88ae52dSdan    INSERT INTO t1 VALUES(2, int2str(1));
53c88ae52dSdan    INSERT INTO t1 VALUES(3, int2str(1));
54c88ae52dSdan
55c88ae52dSdan    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
56c88ae52dSdan    WITH c(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100 )
57c88ae52dSdan    INSERT INTO t2 SELECT x, int2str(x) FROM c;
58c88ae52dSdan  COMMIT;
59c88ae52dSdan
60c88ae52dSdan  PRAGMA lock_status;
6122f60b84Sdan} [list main $unlocked temp closed]
62c88ae52dSdan
63c88ae52dSdando_execsql_test 1.2 {
64c88ae52dSdan  UPDATE t1 SET b=int2str(2);
65c88ae52dSdan  SELECT b=int2str(2) FROM t1
66c88ae52dSdan} {1 1 1}
67c88ae52dSdan
68c88ae52dSdando_execsql_test 1.3 {
69c88ae52dSdan  BEGIN;
70c88ae52dSdan    DELETE FROM t1;
71c88ae52dSdan    UPDATE t2 SET b=int2str(a+1);
72c88ae52dSdan  ROLLBACK;
73c88ae52dSdan}
74c88ae52dSdan
75c88ae52dSdando_execsql_test 1.4 {
76c88ae52dSdan  SELECT b=int2str(2) FROM t1
77c88ae52dSdan} {1 1 1}
78c88ae52dSdan
79*09236755Sdan#-------------------------------------------------------------------------
80*09236755Sdandb close
81*09236755Sdansqlite3 db ""
82*09236755Sdandb func int2str int2str
83*09236755Sdan
84*09236755Sdando_execsql_test 2.0 {
85*09236755Sdan  PRAGMA cache_size = -100;
86*09236755Sdan  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
87*09236755Sdan  WITH c(x) AS ( VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100 )
88*09236755Sdan    INSERT INTO t1 SELECT x, int2str(x) FROM c;
89*09236755Sdan}
90*09236755Sdan
91*09236755Sdando_execsql_test 2.1 {
92*09236755Sdan  INSERT INTO t1 VALUES(10001, int2str(1001) || int2str(1001) || int2str(1001));
93*09236755Sdan}
94*09236755Sdan
95*09236755Sdando_execsql_test 2.2 {
96*09236755Sdan  SELECT b FROM t1 WHERE a = 10001;
97*09236755Sdan} "[int2str 1001][int2str 1001][int2str 1001]"
98*09236755Sdan
99c88ae52dSdanfinish_test
100