Kitxuuu commited on
Commit
a49e237
·
verified ·
1 Parent(s): 661c4fc

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. local-test-sqlite3-delta-03/afc-sqlite3/test/8_3_names.test +197 -0
  2. local-test-sqlite3-delta-03/afc-sqlite3/test/affinity2.test +136 -0
  3. local-test-sqlite3-delta-03/afc-sqlite3/test/affinity3.test +123 -0
  4. local-test-sqlite3-delta-03/afc-sqlite3/test/aggerror.test +78 -0
  5. local-test-sqlite3-delta-03/afc-sqlite3/test/aggnested.test +487 -0
  6. local-test-sqlite3-delta-03/afc-sqlite3/test/all.test +51 -0
  7. local-test-sqlite3-delta-03/afc-sqlite3/test/alter2.test +470 -0
  8. local-test-sqlite3-delta-03/afc-sqlite3/test/alter4.test +427 -0
  9. local-test-sqlite3-delta-03/afc-sqlite3/test/alterauth.test +72 -0
  10. local-test-sqlite3-delta-03/afc-sqlite3/test/altercol.test +949 -0
  11. local-test-sqlite3-delta-03/afc-sqlite3/test/altercorrupt.test +181 -0
  12. local-test-sqlite3-delta-03/afc-sqlite3/test/alterdropcol.test +339 -0
  13. local-test-sqlite3-delta-03/afc-sqlite3/test/alterqf.test +120 -0
  14. local-test-sqlite3-delta-03/afc-sqlite3/test/altertab3.test +789 -0
  15. local-test-sqlite3-delta-03/afc-sqlite3/test/amatch1.test +117 -0
  16. local-test-sqlite3-delta-03/afc-sqlite3/test/analyze5.test +247 -0
  17. local-test-sqlite3-delta-03/afc-sqlite3/test/analyze6.test +126 -0
  18. local-test-sqlite3-delta-03/afc-sqlite3/test/analyze8.test +115 -0
  19. local-test-sqlite3-delta-03/afc-sqlite3/test/analyze9.test +1235 -0
  20. local-test-sqlite3-delta-03/afc-sqlite3/test/analyzeC.test +181 -0
  21. local-test-sqlite3-delta-03/afc-sqlite3/test/analyzeD.test +107 -0
  22. local-test-sqlite3-delta-03/afc-sqlite3/test/analyzeE.test +294 -0
  23. local-test-sqlite3-delta-03/afc-sqlite3/test/analyzer1.test +55 -0
  24. local-test-sqlite3-delta-03/afc-sqlite3/test/atomic2.test +95 -0
  25. local-test-sqlite3-delta-03/afc-sqlite3/test/atrc.c +150 -0
  26. local-test-sqlite3-delta-03/afc-sqlite3/test/attach.test +928 -0
  27. local-test-sqlite3-delta-03/afc-sqlite3/test/attach3.test +353 -0
  28. local-test-sqlite3-delta-03/afc-sqlite3/test/attachmalloc.test +78 -0
  29. local-test-sqlite3-delta-03/afc-sqlite3/test/auth.test +2678 -0
  30. local-test-sqlite3-delta-03/afc-sqlite3/test/auth3.test +134 -0
  31. local-test-sqlite3-delta-03/afc-sqlite3/test/autoindex1.test +566 -0
  32. local-test-sqlite3-delta-03/afc-sqlite3/test/autoindex5.test +234 -0
  33. local-test-sqlite3-delta-03/afc-sqlite3/test/autovacuum.test +715 -0
  34. local-test-sqlite3-delta-03/afc-sqlite3/test/autovacuum2.test +87 -0
  35. local-test-sqlite3-delta-03/afc-sqlite3/test/autovacuum_ioerr2.test +132 -0
  36. local-test-sqlite3-delta-03/afc-sqlite3/test/avtrans.test +927 -0
  37. local-test-sqlite3-delta-03/afc-sqlite3/test/backcompat.test +524 -0
  38. local-test-sqlite3-delta-03/afc-sqlite3/test/backup.test +1004 -0
  39. local-test-sqlite3-delta-03/afc-sqlite3/test/badutf2.test +127 -0
  40. local-test-sqlite3-delta-03/afc-sqlite3/test/basexx1.test +155 -0
  41. local-test-sqlite3-delta-03/afc-sqlite3/test/bestindex3.test +185 -0
  42. local-test-sqlite3-delta-03/afc-sqlite3/test/bestindex8.test +463 -0
  43. local-test-sqlite3-delta-03/afc-sqlite3/test/bestindex9.test +104 -0
  44. local-test-sqlite3-delta-03/afc-sqlite3/test/between.test +143 -0
  45. local-test-sqlite3-delta-03/afc-sqlite3/test/bigmmap.test +108 -0
  46. local-test-sqlite3-delta-03/afc-sqlite3/test/bigrow.test +223 -0
  47. local-test-sqlite3-delta-03/afc-sqlite3/test/bind2.test +63 -0
  48. local-test-sqlite3-delta-03/afc-sqlite3/test/bindxfer.test +76 -0
  49. local-test-sqlite3-delta-03/afc-sqlite3/test/boundary3.tcl +289 -0
  50. local-test-sqlite3-delta-03/afc-sqlite3/test/boundary3.test +0 -0
local-test-sqlite3-delta-03/afc-sqlite3/test/8_3_names.test ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2011 May 17
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # Test cases for the SQLITE_ENABLE_8_3_NAMES feature that forces all
13
+ # filename extensions to be limited to 3 characters. Some embedded
14
+ # systems need this to work around microsoft FAT patents, but this
15
+ # feature should be disabled on most deployments.
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+ ifcapable !8_3_names {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ db close
26
+ sqlite3_shutdown
27
+ sqlite3_config_uri 1
28
+
29
+ do_test 8_3_names-1.0 {
30
+ forcedelete test.db test.nal test.db-journal
31
+ sqlite3 db test.db
32
+ db eval {
33
+ PRAGMA cache_size=10;
34
+ CREATE TABLE t1(x);
35
+ INSERT INTO t1 VALUES(randomblob(20000));
36
+ BEGIN;
37
+ DELETE FROM t1;
38
+ INSERT INTO t1 VALUES(randomblob(15000));
39
+ }
40
+ file exists test.db-journal
41
+ } 1
42
+ do_test 8_3_names-1.1 {
43
+ file exists test.nal
44
+ } 0
45
+ do_test 8_3_names-1.2 {
46
+ db eval {
47
+ ROLLBACK;
48
+ SELECT length(x) FROM t1
49
+ }
50
+ } 20000
51
+
52
+ db close
53
+ do_test 8_3_names-2.0 {
54
+ forcedelete test.db test.nal test.db-journal
55
+ sqlite3 db file:./test.db?8_3_names=1
56
+ db eval {
57
+ PRAGMA cache_size=10;
58
+ CREATE TABLE t1(x);
59
+ INSERT INTO t1 VALUES(randomblob(20000));
60
+ BEGIN;
61
+ DELETE FROM t1;
62
+ INSERT INTO t1 VALUES(randomblob(15000));
63
+ }
64
+ file exists test.db-journal
65
+ } 0
66
+ do_test 8_3_names-2.1 {
67
+ file exists test.nal
68
+ } 1
69
+ forcedelete test2.db test2.nal test2.db-journal
70
+ copy_file test.db test2.db
71
+ copy_file test.nal test2.nal
72
+ do_test 8_3_names-2.2 {
73
+ db eval {
74
+ COMMIT;
75
+ SELECT length(x) FROM t1
76
+ }
77
+ } 15000
78
+ do_test 8_3_names-2.3 {
79
+ sqlite3 db2 file:./test2.db?8_3_names=1
80
+ db2 eval {
81
+ PRAGMA integrity_check;
82
+ SELECT length(x) FROM t1;
83
+ }
84
+ } {ok 20000}
85
+
86
+ db close
87
+ do_test 8_3_names-3.0 {
88
+ forcedelete test.db test.nal test.db-journal
89
+ sqlite3 db file:./test.db?8_3_names=0
90
+ db eval {
91
+ PRAGMA cache_size=10;
92
+ CREATE TABLE t1(x);
93
+ INSERT INTO t1 VALUES(randomblob(20000));
94
+ BEGIN;
95
+ DELETE FROM t1;
96
+ INSERT INTO t1 VALUES(randomblob(15000));
97
+ }
98
+ file exists test.db-journal
99
+ } 1
100
+ do_test 8_3_names-3.1 {
101
+ file exists test.nal
102
+ } 0
103
+ forcedelete test2.db test2.nal test2.db-journal
104
+ copy_file test.db test2.db
105
+ copy_file test.db-journal test2.db-journal
106
+ do_test 8_3_names-3.2 {
107
+ db eval {
108
+ COMMIT;
109
+ SELECT length(x) FROM t1
110
+ }
111
+ } 15000
112
+ do_test 8_3_names-3.3 {
113
+ sqlite3 db2 file:./test2.db?8_3_names=0
114
+ db2 eval {
115
+ PRAGMA integrity_check;
116
+ SELECT length(x) FROM t1;
117
+ }
118
+ } {ok 20000}
119
+
120
+ ##########################################################################
121
+ # Master journals.
122
+ #
123
+ db close
124
+ forcedelete test.db test2.db
125
+ do_test 8_3_names-4.0 {
126
+ sqlite3 db file:./test.db?8_3_names=1
127
+ db eval {
128
+ CREATE TABLE t1(x);
129
+ INSERT INTO t1 VALUES(1);
130
+ ATTACH 'file:./test2.db?8_3_names=1' AS db2;
131
+ CREATE TABLE db2.t2(y);
132
+ INSERT INTO t2 VALUES(2);
133
+ BEGIN;
134
+ INSERT INTO t1 VALUES(3);
135
+ INSERT INTO t2 VALUES(4);
136
+ COMMIT;
137
+ SELECT * FROM t1, t2 ORDER BY x, y
138
+ }
139
+ } {1 2 1 4 3 2 3 4}
140
+
141
+
142
+ ##########################################################################
143
+ # WAL mode.
144
+ #
145
+ ifcapable !wal {
146
+ finish_test
147
+ return
148
+ }
149
+ db close
150
+ forcedelete test.db
151
+ do_test 8_3_names-5.0 {
152
+ sqlite3 db file:./test.db?8_3_names=1
153
+ load_static_extension db wholenumber
154
+ db eval {
155
+ PRAGMA journal_mode=WAL;
156
+ CREATE TABLE t1(x);
157
+ CREATE VIRTUAL TABLE nums USING wholenumber;
158
+ INSERT INTO t1 SELECT value FROM nums WHERE value BETWEEN 1 AND 1000;
159
+ BEGIN;
160
+ UPDATE t1 SET x=x*2;
161
+ }
162
+ sqlite3 db2 file:./test.db?8_3_names=1
163
+ load_static_extension db2 wholenumber
164
+ db2 eval {
165
+ BEGIN;
166
+ SELECT sum(x) FROM t1;
167
+ }
168
+ } {500500}
169
+
170
+ do_test 8_3_names-5.1 {
171
+ file exists test.db-wal
172
+ } 0
173
+ do_test 8_3_names-5.2 {
174
+ file exists test.wal
175
+ } 1
176
+ do_test 8_3_names-5.3 {
177
+ file exists test.db-shm
178
+ } 0
179
+ do_test 8_3_names-5.4 {
180
+ file exists test.shm
181
+ } 1
182
+
183
+
184
+ do_test 8_3_names-5.5 {
185
+ db eval {
186
+ COMMIT;
187
+ SELECT sum(x) FROM t1;
188
+ }
189
+ } {1001000}
190
+ do_test 8_3_names-5.6 {
191
+ db2 eval {
192
+ SELECT sum(x) FROM t1;
193
+ }
194
+ } {500500}
195
+
196
+
197
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/affinity2.test ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2015-06-02
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is type affinity in comparison operations.
13
+ #
14
+
15
+ set testdir [file dirname $argv0]
16
+ source $testdir/tester.tcl
17
+ set testprefix affinity2
18
+
19
+ do_execsql_test affinity2-100 {
20
+ CREATE TABLE t1(
21
+ xi INTEGER,
22
+ xr REAL,
23
+ xb BLOB,
24
+ xn NUMERIC,
25
+ xt TEXT
26
+ );
27
+ INSERT INTO t1(rowid,xi,xr,xb,xn,xt) VALUES(1,1,1,1,1,1);
28
+ INSERT INTO t1(rowid,xi,xr,xb,xn,xt) VALUES(2,'2','2','2','2','2');
29
+ INSERT INTO t1(rowid,xi,xr,xb,xn,xt) VALUES(3,'03','03','03','03','03');
30
+
31
+ } {}
32
+ do_execsql_test affinity2-110 {
33
+ SELECT xi, typeof(xi) FROM t1 ORDER BY rowid;
34
+ } {1 integer 2 integer 3 integer}
35
+ do_execsql_test affinity2-120 {
36
+ SELECT xr, typeof(xr) FROM t1 ORDER BY rowid;
37
+ } {1.0 real 2.0 real 3.0 real}
38
+ do_execsql_test affinity2-130 {
39
+ SELECT xb, typeof(xb) FROM t1 ORDER BY rowid;
40
+ } {1 integer 2 text 03 text}
41
+ do_execsql_test affinity2-140 {
42
+ SELECT xn, typeof(xn) FROM t1 ORDER BY rowid;
43
+ } {1 integer 2 integer 3 integer}
44
+ do_execsql_test affinity2-150 {
45
+ SELECT xt, typeof(xt) FROM t1 ORDER BY rowid;
46
+ } {1 text 2 text 03 text}
47
+
48
+ do_execsql_test affinity2-200 {
49
+ SELECT rowid, xi==xt, xi==xb, xi==+xt FROM t1 ORDER BY rowid;
50
+ } {1 1 1 1 2 1 1 1 3 1 1 1}
51
+ do_execsql_test affinity2-210 {
52
+ SELECT rowid, xr==xt, xr==xb, xr==+xt FROM t1 ORDER BY rowid;
53
+ } {1 1 1 1 2 1 1 1 3 1 1 1}
54
+ do_execsql_test affinity2-220 {
55
+ SELECT rowid, xn==xt, xn==xb, xn==+xt FROM t1 ORDER BY rowid;
56
+ } {1 1 1 1 2 1 1 1 3 1 1 1}
57
+
58
+ do_execsql_test affinity2-300 {
59
+ SELECT rowid, xt==+xi, xt==xi, xt==xb FROM t1 ORDER BY rowid;
60
+ } {1 1 1 0 2 1 1 1 3 0 1 1}
61
+
62
+ #-------------------------------------------------------------------------
63
+ do_execsql_test 400 {
64
+ CREATE TABLE ttt(c0, c1);
65
+ CREATE INDEX ii ON ttt(CAST(c0 AS NUMERIC));
66
+ INSERT INTO ttt VALUES('abc', '-1');
67
+ }
68
+ do_execsql_test 410 {
69
+ SELECT * FROM ttt WHERE CAST(c0 AS NUMERIC) > c1 GROUP BY rowid;
70
+ } {abc -1}
71
+ do_execsql_test 420 {
72
+ SELECT * FROM ttt INDEXED BY ii WHERE CAST(c0 AS NUMERIC) > c1 GROUP BY rowid;
73
+ } {abc -1}
74
+
75
+ do_execsql_test 430 {
76
+ CREATE TABLE t3(a, b, c INTEGER);
77
+ CREATE INDEX t3ac ON t3(a, c-1);
78
+ INSERT INTO t3 VALUES(1, 1, 1);
79
+ INSERT INTO t3 VALUES(2, 1, 0);
80
+ INSERT INTO t3 VALUES(3, 1, 1);
81
+ INSERT INTO t3 VALUES(4, 1, 0);
82
+ INSERT INTO t3 VALUES(5, 1, 1);
83
+ }
84
+ do_execsql_test 440 {
85
+ SELECT * FROM t3 WHERE c='0' ORDER BY a;
86
+ } {2 1 0 4 1 0}
87
+
88
+ # 2019-08-22 ticket https://sqlite.org/src/info/d99f1ffe836c591ac57f
89
+ # False positive in sqlite3ExprNeedsNoAffinityChange()
90
+ #
91
+ do_execsql_test 500 {
92
+ DROP TABLE IF EXISTS t0;
93
+ CREATE TABLE t0(c0 TEXT UNIQUE, c1);
94
+ INSERT INTO t0(c0) VALUES (-1);
95
+ SELECT quote(- x'ce'), quote(t0.c0), quote(- x'ce' >= t0.c0) FROM t0;
96
+ } {0 '-1' 1}
97
+ do_execsql_test 501 {
98
+ SELECT * FROM t0 WHERE - x'ce' >= t0.c0;
99
+ } {-1 {}}
100
+ do_execsql_test 502 {
101
+ SELECT quote(+-+x'ce'), quote(t0.c0), quote(+-+x'ce' >= t0.c0) FROM t0;
102
+ } {0 '-1' 1}
103
+ do_execsql_test 503 {
104
+ SELECT * FROM t0 WHERE +-+x'ce' >= t0.c0;
105
+ } {-1 {}}
106
+ do_execsql_test 504 {
107
+ SELECT quote(- 'ce'), quote(t0.c0), quote(- 'ce' >= t0.c0) FROM t0;
108
+ } {0 '-1' 1}
109
+ do_execsql_test 505 {
110
+ SELECT * FROM t0 WHERE - 'ce' >= t0.c0;
111
+ } {-1 {}}
112
+ do_execsql_test 506 {
113
+ SELECT quote(+-+'ce'), quote(t0.c0), quote(+-+'ce' >= t0.c0) FROM t0;
114
+ } {0 '-1' 1}
115
+ do_execsql_test 507 {
116
+ SELECT * FROM t0 WHERE +-+'ce' >= t0.c0;
117
+ } {-1 {}}
118
+
119
+ # 2019-08-30 ticket https://www.sqlite.org/src/info/40812aea1fde9594
120
+ #
121
+ # Due to some differences in floating point computations, these tests do not
122
+ # work under valgrind.
123
+ #
124
+ if {![info exists ::G(valgrind)]} {
125
+ do_execsql_test 600 {
126
+ DROP TABLE IF EXISTS t0;
127
+ CREATE TABLE t0(c0 REAL UNIQUE);
128
+ INSERT INTO t0(c0) VALUES (3175546974276630385);
129
+ SELECT 3175546974276630385 < c0 FROM t0;
130
+ } {1}
131
+ do_execsql_test 601 {
132
+ SELECT 1 FROM t0 WHERE 3175546974276630385 < c0;
133
+ } {1}
134
+ }
135
+
136
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/affinity3.test ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2017-01-16
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # Test cases for bugs:
13
+ #
14
+ # https://www.sqlite.org/src/info/91e2e8ba6ff2e2
15
+ # https://www.sqlite.org/src/info/7ffd1ca1d2ad4ecf
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+ # Ticket https://www.sqlite.org/src/info/91e2e8ba6ff2e2 (2011-09-19)
22
+ # Automatic index causes undesired type conversions
23
+ #
24
+ do_execsql_test affinity3-100 {
25
+ CREATE TABLE customer (id INT PRIMARY KEY);
26
+ CREATE TABLE apr (id INT PRIMARY KEY, apr REAL);
27
+
28
+ CREATE VIEW v1 AS
29
+ SELECT c.id, i.apr
30
+ FROM customer c
31
+ LEFT JOIN apr i ON i.id=c.id;
32
+
33
+ CREATE VIEW v1rj AS
34
+ SELECT c.id, i.apr
35
+ FROM apr i
36
+ RIGHT JOIN customer c ON i.id=c.id;
37
+
38
+ CREATE VIEW v2 AS
39
+ SELECT c.id, v1.apr
40
+ FROM customer c
41
+ LEFT JOIN v1 ON v1.id=c.id;
42
+
43
+ CREATE VIEW v2rj AS
44
+ SELECT c.id, v1.apr
45
+ FROM v1 RIGHT JOIN customer c ON v1.id=c.id;
46
+
47
+ CREATE VIEW v2rjrj AS
48
+ SELECT c.id, v1rj.apr
49
+ FROM v1rj RIGHT JOIN customer c ON v1rj.id=c.id;
50
+
51
+ INSERT INTO customer (id) VALUES (1);
52
+ INSERT INTO apr (id, apr) VALUES (1, 12);
53
+ INSERT INTO customer (id) VALUES (2);
54
+ INSERT INTO apr (id, apr) VALUES (2, 12.01);
55
+ }
56
+ do_execsql_test affinity3-110 {
57
+ PRAGMA automatic_index=ON;
58
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v1;
59
+ } {1 0.12 real 2 0.1201 real}
60
+ do_execsql_test affinity3-111 {
61
+ PRAGMA automatic_index=ON;
62
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v1rj;
63
+ } {1 0.12 real 2 0.1201 real}
64
+ do_execsql_test affinity3-120 {
65
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v2;
66
+ } {1 0.12 real 2 0.1201 real}
67
+ do_execsql_test affinity3-121 {
68
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rj;
69
+ } {1 0.12 real 2 0.1201 real}
70
+ do_execsql_test affinity3-122 {
71
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rjrj;
72
+ } {1 0.12 real 2 0.1201 real}
73
+ do_execsql_test affinity3-130 {
74
+ PRAGMA automatic_index=OFF;
75
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v1;
76
+ } {1 0.12 real 2 0.1201 real}
77
+ do_execsql_test affinity3-131 {
78
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v1rj;
79
+ } {1 0.12 real 2 0.1201 real}
80
+ do_execsql_test affinity3-140 {
81
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v2;
82
+ } {1 0.12 real 2 0.1201 real}
83
+ do_execsql_test affinity3-141 {
84
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rj;
85
+ } {1 0.12 real 2 0.1201 real}
86
+ do_execsql_test affinity3-142 {
87
+ SELECT id, (apr / 100), typeof(apr) apr_type FROM v2rjrj;
88
+ } {1 0.12 real 2 0.1201 real}
89
+
90
+ # Ticket https://www.sqlite.org/src/info/7ffd1ca1d2ad4ecf (2017-01-16)
91
+ # Incorrect affinity when using automatic indexes
92
+ #
93
+ do_execsql_test affinity3-200 {
94
+ CREATE TABLE map_integer (id INT, name);
95
+ INSERT INTO map_integer VALUES(1,'a');
96
+ CREATE TABLE map_text (id TEXT, name);
97
+ INSERT INTO map_text VALUES('4','e');
98
+ CREATE TABLE data (id TEXT, name);
99
+ INSERT INTO data VALUES(1,'abc');
100
+ INSERT INTO data VALUES('4','xyz');
101
+ CREATE VIEW idmap as
102
+ SELECT * FROM map_integer
103
+ UNION SELECT * FROM map_text;
104
+ CREATE TABLE mzed AS SELECT * FROM idmap;
105
+ }
106
+
107
+ do_execsql_test affinity3-210 {
108
+ PRAGMA automatic_index=ON;
109
+ SELECT * FROM data JOIN idmap USING(id);
110
+ } {4 xyz e}
111
+ do_execsql_test affinity3-220 {
112
+ SELECT * FROM data JOIN mzed USING(id);
113
+ } {4 xyz e}
114
+
115
+ do_execsql_test affinity3-250 {
116
+ PRAGMA automatic_index=OFF;
117
+ SELECT * FROM data JOIN idmap USING(id);
118
+ } {4 xyz e}
119
+ do_execsql_test affinity3-260 {
120
+ SELECT * FROM data JOIN mzed USING(id);
121
+ } {4 xyz e}
122
+
123
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/aggerror.test ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2006 January 20
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library.
12
+ #
13
+ # This file implements tests for calling sqlite3_result_error()
14
+ # from within an aggregate function implementation.
15
+ #
16
+ # $Id: aggerror.test,v 1.3 2006/05/03 23:34:06 drh Exp $
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+
22
+ # Add the x_count aggregate function to the database handle.
23
+ # x_count will error out if its input is 40 or 41 or if its
24
+ # final results is 42. Make sure that such errors are handled
25
+ # appropriately.
26
+ #
27
+ do_test aggerror-1.1 {
28
+ set DB [sqlite3_connection_pointer db]
29
+ sqlite3_create_aggregate $DB
30
+ execsql {
31
+ CREATE TABLE t1(a);
32
+ INSERT INTO t1 VALUES(1);
33
+ INSERT INTO t1 VALUES(2);
34
+ INSERT INTO t1 SELECT a+2 FROM t1;
35
+ INSERT INTO t1 SELECT a+4 FROM t1;
36
+ INSERT INTO t1 SELECT a+8 FROM t1;
37
+ INSERT INTO t1 SELECT a+16 FROM t1;
38
+ INSERT INTO t1 SELECT a+32 FROM t1 ORDER BY a LIMIT 7;
39
+ SELECT x_count(*) FROM t1;
40
+ }
41
+ } {39}
42
+ do_test aggerror-1.2 {
43
+ execsql {
44
+ INSERT INTO t1 VALUES(40);
45
+ SELECT x_count(*) FROM t1;
46
+ }
47
+ } {40}
48
+ do_test aggerror-1.3 {
49
+ catchsql {
50
+ SELECT x_count(a) FROM t1;
51
+ }
52
+ } {1 {value of 40 handed to x_count}}
53
+ ifcapable utf16 {
54
+ do_test aggerror-1.4 {
55
+ execsql {
56
+ UPDATE t1 SET a=41 WHERE a=40
57
+ }
58
+ catchsql {
59
+ SELECT x_count(a) FROM t1;
60
+ }
61
+ } {1 abc}
62
+ }
63
+ do_test aggerror-1.5 {
64
+ execsql {
65
+ SELECT x_count(*) FROM t1
66
+ }
67
+ } 40
68
+ do_test aggerror-1.6 {
69
+ execsql {
70
+ INSERT INTO t1 VALUES(40);
71
+ INSERT INTO t1 VALUES(42);
72
+ }
73
+ catchsql {
74
+ SELECT x_count(*) FROM t1;
75
+ }
76
+ } {1 {x_count totals to 42}}
77
+
78
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/aggnested.test ADDED
@@ -0,0 +1,487 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2012-08-23
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library.
12
+ #
13
+ # This file implements tests for processing aggregate queries with
14
+ # subqueries in which the subqueries hold the aggregate functions
15
+ # or in which the subqueries are themselves aggregate queries
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+ set testprefix aggnested
21
+
22
+ do_test aggnested-1.1 {
23
+ db eval {
24
+ CREATE TABLE t1(a1 INTEGER);
25
+ INSERT INTO t1 VALUES(1), (2), (3);
26
+ CREATE TABLE t2(b1 INTEGER);
27
+ INSERT INTO t2 VALUES(4), (5);
28
+ SELECT (SELECT string_agg(a1,'x') FROM t2) FROM t1;
29
+ }
30
+ } {1x2x3}
31
+ do_test aggnested-1.2 {
32
+ db eval {
33
+ SELECT
34
+ (SELECT string_agg(a1,'x') || '-' || string_agg(b1,'y') FROM t2)
35
+ FROM t1;
36
+ }
37
+ } {1x2x3-4y5}
38
+ do_test aggnested-1.3 {
39
+ db eval {
40
+ SELECT (SELECT string_agg(b1,a1) FROM t2) FROM t1;
41
+ }
42
+ } {415 425 435}
43
+ do_test aggnested-1.4 {
44
+ db eval {
45
+ SELECT (SELECT group_concat(a1,b1) FROM t2) FROM t1;
46
+ }
47
+ } {151 252 353}
48
+
49
+
50
+ # This test case is a copy of the one in
51
+ # http://www.mail-archive.com/sqlite-users@sqlite.org/msg70787.html
52
+ #
53
+ do_test aggnested-2.0 {
54
+ sqlite3 db2 :memory:
55
+ db2 eval {
56
+ CREATE TABLE t1 (A1 INTEGER NOT NULL,A2 INTEGER NOT NULL,A3 INTEGER NOT
57
+ NULL,A4 INTEGER NOT NULL,PRIMARY KEY(A1));
58
+ REPLACE INTO t1 VALUES(1,11,111,1111);
59
+ REPLACE INTO t1 VALUES(2,22,222,2222);
60
+ REPLACE INTO t1 VALUES(3,33,333,3333);
61
+ CREATE TABLE t2 (B1 INTEGER NOT NULL,B2 INTEGER NOT NULL,B3 INTEGER NOT
62
+ NULL,B4 INTEGER NOT NULL,PRIMARY KEY(B1));
63
+ REPLACE INTO t2 VALUES(1,88,888,8888);
64
+ REPLACE INTO t2 VALUES(2,99,999,9999);
65
+ SELECT (SELECT GROUP_CONCAT(CASE WHEN a1=1 THEN'A' ELSE 'B' END) FROM t2),
66
+ t1.*
67
+ FROM t1;
68
+ }
69
+ } {A,B,B 1 11 111 1111}
70
+ db2 close
71
+
72
+ ##################### Test cases for ticket [bfbf38e5e9956ac69f] ############
73
+ #
74
+ # This first test case is the original problem report:
75
+ do_test aggnested-3.0 {
76
+ db eval {
77
+ CREATE TABLE AAA (
78
+ aaa_id INTEGER PRIMARY KEY AUTOINCREMENT
79
+ );
80
+ CREATE TABLE RRR (
81
+ rrr_id INTEGER PRIMARY KEY AUTOINCREMENT,
82
+ rrr_date INTEGER NOT NULL,
83
+ rrr_aaa INTEGER
84
+ );
85
+ CREATE TABLE TTT (
86
+ ttt_id INTEGER PRIMARY KEY AUTOINCREMENT,
87
+ target_aaa INTEGER NOT NULL,
88
+ source_aaa INTEGER NOT NULL
89
+ );
90
+ insert into AAA (aaa_id) values (2);
91
+ insert into TTT (ttt_id, target_aaa, source_aaa)
92
+ values (4469, 2, 2);
93
+ insert into TTT (ttt_id, target_aaa, source_aaa)
94
+ values (4476, 2, 1);
95
+ insert into RRR (rrr_id, rrr_date, rrr_aaa)
96
+ values (0, 0, NULL);
97
+ insert into RRR (rrr_id, rrr_date, rrr_aaa)
98
+ values (2, 4312, 2);
99
+ SELECT i.aaa_id,
100
+ (SELECT sum(CASE WHEN (t.source_aaa == i.aaa_id) THEN 1 ELSE 0 END)
101
+ FROM TTT t
102
+ ) AS segfault
103
+ FROM
104
+ (SELECT curr.rrr_aaa as aaa_id
105
+ FROM RRR curr
106
+ -- you also can comment out the next line
107
+ -- it causes segfault to happen after one row is outputted
108
+ INNER JOIN AAA a ON (curr.rrr_aaa = aaa_id)
109
+ LEFT JOIN RRR r ON (r.rrr_id <> 0 AND r.rrr_date < curr.rrr_date)
110
+ GROUP BY curr.rrr_id
111
+ HAVING r.rrr_date IS NULL
112
+ ) i;
113
+ }
114
+ } {2 1}
115
+
116
+ # Further variants of the test case, as found in the ticket
117
+ #
118
+ do_test aggnested-3.1 {
119
+ db eval {
120
+ DROP TABLE IF EXISTS t1;
121
+ DROP TABLE IF EXISTS t2;
122
+ CREATE TABLE t1 (
123
+ id1 INTEGER PRIMARY KEY AUTOINCREMENT,
124
+ value1 INTEGER
125
+ );
126
+ INSERT INTO t1 VALUES(4469,2),(4476,1);
127
+ CREATE TABLE t2 (
128
+ id2 INTEGER PRIMARY KEY AUTOINCREMENT,
129
+ value2 INTEGER
130
+ );
131
+ INSERT INTO t2 VALUES(0,1),(2,2);
132
+ SELECT
133
+ (SELECT sum(value2==xyz) FROM t2)
134
+ FROM
135
+ (SELECT curr.value1 as xyz
136
+ FROM t1 AS curr LEFT JOIN t1 AS other
137
+ GROUP BY curr.id1);
138
+ }
139
+ } {1 1}
140
+ do_test aggnested-3.1-rj {
141
+ db eval {
142
+ SELECT
143
+ (SELECT sum(value2==xyz) FROM t2)
144
+ FROM
145
+ (SELECT curr.value1 as xyz
146
+ FROM t1 AS other RIGHT JOIN t1 AS curr
147
+ GROUP BY curr.id1);
148
+ }
149
+ } {1 1}
150
+
151
+ do_test aggnested-3.2 {
152
+ db eval {
153
+ DROP TABLE IF EXISTS t1;
154
+ DROP TABLE IF EXISTS t2;
155
+ CREATE TABLE t1 (
156
+ id1 INTEGER,
157
+ value1 INTEGER,
158
+ x1 INTEGER
159
+ );
160
+ INSERT INTO t1 VALUES(4469,2,98),(4469,1,99),(4469,3,97);
161
+ CREATE TABLE t2 (
162
+ value2 INTEGER
163
+ );
164
+ INSERT INTO t2 VALUES(1);
165
+ SELECT
166
+ (SELECT sum(value2==xyz) FROM t2)
167
+ FROM
168
+ (SELECT value1 as xyz, max(x1) AS pqr
169
+ FROM t1
170
+ GROUP BY id1);
171
+ SELECT
172
+ (SELECT sum(value2<>xyz) FROM t2)
173
+ FROM
174
+ (SELECT value1 as xyz, max(x1) AS pqr
175
+ FROM t1
176
+ GROUP BY id1);
177
+ }
178
+ } {1 0}
179
+ do_test aggnested-3.3 {
180
+ db eval {
181
+ DROP TABLE IF EXISTS t1;
182
+ DROP TABLE IF EXISTS t2;
183
+ CREATE TABLE t1(id1, value1);
184
+ INSERT INTO t1 VALUES(4469,2),(4469,1);
185
+ CREATE TABLE t2 (value2);
186
+ INSERT INTO t2 VALUES(1);
187
+ SELECT (SELECT sum(value2=value1) FROM t2), max(value1)
188
+ FROM t1
189
+ GROUP BY id1;
190
+ }
191
+ } {0 2}
192
+
193
+ # A batch of queries all doing approximately the same operation involving
194
+ # two nested aggregate queries.
195
+ #
196
+ do_test aggnested-3.11 {
197
+ db eval {
198
+ DROP TABLE IF EXISTS t1;
199
+ DROP TABLE IF EXISTS t2;
200
+ CREATE TABLE t1(id1, value1);
201
+ INSERT INTO t1 VALUES(4469,12),(4469,11),(4470,34);
202
+ CREATE INDEX t1id1 ON t1(id1);
203
+ CREATE TABLE t2 (value2);
204
+ INSERT INTO t2 VALUES(12),(34),(34);
205
+ INSERT INTO t2 SELECT value2 FROM t2;
206
+
207
+ SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=max(value1))
208
+ FROM t1
209
+ GROUP BY id1;
210
+ }
211
+ } {12 2 34 4}
212
+ do_test aggnested-3.12 {
213
+ db eval {
214
+ SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=value1)
215
+ FROM t1
216
+ GROUP BY id1;
217
+ }
218
+ } {12 2 34 4}
219
+ do_test aggnested-3.13 {
220
+ db eval {
221
+ SELECT value1, (SELECT sum(value2=value1) FROM t2)
222
+ FROM t1;
223
+ }
224
+ } {12 2 11 0 34 4}
225
+ do_test aggnested-3.14 {
226
+ db eval {
227
+ SELECT value1, (SELECT sum(value2=value1) FROM t2)
228
+ FROM t1
229
+ WHERE value1 IN (SELECT max(value1) FROM t1 GROUP BY id1);
230
+ }
231
+ } {12 2 34 4}
232
+ do_test aggnested-3.15 {
233
+ # FIXME: If case 3.16 works, then this case really ought to work too...
234
+ catchsql {
235
+ SELECT max(value1), (SELECT sum(value2=max(value1)) FROM t2)
236
+ FROM t1
237
+ GROUP BY id1;
238
+ }
239
+ } {1 {misuse of aggregate function max()}}
240
+ do_test aggnested-3.16 {
241
+ db eval {
242
+ SELECT max(value1), (SELECT sum(value2=value1) FROM t2)
243
+ FROM t1
244
+ GROUP BY id1;
245
+ }
246
+ } {12 2 34 4}
247
+
248
+ # 2019-08-31
249
+ # Problem found by dbsqlfuzz
250
+ #
251
+ do_execsql_test aggnested-4.1 {
252
+ DROP TABLE IF EXISTS aa;
253
+ DROP TABLE IF EXISTS bb;
254
+ CREATE TABLE aa(x INT); INSERT INTO aa(x) VALUES(123);
255
+ CREATE TABLE bb(y INT); INSERT INTO bb(y) VALUES(456);
256
+ SELECT (SELECT sum(x+(SELECT y)) FROM bb) FROM aa;
257
+ } {579}
258
+ do_execsql_test aggnested-4.2 {
259
+ SELECT (SELECT sum(x+y) FROM bb) FROM aa;
260
+ } {579}
261
+ do_execsql_test aggnested-4.3 {
262
+ DROP TABLE IF EXISTS tx;
263
+ DROP TABLE IF EXISTS ty;
264
+ CREATE TABLE tx(x INT);
265
+ INSERT INTO tx VALUES(1),(2),(3),(4),(5);
266
+ CREATE TABLE ty(y INT);
267
+ INSERT INTO ty VALUES(91),(92),(93);
268
+ SELECT min((SELECT count(y) FROM ty)) FROM tx;
269
+ } {3}
270
+ do_execsql_test aggnested-4.4 {
271
+ SELECT max((SELECT a FROM (SELECT count(*) AS a FROM ty) AS s)) FROM tx;
272
+ } {3}
273
+
274
+ #--------------------------------------------------------------------------
275
+ #
276
+ reset_db
277
+ do_execsql_test 5.0 {
278
+ CREATE TABLE x1(a, b);
279
+ INSERT INTO x1 VALUES(1, 2);
280
+ CREATE TABLE x2(x);
281
+ INSERT INTO x2 VALUES(NULL), (NULL), (NULL);
282
+ }
283
+
284
+ # At one point, aggregate "total()" in the query below was being processed
285
+ # as part of the outer SELECT, not as part of the sub-select with no FROM
286
+ # clause.
287
+ do_execsql_test 5.1 {
288
+ SELECT ( SELECT total( (SELECT b FROM x1) ) ) FROM x2;
289
+ } {2.0 2.0 2.0}
290
+
291
+ do_execsql_test 5.2 {
292
+ SELECT ( SELECT total( (SELECT 2 FROM x1) ) ) FROM x2;
293
+ } {2.0 2.0 2.0}
294
+
295
+ do_execsql_test 5.3 {
296
+ CREATE TABLE t1(a);
297
+ CREATE TABLE t2(b);
298
+ }
299
+
300
+ do_execsql_test 5.4 {
301
+ SELECT(
302
+ SELECT max(b) LIMIT (
303
+ SELECT total( (SELECT a FROM t1) )
304
+ )
305
+ )
306
+ FROM t2;
307
+ } {{}}
308
+
309
+ do_execsql_test 5.5 {
310
+ CREATE TABLE a(b);
311
+ WITH c AS(SELECT a)
312
+ SELECT(SELECT(SELECT string_agg(b, b)
313
+ LIMIT(SELECT 0.100000 *
314
+ AVG(DISTINCT(SELECT 0 FROM a ORDER BY b, b, b))))
315
+ FROM a GROUP BY b,
316
+ b, b) FROM a EXCEPT SELECT b FROM a ORDER BY b,
317
+ b, b;
318
+ }
319
+
320
+ #-------------------------------------------------------------------------
321
+ # dbsqlfuzz a779227f721a834df95f4f42d0c31550a1f8b8a2
322
+ #
323
+ reset_db
324
+ do_execsql_test 6.0 {
325
+ CREATE TABLE t1(a);
326
+ CREATE TABLE t2(b);
327
+
328
+ INSERT INTO t1 VALUES('x');
329
+ INSERT INTO t2 VALUES(1);
330
+ }
331
+
332
+ do_execsql_test 6.1.1 {
333
+ SELECT (
334
+ SELECT t2.b FROM (SELECT t2.b AS c FROM t1) GROUP BY 1 HAVING t2.b
335
+ )
336
+ FROM t2 GROUP BY 'constant_string';
337
+ } {1}
338
+ do_execsql_test 6.1.2 {
339
+ SELECT (
340
+ SELECT c FROM (SELECT t2.b AS c FROM t1) GROUP BY c HAVING t2.b
341
+ )
342
+ FROM t2 GROUP BY 'constant_string';
343
+ } {1}
344
+
345
+ do_execsql_test 6.2.0 {
346
+ UPDATE t2 SET b=0
347
+ }
348
+ do_execsql_test 6.2.1 {
349
+ SELECT (
350
+ SELECT t2.b FROM (SELECT t2.b AS c FROM t1) GROUP BY 1 HAVING t2.b
351
+ )
352
+ FROM t2 GROUP BY 'constant_string';
353
+ } {{}}
354
+ do_execsql_test 6.2.2 {
355
+ SELECT (
356
+ SELECT c FROM (SELECT t2.b AS c FROM t1) GROUP BY c HAVING t2.b
357
+ )
358
+ FROM t2 GROUP BY 'constant_string';
359
+ } {{}}
360
+
361
+ #-------------------------------------------------------------------------
362
+ reset_db
363
+
364
+ do_execsql_test 7.0 {
365
+ CREATE TABLE invoice (
366
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
367
+ amount DOUBLE PRECISION DEFAULT NULL,
368
+ name VARCHAR(100) DEFAULT NULL
369
+ );
370
+
371
+ INSERT INTO invoice (amount, name) VALUES
372
+ (4.0, 'Michael'), (15.0, 'Bara'), (4.0, 'Michael'), (6.0, 'John');
373
+ }
374
+
375
+ do_execsql_test 7.1 {
376
+ SELECT sum(amount), name
377
+ from invoice
378
+ group by name
379
+ having (select v > 6 from (select sum(amount) v) t)
380
+ } {
381
+ 15.0 Bara
382
+ 8.0 Michael
383
+ }
384
+
385
+ do_execsql_test 7.2 {
386
+ SELECT (select 1 from (select sum(amount))) FROM invoice
387
+ } {1}
388
+
389
+ do_execsql_test 8.0 {
390
+ CREATE TABLE t1(x INT);
391
+ INSERT INTO t1 VALUES(100);
392
+ INSERT INTO t1 VALUES(20);
393
+ INSERT INTO t1 VALUES(3);
394
+ SELECT (SELECT y FROM (SELECT sum(x) AS y) AS t2 ) FROM t1;
395
+ } {123}
396
+
397
+ do_execsql_test 8.1 {
398
+ SELECT (
399
+ SELECT y FROM (
400
+ SELECT z AS y FROM (SELECT sum(x) AS z) AS t2
401
+ )
402
+ ) FROM t1;
403
+ } {123}
404
+
405
+ do_execsql_test 8.2 {
406
+ SELECT (
407
+ SELECT a FROM (
408
+ SELECT y AS a FROM (
409
+ SELECT z AS y FROM (SELECT sum(x) AS z) AS t2
410
+ )
411
+ )
412
+ ) FROM t1;
413
+ } {123}
414
+
415
+ #-------------------------------------------------------------------------
416
+ # dbsqlfuzz 04408efc51ae46897c4c122b407412045ed221b4
417
+ #
418
+ reset_db
419
+
420
+ do_execsql_test 9.1 {
421
+ WITH out(i, j, k) AS (
422
+ VALUES(1234, 5678, 9012)
423
+ )
424
+ SELECT (
425
+ SELECT (
426
+ SELECT min(abc) = ( SELECT ( SELECT 1234 fROM (SELECT abc) ) )
427
+ FROM (
428
+ SELECT sum( out.i ) + ( SELECT sum( out.i ) ) AS abc FROM (SELECT out.j)
429
+ )
430
+ )
431
+ ) FROM out;
432
+ } {0}
433
+
434
+ do_execsql_test 9.2 {
435
+ CREATE TABLE t1(a);
436
+ CREATE TABLE t2(b);
437
+ INSERT INTO t1 VALUES(1), (2), (3);
438
+ INSERT INTO t2 VALUES(4), (5), (6);
439
+
440
+ SELECT (
441
+ SELECT min(y) + (SELECT x) FROM (
442
+ SELECT sum(a) AS x, b AS y FROM t2
443
+ )
444
+ )
445
+ FROM t1;
446
+ } {10}
447
+
448
+ do_execsql_test 9.3 {
449
+ SELECT (
450
+ SELECT min(y) + (SELECT (SELECT x)) FROM (
451
+ SELECT sum(a) AS x, b AS y FROM t2
452
+ )
453
+ )
454
+ FROM t1;
455
+ } {10}
456
+
457
+ do_execsql_test 9.4 {
458
+ SELECT (
459
+ SELECT (SELECT x) FROM (
460
+ SELECT sum(a) AS x, b AS y FROM t2
461
+ ) GROUP BY y
462
+ )
463
+ FROM t1;
464
+ } {6}
465
+
466
+ do_execsql_test 9.5 {
467
+ SELECT (
468
+ SELECT (SELECT (SELECT x)) FROM (
469
+ SELECT sum(a) AS x, b AS y FROM t2
470
+ ) GROUP BY y
471
+ )
472
+ FROM t1;
473
+ } {6}
474
+
475
+ # 2023-12-16
476
+ # New test case for check-in [4470f657d2069972] from 2023-11-02
477
+ # https://bugs.chromium.org/p/chromium/issues/detail?id=1511689
478
+ #
479
+ do_execsql_test 10.1 {
480
+ DROP TABLE IF EXISTS t0;
481
+ DROP TABLE IF EXISTS t1;
482
+ CREATE TABLE t0(c1, c2); INSERT INTO t0 VALUES(1,2);
483
+ CREATE TABLE t1(c3, c4); INSERT INTO t1 VALUES(3,4);
484
+ SELECT * FROM t0 WHERE EXISTS (SELECT 1 FROM t1 GROUP BY c3 HAVING ( SELECT count(*) FROM (SELECT 1 UNION ALL SELECT sum(DISTINCT c1) ) ) ) BETWEEN 1 AND 1;
485
+ } {1 2}
486
+
487
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/all.test ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2001 September 15
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file runs all tests.
12
+ #
13
+
14
+ set testdir [file dirname $argv0]
15
+ source $testdir/permutations.test
16
+
17
+ run_test_suite full
18
+
19
+ ifcapable rbu { run_test_suite rbu }
20
+ run_test_suite no_optimization
21
+ run_test_suite memsubsys1
22
+ run_test_suite memsubsys2
23
+ run_test_suite singlethread
24
+ run_test_suite multithread
25
+ run_test_suite onefile
26
+ run_test_suite utf16
27
+ run_test_suite exclusive
28
+ run_test_suite persistent_journal
29
+ run_test_suite persistent_journal_error
30
+ run_test_suite no_journal
31
+ run_test_suite no_journal_error
32
+ run_test_suite autovacuum_ioerr
33
+ run_test_suite no_mutex_try
34
+ run_test_suite fullmutex
35
+ run_test_suite journaltest
36
+ run_test_suite inmemory_journal
37
+ run_test_suite pcache0
38
+ run_test_suite pcache10
39
+ run_test_suite pcache50
40
+ run_test_suite pcache90
41
+ run_test_suite pcache100
42
+ run_test_suite prepare
43
+ run_test_suite mmap
44
+
45
+ if {$::tcl_platform(platform)=="unix"} {
46
+ ifcapable !default_autovacuum {
47
+ run_test_suite autovacuum_crash
48
+ }
49
+ }
50
+
51
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/alter2.test ADDED
@@ -0,0 +1,470 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2005 February 18
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing that SQLite can handle a subtle
13
+ # file format change that may be used in the future to implement
14
+ # "ALTER TABLE ... ADD COLUMN".
15
+ #
16
+ # $Id: alter2.test,v 1.14 2009/04/07 14:14:22 danielk1977 Exp $
17
+ #
18
+
19
+ set testdir [file dirname $argv0]
20
+ source $testdir/tester.tcl
21
+
22
+ # We have to have pragmas in order to do this test
23
+ ifcapable {!pragma} return
24
+
25
+ # Do not use a codec for tests in this file, as the database file is
26
+ # manipulated directly using tcl scripts. See proc [set_file_format].
27
+ #
28
+ do_not_use_codec
29
+
30
+ # The file format change affects the way row-records stored in tables (but
31
+ # not indices) are interpreted. Before version 3.1.3, a row-record for a
32
+ # table with N columns was guaranteed to contain exactly N fields. As
33
+ # of version 3.1.3, the record may contain up to N fields. In this case
34
+ # the M fields that are present are the values for the left-most M
35
+ # columns. The (N-M) rightmost columns contain NULL.
36
+ #
37
+ # If any records in the database contain less fields than their table
38
+ # has columns, then the file-format meta value should be set to (at least) 2.
39
+ #
40
+
41
+ # This procedure sets the value of the file-format in file 'test.db'
42
+ # to $newval. Also, the schema cookie is incremented.
43
+ #
44
+ proc set_file_format {newval} {
45
+ hexio_write test.db 44 [hexio_render_int32 $newval]
46
+ set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
47
+ incr schemacookie
48
+ hexio_write test.db 40 [hexio_render_int32 $schemacookie]
49
+ return {}
50
+ }
51
+
52
+ # This procedure returns the value of the file-format in file 'test.db'.
53
+ #
54
+ proc get_file_format {{fname test.db}} {
55
+ return [hexio_get_int [hexio_read $fname 44 4]]
56
+ }
57
+
58
+ # This procedure sets the SQL statement stored for table $tbl in the
59
+ # sqlite_master table of file 'test.db' to $sql. Also set the file format
60
+ # to the supplied value. This is 2 if the added column has a default that is
61
+ # NULL, or 3 otherwise.
62
+ #
63
+ proc alter_table {tbl sql {file_format 2}} {
64
+ sqlite3 dbat test.db
65
+ set s [string map {' ''} $sql]
66
+ set t [string map {' ''} $tbl]
67
+ sqlite3_db_config dbat DEFENSIVE 0
68
+ dbat eval [subst {
69
+ PRAGMA writable_schema = 1;
70
+ UPDATE sqlite_master SET sql = '$s' WHERE name = '$t' AND type = 'table';
71
+ PRAGMA writable_schema = 0;
72
+ }]
73
+ dbat close
74
+ set_file_format 2
75
+ }
76
+
77
+ # Create bogus application-defined functions for functions used
78
+ # internally by ALTER TABLE, to ensure that ALTER TABLE falls back
79
+ # to the built-in functions.
80
+ #
81
+ proc failing_app_func {args} {error "bad function"}
82
+ do_test alter2-1.0 {
83
+ db func substr failing_app_func
84
+ db func like failing_app_func
85
+ db func sqlite_rename_table failing_app_func
86
+ db func sqlite_rename_trigger failing_app_func
87
+ db func sqlite_rename_parent failing_app_func
88
+ catchsql {SELECT substr('abcdefg',1,3)}
89
+ } {1 {bad function}}
90
+
91
+
92
+ #-----------------------------------------------------------------------
93
+ # Some basic tests to make sure short rows are handled.
94
+ #
95
+ sqlite3_db_config db DEFENSIVE 0
96
+ do_test alter2-1.1 {
97
+ execsql {
98
+ CREATE TABLE abc(a, b);
99
+ INSERT INTO abc VALUES(1, 2);
100
+ INSERT INTO abc VALUES(3, 4);
101
+ INSERT INTO abc VALUES(5, 6);
102
+ }
103
+ } {}
104
+ do_test alter2-1.2 {
105
+ # ALTER TABLE abc ADD COLUMN c;
106
+ alter_table abc {CREATE TABLE abc(a, b, c);}
107
+ } {}
108
+ do_test alter2-1.3 {
109
+ execsql {
110
+ SELECT * FROM abc;
111
+ }
112
+ } {1 2 {} 3 4 {} 5 6 {}}
113
+ do_test alter2-1.4 {
114
+ execsql {
115
+ UPDATE abc SET c = 10 WHERE a = 1;
116
+ SELECT * FROM abc;
117
+ }
118
+ } {1 2 10 3 4 {} 5 6 {}}
119
+ do_test alter2-1.5 {
120
+ execsql {
121
+ CREATE INDEX abc_i ON abc(c);
122
+ }
123
+ } {}
124
+ do_test alter2-1.6 {
125
+ execsql {
126
+ SELECT c FROM abc ORDER BY c;
127
+ }
128
+ } {{} {} 10}
129
+ do_test alter2-1.7 {
130
+ execsql {
131
+ SELECT * FROM abc WHERE c = 10;
132
+ }
133
+ } {1 2 10}
134
+ do_test alter2-1.8 {
135
+ execsql {
136
+ SELECT sum(a), c FROM abc GROUP BY c;
137
+ }
138
+ } {8 {} 1 10}
139
+ do_test alter2-1.9 {
140
+ # ALTER TABLE abc ADD COLUMN d;
141
+ alter_table abc {CREATE TABLE abc(a, b, c, d);}
142
+ if {[permutation] == "prepare"} { db cache flush }
143
+ execsql { SELECT * FROM abc; }
144
+ execsql {
145
+ UPDATE abc SET d = 11 WHERE c IS NULL AND a<4;
146
+ SELECT * FROM abc;
147
+ }
148
+ } {1 2 10 {} 3 4 {} 11 5 6 {} {}}
149
+ do_test alter2-1.10 {
150
+ execsql {
151
+ SELECT typeof(d) FROM abc;
152
+ }
153
+ } {null integer null}
154
+ do_test alter2-1.99 {
155
+ execsql {
156
+ DROP TABLE abc;
157
+ }
158
+ } {}
159
+
160
+ #-----------------------------------------------------------------------
161
+ # Test that views work when the underlying table structure is changed.
162
+ #
163
+ ifcapable view {
164
+ do_test alter2-2.1 {
165
+ execsql {
166
+ CREATE TABLE abc2(a, b, c);
167
+ INSERT INTO abc2 VALUES(1, 2, 10);
168
+ INSERT INTO abc2 VALUES(3, 4, NULL);
169
+ INSERT INTO abc2 VALUES(5, 6, NULL);
170
+ CREATE VIEW abc2_v AS SELECT * FROM abc2;
171
+ SELECT * FROM abc2_v;
172
+ }
173
+ } {1 2 10 3 4 {} 5 6 {}}
174
+ do_test alter2-2.2 {
175
+ # ALTER TABLE abc ADD COLUMN d;
176
+ alter_table abc2 {CREATE TABLE abc2(a, b, c, d);}
177
+ execsql {
178
+ SELECT * FROM abc2_v;
179
+ }
180
+ } {1 2 10 {} 3 4 {} {} 5 6 {} {}}
181
+ do_test alter2-2.3 {
182
+ execsql {
183
+ DROP TABLE abc2;
184
+ DROP VIEW abc2_v;
185
+ }
186
+ } {}
187
+ }
188
+
189
+ #-----------------------------------------------------------------------
190
+ # Test that triggers work when a short row is copied to the old.*
191
+ # trigger pseudo-table.
192
+ #
193
+ ifcapable trigger {
194
+ do_test alter2-3.1 {
195
+ execsql {
196
+ CREATE TABLE abc3(a, b);
197
+ CREATE TABLE blog(o, n);
198
+ CREATE TRIGGER abc3_t AFTER UPDATE OF b ON abc3 BEGIN
199
+ INSERT INTO blog VALUES(old.b, new.b);
200
+ END;
201
+ }
202
+ } {}
203
+ do_test alter2-3.2 {
204
+ execsql {
205
+ INSERT INTO abc3 VALUES(1, 4);
206
+ UPDATE abc3 SET b = 2 WHERE b = 4;
207
+ SELECT * FROM blog;
208
+ }
209
+ } {4 2}
210
+ do_test alter2-3.3 {
211
+ execsql {
212
+ INSERT INTO abc3 VALUES(3, 4);
213
+ INSERT INTO abc3 VALUES(5, 6);
214
+ }
215
+ alter_table abc3 {CREATE TABLE abc3(a, b, c);}
216
+ execsql {
217
+ SELECT * FROM abc3;
218
+ }
219
+ } {1 2 {} 3 4 {} 5 6 {}}
220
+ do_test alter2-3.4 {
221
+ execsql {
222
+ UPDATE abc3 SET b = b*2 WHERE a<4;
223
+ SELECT * FROM abc3;
224
+ }
225
+ } {1 4 {} 3 8 {} 5 6 {}}
226
+ do_test alter2-3.5 {
227
+ execsql {
228
+ SELECT * FROM blog;
229
+ }
230
+ } {4 2 2 4 4 8}
231
+
232
+ do_test alter2-3.6 {
233
+ execsql {
234
+ CREATE TABLE clog(o, n);
235
+ CREATE TRIGGER abc3_t2 AFTER UPDATE OF c ON abc3 BEGIN
236
+ INSERT INTO clog VALUES(old.c, new.c);
237
+ END;
238
+ UPDATE abc3 SET c = a*2;
239
+ SELECT * FROM clog;
240
+ }
241
+ } {{} 2 {} 6 {} 10}
242
+ } else {
243
+ execsql { CREATE TABLE abc3(a, b); }
244
+ }
245
+
246
+ #---------------------------------------------------------------------
247
+ # Check that an error occurs if the database is upgraded to a file
248
+ # format that SQLite does not support (in this case 5). Note: The
249
+ # file format is checked each time the schema is read, so changing the
250
+ # file format requires incrementing the schema cookie.
251
+ #
252
+ do_test alter2-4.1 {
253
+ db close
254
+ set_file_format 5
255
+ catch { sqlite3 db test.db }
256
+ set {} {}
257
+ } {}
258
+ do_test alter2-4.2 {
259
+ # We have to run two queries here because the Tcl interface uses
260
+ # sqlite3_prepare_v2(). In this case, the first query encounters an
261
+ # SQLITE_SCHEMA error. Then, when trying to recompile the statement, the
262
+ # "unsupported file format" error is encountered. So the error code
263
+ # returned is SQLITE_SCHEMA, not SQLITE_ERROR as required by the following
264
+ # test case.
265
+ #
266
+ # When the query is attempted a second time, the same error message is
267
+ # returned but the error code is SQLITE_ERROR, because the unsupported
268
+ # file format was detected during a call to sqlite3_prepare(), not
269
+ # sqlite3_step().
270
+ #
271
+ catchsql { SELECT * FROM sqlite_master; }
272
+ catchsql { SELECT * FROM sqlite_master; }
273
+ } {1 {unsupported file format}}
274
+ do_test alter2-4.3 {
275
+ sqlite3_errcode db
276
+ } {SQLITE_ERROR}
277
+ do_test alter2-4.4 {
278
+ set ::DB [sqlite3_connection_pointer db]
279
+ catchsql {
280
+ SELECT * FROM sqlite_master;
281
+ }
282
+ } {1 {unsupported file format}}
283
+ do_test alter2-4.5 {
284
+ sqlite3_errcode db
285
+ } {SQLITE_ERROR}
286
+
287
+ #---------------------------------------------------------------------
288
+ # Check that executing VACUUM on a file with file-format version 2
289
+ # resets the file format to 1.
290
+ #
291
+ set default_file_format [expr $SQLITE_DEFAULT_FILE_FORMAT==4 ? 4 : 1]
292
+ ifcapable vacuum {
293
+ do_test alter2-5.1 {
294
+ set_file_format 2
295
+ db close
296
+ sqlite3 db test.db
297
+ execsql {SELECT 1 FROM sqlite_master LIMIT 1;}
298
+ get_file_format
299
+ } {2}
300
+ do_test alter2-5.2 {
301
+ execsql { VACUUM }
302
+ } {}
303
+ do_test alter2-5.3 {
304
+ get_file_format
305
+ } $default_file_format
306
+ }
307
+
308
+ #---------------------------------------------------------------------
309
+ # Test that when a database with file-format 2 is opened, new
310
+ # databases are still created with file-format 1.
311
+ #
312
+ do_test alter2-6.1 {
313
+ db close
314
+ set_file_format 2
315
+ sqlite3 db test.db
316
+ get_file_format
317
+ } {2}
318
+ ifcapable attach {
319
+ do_test alter2-6.2 {
320
+ forcedelete test2.db-journal
321
+ forcedelete test2.db
322
+ execsql {
323
+ ATTACH 'test2.db' AS aux;
324
+ CREATE TABLE aux.t1(a, b);
325
+ }
326
+ get_file_format test2.db
327
+ } $default_file_format
328
+ }
329
+ do_test alter2-6.3 {
330
+ execsql {
331
+ CREATE TABLE t1(a, b);
332
+ }
333
+ get_file_format
334
+ } {2}
335
+
336
+ #---------------------------------------------------------------------
337
+ # Test that types and values for columns added with default values
338
+ # other than NULL work with SELECT statements.
339
+ #
340
+ do_test alter2-7.1 {
341
+ execsql {
342
+ DROP TABLE t1;
343
+ CREATE TABLE t1(a);
344
+ INSERT INTO t1 VALUES(1);
345
+ INSERT INTO t1 VALUES(2);
346
+ INSERT INTO t1 VALUES(3);
347
+ INSERT INTO t1 VALUES(4);
348
+ SELECT * FROM t1;
349
+ }
350
+ } {1 2 3 4}
351
+ do_test alter2-7.2 {
352
+ set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')}
353
+ alter_table t1 $sql 3
354
+ execsql {
355
+ SELECT * FROM t1 LIMIT 1;
356
+ }
357
+ } {1 123 123}
358
+ do_test alter2-7.3 {
359
+ execsql {
360
+ SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
361
+ }
362
+ } {1 integer 123 text 123 integer}
363
+ do_test alter2-7.4 {
364
+ execsql {
365
+ SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
366
+ }
367
+ } {1 integer 123 text 123 integer}
368
+ do_test alter2-7.5 {
369
+ set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)}
370
+ alter_table t1 $sql 3
371
+ execsql {
372
+ SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
373
+ }
374
+ } {1 integer -123.0 real 5 text}
375
+
376
+ #-----------------------------------------------------------------------
377
+ # Test that UPDATE trigger tables work with default values, and that when
378
+ # a row is updated the default values are correctly transfered to the
379
+ # new row.
380
+ #
381
+ ifcapable trigger {
382
+ db function set_val {set ::val}
383
+ do_test alter2-8.1 {
384
+ execsql {
385
+ CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
386
+ SELECT set_val(
387
+ old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '||
388
+ new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c)
389
+ );
390
+ END;
391
+ }
392
+ list
393
+ } {}
394
+ }
395
+ do_test alter2-8.2 {
396
+ execsql {
397
+ UPDATE t1 SET c = 10 WHERE a = 1;
398
+ SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
399
+ }
400
+ } {1 integer -123.0 real 10 text}
401
+ ifcapable trigger {
402
+ do_test alter2-8.3 {
403
+ set ::val
404
+ } {-123.0 real 5 text -123.0 real 10 text}
405
+ }
406
+
407
+ #-----------------------------------------------------------------------
408
+ # Test that DELETE trigger tables work with default values, and that when
409
+ # a row is updated the default values are correctly transfered to the
410
+ # new row.
411
+ #
412
+ ifcapable trigger {
413
+ do_test alter2-9.1 {
414
+ execsql {
415
+ CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN
416
+ SELECT set_val(
417
+ old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)
418
+ );
419
+ END;
420
+ }
421
+ list
422
+ } {}
423
+ do_test alter2-9.2 {
424
+ execsql {
425
+ DELETE FROM t1 WHERE a = 2;
426
+ }
427
+ set ::val
428
+ } {-123.0 real 5 text}
429
+ }
430
+
431
+ #-----------------------------------------------------------------------
432
+ # Test creating an index on a column added with a default value.
433
+ #
434
+ ifcapable bloblit {
435
+ do_test alter2-10.1 {
436
+ execsql {
437
+ CREATE TABLE t2(a);
438
+ INSERT INTO t2 VALUES('a');
439
+ INSERT INTO t2 VALUES('b');
440
+ INSERT INTO t2 VALUES('c');
441
+ INSERT INTO t2 VALUES('d');
442
+ }
443
+ alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3
444
+ catchsql {
445
+ SELECT * FROM sqlite_master;
446
+ }
447
+ execsql {
448
+ SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1;
449
+ }
450
+ } {'a' X'ABCD' NULL}
451
+ do_test alter2-10.2 {
452
+ execsql {
453
+ CREATE INDEX i1 ON t2(b);
454
+ SELECT a FROM t2 WHERE b = X'ABCD';
455
+ }
456
+ } {a b c d}
457
+ do_test alter2-10.3 {
458
+ execsql {
459
+ DELETE FROM t2 WHERE a = 'c';
460
+ SELECT a FROM t2 WHERE b = X'ABCD';
461
+ }
462
+ } {a b d}
463
+ do_test alter2-10.4 {
464
+ execsql {
465
+ SELECT count(b) FROM t2 WHERE b = X'ABCD';
466
+ }
467
+ } {3}
468
+ }
469
+
470
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/alter4.test ADDED
@@ -0,0 +1,427 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2009 February 2
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing that SQLite can handle a subtle
13
+ # file format change that may be used in the future to implement
14
+ # "ALTER TABLE ... ADD COLUMN".
15
+ #
16
+ # $Id: alter4.test,v 1.1 2009/02/02 18:03:22 drh Exp $
17
+ #
18
+
19
+ set testdir [file dirname $argv0]
20
+
21
+ source $testdir/tester.tcl
22
+
23
+ # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
24
+ ifcapable !altertable {
25
+ finish_test
26
+ return
27
+ }
28
+
29
+
30
+ # Test Organisation:
31
+ # ------------------
32
+ #
33
+ # alter4-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql.
34
+ # alter4-2.*: Test error messages.
35
+ # alter4-3.*: Test adding columns with default value NULL.
36
+ # alter4-4.*: Test adding columns with default values other than NULL.
37
+ # alter4-5.*: Test adding columns to tables in ATTACHed databases.
38
+ # alter4-6.*: Test that temp triggers are not accidentally dropped.
39
+ # alter4-7.*: Test that VACUUM resets the file-format.
40
+ #
41
+
42
+ do_test alter4-1.1 {
43
+ execsql {
44
+ CREATE TEMP TABLE abc(a, b, c);
45
+ SELECT sql FROM sqlite_temp_master;
46
+ }
47
+ } {{CREATE TABLE abc(a, b, c)}}
48
+ do_test alter4-1.1b {
49
+ execsql {
50
+ SELECT sql FROM temp.sqlite_master;
51
+ }
52
+ } {{CREATE TABLE abc(a, b, c)}}
53
+ do_test alter4-1.2 {
54
+ execsql {ALTER TABLE abc ADD d INTEGER;}
55
+ execsql {
56
+ SELECT sql FROM sqlite_temp_master;
57
+ }
58
+ } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
59
+ do_test alter4-1.2b {
60
+ execsql {
61
+ SELECT sql FROM temp.sqlite_master;
62
+ }
63
+ } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
64
+ do_test alter4-1.3 {
65
+ execsql {ALTER TABLE abc ADD e}
66
+ execsql {
67
+ SELECT sql FROM sqlite_temp_master;
68
+ }
69
+ } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
70
+ do_test alter4-1.3b {
71
+ execsql {
72
+ SELECT sql FROM temp.sqlite_master;
73
+ }
74
+ } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
75
+ do_test alter4-1.4 {
76
+ execsql {
77
+ CREATE TABLE temp.t1(a, b);
78
+ ALTER TABLE t1 ADD c;
79
+ SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1';
80
+ }
81
+ } {{CREATE TABLE t1(a, b, c)}}
82
+ do_test alter4-1.4b {
83
+ execsql {
84
+ SELECT sql FROM temp.sqlite_master WHERE tbl_name = 't1';
85
+ }
86
+ } {{CREATE TABLE t1(a, b, c)}}
87
+ do_test alter4-1.5 {
88
+ execsql {
89
+ ALTER TABLE t1 ADD d CHECK (a>d);
90
+ SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1';
91
+ }
92
+ } {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}}
93
+ ifcapable foreignkey {
94
+ do_test alter4-1.6 {
95
+ execsql {
96
+ CREATE TEMP TABLE t2(a, b, UNIQUE(a, b));
97
+ ALTER TABLE t2 ADD c REFERENCES t1(c) ;
98
+ SELECT sql FROM sqlite_temp_master
99
+ WHERE tbl_name = 't2' AND type = 'table';
100
+ }
101
+ } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}}
102
+ }
103
+ do_test alter4-1.7 {
104
+ execsql {
105
+ CREATE TEMPORARY TABLE t3(a, b, UNIQUE(a, b));
106
+ ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20);
107
+ SELECT sql FROM sqlite_temp_master
108
+ WHERE tbl_name = 't3' AND type = 'table';
109
+ }
110
+ } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}}
111
+ do_test alter4-1.99 {
112
+ catchsql {
113
+ # May not exist if foriegn-keys are omitted at compile time.
114
+ DROP TABLE t2;
115
+ }
116
+ execsql {
117
+ DROP TABLE abc;
118
+ DROP TABLE t1;
119
+ DROP TABLE t3;
120
+ }
121
+ } {}
122
+
123
+ do_test alter4-2.1 {
124
+ execsql {
125
+ CREATE TABLE temp.t1(a, b);
126
+ INSERT INTO t1 VALUES(1,2);
127
+ }
128
+ catchsql {
129
+ ALTER TABLE t1 ADD c PRIMARY KEY;
130
+ }
131
+ } {1 {Cannot add a PRIMARY KEY column}}
132
+ do_test alter4-2.2 {
133
+ catchsql {
134
+ ALTER TABLE t1 ADD c UNIQUE
135
+ }
136
+ } {1 {Cannot add a UNIQUE column}}
137
+ do_test alter4-2.3 {
138
+ catchsql {
139
+ ALTER TABLE t1 ADD b VARCHAR(10)
140
+ }
141
+ } {1 {duplicate column name: b}}
142
+ do_test alter4-2.3 {
143
+ catchsql {
144
+ ALTER TABLE t1 ADD c NOT NULL;
145
+ }
146
+ } {1 {Cannot add a NOT NULL column with default value NULL}}
147
+ do_test alter4-2.4 {
148
+ catchsql {
149
+ ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
150
+ }
151
+ } {0 {}}
152
+ ifcapable view {
153
+ do_test alter4-2.5 {
154
+ execsql {
155
+ CREATE TEMPORARY VIEW v1 AS SELECT * FROM t1;
156
+ }
157
+ catchsql {
158
+ alter table v1 add column d;
159
+ }
160
+ } {1 {Cannot add a column to a view}}
161
+ }
162
+ do_test alter4-2.6 {
163
+ catchsql {
164
+ alter table t1 add column d DEFAULT CURRENT_TIME;
165
+ }
166
+ } {1 {Cannot add a column with non-constant default}}
167
+ do_test alter4-2.7 {
168
+ catchsql {
169
+ alter table t1 add column d default (-5+1);
170
+ }
171
+ } {1 {Cannot add a column with non-constant default}}
172
+ do_test alter4-2.99 {
173
+ execsql {
174
+ DROP TABLE t1;
175
+ }
176
+ } {}
177
+
178
+ do_test alter4-3.1 {
179
+ execsql {
180
+ CREATE TEMP TABLE t1(a, b);
181
+ INSERT INTO t1 VALUES(1, 100);
182
+ INSERT INTO t1 VALUES(2, 300);
183
+ SELECT * FROM t1;
184
+ }
185
+ } {1 100 2 300}
186
+ do_test alter4-3.1 {
187
+ execsql {
188
+ PRAGMA schema_version = 10;
189
+ }
190
+ } {}
191
+ do_test alter4-3.2 {
192
+ execsql {
193
+ ALTER TABLE t1 ADD c;
194
+ SELECT * FROM t1;
195
+ }
196
+ } {1 100 {} 2 300 {}}
197
+ ifcapable schema_version {
198
+ do_test alter4-3.4 {
199
+ execsql {
200
+ PRAGMA schema_version;
201
+ }
202
+ } {10}
203
+ }
204
+
205
+ do_test alter4-4.1 {
206
+ db close
207
+ forcedelete test.db
208
+ set ::DB [sqlite3 db test.db]
209
+ execsql {
210
+ CREATE TEMP TABLE t1(a, b);
211
+ INSERT INTO t1 VALUES(1, 100);
212
+ INSERT INTO t1 VALUES(2, 300);
213
+ SELECT * FROM t1;
214
+ }
215
+ } {1 100 2 300}
216
+ do_test alter4-4.1 {
217
+ execsql {
218
+ PRAGMA schema_version = 20;
219
+ }
220
+ } {}
221
+ do_test alter4-4.2 {
222
+ execsql {
223
+ ALTER TABLE t1 ADD c DEFAULT 'hello world';
224
+ SELECT * FROM t1;
225
+ }
226
+ } {1 100 {hello world} 2 300 {hello world}}
227
+ ifcapable schema_version {
228
+ do_test alter4-4.4 {
229
+ execsql {
230
+ PRAGMA schema_version;
231
+ }
232
+ } {20}
233
+ }
234
+ do_test alter4-4.99 {
235
+ execsql {
236
+ DROP TABLE t1;
237
+ }
238
+ } {}
239
+
240
+ ifcapable attach {
241
+ do_test alter4-5.1 {
242
+ forcedelete test2.db
243
+ forcedelete test2.db-journal
244
+ execsql {
245
+ CREATE TEMP TABLE t1(a, b);
246
+ INSERT INTO t1 VALUES(1, 'one');
247
+ INSERT INTO t1 VALUES(2, 'two');
248
+ ATTACH 'test2.db' AS aux;
249
+ CREATE TABLE aux.t1 AS SELECT * FROM t1;
250
+ PRAGMA aux.schema_version = 30;
251
+ SELECT sql FROM aux.sqlite_master;
252
+ }
253
+ } {{CREATE TABLE t1(a,b)}}
254
+ do_test alter4-5.2 {
255
+ execsql {
256
+ ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
257
+ SELECT sql FROM aux.sqlite_master;
258
+ }
259
+ } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
260
+ do_test alter4-5.3 {
261
+ execsql {
262
+ SELECT * FROM aux.t1;
263
+ }
264
+ } {1 one {} 2 two {}}
265
+ ifcapable schema_version {
266
+ do_test alter4-5.4 {
267
+ execsql {
268
+ PRAGMA aux.schema_version;
269
+ }
270
+ } {31}
271
+ }
272
+ do_test alter4-5.6 {
273
+ execsql {
274
+ ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
275
+ SELECT sql FROM aux.sqlite_master;
276
+ }
277
+ } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
278
+ do_test alter4-5.7 {
279
+ execsql {
280
+ SELECT * FROM aux.t1;
281
+ }
282
+ } {1 one {} 1000 2 two {} 1000}
283
+ ifcapable schema_version {
284
+ do_test alter4-5.8 {
285
+ execsql {
286
+ PRAGMA aux.schema_version;
287
+ }
288
+ } {32}
289
+ }
290
+ do_test alter4-5.9 {
291
+ execsql {
292
+ SELECT * FROM t1;
293
+ }
294
+ } {1 one 2 two}
295
+ do_test alter4-5.99 {
296
+ execsql {
297
+ DROP TABLE aux.t1;
298
+ DROP TABLE t1;
299
+ }
300
+ } {}
301
+ }
302
+
303
+ #----------------------------------------------------------------
304
+ # Test that the table schema is correctly reloaded when a column
305
+ # is added to a table.
306
+ #
307
+ ifcapable trigger&&tempdb {
308
+ do_test alter4-6.1 {
309
+ execsql {
310
+ CREATE TEMP TABLE t1(a, b);
311
+ CREATE TEMP TABLE log(trig, a, b);
312
+
313
+ CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
314
+ INSERT INTO log VALUES('a', new.a, new.b);
315
+ END;
316
+ CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
317
+ INSERT INTO log VALUES('b', new.a, new.b);
318
+ END;
319
+
320
+ INSERT INTO t1 VALUES(1, 2);
321
+ SELECT * FROM log ORDER BY trig, a, b;
322
+ }
323
+ } {a 1 2 b 1 2}
324
+ do_test alter4-6.2 {
325
+ execsql {
326
+ ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
327
+ INSERT INTO t1(a, b) VALUES(3, 4);
328
+ SELECT * FROM log ORDER BY trig, a, b;
329
+ }
330
+ } {a 1 2 a 3 4 b 1 2 b 3 4}
331
+ }
332
+
333
+ # Ticket #1183 - Make sure adding columns to large tables does not cause
334
+ # memory corruption (as was the case before this bug was fixed).
335
+ do_test alter4-8.1 {
336
+ execsql {
337
+ CREATE TEMP TABLE t4(c1);
338
+ }
339
+ } {}
340
+ set ::sql ""
341
+ do_test alter4-8.2 {
342
+ set cols c1
343
+ for {set i 2} {$i < 100} {incr i} {
344
+ execsql "
345
+ ALTER TABLE t4 ADD c$i
346
+ "
347
+ lappend cols c$i
348
+ }
349
+ set ::sql "CREATE TABLE t4([join $cols {, }])"
350
+ list
351
+ } {}
352
+ do_test alter4-8.2 {
353
+ execsql {
354
+ SELECT sql FROM sqlite_temp_master WHERE name = 't4';
355
+ }
356
+ } [list $::sql]
357
+
358
+
359
+ # Test that a default value equal to -1 multipied by the smallest possible
360
+ # 64-bit integer is correctly converted to a real.
361
+ do_execsql_test alter4-9.1 {
362
+ CREATE TABLE t5(
363
+ a INTEGER DEFAULT -9223372036854775808,
364
+ b INTEGER DEFAULT (-(-9223372036854775808))
365
+ );
366
+ INSERT INTO t5 DEFAULT VALUES;
367
+ }
368
+
369
+ do_execsql_test alter4-9.2 { SELECT typeof(a), a, typeof(b), b FROM t5; } {
370
+ integer -9223372036854775808
371
+ real 9.22337203685478e+18
372
+ }
373
+
374
+ do_execsql_test alter4-9.3 {
375
+ ALTER TABLE t5 ADD COLUMN c INTEGER DEFAULT (-(-9223372036854775808));
376
+ SELECT typeof(c), c FROM t5;
377
+ } {real 9.22337203685478e+18}
378
+
379
+ # Confirm that doing an ALTER TABLE on a legacy format database
380
+ # does not corrupt DESC indexes.
381
+ #
382
+ # Ticket https://www.sqlite.org/src/tktview/f68bf68513a1c
383
+ #
384
+ do_test alter4-10.1 {
385
+ db close
386
+ sqlite3 db :memory:
387
+ sqlite3_db_config db LEGACY_FILE_FORMAT 1
388
+ db eval {
389
+ CREATE TABLE t1(a,b,c);
390
+ CREATE INDEX t1a ON t1(a DESC);
391
+ INSERT INTO t1 VALUES(1,2,3);
392
+ INSERT INTO t1 VALUES(2,3,4);
393
+ ALTER TABLE t1 ADD COLUMN d;
394
+ PRAGMA integrity_check;
395
+ }
396
+ } {ok}
397
+
398
+ reset_db
399
+ do_execsql_test alter4-11.0 {
400
+ CREATE TABLE t1(c INTEGER PRIMARY KEY, d);
401
+ INSERT INTO t1(c,d) VALUES(1,2);
402
+ PRAGMA foreign_keys = on;
403
+ ALTER TABLE t1 ADD COLUMN e;
404
+ }
405
+
406
+ do_execsql_test alter4-11.1 {
407
+ ALTER TABLE t1 ADD COLUMN f REFERENCES t1;
408
+ }
409
+
410
+ do_catchsql_test alter4-11.2 {
411
+ ALTER TABLE t1 ADD COLUMN g REFERENCES t1 DEFAULT 4;
412
+ } {1 {Cannot add a REFERENCES column with non-NULL default value}}
413
+
414
+ do_catchsql_test alter4-11.3 {
415
+ ALTER TABLE t2 ADD COLUMN g;
416
+ } {1 {no such table: t2}}
417
+
418
+ ifcapable fts5 {
419
+ do_execsql_test alter4-11.4 {
420
+ CREATE VIRTUAL TABLE fff USING fts5(f);
421
+ }
422
+ do_catchsql_test alter4-11.2 {
423
+ ALTER TABLE fff ADD COLUMN g;
424
+ } {1 {virtual tables may not be altered}}
425
+ }
426
+
427
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/alterauth.test ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2018 September 2
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+
15
+ source $testdir/tester.tcl
16
+
17
+ # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18
+ ifcapable !altertable {
19
+ finish_test
20
+ return
21
+ }
22
+ set testprefix alterauth
23
+
24
+ set ::auth [list]
25
+ proc xAuth {type args} {
26
+ if {$type == "SQLITE_ALTER_TABLE"} {
27
+ lappend ::auth [concat $type [lrange $args 0 3]]
28
+ }
29
+ return SQLITE_OK
30
+ }
31
+ db auth xAuth
32
+
33
+ do_execsql_test 1.0 { CREATE TABLE t1(a, b, c); }
34
+
35
+ do_test 1.1 {
36
+ set ::auth [list]
37
+ execsql { ALTER TABLE t1 RENAME TO t2 }
38
+ set ::auth
39
+ } {{SQLITE_ALTER_TABLE main t1 {} {}}}
40
+
41
+ do_test 1.2 {
42
+ set ::auth [list]
43
+ execsql { ALTER TABLE t2 RENAME c TO ccc }
44
+ set ::auth
45
+ } {{SQLITE_ALTER_TABLE main t2 {} {}}}
46
+
47
+ do_test 1.3 {
48
+ set ::auth [list]
49
+ execsql { ALTER TABLE t2 ADD COLUMN d }
50
+ set ::auth
51
+ } {{SQLITE_ALTER_TABLE main t2 {} {}}}
52
+
53
+ proc xAuth {type args} {
54
+ if {$type == "SQLITE_ALTER_TABLE"} {
55
+ return SQLITE_DENY
56
+ }
57
+ return SQLITE_OK
58
+ }
59
+
60
+ do_test 2.1 {
61
+ catchsql { ALTER TABLE t2 RENAME TO t3 }
62
+ } {1 {not authorized}}
63
+
64
+ do_test 2.2 {
65
+ catchsql { ALTER TABLE t2 RENAME d TO ddd }
66
+ } {1 {not authorized}}
67
+
68
+ do_test 2.3 {
69
+ catchsql { ALTER TABLE t2 ADD COLUMN e }
70
+ } {1 {not authorized}}
71
+
72
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/altercol.test ADDED
@@ -0,0 +1,949 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2009 February 2
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing that SQLite can handle a subtle
13
+ # file format change that may be used in the future to implement
14
+ # "ALTER TABLE ... RENAME COLUMN ... TO".
15
+ #
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+ set testprefix altercol
20
+
21
+ # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
22
+ ifcapable !altertable {
23
+ finish_test
24
+ return
25
+ }
26
+
27
+ # Drop all the tables and views in the 'main' database of database connect
28
+ # [db]. Sort the objects by name before dropping them.
29
+ #
30
+ proc drop_all_tables_and_views {db} {
31
+ set SQL {
32
+ SELECT name, type FROM sqlite_master
33
+ WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
34
+ ORDER BY 1
35
+ }
36
+ foreach {z t} [db eval $SQL] {
37
+ db eval "DROP $t $z"
38
+ }
39
+ }
40
+
41
+ foreach {tn before after} {
42
+ 1 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB)}
43
+ {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB)}
44
+
45
+ 2 {CREATE TABLE t1(a INTEGER, x TEXT, "b" BLOB)}
46
+ {CREATE TABLE t1(a INTEGER, x TEXT, "d" BLOB)}
47
+
48
+ 3 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(b!=''))}
49
+ {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(d!=''))}
50
+
51
+ 4 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(t1.b!=''))}
52
+ {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(t1.d!=''))}
53
+
54
+ 5 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK( coalesce(b,c) ))}
55
+ {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK( coalesce(d,c) ))}
56
+
57
+ 6 {CREATE TABLE t1(a INTEGER, "b"TEXT, c BLOB, CHECK( coalesce(b,c) ))}
58
+ {CREATE TABLE t1(a INTEGER, "d"TEXT, c BLOB, CHECK( coalesce(d,c) ))}
59
+
60
+ 7 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b, c))}
61
+ {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d, c))}
62
+
63
+ 8 {CREATE TABLE t1(a INTEGER, b TEXT PRIMARY KEY, c BLOB)}
64
+ {CREATE TABLE t1(a INTEGER, d TEXT PRIMARY KEY, c BLOB)}
65
+
66
+ 9 {CREATE TABLE t1(a, b TEXT, c, PRIMARY KEY(a, b), UNIQUE("B"))}
67
+ {CREATE TABLE t1(a, d TEXT, c, PRIMARY KEY(a, d), UNIQUE("d"))}
68
+
69
+ 10 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(a, c)}
70
+ {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(a, c)}}
71
+
72
+ 11 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b, c)}
73
+ {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d, c)}}
74
+
75
+ 12 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b+b+b+b, c) WHERE b>0}
76
+ {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d+d+d+d, c) WHERE d>0}}
77
+
78
+ 13 {CREATE TABLE t1(a, b, c, FOREIGN KEY (b) REFERENCES t2)}
79
+ {CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)}
80
+
81
+ 14 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b))}
82
+ {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d))}
83
+
84
+ 15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
85
+ {CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}
86
+
87
+ 16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
88
+ {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}
89
+
90
+ 17 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (b) REFERENCES t2)}
91
+ {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (d) REFERENCES t2)}
92
+
93
+ } {
94
+ reset_db
95
+ do_execsql_test 1.$tn.0 $before
96
+
97
+ do_execsql_test 1.$tn.1 {
98
+ INSERT INTO t1 VALUES(1, 2, 3);
99
+ }
100
+
101
+ do_execsql_test 1.$tn.2 {
102
+ ALTER TABLE t1 RENAME COLUMN b TO d;
103
+ }
104
+
105
+ do_execsql_test 1.$tn.3 {
106
+ SELECT * FROM t1;
107
+ } {1 2 3}
108
+
109
+ if {[string first INDEX $before]>0} {
110
+ set res $after
111
+ } else {
112
+ set res [list $after]
113
+ }
114
+ do_execsql_test 1.$tn.4 {
115
+ SELECT sql FROM sqlite_master WHERE tbl_name='t1' AND sql!=''
116
+ } $res
117
+ }
118
+
119
+ #-------------------------------------------------------------------------
120
+ #
121
+ do_execsql_test 2.0 {
122
+ CREATE TABLE t3(a, b, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (b, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4);
123
+ }
124
+
125
+ sqlite3 db2 test.db
126
+ do_execsql_test -db db2 2.1 { SELECT b FROM t3 }
127
+
128
+ do_execsql_test 2.2 {
129
+ ALTER TABLE t3 RENAME b TO biglongname;
130
+ SELECT sql FROM sqlite_master WHERE name='t3';
131
+ } {{CREATE TABLE t3(a, biglongname, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (biglongname, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4)}}
132
+
133
+ do_execsql_test -db db2 2.3 { SELECT biglongname FROM t3 }
134
+
135
+ #-------------------------------------------------------------------------
136
+ #
137
+ do_execsql_test 3.0 {
138
+ CREATE TABLE t4(x, y, z);
139
+ CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.y<0 BEGIN
140
+ SELECT x, y, z FROM t4;
141
+ DELETE FROM t4 WHERE y=32;
142
+ UPDATE t4 SET x=y+1, y=0 WHERE y=32;
143
+ INSERT INTO t4(x, y, z) SELECT 4, 5, 6 WHERE 0;
144
+ END;
145
+ INSERT INTO t4 VALUES(3, 2, 1);
146
+ }
147
+
148
+ do_execsql_test 3.1 {
149
+ ALTER TABLE t4 RENAME y TO abc;
150
+ SELECT sql FROM sqlite_master WHERE name='t4';
151
+ } {{CREATE TABLE t4(x, abc, z)}}
152
+
153
+ do_execsql_test 3.2 {
154
+ SELECT * FROM t4;
155
+ } {3 2 1}
156
+
157
+ do_execsql_test 3.3 { INSERT INTO t4 VALUES(6, 5, 4); } {}
158
+
159
+ do_execsql_test 3.4 { SELECT sql FROM sqlite_master WHERE type='trigger' } {
160
+ {CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.abc<0 BEGIN
161
+ SELECT x, abc, z FROM t4;
162
+ DELETE FROM t4 WHERE abc=32;
163
+ UPDATE t4 SET x=abc+1, abc=0 WHERE abc=32;
164
+ INSERT INTO t4(x, abc, z) SELECT 4, 5, 6 WHERE 0;
165
+ END}
166
+ }
167
+
168
+ #-------------------------------------------------------------------------
169
+ #
170
+ do_execsql_test 4.0 {
171
+ CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, d));
172
+ CREATE TABLE p1(c, d, PRIMARY KEY(c, d));
173
+ PRAGMA foreign_keys = 1;
174
+ INSERT INTO p1 VALUES(1, 2);
175
+ INSERT INTO p1 VALUES(3, 4);
176
+ }
177
+
178
+ do_execsql_test 4.1 {
179
+ ALTER TABLE p1 RENAME d TO "silly name";
180
+ SELECT sql FROM sqlite_master WHERE name IN ('c1', 'p1');
181
+ } {
182
+ {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "silly name"))}
183
+ {CREATE TABLE p1(c, "silly name", PRIMARY KEY(c, "silly name"))}
184
+ }
185
+
186
+ do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); }
187
+
188
+ do_execsql_test 4.3 {
189
+ CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1);
190
+ }
191
+
192
+ do_execsql_test 4.4 {
193
+ ALTER TABLE p1 RENAME "silly name" TO reasonable;
194
+ SELECT sql FROM sqlite_master WHERE name IN ('c1', 'c2', 'p1');
195
+ } {
196
+ {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "reasonable"))}
197
+ {CREATE TABLE p1(c, "reasonable", PRIMARY KEY(c, "reasonable"))}
198
+ {CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1)}
199
+ }
200
+
201
+ #-------------------------------------------------------------------------
202
+
203
+ do_execsql_test 5.0 {
204
+ CREATE TABLE t5(a, b, c);
205
+ CREATE INDEX t5a ON t5(a);
206
+ INSERT INTO t5 VALUES(1, 2, 3), (4, 5, 6);
207
+ ANALYZE;
208
+ }
209
+
210
+ do_execsql_test 5.1 {
211
+ ALTER TABLE t5 RENAME b TO big;
212
+ SELECT big FROM t5;
213
+ } {2 5}
214
+
215
+ do_catchsql_test 6.1 {
216
+ ALTER TABLE sqlite_stat1 RENAME tbl TO thetable;
217
+ } {1 {table sqlite_stat1 may not be altered}}
218
+
219
+ #-------------------------------------------------------------------------
220
+ #
221
+ do_execsql_test 6.0 {
222
+ CREATE TABLE blob(
223
+ rid INTEGER PRIMARY KEY,
224
+ rcvid INTEGER,
225
+ size INTEGER,
226
+ uuid TEXT UNIQUE NOT NULL,
227
+ content BLOB,
228
+ CHECK( length(uuid)>=40 AND rid>0 )
229
+ );
230
+ }
231
+
232
+ do_execsql_test 6.1 {
233
+ ALTER TABLE "blob" RENAME COLUMN "rid" TO "a1";
234
+ }
235
+
236
+ do_catchsql_test 6.2 {
237
+ ALTER TABLE "blob" RENAME COLUMN "a1" TO [where];
238
+ } {0 {}}
239
+
240
+ do_execsql_test 6.3 {
241
+ SELECT "where" FROM blob;
242
+ } {}
243
+
244
+ #-------------------------------------------------------------------------
245
+ # Triggers.
246
+ #
247
+ db close
248
+ db2 close
249
+ reset_db
250
+ do_execsql_test 7.0 {
251
+ CREATE TABLE c(x);
252
+ INSERT INTO c VALUES(0);
253
+ CREATE TABLE t6("col a", "col b", "col c");
254
+ CREATE TRIGGER zzz AFTER UPDATE OF "col a", "col c" ON t6 BEGIN
255
+ UPDATE c SET x=x+1;
256
+ END;
257
+ }
258
+
259
+ do_execsql_test 7.1.1 {
260
+ INSERT INTO t6 VALUES(0, 0, 0);
261
+ UPDATE t6 SET "col c" = 1;
262
+ SELECT * FROM c;
263
+ } {1}
264
+
265
+ do_execsql_test 7.1.2 {
266
+ ALTER TABLE t6 RENAME "col c" TO "col 3";
267
+ }
268
+
269
+ do_execsql_test 7.1.3 {
270
+ UPDATE t6 SET "col 3" = 0;
271
+ SELECT * FROM c;
272
+ } {2}
273
+
274
+ #-------------------------------------------------------------------------
275
+ # Views.
276
+ #
277
+ reset_db
278
+ do_execsql_test 8.0 {
279
+ CREATE TABLE a1(x INTEGER, y TEXT, z BLOB, PRIMARY KEY(x));
280
+ CREATE TABLE a2(a, b, c);
281
+ CREATE VIEW v1 AS SELECT x, y, z FROM a1;
282
+ }
283
+
284
+ do_execsql_test 8.1 {
285
+ ALTER TABLE a1 RENAME y TO yyy;
286
+ SELECT sql FROM sqlite_master WHERE type='view';
287
+ } {{CREATE VIEW v1 AS SELECT x, yyy, z FROM a1}}
288
+
289
+ do_execsql_test 8.2.1 {
290
+ DROP VIEW v1;
291
+ CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2;
292
+ } {}
293
+ do_execsql_test 8.2.2 {
294
+ ALTER TABLE a1 RENAME x TO xxx;
295
+ }
296
+ do_execsql_test 8.2.3 {
297
+ SELECT sql FROM sqlite_master WHERE type='view';
298
+ } {{CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2}}
299
+
300
+ do_execsql_test 8.3.1 {
301
+ DROP TABLE a2;
302
+ DROP VIEW v2;
303
+ CREATE TABLE a2(a INTEGER PRIMARY KEY, b, c);
304
+ CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2;
305
+ } {}
306
+ do_execsql_test 8.3.2 {
307
+ ALTER TABLE a1 RENAME xxx TO x;
308
+ }
309
+ do_execsql_test 8.3.3 {
310
+ SELECT sql FROM sqlite_master WHERE type='view';
311
+ } {{CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2}}
312
+
313
+ do_execsql_test 8.4.0 {
314
+ CREATE TABLE b1(a, b, c);
315
+ CREATE TABLE b2(x, y, z);
316
+ }
317
+
318
+ do_execsql_test 8.4.1 {
319
+ CREATE VIEW vvv AS SELECT c+c || coalesce(c, c) FROM b1, b2 WHERE x=c GROUP BY c HAVING c>0;
320
+ ALTER TABLE b1 RENAME c TO "a;b";
321
+ SELECT sql FROM sqlite_master WHERE name='vvv';
322
+ } {{CREATE VIEW vvv AS SELECT "a;b"+"a;b" || coalesce("a;b", "a;b") FROM b1, b2 WHERE x="a;b" GROUP BY "a;b" HAVING "a;b">0}}
323
+
324
+ do_execsql_test 8.4.2 {
325
+ CREATE VIEW www AS SELECT b FROM b1 UNION ALL SELECT y FROM b2;
326
+ ALTER TABLE b1 RENAME b TO bbb;
327
+ SELECT sql FROM sqlite_master WHERE name='www';
328
+ } {{CREATE VIEW www AS SELECT bbb FROM b1 UNION ALL SELECT y FROM b2}}
329
+
330
+ db collate nocase {string compare}
331
+
332
+ do_execsql_test 8.4.3 {
333
+ CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT x FROM b2 ORDER BY 1 COLLATE nocase;
334
+ }
335
+
336
+ do_execsql_test 8.4.4 {
337
+ ALTER TABLE b2 RENAME x TO hello;
338
+ SELECT sql FROM sqlite_master WHERE name='xxx';
339
+ } {{CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT hello FROM b2 ORDER BY 1 COLLATE nocase}}
340
+
341
+ do_catchsql_test 8.4.5 {
342
+ CREATE VIEW zzz AS SELECT george, ringo FROM b1;
343
+ ALTER TABLE b1 RENAME a TO aaa;
344
+ } {1 {error in view zzz: no such column: george}}
345
+
346
+ do_execsql_test 8.5 {
347
+ DROP VIEW zzz;
348
+ CREATE TABLE t5(a TEXT, b INT);
349
+ INSERT INTO t5(a,b) VALUES('aaa',7),('bbb',3),('ccc',4);
350
+ CREATE VIEW vt5(x) AS SELECT group_concat(a ORDER BY b) FROM t5;
351
+ SELECT x FROM vt5;
352
+ } {bbb,ccc,aaa}
353
+ do_execsql_test 8.5.1 {
354
+ ALTER TABLE t5 RENAME COLUMN b TO bbb;
355
+ SELECT sql FROM sqlite_schema WHERE name='vt5';
356
+ } {{CREATE VIEW vt5(x) AS SELECT group_concat(a ORDER BY bbb) FROM t5}}
357
+ do_execsql_test 8.5.2 {
358
+ SELECT x FROM vt5;
359
+ } {bbb,ccc,aaa}
360
+
361
+ #-------------------------------------------------------------------------
362
+ # More triggers.
363
+ #
364
+ proc do_rename_column_test {tn old new lSchema} {
365
+ for {set i 0} {$i < 2} {incr i} {
366
+ drop_all_tables_and_views db
367
+
368
+ set lSorted [list]
369
+ foreach sql $lSchema {
370
+ execsql $sql
371
+ lappend lSorted [string trim $sql]
372
+ }
373
+ set lSorted [lsort $lSorted]
374
+
375
+ do_execsql_test $tn.$i.1 {
376
+ SELECT sql FROM sqlite_master WHERE sql!='' ORDER BY 1
377
+ } $lSorted
378
+
379
+ if {$i==1} {
380
+ db close
381
+ sqlite3 db test.db
382
+ }
383
+
384
+ do_execsql_test $tn.$i.2 "ALTER TABLE t1 RENAME $old TO $new"
385
+
386
+ do_execsql_test $tn.$i.3 {
387
+ SELECT sql FROM sqlite_master ORDER BY 1
388
+ } [string map [list $old $new] $lSorted]
389
+ }
390
+ }
391
+
392
+ foreach {tn old new lSchema} {
393
+ 1 _x_ _xxx_ {
394
+ { CREATE TABLE t1(a, b, _x_) }
395
+ { CREATE TRIGGER AFTER INSERT ON t1 BEGIN
396
+ SELECT _x_ FROM t1;
397
+ END }
398
+ }
399
+
400
+ 2 _x_ _xxx_ {
401
+ { CREATE TABLE t1(a, b, _x_) }
402
+ { CREATE TABLE t2(c, d, e) }
403
+ { CREATE TRIGGER ttt AFTER INSERT ON t2 BEGIN
404
+ SELECT _x_ FROM t1;
405
+ END }
406
+ }
407
+
408
+ 3 _x_ _xxx_ {
409
+ { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
410
+ { CREATE TABLE t2(c, d, e) }
411
+ { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN
412
+ INSERT INTO t2 VALUES(new.a, new.b, new._x_);
413
+ END }
414
+ }
415
+
416
+ 4 _x_ _xxx_ {
417
+ { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
418
+ { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN
419
+ INSERT INTO t1 VALUES(new.a, new.b, new._x_)
420
+ ON CONFLICT (_x_) WHERE _x_>10 DO UPDATE SET _x_ = _x_+1;
421
+ END }
422
+ }
423
+
424
+ 4 _x_ _xxx_ {
425
+ { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
426
+ { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN
427
+ INSERT INTO t1 VALUES(new.a, new.b, new._x_)
428
+ ON CONFLICT (_x_) WHERE _x_>10 DO NOTHING;
429
+ END }
430
+ }
431
+ } {
432
+ do_rename_column_test 9.$tn $old $new $lSchema
433
+ }
434
+
435
+ #-------------------------------------------------------------------------
436
+ # Test that views can be edited even if there are missing collation
437
+ # sequences or user defined functions.
438
+ #
439
+ reset_db
440
+
441
+ ifcapable vtab {
442
+ foreach {tn old new lSchema} {
443
+ 1 _x_ _xxx_ {
444
+ { CREATE TABLE t1(a, b, _x_) }
445
+ { CREATE VIEW s1 AS SELECT a, b, _x_ FROM t1 WHERE _x_='abc' COLLATE xyz }
446
+ }
447
+
448
+ 2 _x_ _xxx_ {
449
+ { CREATE TABLE t1(a, b, _x_) }
450
+ { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE scalar(_x_) }
451
+ }
452
+
453
+ 3 _x_ _xxx_ {
454
+ { CREATE TABLE t1(a, b, _x_) }
455
+ { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE _x_ = unicode(1, 2, 3) }
456
+ }
457
+
458
+ 4 _x_ _xxx_ {
459
+ { CREATE TABLE t1(a, b, _x_) }
460
+ { CREATE VIRTUAL TABLE e1 USING echo(t1) }
461
+ }
462
+ } {
463
+ register_echo_module db
464
+ do_rename_column_test 10.$tn $old $new $lSchema
465
+ }
466
+
467
+ #--------------------------------------------------------------------------
468
+ # Test that if a view or trigger refers to a virtual table for which the
469
+ # module is not available, RENAME COLUMN cannot proceed.
470
+ #
471
+ reset_db
472
+ register_echo_module db
473
+ do_execsql_test 11.0 {
474
+ CREATE TABLE x1(a, b, c);
475
+ CREATE VIRTUAL TABLE e1 USING echo(x1);
476
+ }
477
+ db close
478
+ sqlite3 db test.db
479
+
480
+ do_execsql_test 11.1 {
481
+ ALTER TABLE x1 RENAME b TO bbb;
482
+ SELECT sql FROM sqlite_master;
483
+ } { {CREATE TABLE x1(a, bbb, c)} {CREATE VIRTUAL TABLE e1 USING echo(x1)} }
484
+
485
+ do_execsql_test 11.2 {
486
+ CREATE VIEW v1 AS SELECT e1.*, x1.c FROM e1, x1;
487
+ }
488
+
489
+ do_catchsql_test 11.3 {
490
+ ALTER TABLE x1 RENAME c TO ccc;
491
+ } {1 {error in view v1: no such module: echo}}
492
+ }
493
+
494
+ #-------------------------------------------------------------------------
495
+ # Test some error conditions:
496
+ #
497
+ # 1. Renaming a column of a system table,
498
+ # 2. Renaming a column of a VIEW,
499
+ # 3. Renaming a column of a virtual table.
500
+ # 4. Renaming a column that does not exist.
501
+ # 5. Renaming a column of a table that does not exist.
502
+ #
503
+ reset_db
504
+ do_execsql_test 12.1.1 {
505
+ CREATE TABLE t1(a, b);
506
+ CREATE INDEX t1a ON t1(a);
507
+ INSERT INTO t1 VALUES(1, 1), (2, 2), (3, 4);
508
+ ANALYZE;
509
+ }
510
+ do_catchsql_test 12.1.2 {
511
+ ALTER TABLE sqlite_stat1 RENAME idx TO theindex;
512
+ } {1 {table sqlite_stat1 may not be altered}}
513
+ do_execsql_test 12.1.3 {
514
+ SELECT sql FROM sqlite_master WHERE tbl_name = 'sqlite_stat1'
515
+ } {{CREATE TABLE sqlite_stat1(tbl,idx,stat)}}
516
+
517
+ do_execsql_test 12.2.1 {
518
+ CREATE VIEW v1 AS SELECT * FROM t1;
519
+ CREATE VIEW v2(c, d) AS SELECT * FROM t1;
520
+ }
521
+ do_catchsql_test 12.2.2 {
522
+ ALTER TABLE v1 RENAME a TO z;
523
+ } {1 {cannot rename columns of view "v1"}}
524
+ do_catchsql_test 12.2.3 {
525
+ ALTER TABLE v2 RENAME c TO y;
526
+ } {1 {cannot rename columns of view "v2"}}
527
+
528
+ ifcapable fts5 {
529
+ do_execsql_test 12.3.1 {
530
+ CREATE VIRTUAL TABLE ft USING fts5(a, b, c);
531
+ }
532
+ do_catchsql_test 12.3.2 {
533
+ ALTER TABLE ft RENAME a TO z;
534
+ } {1 {cannot rename columns of virtual table "ft"}}
535
+ }
536
+
537
+ do_execsql_test 12.4.1 {
538
+ CREATE TABLE t2(x, y, z);
539
+ }
540
+ do_catchsql_test 12.4.2 {
541
+ ALTER TABLE t2 RENAME COLUMN a TO b;
542
+ } {1 {no such column: "a"}}
543
+
544
+ do_catchsql_test 12.5.1 {
545
+ ALTER TABLE t3 RENAME COLUMN a TO b;
546
+ } {1 {no such table: t3}}
547
+
548
+ #-------------------------------------------------------------------------
549
+ # Test the effect of some parse/resolve errors.
550
+ #
551
+ reset_db
552
+ do_execsql_test 13.1.1 {
553
+ CREATE TABLE x1(i INTEGER, t TEXT UNIQUE);
554
+ CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
555
+ SELECT * FROM nosuchtable;
556
+ END;
557
+ }
558
+
559
+ do_catchsql_test 13.1.2 {
560
+ ALTER TABLE x1 RENAME COLUMN t TO ttt;
561
+ } {1 {error in trigger tr1: no such table: main.nosuchtable}}
562
+
563
+ do_execsql_test 13.1.3 {
564
+ DROP TRIGGER tr1;
565
+ CREATE INDEX x1i ON x1(i);
566
+ SELECT sql FROM sqlite_master WHERE name='x1i';
567
+ } {{CREATE INDEX x1i ON x1(i)}}
568
+
569
+ sqlite3_db_config db DEFENSIVE 0
570
+ do_execsql_test 13.1.4 {
571
+ PRAGMA writable_schema = ON;
572
+ UPDATE sqlite_master SET sql = 'CREATE INDEX x1i ON x1(j)' WHERE name='x1i';
573
+ PRAGMA writable_schema = OFF;
574
+ } {}
575
+
576
+ do_catchsql_test 13.1.5 {
577
+ ALTER TABLE x1 RENAME COLUMN t TO ttt;
578
+ } {1 {error in index x1i: no such column: j}}
579
+
580
+ do_execsql_test 13.1.6 {
581
+ PRAGMA writable_schema = ON;
582
+ UPDATE sqlite_master SET sql = '' WHERE name='x1i';
583
+ PRAGMA writable_schema = OFF;
584
+ } {}
585
+
586
+ do_catchsql_test 13.1.7 {
587
+ ALTER TABLE x1 RENAME COLUMN t TO ttt;
588
+ } {1 {error in index x1i: }}
589
+
590
+ do_execsql_test 13.1.8 {
591
+ PRAGMA writable_schema = ON;
592
+ DELETE FROM sqlite_master WHERE name = 'x1i';
593
+ PRAGMA writable_schema = OFF;
594
+ }
595
+
596
+ do_execsql_test 13.2.0 {
597
+ CREATE TABLE data(x UNIQUE, y, z);
598
+ }
599
+ foreach {tn trigger error} {
600
+ 1 {
601
+ CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
602
+ UPDATE data SET x=x+1 WHERE zzz=new.i;
603
+ END;
604
+ } {no such column: zzz}
605
+
606
+ 2 {
607
+ CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
608
+ INSERT INTO data(x, y) VALUES(new.i, new.t, 1)
609
+ ON CONFLICT (x) DO UPDATE SET z=zz+1;
610
+ END;
611
+ } {no such column: zz}
612
+
613
+ 3 {
614
+ CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
615
+ INSERT INTO x1(i, t) VALUES(new.i+1, new.t||'1')
616
+ ON CONFLICT (tttttt) DO UPDATE SET t=i+1;
617
+ END;
618
+ } {no such column: tttttt}
619
+
620
+ 4 {
621
+ CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
622
+ INSERT INTO nosuchtable VALUES(new.i, new.t);
623
+ END;
624
+ } {no such table: main.nosuchtable}
625
+ } {
626
+ do_execsql_test 13.2.$tn.1 "
627
+ DROP TRIGGER IF EXISTS tr1;
628
+ $trigger
629
+ "
630
+
631
+ do_catchsql_test 13.2.$tn.2 {
632
+ ALTER TABLE x1 RENAME COLUMN t TO ttt;
633
+ } "1 {error in trigger tr1: $error}"
634
+ }
635
+
636
+ #-------------------------------------------------------------------------
637
+ # Passing invalid parameters directly to sqlite_rename_column().
638
+ #
639
+ sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
640
+ do_execsql_test 14.1 {
641
+ CREATE TABLE ddd(sql, type, object, db, tbl, icol, znew, bquote);
642
+ INSERT INTO ddd VALUES(
643
+ 'CREATE TABLE x1(i INTEGER, t TEXT)',
644
+ 'table', 'x1', 'main', 'x1', -1, 'zzz', 0
645
+ ), (
646
+ 'CREATE TABLE x1(i INTEGER, t TEXT)',
647
+ 'table', 'x1', 'main', 'x1', 2, 'zzz', 0
648
+ ), (
649
+ 'CREATE TABLE x1(i INTEGER, t TEXT)',
650
+ 'table', 'x1', 'main', 'notable', 0, 'zzz', 0
651
+ ), (
652
+ 'CREATE TABLE x1(i INTEGER, t TEXT)',
653
+ 'table', 'x1', 'main', 'ddd', -1, 'zzz', 0
654
+ );
655
+ } {}
656
+
657
+ do_execsql_test 14.2 {
658
+ SELECT
659
+ sqlite_rename_column(sql, type, object, db, tbl, icol, znew, bquote, 0)
660
+ FROM ddd;
661
+ } {{} {} {} {}}
662
+ sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
663
+
664
+ # If the INTERNAL_FUNCTIONS test-control is disabled (which is the default)
665
+ # then the sqlite_rename_table() SQL function is not accessible to
666
+ # ordinary SQL.
667
+ #
668
+ do_catchsql_test 14.3 {
669
+ SELECT sqlite_rename_column(0,0,0,0,0,0,0,0,0);
670
+ } {1 {no such function: sqlite_rename_column}}
671
+
672
+ #-------------------------------------------------------------------------
673
+ #
674
+ reset_db
675
+ do_execsql_test 15.0 {
676
+ CREATE TABLE xxx(a, b, c);
677
+ SELECT a AS d FROM xxx WHERE d=0;
678
+ }
679
+
680
+ do_execsql_test 15.1 {
681
+ CREATE VIEW vvv AS SELECT a AS d FROM xxx WHERE d=0;
682
+ ALTER TABLE xxx RENAME a TO xyz;
683
+ }
684
+
685
+ do_execsql_test 15.2 {
686
+ SELECT sql FROM sqlite_master WHERE type='view';
687
+ } {{CREATE VIEW vvv AS SELECT xyz AS d FROM xxx WHERE d=0}}
688
+
689
+ #-------------------------------------------------------------------------
690
+ #
691
+ do_execsql_test 16.1.0 {
692
+ CREATE TABLE t1(a,b,c);
693
+ CREATE TABLE t2(d,e,f);
694
+ INSERT INTO t1 VALUES(1,2,3);
695
+ INSERT INTO t2 VALUES(4,5,6);
696
+ CREATE VIEW v4 AS SELECT a, d FROM t1, t2;
697
+ SELECT * FROM v4;
698
+ } {1 4}
699
+
700
+ do_catchsql_test 16.1.1 {
701
+ ALTER TABLE t2 RENAME d TO a;
702
+ } {1 {error in view v4 after rename: ambiguous column name: a}}
703
+
704
+ do_execsql_test 16.1.2 {
705
+ SELECT * FROM v4;
706
+ } {1 4}
707
+
708
+ do_execsql_test 16.1.3 {
709
+ CREATE UNIQUE INDEX t2d ON t2(d);
710
+ CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
711
+ INSERT INTO t2 VALUES(new.a, new.b, new.c)
712
+ ON CONFLICT(d) DO UPDATE SET f = excluded.f;
713
+ END;
714
+ }
715
+
716
+ do_execsql_test 16.1.4 {
717
+ INSERT INTO t1 VALUES(4, 8, 456);
718
+ SELECT * FROM t2;
719
+ } {4 5 456}
720
+
721
+ do_execsql_test 16.1.5 {
722
+ ALTER TABLE t2 RENAME COLUMN f TO "big f";
723
+ INSERT INTO t1 VALUES(4, 0, 20456);
724
+ SELECT * FROM t2;
725
+ } {4 5 20456}
726
+
727
+ do_execsql_test 16.1.6 {
728
+ ALTER TABLE t1 RENAME COLUMN c TO "big c";
729
+ INSERT INTO t1 VALUES(4, 0, 0);
730
+ SELECT * FROM t2;
731
+ } {4 5 0}
732
+
733
+ do_execsql_test 16.2.1 {
734
+ CREATE VIEW temp.v5 AS SELECT "big c" FROM t1;
735
+ SELECT * FROM v5;
736
+ } {3 456 20456 0}
737
+
738
+ do_execsql_test 16.2.2 {
739
+ ALTER TABLE t1 RENAME COLUMN "big c" TO reallybigc;
740
+ } {}
741
+
742
+ do_execsql_test 16.2.3 {
743
+ SELECT * FROM v5;
744
+ } {3 456 20456 0}
745
+
746
+ #-------------------------------------------------------------------------
747
+ #
748
+ do_execsql_test 17.0 {
749
+ CREATE TABLE u7(x, y, z);
750
+ CREATE TRIGGER u7t AFTER INSERT ON u7 BEGIN
751
+ INSERT INTO u8 VALUES(new.x, new.y, new.z);
752
+ END;
753
+ } {}
754
+ do_catchsql_test 17.1 {
755
+ ALTER TABLE u7 RENAME x TO xxx;
756
+ } {1 {error in trigger u7t: no such table: main.u8}}
757
+
758
+ do_execsql_test 17.2 {
759
+ CREATE TEMP TABLE uu7(x, y, z);
760
+ CREATE TRIGGER uu7t AFTER INSERT ON uu7 BEGIN
761
+ INSERT INTO u8 VALUES(new.x, new.y, new.z);
762
+ END;
763
+ } {}
764
+ do_catchsql_test 17.3 {
765
+ ALTER TABLE uu7 RENAME x TO xxx;
766
+ } {1 {error in trigger uu7t: no such table: u8}}
767
+
768
+ reset_db
769
+ forcedelete test.db2
770
+ do_execsql_test 18.0 {
771
+ ATTACH 'test.db2' AS aux;
772
+ CREATE TABLE t1(a);
773
+ CREATE TABLE aux.log(v);
774
+ CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN
775
+ INSERT INTO log VALUES(new.a);
776
+ END;
777
+ INSERT INTO t1 VALUES(111);
778
+ SELECT v FROM log;
779
+ } {111}
780
+
781
+ do_execsql_test 18.1 {
782
+ ALTER TABLE t1 RENAME a TO b;
783
+ }
784
+
785
+ reset_db
786
+ do_execsql_test 19.0 {
787
+ CREATE TABLE t1(a, b);
788
+ CREATE TABLE t2(c, d);
789
+ CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.a) FROM t1, t2 WHERE t1.b=t2.d;
790
+ }
791
+
792
+ do_execsql_test 19.1 {
793
+ ALTER TABLE t1 RENAME a TO f;
794
+ SELECT sql FROM sqlite_master WHERE name = 'v2';
795
+ } {
796
+ {CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.f) FROM t1, t2 WHERE t1.b=t2.d}
797
+ }
798
+
799
+ # 2019-01-08: https://www.sqlite.org/src/tktview/bc8d94f0fbd633fd9a051e3
800
+ #
801
+ # ALTER TABLE RENAME COLUMN does not work for tables that have redundant
802
+ # UNIQUE constraints.
803
+ #
804
+ sqlite3 db :memory:
805
+ do_execsql_test 20.100 {
806
+ CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA));
807
+ ALTER TABLE t1 RENAME aaa TO bbb;
808
+ SELECT sql FROM sqlite_master WHERE name='t1';
809
+ } {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))}}
810
+ do_execsql_test 20.105 {
811
+ DROP TABLE t1;
812
+ CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA))WITHOUT ROWID;
813
+ ALTER TABLE t1 RENAME aaa TO bbb;
814
+ SELECT sql FROM sqlite_master WHERE name='t1';
815
+ } {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))WITHOUT ROWID}}
816
+ do_execsql_test 20.110 {
817
+ DROP TABLE t1;
818
+ CREATE TABLE t1(aa UNIQUE,bb UNIQUE,cc UNIQUE,UNIQUE(aA),PRIMARY KEY(bB),UNIQUE(cC));
819
+ ALTER TABLE t1 RENAME aa TO xx;
820
+ ALTER TABLE t1 RENAME bb TO yy;
821
+ ALTER TABLE t1 RENAME cc TO zz;
822
+ SELECT sql FROM sqlite_master WHERE name='t1';
823
+ } {{CREATE TABLE t1(xx UNIQUE,yy UNIQUE,zz UNIQUE,UNIQUE(xx),PRIMARY KEY(yy),UNIQUE(zz))}}
824
+
825
+ #-------------------------------------------------------------------------
826
+ reset_db
827
+ do_execsql_test 21.0 {
828
+ CREATE TABLE t1(a, b, c NOT NULL);
829
+ CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.c IS NOT NULL BEGIN
830
+ SELECT c NOT NULL FROM t1;
831
+ END;
832
+ }
833
+
834
+ do_execsql_test 21.1 {
835
+ ALTER TABLE t1 RENAME c TO d;
836
+ }
837
+
838
+ do_execsql_test 21.2 {
839
+ SELECT sql FROM sqlite_schema WHERE name IS 'tr1'
840
+ } {{CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.d IS NOT NULL BEGIN
841
+ SELECT d NOT NULL FROM t1;
842
+ END}
843
+ }
844
+
845
+ #-------------------------------------------------------------------------
846
+ #
847
+ reset_db
848
+ do_execsql_test 22.0 {
849
+ CREATE TABLE t1(a, b);
850
+ CREATE TABLE t2(c, othername, extra AS (c + 1));
851
+ ALTER TABLE t1 RENAME a to othername;
852
+ SELECT sql FROM sqlite_schema;
853
+ } {
854
+ {CREATE TABLE t1(othername, b)}
855
+ {CREATE TABLE t2(c, othername, extra AS (c + 1))}
856
+ }
857
+
858
+ #-------------------------------------------------------------------------
859
+ #
860
+ reset_db
861
+ sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1
862
+ sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
863
+ do_execsql_test 22.0 {
864
+ CREATE TABLE t1(a, b);
865
+ CREATE INDEX x1 on t1("c"=b);
866
+ INSERT INTO t1 VALUES('a', 'a');
867
+ INSERT INTO t1 VALUES('b', 'b');
868
+ INSERT INTO t1 VALUES('c', 'c');
869
+ ALTER TABLE t1 RENAME COLUMN a TO "c";
870
+ PRAGMA integrity_check;
871
+ } {ok}
872
+
873
+ reset_db
874
+ do_execsql_test 23.0 {
875
+ CREATE TABLE t1('a'"b",c);
876
+ CREATE INDEX i1 ON t1('a');
877
+ INSERT INTO t1 VALUES(1,2), (3,4);
878
+ ALTER TABLE t1 RENAME COLUMN a TO x;
879
+ PRAGMA integrity_check;
880
+ SELECT sql FROM sqlite_schema WHERE name='t1';
881
+
882
+ } {ok {CREATE TABLE t1("x" "b",c)}}
883
+
884
+ # 2022-02-04
885
+ # Do not complain about syntax errors in the schema if
886
+ # in PRAGMA writable_schema=ON mode.
887
+ #
888
+ reset_db
889
+ do_execsql_test 23.0 {
890
+ CREATE TABLE t1(a INT, b REAL, c TEXT, d BLOB, e ANY);
891
+ CREATE INDEX t1abx ON t1(a, b, a+b) WHERE c IS NOT NULL;
892
+ CREATE VIEW t2 AS SELECT a+10, b*5.0, xyz FROM t1; -- unknown column "xyz"
893
+ CREATE TABLE schema_copy(name TEXT, sql TEXT);
894
+ INSERT INTO schema_copy(name,sql) SELECT name, sql FROM sqlite_schema WHERE sql IS NOT NULL;
895
+ } {}
896
+ do_catchsql_test 23.1 {
897
+ ALTER TABLE t1 RENAME COLUMN e TO eeee;
898
+ } {1 {error in view t2: no such column: xyz}}
899
+ do_execsql_test 23.2 {
900
+ SELECT name, sql FROM sqlite_master
901
+ EXCEPT SELECT name, sql FROM schema_copy;
902
+ } {}
903
+ do_execsql_test 23.3 {
904
+ BEGIN;
905
+ PRAGMA writable_schema=ON;
906
+ ALTER TABLE t1 RENAME COLUMN e TO eeee;
907
+ PRAGMA writable_schema=OFF;
908
+ SELECT name FROM sqlite_master
909
+ WHERE (name, sql) NOT IN (SELECT name, sql FROM schema_copy);
910
+ ROLLBACK;
911
+ } {t1}
912
+ do_execsql_test 23.10 {
913
+ DROP VIEW t2;
914
+ CREATE TRIGGER r3 AFTER INSERT ON t1 BEGIN
915
+ INSERT INTO t3(x,y) VALUES(new.a, new.b);
916
+ INSERT INTO t4(p) VALUES(new.c); -- no such table "t4"
917
+ END;
918
+ DELETE FROM schema_copy;
919
+ INSERT INTO schema_copy(name,sql) SELECT name, sql FROM sqlite_schema WHERE sql IS NOT NULL;
920
+ } {}
921
+ do_catchsql_test 23.11 {
922
+ ALTER TABLE t1 RENAME COLUMN e TO eeee;
923
+ } {1 {error in trigger r3: no such table: main.t3}}
924
+ do_execsql_test 23.12 {
925
+ SELECT name, sql FROM sqlite_master
926
+ EXCEPT SELECT name, sql FROM schema_copy;
927
+ } {}
928
+ do_execsql_test 23.13 {
929
+ BEGIN;
930
+ PRAGMA writable_schema=ON;
931
+ ALTER TABLE t1 RENAME COLUMN e TO eeee;
932
+ PRAGMA writable_schema=OFF;
933
+ SELECT name FROM sqlite_master
934
+ WHERE (name, sql) NOT IN (SELECT name, sql FROM schema_copy);
935
+ ROLLBACK;
936
+ } {t1}
937
+ do_execsql_test 23.20 {
938
+ CREATE TABLE t4(id INTEGER PRIMARY KEY, c1 INT, c2 INT);
939
+ CREATE VIEW t4v1 AS SELECT id, c1, c99 FROM t4;
940
+ DELETE FROM schema_copy;
941
+ INSERT INTO schema_copy SELECT name, sql FROM sqlite_schema;
942
+ BEGIN;
943
+ PRAGMA writable_schema=ON;
944
+ ALTER TABLE t4 RENAME to t4new;
945
+ SELECT name FROM sqlite_schema WHERE (name,sql) NOT IN (SELECT * FROM schema_copy);
946
+ ROLLBACK;
947
+ } {t4new}
948
+
949
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/altercorrupt.test ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2019-01-11
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+ source $testdir/tester.tcl
15
+ set testprefix altercorrupt
16
+
17
+ # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18
+ ifcapable !altertable {
19
+ finish_test
20
+ return
21
+ }
22
+
23
+ database_may_be_corrupt
24
+
25
+ #--------------------------------------------------------------------------
26
+ reset_db
27
+ do_test 1.0 {
28
+ sqlite3 db {}
29
+ db deserialize [decode_hexdb {
30
+ .open --hexdb
31
+ | size 24576 pagesize 4096 filename crash-685346d89b5e5f.db
32
+ | page 1 offset 0
33
+ | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3.
34
+ | 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........
35
+ | 32: 00 00 63 00 00 05 f0 00 00 00 00 04 10 00 00 04 ..c.............
36
+ | 48: 00 00 00 00 00 00 0f f0 00 00 00 01 00 00 00 00 ................
37
+ | 64: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
38
+ | 96: 00 00 00 00 0d 0f f8 00 05 0e cf 00 0f 79 0f d3 .............y..
39
+ | 112: 0f 2e 0e f3 0e cf 00 00 00 00 00 00 00 00 00 00 ................
40
+ | 3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................
41
+ | 3792: 05 06 17 11 11 01 31 74 61 62 6c 65 74 34 74 34 ......1tablet4t4
42
+ | 3808: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 34 .CREATE TABLE t4
43
+ | 3824: 28 7a 29 39 04 06 17 11 11 01 5f 74 61 62 6c 65 (z)9......_table
44
+ | 3840: 74 33 74 33 05 43 52 45 41 54 45 20 54 41 42 4c t3t3.CREATE TABL
45
+ | 3856: 45 20 74 33 28 78 20 49 4e 54 45 47 45 52 20 50 E t3(x INTEGER P
46
+ | 3872: 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 49 03 RIMARY KEY, y)I.
47
+ | 3888: 06 17 11 11 01 7f 74 61 62 6c 65 74 32 74 32 04 ......tablet2t2.
48
+ | 3904: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 CREATE TABLE t2(
49
+ | 3920: 61 2c 62 2c 63 20 50 52 49 4d 41 52 59 20 4b 45 a,b,c PRIMARY KE
50
+ | 3936: 59 2c 20 64 2c 20 65 2c 20 66 29 20 57 49 54 48 Y, d, e, f) WITH
51
+ | 3952: 4f 55 54 20 52 4f 57 49 44 58 03 07 17 11 11 01 OUT ROWIDX......
52
+ | 3968: 81 1b 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 ..tablet1t1.CREA
53
+ | 3984: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c TE TABLE t1(a,b,
54
+ | 4000: 63 20 41 53 20 28 2d 62 29 20 56 49 52 54 55 41 c AS (-b) VIRTUA
55
+ | 4016: 4c 2c 64 20 43 48 45 43 4b 28 64 3e 35 29 2c 65 L,d CHECK(d>5),e
56
+ | 4032: 20 55 4e 49 51 55 45 2c 20 66 20 41 53 20 28 2b UNIQUE, f AS (+
57
+ | 4048: 62 29 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 b))#...7...index
58
+ | 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex
59
+ | 4080: 5f 74 31 5f 31 74 31 03 00 00 00 08 00 00 00 00 _t1_1t1.........
60
+ | page 2 offset 4096
61
+ | 0: 0d 00 00 00 0a 0f 93 00 0f f6 0f eb 0f e0 0f d5 ................
62
+ | 16: 0f ca 0f 8f 0f b4 0f a9 0f 9e 0f 93 00 00 00 00 ................
63
+ | 3984: 00 00 00 09 0a 05 01 01 01 01 0a 64 6e 14 09 09 ...........dn...
64
+ | 4000: 05 01 01 01 01 09 5a 6d 12 09 08 05 01 01 01 01 ......Zm........
65
+ | 4016: 08 50 6c 10 09 07 05 01 01 01 01 07 46 6b 0e 09 .Pl.........Fk..
66
+ | 4032: 06 05 01 01 01 01 06 3c 6a 0c 09 05 05 01 01 01 .......<j.......
67
+ | 4048: 01 05 32 69 0a 09 04 05 01 01 01 01 04 28 68 08 ..2i.........(h.
68
+ | 4064: 09 03 05 01 01 01 01 03 1e 67 06 09 02 05 01 01 .........g......
69
+ | 4080: 01 01 02 14 66 04 08 01 05 09 01 01 01 0a 65 02 ....f.........e.
70
+ | page 3 offset 8192
71
+ | 0: 0a 00 00 00 0a 0f c5 00 0f fb 0f f5 0f ef 0f e9 ................
72
+ | 16: 0f e3 0f dd 0f d7 0f d1 0f cb 0f c5 00 00 00 00 ................
73
+ | 4032: 00 00 00 00 00 05 03 01 01 14 0a 05 03 01 01 12 ................
74
+ | 4048: 09 05 03 01 01 10 08 05 03 01 01 0e 07 05 03 01 ................
75
+ | 4064: 01 0c 06 05 03 01 01 0a 05 05 03 01 01 08 04 05 ................
76
+ | 4080: 03 01 01 06 03 05 03 01 01 04 02 04 03 01 09 02 ................
77
+ | page 4 offset 12288
78
+ | 0: 0a 00 00 00 0a 0f 75 00 0f 75 0f 83 0f 91 0f 9f ......u..u......
79
+ | 16: 0f ad 0f bb 0f 00 00 00 00 00 00 00 00 00 00 00 ................
80
+ | 3952: 00 00 00 00 00 0d 07 01 01 01 01 01 01 9c 0a 64 ...............d
81
+ | 3968: 6e 14 64 0d 07 02 01 01 01 01 01 a6 09 5a 6d 12 n.d..........Zm.
82
+ | 3984: 5a 0d 07 01 01 01 01 01 01 b0 08 50 6c 10 50 0d Z..........Pl.P.
83
+ | 4000: 07 01 01 01 01 01 01 ba 07 46 6b 0e 46 0d 07 01 .........Fk.F...
84
+ | 4016: 01 01 01 01 01 c4 06 3c 6a 0c 3c 0d 07 01 01 01 .......<j.<.....
85
+ | 4032: 01 01 01 ce 05 32 69 0a 32 0d 07 01 01 01 01 01 .....2i.2.......
86
+ | 4048: 01 d8 04 28 68 08 28 0d 07 01 01 01 01 01 01 e2 ...(h.(.........
87
+ | 4064: 03 1e 67 06 1e 0d 07 01 01 01 01 01 01 ec 02 14 ..g.............
88
+ | 4080: 66 04 14 0c 07 01 09 01 01 01 01 f6 0a 65 02 0a f............e..
89
+ | page 5 offset 16384
90
+ | 0: 0d 00 00 00 03 0f e9 00 0f e9 0f fb 0f f6 00 00 ................
91
+ | 16: 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
92
+ | 4064: 00 00 00 00 00 00 00 00 00 03 ff ff ff ff ff ff ................
93
+ | 4080: ff ff 9c 03 00 00 03 64 03 00 00 03 01 03 00 00 .......d........
94
+ | page 6 offset 20480
95
+ | 0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 ................
96
+ | end crash-685346d89b5e5f.db
97
+ }]} {}
98
+
99
+ do_catchsql_test 1.1 {
100
+ ALTER TABLE t2 DROP COLUMN e;
101
+ ALTER TABLE t1 DROP COLUMN f;
102
+ } {1 {database disk image is malformed}}
103
+
104
+
105
+ #--------------------------------------------------------------------------
106
+ reset_db
107
+ do_test 2.0 {
108
+ sqlite3 db {}
109
+ db deserialize [decode_hexdb {
110
+ .open --hexdb
111
+ | size 24576 pagesize 4096 filename crash-0572db8f391431.db
112
+ | page 1 offset 0
113
+ | 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3.
114
+ | 16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06 .....@ ........
115
+ | 32: 00 00 63 00 10 05 f0 00 00 00 00 04 10 00 00 04 ..c.............
116
+ | 48: 00 00 00 00 00 00 0f f0 00 00 00 00 00 00 00 00 ................
117
+ | 96: 00 00 00 00 0d 0f f8 00 05 0e cf 00 0f 79 0f d3 .............y..
118
+ | 112: 0f 2e 0e f3 0e cf 00 00 00 00 00 00 00 00 00 00 ................
119
+ | 3776: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22 ................
120
+ | 3792: 05 06 17 11 11 01 31 74 61 62 6c 65 74 34 74 34 ......1tablet4t4
121
+ | 3808: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 34 .CREATE TABLE t4
122
+ | 3824: 28 7a 29 39 04 06 17 11 11 01 5f 74 61 62 6c 65 (z)9......_table
123
+ | 3840: 74 33 74 33 05 43 52 45 41 54 45 20 54 41 42 4c t3t3.CREATE TABL
124
+ | 3856: 45 20 74 33 28 78 20 49 4e 54 55 47 45 52 20 50 E t3(x INTUGER P
125
+ | 3872: 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 49 03 RIMARY KEY, y)I.
126
+ | 3888: 06 17 11 11 01 7f 74 61 62 6c 65 74 32 74 32 04 ......tablet2t2.
127
+ | 3904: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 CREATE TABLE t2(
128
+ | 3920: 61 2c 62 2c 63 20 50 52 49 4d 41 52 59 20 4b 45 a,b,c PRIMARY KE
129
+ | 3936: 59 2c 20 64 2c 20 65 2c 20 66 29 20 57 49 54 48 Y, d, e, f) WITH
130
+ | 3952: 4f 55 54 20 52 4f 57 49 44 58 05 07 17 11 11 01 OUT ROWIDX......
131
+ | 3968: 81 1b 74 61 62 6c 65 74 31 74 31 02 43 52 45 41 ..tablet1t1.CREA
132
+ | 3984: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 2c TE TABLE t1(a,b,
133
+ | 4000: 63 20 41 53 20 28 2d 62 29 20 56 49 52 54 55 41 c AS (-b) VIRTUA
134
+ | 4016: 4c 2c 64 20 43 48 45 43 4b 28 64 3e 35 29 2c 65 L,d CHECK(d>5),e
135
+ | 4032: 20 55 4e 49 51 55 45 2c 20 66 20 41 53 20 28 2b UNIQUE, f AS (+
136
+ | 4048: 62 29 29 23 02 06 17 37 11 01 00 69 6e 64 65 78 b))#...7...index
137
+ | 4064: 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 sqlite_autoindex
138
+ | 4080: 5f 74 31 5f 31 84 31 03 01 00 00 08 00 00 00 00 _t1_1.1.........
139
+ | page 2 offset 4096
140
+ | 0: 0d 00 00 00 0a 0f 93 00 0f f6 0f eb 0f e0 0f d5 ................
141
+ | 16: 0f ca 0f 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
142
+ | 3984: 00 00 00 09 0a 05 01 01 01 01 0a 64 6e 14 09 09 ...........dn...
143
+ | 4000: 05 01 01 01 01 09 5a 6d 12 09 08 05 01 00 f1 01 ......Zm........
144
+ | 4016: 08 50 6c 10 09 07 05 01 01 01 01 07 46 6b 0e 09 .Pl.........Fk..
145
+ | 4032: 06 05 01 00 f1 01 06 3c 6a 0c 09 05 05 01 01 01 .......<j.......
146
+ | 4048: 01 05 32 69 0a 09 04 05 01 01 01 01 04 28 68 08 ..2i.........(h.
147
+ | 4064: 57 03 05 01 01 01 01 03 1e 67 06 09 02 05 01 01 W........g......
148
+ | 4080: 01 01 02 14 66 04 08 01 05 09 01 01 01 0a 65 02 ....f.........e.
149
+ | page 3 offset 8192
150
+ | 0: 09 ff ff ff fa 0f c5 00 0f fb 0f f5 0f ef 0f e9 ................
151
+ | 16: 0f e3 0f dd 0f d7 00 00 00 00 00 00 00 00 00 00 ................
152
+ | 4032: 00 00 00 00 00 05 03 01 01 14 0a 05 03 01 01 12 ................
153
+ | 4048: 09 05 03 01 01 10 08 05 03 01 01 0e 07 05 03 01 ................
154
+ | 4064: 01 0c 06 05 03 01 01 0a 05 05 03 01 01 08 04 05 ................
155
+ | 4080: 03 01 01 06 03 05 03 01 01 04 02 04 03 01 09 02 ................
156
+ | page 4 offset 12288
157
+ | 0: 0a 00 00 00 0a 0f 75 00 0f 75 0f 83 0f 91 0f 9f ......u..u......
158
+ | 16: 0f ad 0f bb 0f 00 00 00 00 00 01 00 00 00 00 00 ................
159
+ | 3952: 00 00 00 00 00 0d 07 01 01 01 01 01 01 9c 0a 64 ...............d
160
+ | 3968: 6e 14 64 0d 07 02 01 01 01 01 01 a6 09 5a 6d 12 n.d..........Zm.
161
+ | 3984: 5a 0d 07 01 01 01 01 d4 01 b0 08 50 6c 10 50 0d Z..........Pl.P.
162
+ | 4000: 07 01 01 01 01 01 01 ba 07 46 6b 0e 46 0d 07 00 .........Fk.F...
163
+ | 4016: 01 01 01 01 01 c4 06 3c 6a 0c 3c 0d 07 01 01 01 .......<j.<.....
164
+ | 4032: 01 01 01 ce 05 32 69 0a 32 0d 07 01 01 01 01 01 .....2i.2.......
165
+ | 4048: 01 d8 04 28 68 08 28 0d 07 01 01 01 01 01 01 e2 ...(h.(.........
166
+ | 4064: 03 1e 67 06 1e 0d 07 01 01 01 01 01 01 ec 02 14 ..g.............
167
+ | 4080: 66 04 14 0c 07 01 09 01 01 00 f1 f6 0a 65 02 0a f............e..
168
+ | page 5 offset 16384
169
+ | 0: 0d 00 00 00 03 0f e9 00 0f e9 0f fb 0f f6 00 00 ................
170
+ | 4064: 00 00 00 00 00 00 00 00 00 03 ff ff ff ff ff ff ................
171
+ | 4080: ff ff 9c 03 00 00 03 64 03 00 01 03 01 03 00 00 .......d........
172
+ | page 6 offset 20480
173
+ | 0: 0d 00 10 00 00 10 01 00 00 00 00 00 00 00 00 00 ................
174
+ | end crash-0572db8f391431.db
175
+ }]} {}
176
+
177
+ do_catchsql_test 2.1 {
178
+ ALTER TABLE t1 DROP COLUMN a;
179
+ } {1 {database disk image is malformed}}
180
+
181
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/alterdropcol.test ADDED
@@ -0,0 +1,339 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2021 February 16
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+ source $testdir/tester.tcl
15
+ set testprefix alterdropcol
16
+
17
+ # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18
+ ifcapable !altertable {
19
+ finish_test
20
+ return
21
+ }
22
+
23
+ do_execsql_test 1.0 {
24
+ CREATE TABLE t1(a, b, c);
25
+ CREATE VIEW v1 AS SELECT * FROM t1;
26
+
27
+ CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z UNIQUE);
28
+ CREATE INDEX t2y ON t2(y);
29
+
30
+ CREATE TABLE t3(q, r, s);
31
+ CREATE INDEX t3rs ON t3(r+s);
32
+ }
33
+
34
+ do_catchsql_test 1.1 {
35
+ ALTER TABLE nosuch DROP COLUMN z;
36
+ } {1 {no such table: nosuch}}
37
+
38
+ do_catchsql_test 1.2 {
39
+ ALTER TABLE v1 DROP COLUMN c;
40
+ } {1 {cannot drop column from view "v1"}}
41
+
42
+ ifcapable fts5 {
43
+ do_execsql_test 1.3.1 {
44
+ CREATE VIRTUAL TABLE ft1 USING fts5(one, two);
45
+ }
46
+ do_catchsql_test 1.3.2 {
47
+ ALTER TABLE ft1 DROP COLUMN two;
48
+ } {1 {cannot drop column from virtual table "ft1"}}
49
+ }
50
+
51
+ do_catchsql_test 1.4 {
52
+ ALTER TABLE sqlite_schema DROP COLUMN sql;
53
+ } {1 {table sqlite_master may not be altered}}
54
+
55
+ do_catchsql_test 1.5 {
56
+ ALTER TABLE t1 DROP COLUMN d;
57
+ } {1 {no such column: "d"}}
58
+
59
+ do_execsql_test 1.6.1 {
60
+ ALTER TABLE t1 DROP COLUMN b;
61
+ }
62
+ do_execsql_test 1.6.2 {
63
+ SELECT sql FROM sqlite_schema WHERE name = 't1'
64
+ } {{CREATE TABLE t1(a, c)}}
65
+
66
+ do_execsql_test 1.7.1 {
67
+ ALTER TABLE t1 DROP COLUMN c;
68
+ }
69
+ do_execsql_test 1.7.2 {
70
+ SELECT sql FROM sqlite_schema WHERE name = 't1'
71
+ } {{CREATE TABLE t1(a)}}
72
+
73
+ do_catchsql_test 1.7.3 {
74
+ ALTER TABLE t1 DROP COLUMN a;
75
+ } {1 {cannot drop column "a": no other columns exist}}
76
+
77
+
78
+ do_catchsql_test 1.8 {
79
+ ALTER TABLE t2 DROP COLUMN z
80
+ } {1 {cannot drop UNIQUE column: "z"}}
81
+
82
+ do_catchsql_test 1.9 {
83
+ ALTER TABLE t2 DROP COLUMN x
84
+ } {1 {cannot drop PRIMARY KEY column: "x"}}
85
+
86
+ do_catchsql_test 1.10 {
87
+ ALTER TABLE t2 DROP COLUMN y
88
+ } {1 {error in index t2y after drop column: no such column: y}}
89
+
90
+ do_catchsql_test 1.11 {
91
+ ALTER TABLE t3 DROP COLUMN s
92
+ } {1 {error in index t3rs after drop column: no such column: s}}
93
+
94
+ #-------------------------------------------------------------------------
95
+
96
+ foreach {tn wo} {
97
+ 1 {}
98
+ 2 {WITHOUT ROWID}
99
+ } { eval [string map [list %TN% $tn %WO% $wo] {
100
+
101
+ reset_db
102
+ do_execsql_test 2.%TN%.0 {
103
+ CREATE TABLE t1(x, y INTEGER PRIMARY KEY, z) %WO% ;
104
+ INSERT INTO t1 VALUES(1, 2, 3);
105
+ INSERT INTO t1 VALUES(4, 5, 6);
106
+ INSERT INTO t1 VALUES(7, 8, 9);
107
+ }
108
+
109
+ do_execsql_test 2.%TN%.1 {
110
+ ALTER TABLE t1 DROP COLUMN x;
111
+ SELECT * FROM t1;
112
+ } {
113
+ 2 3 5 6 8 9
114
+ }
115
+ do_execsql_test 2.%TN%.2 {
116
+ ALTER TABLE t1 DROP COLUMN z;
117
+ SELECT * FROM t1;
118
+ } {
119
+ 2 5 8
120
+ }
121
+ }]}
122
+
123
+ #-------------------------------------------------------------------------
124
+ reset_db
125
+
126
+ do_execsql_test 3.0 {
127
+ CREATE TABLE t12(a, b, c, CHECK(c>10));
128
+ CREATE TABLE t13(a, b, c CHECK(c>10));
129
+ }
130
+ do_catchsql_test 3.1 {
131
+ ALTER TABLE t12 DROP COLUMN c;
132
+ } {1 {error in table t12 after drop column: no such column: c}}
133
+
134
+ do_catchsql_test 3.2 {
135
+ ALTER TABLE t13 DROP COLUMN c;
136
+ } {0 {}}
137
+
138
+ #-------------------------------------------------------------------------
139
+ # Test that generated columns can be dropped. And that other columns from
140
+ # tables that contain generated columns can be dropped.
141
+ #
142
+ foreach {tn wo vs} {
143
+ 1 "" ""
144
+ 2 "" VIRTUAL
145
+ 3 "" STORED
146
+ 4 "WITHOUT ROWID" STORED
147
+ 5 "WITHOUT ROWID" VIRTUAL
148
+ } {
149
+ reset_db
150
+
151
+ do_execsql_test 4.$tn.0 "
152
+ CREATE TABLE 'my table'(a, b PRIMARY KEY, c AS (a+b) $vs, d) $wo
153
+ "
154
+ do_execsql_test 4.$tn.1 {
155
+ INSERT INTO "my table"(a, b, d) VALUES(1, 2, 'hello');
156
+ INSERT INTO "my table"(a, b, d) VALUES(3, 4, 'world');
157
+
158
+ SELECT * FROM "my table"
159
+ } {
160
+ 1 2 3 hello
161
+ 3 4 7 world
162
+ }
163
+
164
+ do_execsql_test 4.$tn.2 {
165
+ ALTER TABLE "my table" DROP COLUMN c;
166
+ }
167
+ do_execsql_test 4.$tn.3 {
168
+ SELECT * FROM "my table"
169
+ } {
170
+ 1 2 hello
171
+ 3 4 world
172
+ }
173
+
174
+ do_execsql_test 4.$tn.4 "
175
+ CREATE TABLE x1(a, b, c PRIMARY KEY, d AS (b+c) $vs, e) $wo
176
+ "
177
+ do_execsql_test 4.$tn.5 {
178
+ INSERT INTO x1(a, b, c, e) VALUES(1, 2, 3, 4);
179
+ INSERT INTO x1(a, b, c, e) VALUES(5, 6, 7, 8);
180
+ INSERT INTO x1(a, b, c, e) VALUES(9, 10, 11, 12);
181
+ SELECT * FROM x1;
182
+ } {
183
+ 1 2 3 5 4
184
+ 5 6 7 13 8
185
+ 9 10 11 21 12
186
+ }
187
+
188
+ do_execsql_test 4.$tn.6 {
189
+ ALTER TABLE x1 DROP COLUMN a
190
+ }
191
+ do_execsql_test 4.$tn.7 {
192
+ SELECT * FROM x1
193
+ } {
194
+ 2 3 5 4
195
+ 6 7 13 8
196
+ 10 11 21 12
197
+ }
198
+ do_execsql_test 4.$tn.8 {
199
+ ALTER TABLE x1 DROP COLUMN e
200
+ }
201
+ do_execsql_test 4.$tn.9 {
202
+ SELECT * FROM x1
203
+ } {
204
+ 2 3 5
205
+ 6 7 13
206
+ 10 11 21
207
+ }
208
+ }
209
+
210
+ #-------------------------------------------------------------------------
211
+ reset_db
212
+ do_execsql_test 5.0 {
213
+ CREATE TABLE p1(a PRIMARY KEY, b UNIQUE);
214
+ CREATE TABLE c1(x, y, z REFERENCES p1(c));
215
+ CREATE TABLE c2(x, y, z, w REFERENCES p1(b));
216
+ }
217
+ do_execsql_test 5.1 {
218
+ ALTER TABLE c1 DROP COLUMN z;
219
+ ALTER TABLE c2 DROP COLUMN z;
220
+ SELECT sql FROM sqlite_schema WHERE name IN ('c1', 'c2');
221
+ } {
222
+ {CREATE TABLE c1(x, y)}
223
+ {CREATE TABLE c2(x, y, w REFERENCES p1(b))}
224
+ }
225
+
226
+ do_execsql_test 5.2.1 {
227
+ CREATE VIEW v1 AS SELECT d, e FROM p1
228
+ }
229
+ do_catchsql_test 5.2.2 {
230
+ ALTER TABLE c1 DROP COLUMN x
231
+ } {1 {error in view v1: no such column: d}}
232
+ do_execsql_test 5.3.1 {
233
+ DROP VIEW v1;
234
+ CREATE VIEW v1 AS SELECT x, y FROM c1;
235
+ }
236
+ do_catchsql_test 5.3.2 {
237
+ ALTER TABLE c1 DROP COLUMN x
238
+ } {1 {error in view v1 after drop column: no such column: x}}
239
+
240
+ do_execsql_test 5.4.1 {
241
+ CREATE TRIGGER tr AFTER INSERT ON c1 BEGIN
242
+ INSERT INTO p1 VALUES(new.y, new.xyz);
243
+ END;
244
+ }
245
+ do_catchsql_test 5.4.2 {
246
+ ALTER TABLE c1 DROP COLUMN y
247
+ } {1 {error in trigger tr: no such column: new.xyz}}
248
+ do_execsql_test 5.5.1 {
249
+ DROP TRIGGER tr;
250
+ CREATE TRIGGER tr AFTER INSERT ON c1 BEGIN
251
+ INSERT INTO p1 VALUES(new.y, new.z);
252
+ END;
253
+ }
254
+ do_catchsql_test 5.5.2 {
255
+ ALTER TABLE c1 DROP COLUMN y
256
+ } {1 {error in trigger tr: no such column: new.z}}
257
+
258
+ # 2021-03-06 dbsqlfuzz crash-419aa525df93db6e463772c686ac6da27b46da9e
259
+ reset_db
260
+ do_catchsql_test 6.0 {
261
+ CREATE TABLE t1(a,b,c);
262
+ CREATE TABLE t2(x,y,z);
263
+ PRAGMA writable_schema=ON;
264
+ UPDATE sqlite_schema SET sql='CREATE INDEX t1b ON t1(b)' WHERE name='t2';
265
+ PRAGMA writable_schema=OFF;
266
+ ALTER TABLE t2 DROP COLUMN z;
267
+ } {1 {database disk image is malformed}}
268
+ reset_db
269
+ do_catchsql_test 6.1 {
270
+ CREATE TABLE t1(a,b,c);
271
+ CREATE TABLE t2(x,y,z);
272
+ PRAGMA writable_schema=ON;
273
+ UPDATE sqlite_schema SET sql='CREATE VIEW t2(x,y,z) AS SELECT b,a,c FROM t1'
274
+ WHERE name='t2';
275
+ PRAGMA writable_schema=OFF;
276
+ ALTER TABLE t2 DROP COLUMN z;
277
+ } {1 {database disk image is malformed}}
278
+
279
+ # 2021-04-06 dbsqlfuzz crash-331c5c29bb76257b198f1318eef3288f9624c8ce
280
+ reset_db
281
+ do_execsql_test 7.0 {
282
+ CREATE TABLE t1(a, b, c, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID;
283
+ INSERT INTO t1 VALUES(1, 2, 3);
284
+ INSERT INTO t1 VALUES(4, 5, 6);
285
+ }
286
+ do_execsql_test 7.1 {
287
+ ALTER TABLE t1 DROP COLUMN c;
288
+ }
289
+ do_execsql_test 7.2 {
290
+ SELECT sql FROM sqlite_schema;
291
+ } {{CREATE TABLE t1(a, b, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID}}
292
+ do_execsql_test 7.3 {
293
+ SELECT * FROM t1;
294
+ } {1 2 4 5}
295
+
296
+ reset_db
297
+ do_execsql_test 8.0 {
298
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
299
+ PRAGMA writable_schema = 1;
300
+ UPDATE sqlite_schema
301
+ SET sql = 'CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b)'
302
+ }
303
+ db close
304
+ sqlite3 db test.db
305
+ do_execsql_test 8.1 {
306
+ ALTER TABLE t1 DROP COLUMN b;
307
+ }
308
+ do_execsql_test 8.2 {
309
+ SELECT sql FROM sqlite_schema;
310
+ } {{CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT)}}
311
+
312
+ #-------------------------------------------------------------------------
313
+
314
+ foreach {tn wo} {
315
+ 1 {}
316
+ 2 {WITHOUT ROWID}
317
+ } {
318
+ reset_db
319
+ do_execsql_test 9.$tn.0 "
320
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c) $wo;
321
+ "
322
+ do_execsql_test 9.$tn.1 {
323
+ WITH s(i) AS (
324
+ SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000
325
+ )
326
+ INSERT INTO t1(a, b, c) SELECT i, 123, 456 FROM s;
327
+ }
328
+ do_execsql_test 9.$tn.2 {
329
+ ALTER TABLE t1 DROP COLUMN b;
330
+ }
331
+
332
+ do_execsql_test 9.$tn.3 {
333
+ SELECT count(*), c FROM t1 GROUP BY c;
334
+ } {50000 456}
335
+ }
336
+
337
+
338
+
339
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/alterqf.test ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2021 March 16
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. This
12
+ # script focuses on testing internal function sqlite_rename_quotefix().
13
+ #
14
+
15
+
16
+ set testdir [file dirname $argv0]
17
+ source $testdir/tester.tcl
18
+ set testprefix alterqf
19
+
20
+ # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
21
+ ifcapable !altertable {
22
+ finish_test
23
+ return
24
+ }
25
+
26
+
27
+ sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
28
+ sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1
29
+ sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
30
+
31
+ do_execsql_test 1.0 {
32
+ CREATE TABLE t1(a, b, c);
33
+ }
34
+
35
+ foreach {tn before after} {
36
+ 1 {CREATE VIEW v1 AS SELECT "a", "b", "notacolumn!", "c" FROM t1}
37
+ {CREATE VIEW v1 AS SELECT "a", "b", 'notacolumn!', "c" FROM t1}
38
+
39
+ 2 {CREATE VIEW v1 AS SELECT "a", "b", "not'a'column!", "c" FROM t1}
40
+ {CREATE VIEW v1 AS SELECT "a", "b", 'not''a''column!', "c" FROM t1}
41
+
42
+ 3 {CREATE VIEW v1 AS SELECT "a", "b", "not""a""column!", "c" FROM t1}
43
+ {CREATE VIEW v1 AS SELECT "a", "b", 'not"a"column!', "c" FROM t1}
44
+
45
+ 4 {CREATE VIEW v1 AS SELECT "val", count("b") FROM t1 GROUP BY "abc"}
46
+ {CREATE VIEW v1 AS SELECT 'val', count("b") FROM t1 GROUP BY 'abc'}
47
+
48
+ 5 {CREATE TABLE xyz(a CHECK (a!="str"), b AS (a||"str"))}
49
+ {CREATE TABLE xyz(a CHECK (a!='str'), b AS (a||'str'))}
50
+
51
+ 6 {CREATE INDEX i1 ON t1(a || "str", "b", "val")}
52
+ {CREATE INDEX i1 ON t1(a || 'str', "b", 'val')}
53
+
54
+ 7 {CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN SELECT "abcd"; END}
55
+ {CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN SELECT 'abcd'; END}
56
+
57
+ 8 {CREATE VIEW v1 AS SELECT "string"'alias' FROM t1}
58
+ {CREATE VIEW v1 AS SELECT 'string' 'alias' FROM t1}
59
+
60
+ 9 {CREATE INDEX i1 ON t1(a) WHERE "b"="bb"}
61
+ {CREATE INDEX i1 ON t1(a) WHERE "b"='bb'}
62
+
63
+ 10 {CREATE TABLE t2(abc, xyz CHECK (xyz != "123"))}
64
+ {CREATE TABLE t2(abc, xyz CHECK (xyz != '123'))}
65
+
66
+ 11 {CREATE TRIGGER ott AFTER UPDATE ON t1 BEGIN
67
+ SELECT max("str", new."a") FROM t1
68
+ WHERE string_agg("b", ",") OVER (ORDER BY c||"str");
69
+ UPDATE t1 SET c= b + "str";
70
+ DELETE FROM t1 WHERE EXISTS (
71
+ SELECT 1 FROM t1 AS o WHERE o."a" = "o.a" AND t1.b IN("t1.b")
72
+ );
73
+ END;
74
+ } {CREATE TRIGGER ott AFTER UPDATE ON t1 BEGIN
75
+ SELECT max('str', new."a") FROM t1
76
+ WHERE string_agg("b", ',') OVER (ORDER BY c||'str');
77
+ UPDATE t1 SET c= b + 'str';
78
+ DELETE FROM t1 WHERE EXISTS (
79
+ SELECT 1 FROM t1 AS o WHERE o."a" = 'o.a' AND t1.b IN('t1.b')
80
+ );
81
+ END;
82
+ }
83
+
84
+ } {
85
+ do_execsql_test 1.$tn {
86
+ SELECT sqlite_rename_quotefix('main', $before)
87
+ } [list $after]
88
+ }
89
+
90
+ #-------------------------------------------------------------------------
91
+ reset_db
92
+ sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1
93
+ sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
94
+ do_execsql_test 2.0 {
95
+ CREATE TABLE x1(
96
+ one, two, three, PRIMARY KEY(one),
97
+ CHECK (three!="xyz"), CHECK (two!="one")
98
+ ) WITHOUT ROWID;
99
+ CREATE INDEX x1i ON x1(one+"two"+"four") WHERE "five";
100
+ CREATE TEMP TRIGGER AFTER INSERT ON x1 BEGIN
101
+ UPDATE x1 SET two=new.three || "new" WHERE one=new.one||"";
102
+ END;
103
+ }
104
+
105
+ do_execsql_test 2.1 {
106
+ ALTER TABLE x1 RENAME two TO 'four';
107
+ SELECT sql FROM sqlite_schema;
108
+ SELECT sql FROM sqlite_temp_schema;
109
+ } {{CREATE TABLE x1(
110
+ one, "four", three, PRIMARY KEY(one),
111
+ CHECK (three!='xyz'), CHECK ("four"!="one")
112
+ ) WITHOUT ROWID}
113
+ {CREATE INDEX x1i ON x1(one+"four"+'four') WHERE 'five'}
114
+ {CREATE TRIGGER AFTER INSERT ON x1 BEGIN
115
+ UPDATE x1 SET "four"=new.three || 'new' WHERE one=new.one||'';
116
+ END}
117
+ }
118
+
119
+
120
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/altertab3.test ADDED
@@ -0,0 +1,789 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2019 January 23
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+ source $testdir/tester.tcl
15
+ set testprefix altertab3
16
+
17
+ # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18
+ ifcapable !altertable {
19
+ finish_test
20
+ return
21
+ }
22
+
23
+ ifcapable windowfunc {
24
+ do_execsql_test 1.0 {
25
+ CREATE TABLE t1(a, b);
26
+ CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
27
+ SELECT sum(b) OVER w FROM t1 WINDOW w AS (ORDER BY a);
28
+ END;
29
+ }
30
+
31
+ do_execsql_test 1.1 {
32
+ ALTER TABLE t1 RENAME a TO aaa;
33
+ }
34
+
35
+ do_execsql_test 1.2 {
36
+ SELECT sql FROM sqlite_master WHERE name='tr1'
37
+ } {{CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
38
+ SELECT sum(b) OVER w FROM t1 WINDOW w AS (ORDER BY aaa);
39
+ END}}
40
+
41
+ do_execsql_test 1.3 {
42
+ INSERT INTO t1 VALUES(1, 2);
43
+ }
44
+ } ;# windowfunc
45
+
46
+ #-------------------------------------------------------------------------
47
+ reset_db
48
+ do_execsql_test 2.0 {
49
+ CREATE TABLE t1(a,b,c);
50
+ CREATE TABLE t2(a,b,c);
51
+ CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN
52
+ SELECT a,b, a name FROM t1
53
+ INTERSECT
54
+ SELECT a,b,c FROM t1 WHERE b>='d' ORDER BY name;
55
+ SELECT new.c;
56
+ END;
57
+ }
58
+
59
+ do_execsql_test 2.1 {
60
+ ALTER TABLE t1 RENAME TO t1x;
61
+ SELECT sql FROM sqlite_master WHERE name = 'r1';
62
+ } {{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN new.a NOT NULL BEGIN
63
+ SELECT a,b, a name FROM "t1x"
64
+ INTERSECT
65
+ SELECT a,b,c FROM "t1x" WHERE b>='d' ORDER BY name;
66
+ SELECT new.c;
67
+ END}}
68
+
69
+ #-------------------------------------------------------------------------
70
+ reset_db
71
+ do_execsql_test 3.0 {
72
+ CREATE TABLE t1(a, b, c, d);
73
+ CREATE VIEW v1 AS SELECT * FROM t1 WHERE a=1 OR (b IN ());
74
+ }
75
+
76
+ do_execsql_test 3.1 {
77
+ ALTER TABLE t1 RENAME b TO bbb;
78
+ }
79
+
80
+ do_execsql_test 3.2 {
81
+ SELECT sql FROM sqlite_master WHERE name = 'v1'
82
+ } {{CREATE VIEW v1 AS SELECT * FROM t1 WHERE a=1 OR (b IN ())}}
83
+
84
+ #-------------------------------------------------------------------------
85
+ reset_db
86
+ do_execsql_test 4.0 {
87
+ CREATE TABLE t1(a, b);
88
+ CREATE TABLE t3(e, f);
89
+ CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
90
+ INSERT INTO t2 VALUES(new.a, new.b);
91
+ END;
92
+ }
93
+
94
+ do_catchsql_test 4.1.2 {
95
+ BEGIN;
96
+ ALTER TABLE t3 RENAME TO t4;
97
+ } {1 {error in trigger tr1: no such table: main.t2}}
98
+ do_execsql_test 4.1.2 {
99
+ COMMIT;
100
+ }
101
+ do_execsql_test 4.1.3 {
102
+ SELECT type, name, tbl_name, sql
103
+ FROM sqlite_master WHERE type='table' AND name!='t1';
104
+ } {table t3 t3 {CREATE TABLE t3(e, f)}}
105
+
106
+
107
+ do_catchsql_test 4.2.1 {
108
+ BEGIN;
109
+ ALTER TABLE t3 RENAME e TO eee;
110
+ } {1 {error in trigger tr1: no such table: main.t2}}
111
+ do_execsql_test 4.2.2 {
112
+ COMMIT;
113
+ }
114
+ do_execsql_test 4.2.3 {
115
+ SELECT type, name, tbl_name, sql
116
+ FROM sqlite_master WHERE type='table' AND name!='t1';
117
+ } {table t3 t3 {CREATE TABLE t3(e, f)}}
118
+
119
+ #-------------------------------------------------------------------------
120
+ reset_db
121
+ do_execsql_test 5.0 {
122
+ CREATE TABLE t1 (
123
+ c1 integer, c2, PRIMARY KEY(c1 collate rtrim),
124
+ UNIQUE(c2)
125
+ )
126
+ }
127
+ do_execsql_test 5.1 {
128
+ ALTER TABLE t1 RENAME c1 TO c3;
129
+ }
130
+
131
+ #-------------------------------------------------------------------------
132
+ reset_db
133
+ do_execsql_test 6.0 {
134
+ CREATE TEMPORARY TABLE Table0 (
135
+ Col0 INTEGER,
136
+ PRIMARY KEY(Col0 COLLATE RTRIM),
137
+ FOREIGN KEY (Col0) REFERENCES Table0
138
+ );
139
+ }
140
+
141
+ do_execsql_test 6.1 {
142
+ ALTER TABLE Table0 RENAME Col0 TO Col0;
143
+ }
144
+
145
+ #-------------------------------------------------------------------------
146
+ reset_db
147
+ do_execsql_test 7.1.0 {
148
+ CREATE TABLE t1(a,b,c);
149
+ CREATE TRIGGER AFTER INSERT ON t1 BEGIN
150
+ SELECT a, rank() OVER w1 FROM t1
151
+ WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1);
152
+ END;
153
+ }
154
+
155
+ do_execsql_test 7.1.2 {
156
+ ALTER TABLE t1 RENAME TO t1x;
157
+ SELECT sql FROM sqlite_master;
158
+ } {
159
+ {CREATE TABLE "t1x"(a,b,c)}
160
+ {CREATE TRIGGER AFTER INSERT ON "t1x" BEGIN
161
+ SELECT a, rank() OVER w1 FROM "t1x"
162
+ WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1);
163
+ END}
164
+ }
165
+
166
+ do_execsql_test 7.2.1 {
167
+ DROP TRIGGER after;
168
+ CREATE TRIGGER AFTER INSERT ON t1x BEGIN
169
+ SELECT a, rank() OVER w1 FROM t1x
170
+ WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1 ORDER BY d);
171
+ END;
172
+ }
173
+
174
+ do_catchsql_test 7.2.2 {
175
+ ALTER TABLE t1x RENAME TO t1;
176
+ } {1 {error in trigger AFTER: no such column: d}}
177
+
178
+ #-------------------------------------------------------------------------
179
+ reset_db
180
+ do_execsql_test 8.0 {
181
+ CREATE TABLE t0(c0);
182
+ CREATE INDEX i0 ON t0('1' IN ());
183
+ }
184
+ do_execsql_test 8.1 {
185
+ ALTER TABLE t0 RENAME TO t1;
186
+ SELECT sql FROM sqlite_master;
187
+ } {
188
+ {CREATE TABLE "t1"(c0)}
189
+ {CREATE INDEX i0 ON "t1"('1' IN ())}
190
+ }
191
+ do_execsql_test 8.2.1 {
192
+ CREATE TABLE t2 (c0);
193
+ CREATE INDEX i2 ON t2((LIKELIHOOD(c0, 100) IN ()));
194
+ ALTER TABLE t2 RENAME COLUMN c0 TO c1;
195
+ }
196
+ do_execsql_test 8.2.2 {
197
+ SELECT sql FROM sqlite_master WHERE tbl_name = 't2';
198
+ } {
199
+ {CREATE TABLE t2 (c1)}
200
+ {CREATE INDEX i2 ON t2((LIKELIHOOD(c0, 100) IN ()))}
201
+ }
202
+ do_test 8.2.3 {
203
+ sqlite3 db2 test.db
204
+ db2 eval { INSERT INTO t2 VALUES (1), (2), (3) }
205
+ db close
206
+ } {}
207
+ db2 close
208
+
209
+ #-------------------------------------------------------------------------
210
+ reset_db
211
+ do_execsql_test 9.1 {
212
+ CREATE TABLE t1(a,b,c);
213
+ CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN
214
+ SELECT true WHERE (SELECT a, b FROM (t1)) IN ();
215
+ END;
216
+ }
217
+ do_execsql_test 9.2 {
218
+ ALTER TABLE t1 RENAME TO t1x;
219
+ }
220
+
221
+ #-------------------------------------------------------------------------
222
+ reset_db
223
+ do_execsql_test 10.1 {
224
+ CREATE TABLE t1(a, b, c);
225
+ CREATE TABLE t2(a, b, c);
226
+ CREATE VIEW v1 AS SELECT * FROM t1 WHERE (
227
+ SELECT t1.a FROM t1, t2
228
+ ) IN () OR t1.a=5;
229
+ }
230
+
231
+ do_execsql_test 10.2 {
232
+ ALTER TABLE t2 RENAME TO t3;
233
+ SELECT sql FROM sqlite_master WHERE name='v1';
234
+ } {
235
+ {CREATE VIEW v1 AS SELECT * FROM t1 WHERE (
236
+ SELECT t1.a FROM t1, t2
237
+ ) IN () OR t1.a=5}
238
+ }
239
+
240
+ #-------------------------------------------------------------------------
241
+ reset_db
242
+ do_execsql_test 11.1 {
243
+ CREATE TABLE t1(
244
+ a,b,c,d,e,f,g,h,j,jj,jjb,k,aa,bb,cc,dd,ee DEFAULT 3.14,
245
+ ff DEFAULT('hiccup'),Wg NOD NULL DEFAULT(false)
246
+ );
247
+
248
+ CREATE TRIGGER b AFTER INSERT ON t1 WHEN new.a BEGIN
249
+ SELECT a, sum() w3 FROM t1
250
+ WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM abc));
251
+ END;
252
+ }
253
+
254
+ do_catchsql_test 11.2 {
255
+ ALTER TABLE t1 RENAME TO t1x;
256
+ } {1 {error in trigger b: no such table: main.abc}}
257
+
258
+ do_execsql_test 11.3 {
259
+ DROP TRIGGER b;
260
+ CREATE TRIGGER b AFTER INSERT ON t1 WHEN new.a BEGIN
261
+ SELECT a, sum() w3 FROM t1
262
+ WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM t1));
263
+ END;
264
+ } {}
265
+
266
+ do_execsql_test 11.4 {
267
+ ALTER TABLE t1 RENAME TO t1x;
268
+ SELECT sql FROM sqlite_master WHERE name = 'b';
269
+ } {
270
+ {CREATE TRIGGER b AFTER INSERT ON "t1x" WHEN new.a BEGIN
271
+ SELECT a, sum() w3 FROM "t1x"
272
+ WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM "t1x"));
273
+ END}
274
+ }
275
+
276
+ #-------------------------------------------------------------------------
277
+ reset_db
278
+ do_execsql_test 12.1 {
279
+ CREATE TABLE t1(a,b,c,d,e,f,g,h,j,jj,Zjj,k,aQ,bb,cc,dd,ee DEFAULT 3.14,
280
+ ff DEFAULT('hiccup'),gg NOD NULL DEFAULT(false));
281
+ CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN
282
+
283
+ SELECT b () OVER , dense_rank() OVER d, d () OVER w1
284
+ FROM t1
285
+ WINDOW
286
+ w1 AS
287
+ ( w1 ORDER BY d
288
+ ROWS BETWEEN 2 NOT IN(SELECT a, sum(d) w2,max(d)OVER FROM t1
289
+ WINDOW
290
+ w1 AS
291
+ (PARTITION BY d
292
+ ROWS BETWEEN '' PRECEDING AND false FOLLOWING),
293
+ d AS
294
+ (PARTITION BY b ORDER BY d
295
+ ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
296
+ ) PRECEDING AND 1 FOLLOWING),
297
+ w2 AS
298
+ (PARTITION BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
299
+ w3 AS
300
+ (PARTITION BY b ORDER BY d
301
+ ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
302
+ ;
303
+ SELECT a, sum(d) w2,max(d)OVER FROM t1
304
+ WINDOW
305
+ w1 AS
306
+ (PARTITION BY d
307
+ ROWS BETWEEN '' PRECEDING AND false FOLLOWING),
308
+ d AS
309
+ (PARTITION BY b ORDER BY d
310
+ ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
311
+ ;
312
+
313
+ END;
314
+ }
315
+
316
+ do_execsql_test 12.2 {
317
+ ALTER TABLE t1 RENAME TO t1x;
318
+ }
319
+
320
+ #-------------------------------------------------------------------------
321
+ reset_db
322
+ do_execsql_test 13.1 {
323
+ CREATE TABLE t1(a);
324
+ CREATE TRIGGER r1 INSERT ON t1 BEGIN
325
+ SELECT a(*) OVER (ORDER BY (SELECT 1)) FROM t1;
326
+ END;
327
+ }
328
+
329
+ do_execsql_test 13.2 {
330
+ ALTER TABLE t1 RENAME TO t1x;
331
+ }
332
+
333
+ #-------------------------------------------------------------------------
334
+ reset_db
335
+ do_execsql_test 14.1 {
336
+ CREATE TABLE t1(a);
337
+ CREATE TABLE t2(b);
338
+ CREATE TRIGGER AFTER INSERT ON t1 BEGIN
339
+ SELECT sum() FILTER (WHERE (SELECT sum() FILTER (WHERE 0)) AND a);
340
+ END;
341
+ }
342
+
343
+ do_catchsql_test 14.2 {
344
+ ALTER TABLE t1 RENAME TO t1x;
345
+ } {1 {error in trigger AFTER: no such column: a}}
346
+
347
+ #-------------------------------------------------------------------------
348
+ reset_db
349
+
350
+ do_execsql_test 16.1 {
351
+ CREATE TABLE t1(x);
352
+ CREATE TRIGGER AFTER INSERT ON t1 BEGIN
353
+ SELECT (WITH t2 AS (WITH t3 AS (SELECT true)
354
+ SELECT * FROM t3 ORDER BY true COLLATE nocase)
355
+ SELECT 11);
356
+
357
+ WITH t4 AS (SELECT * FROM t1) SELECT 33;
358
+ END;
359
+ }
360
+ do_execsql_test 16.2 {
361
+ ALTER TABLE t1 RENAME TO t1x;
362
+ }
363
+
364
+ #-------------------------------------------------------------------------
365
+ reset_db
366
+ do_execsql_test 17.1 {
367
+ CREATE TABLE t1(a,b,c);
368
+ CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN
369
+ SELECT a () FILTER (WHERE a>0) FROM t1;
370
+ END;
371
+ }
372
+
373
+ do_execsql_test 17.2 {
374
+ ALTER TABLE t1 RENAME TO t1x;
375
+ ALTER TABLE t1x RENAME a TO aaa;
376
+ SELECT sql FROM sqlite_master WHERE type='trigger';
377
+ } {
378
+ {CREATE TRIGGER AFTER INSERT ON "t1x" WHEN new.aaa NOT NULL BEGIN
379
+ SELECT a () FILTER (WHERE aaa>0) FROM "t1x";
380
+ END}
381
+ }
382
+
383
+ #-------------------------------------------------------------------------
384
+ reset_db
385
+ do_execsql_test 18.1 {
386
+ CREATE TABLE t1(a,b);
387
+ CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
388
+ SELECT a, b FROM t1
389
+ INTERSECT SELECT b,a FROM t1
390
+ ORDER BY b IN (
391
+ SELECT a UNION SELECT b
392
+ FROM t1
393
+ ORDER BY b COLLATE nocase
394
+ )
395
+ ;
396
+ END;
397
+ }
398
+
399
+ do_catchsql_test 18.2 {
400
+ SELECT a, b FROM t1
401
+ INTERSECT
402
+ SELECT b,a FROM t1
403
+ ORDER BY b IN (
404
+ SELECT a UNION SELECT b
405
+ FROM t1
406
+ ORDER BY b COLLATE nocase
407
+ );
408
+ } {1 {1st ORDER BY term does not match any column in the result set}}
409
+
410
+ do_catchsql_test 18.3 {
411
+ ALTER TABLE t1 RENAME TO t1x;
412
+ } {1 {error in trigger r1: 1st ORDER BY term does not match any column in the result set}}
413
+
414
+ #-------------------------------------------------------------------------
415
+ reset_db
416
+ do_execsql_test 19.0 {
417
+ CREATE TABLE a(a,h CONSTRAINT a UNIQUE ON CONFLICT FAIL,CONSTRAINT a);
418
+ }
419
+
420
+ foreach {tn v res} {
421
+ 1 {
422
+ CREATE VIEW q AS SELECT 123
423
+
424
+ WINDOW x AS (
425
+ RANGE BETWEEN UNBOUNDED PRECEDING AND INDEXED() OVER(
426
+ PARTITION BY ( WITH x AS(VALUES(col1)) VALUES(453) )
427
+ )
428
+ FOLLOWING
429
+ )
430
+ } {1 {error in view q: no such column: col1}}
431
+
432
+ 2 {
433
+ CREATE VIEW q AS SELECT
434
+ CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(CAST(RIGHT
435
+ AS)AS)AS)AS)AS)AS)AS)AS)AS)AS)AS)WINDOW x AS(RANGE BETWEEN UNBOUNDED
436
+ PRECEDING AND INDEXED(*)OVER(PARTITION BY
437
+ CROSS,CROSS,NATURAL,sqlite_master(*)OVER a,(WITH a AS(VALUES(LEFT)UNION
438
+ VALUES(LEFT)UNION VALUES(LEFT)UNION VALUES(LEFT)UNION VALUES(LEFT)UNION
439
+ VALUES(LEFT)UNION VALUES(LEFT))VALUES(LEFT))IN
440
+ STORED,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT)*LEFT FOLLOWING)ORDER BY
441
+ LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT LIMIT
442
+ LEFT,INDEXED(*)OVER(PARTITION BY
443
+ CROSS,CROSS,CROSS,LEFT,INDEXED(*)OVER(PARTITION BY
444
+ CROSS,CROSS,CROSS),INDEXED(*)OVER(PARTITION BY
445
+ LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT,LEFT),
446
+ LEFT,LEFT,INNER,CROSS,CROSS,CROSS,INNER,NATURAL ORDER BY
447
+ OUTER,NATURAL,NATURAL,NATURAL,NATURAL,NATURAL,NATURAL,NATURAL,INNER,
448
+ INNER,INNER NULLS LAST GROUPS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
449
+ FOLLOWING);
450
+ } {1 {error in view q: no such column: LEFT}}
451
+
452
+ 3 {
453
+ CREATE VIEW q AS SELECT 99 WINDOW x AS (RANGE BETWEEN UNBOUNDED PRECEDING
454
+ AND count(*)OVER(PARTITION BY (WITH a AS(VALUES(2),(x3))VALUES(0)))
455
+ FOLLOWING)ORDER BY x2,sum(1)OVER(PARTITION BY avg(5)OVER(PARTITION BY x1));
456
+ } {1 {error in view q: no such column: x3}}
457
+ } {
458
+ do_execsql_test 19.$tn.1 "
459
+ DROP VIEW IF EXISTS q;
460
+ $v
461
+ " {}
462
+
463
+ do_catchsql_test 19.$tn.2 {
464
+ ALTER TABLE a RENAME TO g;
465
+ } $res
466
+ }
467
+
468
+ # Verify that the "if( pParse->nErr ) return WRC_Abort" at the top of the
469
+ # renameUnmapSelectCb() routine in alter.c (2019-12-04) is really required.
470
+ #
471
+ sqlite3 db :memory:
472
+ do_catchsql_test 20.10 {
473
+ CREATE TABLE s(a, b, c);
474
+ CREATE INDEX k ON s( (WITH s AS( SELECT * ) VALUES(2) ) IN () );
475
+ ALTER TABLE s RENAME a TO a2;
476
+ } {1 {error in index k: no tables specified}}
477
+
478
+ #------------------------------------------------------------------------
479
+ #
480
+ reset_db
481
+ do_execsql_test 21.1 {
482
+ CREATE TABLE s(col);
483
+ CREATE VIEW v AS SELECT (
484
+ WITH x(a) AS(SELECT * FROM s) VALUES(RIGHT)
485
+ ) IN() ;
486
+ CREATE TABLE a(a);
487
+ ALTER TABLE a RENAME a TO b;
488
+ }
489
+
490
+ #------------------------------------------------------------------------
491
+ #
492
+ reset_db
493
+ do_execsql_test 22.1 {
494
+ CREATE TABLE t1(a);
495
+ CREATE VIEW v2(b) AS SELECT * FROM v2;
496
+ }
497
+
498
+ do_catchsql_test 22.2 {
499
+ ALTER TABLE t1 RENAME TO t4;
500
+ } {1 {error in view v2: view v2 is circularly defined}}
501
+
502
+ do_execsql_test 22.3 {
503
+ DROP VIEW v2;
504
+ CREATE VIEW v2(b) AS WITH t3 AS (SELECT b FROM v2) SELECT * FROM t3;
505
+ }
506
+
507
+ do_catchsql_test 22.4 {
508
+ ALTER TABLE t1 RENAME TO t4;
509
+ } {1 {error in view v2: view v2 is circularly defined}}
510
+
511
+ do_execsql_test 22.5 {
512
+ DROP VIEW v2;
513
+ CREATE VIEW v2(b) AS WITH t3 AS (SELECT b FROM v2) VALUES(1);
514
+ }
515
+
516
+ do_catchsql_test 22.6 {
517
+ ALTER TABLE t1 RENAME TO t4;
518
+ } {0 {}}
519
+
520
+ #------------------------------------------------------------------------
521
+ #
522
+ reset_db
523
+ do_execsql_test 23.1 {
524
+ CREATE TABLE t1(x);
525
+ CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
526
+ UPDATE t1 SET (c,d)=((SELECT 1 FROM t1 JOIN t2 ON b=x),1);
527
+ END;
528
+ }
529
+
530
+ do_catchsql_test 23.2 {
531
+ ALTER TABLE t1 RENAME TO t1x;
532
+ } {1 {error in trigger r1: no such table: main.t2}}
533
+
534
+ #------------------------------------------------------------------------
535
+ #
536
+ reset_db
537
+ do_execsql_test 23.1 {
538
+ CREATE TABLE v0 (a);
539
+ CREATE VIEW v2 (v3) AS
540
+ WITH x1 AS (SELECT * FROM v2)
541
+ SELECT v3 AS x, v3 AS y FROM v2;
542
+ }
543
+
544
+ do_catchsql_test 23.2 {
545
+ SELECT * FROM v2
546
+ } {1 {view v2 is circularly defined}}
547
+
548
+ db close
549
+ sqlite3 db test.db
550
+
551
+ do_catchsql_test 23.3 {
552
+ ALTER TABLE v0 RENAME TO t3 ;
553
+ } {1 {error in view v2: view v2 is circularly defined}}
554
+
555
+ #------------------------------------------------------------------------
556
+ #
557
+ reset_db
558
+ do_execsql_test 24.1 {
559
+ CREATE TABLE v0 (v1);
560
+ CREATE TABLE v2 (v3 INTEGER UNIQUE ON CONFLICT ABORT);
561
+ CREATE TRIGGER x AFTER INSERT ON v2 WHEN (
562
+ ( SELECT v1 AS PROMO_REVENUE FROM v2 JOIN v0 USING ( VALUE ) ) AND 0 )
563
+ BEGIN
564
+ DELETE FROM v2;
565
+ END;
566
+ }
567
+ do_catchsql_test 24.2 {
568
+ ALTER TABLE v0 RENAME TO x ;
569
+ } {1 {error in trigger x: cannot join using column VALUE - column not present in both tables}}
570
+
571
+ do_execsql_test 24.3 {
572
+ DROP TRIGGER x;
573
+ CREATE TRIGGER x AFTER INSERT ON v2 WHEN (
574
+ 0 AND (SELECT rowid FROM v0)
575
+ ) BEGIN
576
+ DELETE FROM v2;
577
+ END;
578
+ }
579
+
580
+ do_execsql_test 24.4 {
581
+ ALTER TABLE v0 RENAME TO xyz;
582
+ SELECT sql FROM sqlite_master WHERE type='trigger'
583
+ } {{CREATE TRIGGER x AFTER INSERT ON v2 WHEN (
584
+ 0 AND (SELECT rowid FROM "xyz")
585
+ ) BEGIN
586
+ DELETE FROM v2;
587
+ END}}
588
+
589
+ #------------------------------------------------------------------------
590
+ #
591
+ reset_db
592
+ do_execsql_test 25.1 {
593
+ CREATE TABLE t1(a, b, c);
594
+ CREATE TABLE t2(a, b, c);
595
+ CREATE TRIGGER ttt AFTER INSERT ON t1 BEGIN
596
+ UPDATE t1 SET a=t2.a FROM t2 WHERE t1.a=t2.a;
597
+ END;
598
+ }
599
+ #do_execsql_test 25.2 {
600
+ # ALTER TABLE t2 RENAME COLUMN a TO aaa;
601
+ #}
602
+
603
+ #------------------------------------------------------------------------
604
+ #
605
+ reset_db
606
+ do_execsql_test 26.1 {
607
+ CREATE TABLE t1(x);
608
+
609
+ CREATE TABLE t3(y);
610
+ CREATE TABLE t4(z);
611
+
612
+ CREATE TRIGGER tr1 INSERT ON t3 BEGIN
613
+ UPDATE t3 SET y=z FROM (SELECT z FROM t4);
614
+ END;
615
+
616
+ CREATE TRIGGER tr2 INSERT ON t3 BEGIN
617
+ UPDATE t3 SET y=abc FROM (SELECT x AS abc FROM t1);
618
+ END;
619
+ }
620
+
621
+ do_execsql_test 26.2 {
622
+ ALTER TABLE t1 RENAME TO t2;
623
+ }
624
+
625
+ do_execsql_test 26.3 {
626
+ ALTER TABLE t2 RENAME x TO xx;
627
+ }
628
+
629
+ do_execsql_test 26.4 {
630
+ SELECT sql FROM sqlite_schema WHERE name='tr2'
631
+ } {
632
+ {CREATE TRIGGER tr2 INSERT ON t3 BEGIN
633
+ UPDATE t3 SET y=abc FROM (SELECT xx AS abc FROM "t2");
634
+ END}
635
+ }
636
+
637
+ # 2020-11-02 OSSFuzz
638
+ #
639
+ reset_db
640
+ do_execsql_test 26.5 {
641
+ CREATE TABLE t1(xx);
642
+ CREATE TRIGGER xx INSERT ON t1 BEGIN
643
+ UPDATE t1 SET xx=xx FROM(SELECT xx);
644
+ END;
645
+ } {}
646
+ do_catchsql_test 26.6 {
647
+ ALTER TABLE t1 RENAME TO t2;
648
+ } {1 {error in trigger xx: no such column: xx}}
649
+
650
+
651
+ #-------------------------------------------------------------------------
652
+ reset_db
653
+
654
+ do_execsql_test 27.1 {
655
+ CREATE TABLE t1(a, b AS ((WITH w1 (xyz) AS ( SELECT t1.b FROM t1 ) SELECT 123) IN ()), c);
656
+ }
657
+
658
+ do_execsql_test 27.2 {
659
+ ALTER TABLE t1 DROP COLUMN c;
660
+ SELECT sql FROM sqlite_schema WHERE name = 't1';
661
+ } {
662
+ {CREATE TABLE t1(a, b AS ((WITH w1 (xyz) AS ( SELECT t1.b FROM t1 ) SELECT 123) IN ()))}
663
+ }
664
+
665
+ do_execsql_test 27.3 {
666
+ CREATE TABLE t0(c0 , c1 AS (CASE TRUE NOT IN () WHEN NULL THEN CASE + 0xa ISNULL WHEN NOT + 0x9 THEN t0.c1 ELSE CURRENT_TIME LIKE CAST (t0.c1 REGEXP '-([1-9]\d*.\d*|0\.\d*[1-9]\d*)'ESCAPE (c1) COLLATE BINARY BETWEEN c1 AND c1 NOT IN (WITH t4 (c0) AS (WITH t3 (c0) AS NOT MATERIALIZED (WITH RECURSIVE t2 (c0) AS (WITH RECURSIVE t1 AS (VALUES (x'717171ff71717171' ) ) SELECT DISTINCT t0.c0 FROM t0 NOT INDEXED WHERE t0.c0 =t0.c0 GROUP BY 0x9 ) SELECT DISTINCT t0.c0 FROM t0 NOT INDEXED WHERE t0.c0 =t0.c1 ) SELECT DISTINCT t0.c0 FROM t0 NOT INDEXED WHERE t0.c0 =t0.c0 GROUP BY typeof(0x9 ) ) SELECT DISTINCT t0.c0 FROM t0 NOT INDEXED WHERE t0.c0 =t0.c0 GROUP BY typeof(typeof(0x9 ) ) ) IN t0 BETWEEN typeof(typeof(typeof(hex(*) FILTER (WHERE + x'5ccd1e68' ) ) ) ) AND 1 >0xa AS BLOB (+4.4E4 , -0xe ) ) END <> c1 IN () END ) VIRTUAL , c35 PRIMARY KEY , c60 , c64 NUMERIC (-6.8 , -0xE ) ) WITHOUT ROWID ;
667
+ } {}
668
+
669
+ do_execsql_test 27.4 {
670
+ ALTER TABLE t0 DROP COLUMN c60;
671
+ } {}
672
+
673
+ #-------------------------------------------------------------------------
674
+ reset_db
675
+ do_execsql_test 28.1 {
676
+ CREATE TABLE t1(a,b,c,d);
677
+ CREATE TRIGGER AFTER INSERT ON t1 BEGIN
678
+ UPDATE t1 SET (c,d)=(a,b);
679
+ END;
680
+ ALTER TABLE t1 RENAME TO t2;
681
+ }
682
+
683
+ do_execsql_test 28.2 {
684
+ SELECT sql FROM sqlite_schema WHERE type='trigger'
685
+ } {{CREATE TRIGGER AFTER INSERT ON "t2" BEGIN
686
+ UPDATE "t2" SET (c,d)=(a,b);
687
+ END}}
688
+
689
+
690
+ #-------------------------------------------------------------------------
691
+ reset_db
692
+ do_execsql_test 29.1 {
693
+ CREATE TABLE t1(x, y);
694
+ CREATE TRIGGER Trigger1 DELETE ON t1
695
+ BEGIN
696
+ SELECT t1.*, t1.x FROM t1 ORDER BY t1.x;
697
+ END;
698
+ }
699
+
700
+
701
+ do_execsql_test 29.2 {
702
+ ALTER TABLE t1 RENAME x TO z;
703
+ }
704
+
705
+ do_execsql_test 29.3 {
706
+ ALTER TABLE t1 RENAME TO t2;
707
+ }
708
+
709
+ do_execsql_test 29.4 {
710
+ CREATE TRIGGER tr2 AFTER DELETE ON t2 BEGIN
711
+ SELECT z, y FROM (
712
+ SELECT t2.* FROM t2
713
+ );
714
+ END;
715
+ }
716
+
717
+ do_execsql_test 29.5 {
718
+ DELETE FROM t2
719
+ }
720
+
721
+ do_execsql_test 29.6 {
722
+ ALTER TABLE t2 RENAME TO t3;
723
+ }
724
+
725
+ do_execsql_test 29.7 {
726
+ SELECT sql FROM sqlite_schema WHERE type='trigger'
727
+ } {
728
+ {CREATE TRIGGER Trigger1 DELETE ON "t3"
729
+ BEGIN
730
+ SELECT "t3".*, "t3".z FROM "t3" ORDER BY "t3".z;
731
+ END}
732
+ {CREATE TRIGGER tr2 AFTER DELETE ON "t3" BEGIN
733
+ SELECT z, y FROM (
734
+ SELECT "t3".* FROM "t3"
735
+ );
736
+ END}
737
+ }
738
+
739
+ #-------------------------------------------------------------------------
740
+ reset_db
741
+ do_execsql_test 30.0 {
742
+ CREATE TABLE t1(a, b);
743
+ CREATE VIEW v1 AS
744
+ SELECT ( VALUES(a), (b) ) FROM (
745
+ SELECT a, b FROM t1
746
+ )
747
+ ;
748
+ }
749
+
750
+ do_execsql_test 30.1 {
751
+ SELECT * FROM v1
752
+ }
753
+
754
+ do_execsql_test 30.1 {
755
+ ALTER TABLE t1 RENAME TO t2;
756
+ }
757
+ do_execsql_test 30.2 {
758
+ SELECT sql FROM sqlite_schema WHERE type='view'
759
+ } {
760
+ {CREATE VIEW v1 AS
761
+ SELECT ( VALUES(a), (b) ) FROM (
762
+ SELECT a, b FROM "t2"
763
+ )}
764
+ }
765
+
766
+ #-------------------------------------------------------------------------
767
+ reset_db
768
+ do_execsql_test 31.0 {
769
+ CREATE TABLE t1(ii INTEGER PRIMARY KEY, tt INTEGER, rr REAL);
770
+ WITH s(i) AS (
771
+ SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000
772
+ )
773
+ INSERT INTO t1 SELECT NULL, i, 5.0 FROM s;
774
+ }
775
+
776
+ do_test 31.1 {
777
+ set pg [db one {PRAGMA page_count}]
778
+ execsql {
779
+ ALTER TABLE t1 DROP COLUMN tt;
780
+ }
781
+ set pg2 [db one {PRAGMA page_count}]
782
+ expr $pg==$pg2
783
+ } {1}
784
+
785
+ do_execsql_test 31.2 {
786
+ SELECT rr FROM t1 LIMIT 1
787
+ } {5.0}
788
+
789
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/amatch1.test ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2013-09-30
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ # This file implements regression tests for "approximate_match" virtual
12
+ # table that is in the "amatch.c" extension.
13
+ #
14
+ #
15
+
16
+ set testdir [file dirname $argv0]
17
+ source $testdir/tester.tcl
18
+
19
+ # If SQLITE_ENABLE_FTS4 is defined, omit this file.
20
+ ifcapable !fts3 {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ # Create the fts_kjv_genesis procedure which fills and FTS3/4 table with
26
+ # the complete text of the Book of Genesis.
27
+ #
28
+ source $testdir/genesis.tcl
29
+
30
+
31
+
32
+ do_test amatch1-1.0 {
33
+ db eval {
34
+ CREATE VIRTUAL TABLE t1 USING fts4(words); --, tokenize porter);
35
+ }
36
+ fts_kjv_genesis
37
+ db eval {
38
+ INSERT INTO t1(t1) VALUES('optimize');
39
+ CREATE VIRTUAL TABLE temp.t1aux USING fts4aux(main, t1);
40
+ SELECT term FROM t1aux WHERE col=0 ORDER BY 1 LIMIT 5
41
+ }
42
+ } {a abated abel abelmizraim abidah}
43
+ do_test amatch1-1.1 {
44
+ db eval {
45
+ SELECT term FROM t1aux WHERE term>'b' AND col=0 ORDER BY 1 LIMIT 5
46
+ }
47
+ } {baalhanan babel back backward bad}
48
+ do_test amatch1-1.2 {
49
+ db eval {
50
+ SELECT term FROM t1aux WHERE term>'b' AND col=0 LIMIT 5
51
+ }
52
+ } {baalhanan babel back backward bad}
53
+
54
+ # Load the amatch extension
55
+ load_static_extension db amatch
56
+
57
+ do_execsql_test amatch1-2.0 {
58
+ CREATE TABLE costs(iLang, cFrom, cTo, Cost);
59
+ INSERT INTO costs VALUES(0, '', '?', 100);
60
+ INSERT INTO costs VALUES(0, '?', '', 100);
61
+ INSERT INTO costs VALUES(0, '?', '?', 150);
62
+ CREATE TABLE vocab(w TEXT UNIQUE);
63
+ INSERT OR IGNORE INTO vocab SELECT term FROM t1aux;
64
+ CREATE VIRTUAL TABLE t2 USING approximate_match(
65
+ vocabulary_table=t1aux,
66
+ vocabulary_word=term,
67
+ edit_distances=costs
68
+ );
69
+ CREATE VIRTUAL TABLE t3 USING approximate_match(
70
+ vocabulary_table=vocab,
71
+ vocabulary_word=w,
72
+ edit_distances=costs
73
+ );
74
+ CREATE VIRTUAL TABLE t4 USING approximate_match(
75
+ vocabulary_table=vtemp,
76
+ vocabulary_word=w,
77
+ edit_distances=costs
78
+ );
79
+ } {}
80
+ puts "Query against fts4aux: [time {
81
+ do_execsql_test amatch1-2.1 {
82
+ SELECT word, distance FROM t2
83
+ WHERE word MATCH 'josxph' AND distance<300;
84
+ } {joseph 150}} 1]"
85
+ puts "Query against ordinary table: [time {
86
+ do_execsql_test amatch1-2.2 {
87
+ SELECT word, distance FROM t3
88
+ WHERE word MATCH 'josxph' AND distance<300;
89
+ } {joseph 150}} 1]"
90
+ puts "Temp table initialized from fts4aux: [time {
91
+ do_execsql_test amatch1-2.3a {
92
+ CREATE TEMP TABLE vtemp(w TEXT UNIQUE);
93
+ INSERT OR IGNORE INTO vtemp SELECT term FROM t1aux;
94
+ } {}} 1]"
95
+ puts "Query against temp table: [time {
96
+ do_execsql_test amatch1-2.3b {
97
+ SELECT word, distance FROM t4
98
+ WHERE word MATCH 'josxph' AND distance<300;
99
+ } {joseph 150}} 1]"
100
+ do_execsql_test amatch1-2.11 {
101
+ SELECT word, distance FROM t2
102
+ WHERE word MATCH 'joxxph' AND distance<=300;
103
+ } {joseph 300}
104
+ do_execsql_test amatch1-2.12 {
105
+ SELECT word, distance FROM t3
106
+ WHERE word MATCH 'joxxph' AND distance<=300;
107
+ } {joseph 300}
108
+ do_execsql_test amatch1-2.21 {
109
+ SELECT word, distance FROM t2
110
+ WHERE word MATCH 'joxxph' AND distance<300;
111
+ } {}
112
+ do_execsql_test amatch1-2.22 {
113
+ SELECT word, distance FROM t3
114
+ WHERE word MATCH 'joxxph' AND distance<300;
115
+ } {}
116
+
117
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyze5.test ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2011 January 19
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # This file implements tests for SQLite library. The focus of the tests
13
+ # in this file is the use of the sqlite_stat4 histogram data on tables
14
+ # with many repeated values and only a few distinct values.
15
+ #
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ ifcapable !stat4 {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ set testprefix analyze5
26
+
27
+ proc eqp {sql {db db}} {
28
+ uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
29
+ }
30
+
31
+ proc alpha {blob} {
32
+ set ret ""
33
+ foreach c [split $blob {}] {
34
+ if {[string is alpha $c]} {append ret $c}
35
+ }
36
+ return $ret
37
+ }
38
+ db func alpha alpha
39
+
40
+ db func lindex lindex
41
+
42
+ unset -nocomplain i t u v w x y z
43
+ do_test analyze5-1.0 {
44
+ db eval {CREATE TABLE t1(t,u,v TEXT COLLATE nocase,w,x,y,z)}
45
+ for {set i 0} {$i < 1000} {incr i} {
46
+ set y [expr {$i>=25 && $i<=50}]
47
+ set z [expr {($i>=400) + ($i>=700) + ($i>=875)}]
48
+ set x $z
49
+ set w $z
50
+ set t [expr {$z+0.5}]
51
+ switch $z {
52
+ 0 {set u "alpha"; unset x}
53
+ 1 {set u "bravo"}
54
+ 2 {set u "charlie"}
55
+ 3 {set u "delta"; unset w}
56
+ }
57
+ if {$i%2} {set v $u} {set v [string toupper $u]}
58
+ db eval {INSERT INTO t1 VALUES($t,$u,$v,$w,$x,$y,$z)}
59
+ }
60
+ db eval {
61
+ CREATE INDEX t1t ON t1(t); -- 0.5, 1.5, 2.5, and 3.5
62
+ CREATE INDEX t1u ON t1(u); -- text
63
+ CREATE INDEX t1v ON t1(v); -- mixed case text
64
+ CREATE INDEX t1w ON t1(w); -- integers 0, 1, 2 and a few NULLs
65
+ CREATE INDEX t1x ON t1(x); -- integers 1, 2, 3 and many NULLs
66
+ CREATE INDEX t1y ON t1(y); -- integers 0 and very few 1s
67
+ CREATE INDEX t1z ON t1(z); -- integers 0, 1, 2, and 3
68
+ ANALYZE;
69
+ }
70
+ db eval {
71
+ SELECT DISTINCT lindex(test_decode(sample),0)
72
+ FROM sqlite_stat4 WHERE idx='t1u' ORDER BY nlt;
73
+ }
74
+ } {alpha bravo charlie delta}
75
+
76
+ do_test analyze5-1.1 {
77
+ db eval {
78
+ SELECT DISTINCT lower(lindex(test_decode(sample), 0))
79
+ FROM sqlite_stat4 WHERE idx='t1v' ORDER BY 1
80
+ }
81
+ } {alpha bravo charlie delta}
82
+ do_test analyze5-1.2 {
83
+ db eval {SELECT idx, count(*) FROM sqlite_stat4 GROUP BY 1 ORDER BY 1}
84
+ } {t1t 8 t1u 8 t1v 8 t1w 8 t1x 8 t1y 9 t1z 8}
85
+
86
+ # Verify that range queries generate the correct row count estimates
87
+ #
88
+ foreach {testid where index rows} {
89
+ 1 {z>=0 AND z<=0} t1z 400
90
+ 2 {z>=1 AND z<=1} t1z 300
91
+ 3 {z>=2 AND z<=2} t1z 175
92
+ 4 {z>=3 AND z<=3} t1z 125
93
+ 5 {z>=4 AND z<=4} t1z 1
94
+ 6 {z>=-1 AND z<=-1} t1z 1
95
+ 7 {z>1 AND z<3} t1z 175
96
+ 8 {z>0 AND z<100} t1z 600
97
+ 9 {z>=1 AND z<100} t1z 600
98
+ 10 {z>1 AND z<100} t1z 300
99
+ 11 {z>=2 AND z<100} t1z 300
100
+ 12 {z>2 AND z<100} t1z 125
101
+ 13 {z>=3 AND z<100} t1z 125
102
+ 14 {z>3 AND z<100} t1z 1
103
+ 15 {z>=4 AND z<100} t1z 1
104
+ 16 {z>=-100 AND z<=-1} t1z 1
105
+ 17 {z>=-100 AND z<=0} t1z 400
106
+ 18 {z>=-100 AND z<0} t1z 1
107
+ 19 {z>=-100 AND z<=1} t1z 700
108
+ 20 {z>=-100 AND z<2} t1z 700
109
+ 21 {z>=-100 AND z<=2} t1z 875
110
+ 22 {z>=-100 AND z<3} t1z 875
111
+
112
+ 31 {z>=0.0 AND z<=0.0} t1z 400
113
+ 32 {z>=1.0 AND z<=1.0} t1z 300
114
+ 33 {z>=2.0 AND z<=2.0} t1z 175
115
+ 34 {z>=3.0 AND z<=3.0} t1z 125
116
+ 35 {z>=4.0 AND z<=4.0} t1z 1
117
+ 36 {z>=-1.0 AND z<=-1.0} t1z 1
118
+ 37 {z>1.5 AND z<3.0} t1z 174
119
+ 38 {z>0.5 AND z<100} t1z 599
120
+ 39 {z>=1.0 AND z<100} t1z 600
121
+ 40 {z>1.5 AND z<100} t1z 299
122
+ 41 {z>=2.0 AND z<100} t1z 300
123
+ 42 {z>2.1 AND z<100} t1z 124
124
+ 43 {z>=3.0 AND z<100} t1z 125
125
+ 44 {z>3.2 AND z<100} t1z 1
126
+ 45 {z>=4.0 AND z<100} t1z 1
127
+ 46 {z>=-100 AND z<=-1.0} t1z 1
128
+ 47 {z>=-100 AND z<=0.0} t1z 400
129
+ 48 {z>=-100 AND z<0.0} t1z 1
130
+ 49 {z>=-100 AND z<=1.0} t1z 700
131
+ 50 {z>=-100 AND z<2.0} t1z 700
132
+ 51 {z>=-100 AND z<=2.0} t1z 875
133
+ 52 {z>=-100 AND z<3.0} t1z 875
134
+
135
+ 101 {z=-1} t1z 1
136
+ 102 {z=0} t1z 400
137
+ 103 {z=1} t1z 300
138
+ 104 {z=2} t1z 175
139
+ 105 {z=3} t1z 125
140
+ 106 {z=4} t1z 1
141
+ 107 {z=-10.0} t1z 1
142
+ 108 {z=0.0} t1z 400
143
+ 109 {z=1.0} t1z 300
144
+ 110 {z=2.0} t1z 175
145
+ 111 {z=3.0} t1z 125
146
+ 112 {z=4.0} t1z 1
147
+ 113 {z=1.5} t1z 1
148
+ 114 {z=2.5} t1z 1
149
+
150
+ 201 {z IN (-1)} t1z 1
151
+ 202 {z IN (0)} t1z 400
152
+ 203 {z IN (1)} t1z 300
153
+ 204 {z IN (2)} t1z 175
154
+ 205 {z IN (3)} t1z 125
155
+ 206 {z IN (4)} t1z 1
156
+ 207 {z IN (0.5)} t1z 1
157
+ 208 {z IN (0,1)} t1z 700
158
+ 209 {z IN (0,1,2)} t1z 875
159
+ 210 {z IN (0,1,2,3)} {} 100
160
+ 211 {z IN (0,1,2,3,4,5)} {} 100
161
+ 212 {z IN (1,2)} t1z 475
162
+ 213 {z IN (2,3)} t1z 300
163
+ 214 {z=3 OR z=2} t1z 300
164
+ 215 {z IN (-1,3)} t1z 126
165
+ 216 {z=-1 OR z=3} t1z 126
166
+
167
+ 300 {y=0} t1y 974
168
+ 301 {y=1} t1y 26
169
+ 302 {y=0.1} t1y 1
170
+
171
+ 400 {x IS NULL} t1x 400
172
+
173
+ } {
174
+ # Verify that the expected index is used with the expected row count
175
+ # No longer valid due to an EXPLAIN QUERY PLAN output format change
176
+ # do_test analyze5-1.${testid}a {
177
+ # set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
178
+ # set idx {}
179
+ # regexp {INDEX (t1.) } $x all idx
180
+ # regexp {~([0-9]+) rows} $x all nrow
181
+ # list $idx $nrow
182
+ # } [list $index $rows]
183
+
184
+ # Verify that the same result is achieved regardless of whether or not
185
+ # the index is used
186
+ do_test analyze5-1.${testid}b {
187
+ set w2 [string map {y +y z +z} $where]
188
+ set a1 [db eval "SELECT rowid FROM t1 NOT INDEXED WHERE $w2\
189
+ ORDER BY +rowid"]
190
+ set a2 [db eval "SELECT rowid FROM t1 WHERE $where ORDER BY +rowid"]
191
+ if {$a1==$a2} {
192
+ set res ok
193
+ } else {
194
+ set res "a1=\[$a1\] a2=\[$a2\]"
195
+ }
196
+ set res
197
+ } {ok}
198
+ }
199
+
200
+ # Increase the number of NULLs in column x
201
+ #
202
+ db eval {
203
+ UPDATE t1 SET x=NULL;
204
+ UPDATE t1 SET x=rowid
205
+ WHERE rowid IN (SELECT rowid FROM t1 ORDER BY random() LIMIT 5);
206
+ ANALYZE;
207
+ }
208
+
209
+ # Verify that range queries generate the correct row count estimates
210
+ #
211
+ foreach {testid where index rows} {
212
+ 500 {x IS NULL AND u='charlie'} t1u 17
213
+ 501 {x=1 AND u='charlie'} t1x 1
214
+ 502 {x IS NULL} t1x 995
215
+ 503 {x=1} t1x 1
216
+ 504 {x IS NOT NULL} t1x 2
217
+ 505 {+x IS NOT NULL} {} 500
218
+ 506 {upper(x) IS NOT NULL} {} 500
219
+
220
+ } {
221
+ # Verify that the expected index is used with the expected row count
222
+ # No longer valid due to an EXPLAIN QUERY PLAN format change
223
+ # do_test analyze5-1.${testid}a {
224
+ # set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
225
+ # set idx {}
226
+ # regexp {INDEX (t1.) } $x all idx
227
+ # regexp {~([0-9]+) rows} $x all nrow
228
+ # list $idx $nrow
229
+ # } [list $index $rows]
230
+
231
+ # Verify that the same result is achieved regardless of whether or not
232
+ # the index is used
233
+ do_test analyze5-1.${testid}b {
234
+ set w2 [string map {y +y z +z} $where]
235
+ set a1 [db eval "SELECT rowid FROM t1 NOT INDEXED WHERE $w2\
236
+ ORDER BY +rowid"]
237
+ set a2 [db eval "SELECT rowid FROM t1 WHERE $where ORDER BY +rowid"]
238
+ if {$a1==$a2} {
239
+ set res ok
240
+ } else {
241
+ set res "a1=\[$a1\] a2=\[$a2\]"
242
+ }
243
+ set res
244
+ } {ok}
245
+ }
246
+
247
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyze6.test ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2011 March 3
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # This file implements tests for SQLite library. The focus of the tests
13
+ # in this file a corner-case query planner optimization involving the
14
+ # join order of two tables of different sizes.
15
+ #
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ ifcapable !stat4 {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ set testprefix analyze6
26
+
27
+ proc eqp {sql {db db}} {
28
+ uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
29
+ }
30
+
31
+ do_test analyze6-1.0 {
32
+ db eval {
33
+ CREATE TABLE cat(x INT, yz TEXT);
34
+ CREATE UNIQUE INDEX catx ON cat(x);
35
+ /* Give cat 16 unique integers */
36
+ INSERT INTO cat(x) VALUES(1);
37
+ INSERT INTO cat(x) VALUES(2);
38
+ INSERT INTO cat(x) SELECT x+2 FROM cat;
39
+ INSERT INTO cat(x) SELECT x+4 FROM cat;
40
+ INSERT INTO cat(x) SELECT x+8 FROM cat;
41
+
42
+ CREATE TABLE ev(y INT);
43
+ CREATE INDEX evy ON ev(y);
44
+ /* ev will hold 32 copies of 16 integers found in cat */
45
+ INSERT INTO ev SELECT x FROM cat;
46
+ INSERT INTO ev SELECT x FROM cat;
47
+ INSERT INTO ev SELECT y FROM ev;
48
+ INSERT INTO ev SELECT y FROM ev;
49
+ INSERT INTO ev SELECT y FROM ev;
50
+ INSERT INTO ev SELECT y FROM ev;
51
+ ANALYZE;
52
+ SELECT count(*) FROM cat;
53
+ SELECT count(*) FROM ev;
54
+ }
55
+ } {16 512}
56
+
57
+ # The lowest cost plan is to scan CAT and for each integer there, do a single
58
+ # lookup of the first corresponding entry in EV then read off the equal values
59
+ # in EV. (Prior to the 2011-03-04 enhancement to where.c, this query would
60
+ # have used EV for the outer loop instead of CAT - which was about 3x slower.)
61
+ #
62
+ do_test analyze6-1.1 {
63
+ eqp {SELECT count(*) FROM ev, cat WHERE x=y}
64
+ } {/*SCAN cat USING COVERING INDEX catx*SEARCH ev USING COVERING INDEX evy (y=?)*/}
65
+
66
+ # The same plan is chosen regardless of the order of the tables in the
67
+ # FROM clause.
68
+ #
69
+ do_eqp_test analyze6-1.2 {
70
+ SELECT count(*) FROM cat, ev WHERE x=y
71
+ } {
72
+ QUERY PLAN
73
+ |--SCAN cat USING COVERING INDEX catx
74
+ `--SEARCH ev USING COVERING INDEX evy (y=?)
75
+ }
76
+
77
+
78
+ # Ticket [83ea97620bd3101645138b7b0e71c12c5498fe3d] 2011-03-30
79
+ # If ANALYZE is run on an empty table, make sure indices are used
80
+ # on the table.
81
+ #
82
+ do_test analyze6-2.1 {
83
+ execsql {
84
+ CREATE TABLE t201(x INTEGER PRIMARY KEY, y UNIQUE, z);
85
+ CREATE INDEX t201z ON t201(z);
86
+ ANALYZE;
87
+ }
88
+ eqp {SELECT * FROM t201 WHERE z=5}
89
+ } {/*SEARCH t201 USING INDEX t201z (z=?)*/}
90
+ do_test analyze6-2.2 {
91
+ eqp {SELECT * FROM t201 WHERE y=5}
92
+ } {/*SEARCH t201 USING INDEX sqlite_autoindex_t201_1 (y=?)*/}
93
+ do_test analyze6-2.3 {
94
+ eqp {SELECT * FROM t201 WHERE x=5}
95
+ } {/*SEARCH t201 USING INTEGER PRIMARY KEY (rowid=?)*/}
96
+ do_test analyze6-2.4 {
97
+ execsql {
98
+ INSERT INTO t201 VALUES(1,2,3),(2,3,4),(3,4,5);
99
+ ANALYZE t201;
100
+ }
101
+ eqp {SELECT * FROM t201 WHERE z=5}
102
+ } {/*SEARCH t201 USING INDEX t201z (z=?)*/}
103
+ do_test analyze6-2.5 {
104
+ eqp {SELECT * FROM t201 WHERE y=5}
105
+ } {/*SEARCH t201 USING INDEX sqlite_autoindex_t201_1 (y=?)*/}
106
+ do_test analyze6-2.6 {
107
+ eqp {SELECT * FROM t201 WHERE x=5}
108
+ } {/*SEARCH t201 USING INTEGER PRIMARY KEY (rowid=?)*/}
109
+ do_test analyze6-2.7 {
110
+ execsql {
111
+ INSERT INTO t201 VALUES(4,5,7);
112
+ INSERT INTO t201 SELECT x+100, y+100, z+100 FROM t201;
113
+ INSERT INTO t201 SELECT x+200, y+200, z+200 FROM t201;
114
+ INSERT INTO t201 SELECT x+400, y+400, z+400 FROM t201;
115
+ ANALYZE t201;
116
+ }
117
+ eqp {SELECT * FROM t201 WHERE z=5}
118
+ } {/*SEARCH t201 USING INDEX t201z (z=?)*/}
119
+ do_test analyze6-2.8 {
120
+ eqp {SELECT * FROM t201 WHERE y=5}
121
+ } {/*SEARCH t201 USING INDEX sqlite_autoindex_t201_1 (y=?)*/}
122
+ do_test analyze6-2.9 {
123
+ eqp {SELECT * FROM t201 WHERE x=5}
124
+ } {/*SEARCH t201 USING INTEGER PRIMARY KEY (rowid=?)*/}
125
+
126
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyze8.test ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2011 August 13
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # This file implements tests for SQLite library. The focus of the tests
13
+ # in this file is testing the capabilities of sqlite_stat4.
14
+ #
15
+
16
+ set testdir [file dirname $argv0]
17
+ source $testdir/tester.tcl
18
+
19
+ ifcapable !stat4 {
20
+ finish_test
21
+ return
22
+ }
23
+
24
+ set testprefix analyze8
25
+
26
+ proc eqp {sql {db db}} {
27
+ uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
28
+ }
29
+
30
+ # Scenario:
31
+ #
32
+ # Two indices. One has mostly singleton entries, but for a few
33
+ # values there are hundreds of entries. The other has 10-20
34
+ # entries per value.
35
+ #
36
+ # Verify that the query planner chooses the first index for the singleton
37
+ # entries and the second index for the others.
38
+ #
39
+ do_test 1.0 {
40
+ db eval {
41
+ CREATE TABLE t1(a,b,c,d);
42
+ CREATE INDEX t1a ON t1(a);
43
+ CREATE INDEX t1b ON t1(b);
44
+ CREATE INDEX t1c ON t1(c);
45
+ }
46
+ for {set i 0} {$i<1000} {incr i} {
47
+ if {$i%2==0} {set a $i} {set a [expr {($i%8)*100}]}
48
+ set b [expr {$i/10}]
49
+ set c [expr {$i/8}]
50
+ set c [expr {$c*$c*$c}]
51
+ db eval {INSERT INTO t1 VALUES($a,$b,$c,$i)}
52
+ }
53
+ db eval {ANALYZE}
54
+ } {}
55
+
56
+ # The a==100 comparison is expensive because there are many rows
57
+ # with a==100. And so for those cases, choose the t1b index.
58
+ #
59
+ # Buf ro a==99 and a==101, there are far fewer rows so choose
60
+ # the t1a index.
61
+ #
62
+ do_test 1.1 {
63
+ eqp {SELECT * FROM t1 WHERE a=100 AND b=55}
64
+ } {/*SEARCH t1 USING INDEX t1b (b=?)*/}
65
+ do_test 1.2 {
66
+ eqp {SELECT * FROM t1 WHERE a=99 AND b=55}
67
+ } {/*SEARCH t1 USING INDEX t1a (a=?)*/}
68
+ do_test 1.3 {
69
+ eqp {SELECT * FROM t1 WHERE a=101 AND b=55}
70
+ } {/*SEARCH t1 USING INDEX t1a (a=?)*/}
71
+ do_test 1.4 {
72
+ eqp {SELECT * FROM t1 WHERE a=100 AND b=56}
73
+ } {/*SEARCH t1 USING INDEX t1b (b=?)*/}
74
+ do_test 1.5 {
75
+ eqp {SELECT * FROM t1 WHERE a=99 AND b=56}
76
+ } {/*SEARCH t1 USING INDEX t1a (a=?)*/}
77
+ do_test 1.6 {
78
+ eqp {SELECT * FROM t1 WHERE a=101 AND b=56}
79
+ } {/*SEARCH t1 USING INDEX t1a (a=?)*/}
80
+ do_test 2.1 {
81
+ eqp {SELECT * FROM t1 WHERE a=100 AND b BETWEEN 50 AND 54}
82
+ } {/*SEARCH t1 USING INDEX t1b (b>? AND b<?)*/}
83
+
84
+ # There are many more values of c between 0 and 100000 than there are
85
+ # between 800000 and 900000. So t1c is more selective for the latter
86
+ # range.
87
+ #
88
+ # Test 3.2 is a little unstable. It depends on the planner estimating
89
+ # that (b BETWEEN 30 AND 34) will match more rows than (c BETWEEN
90
+ # 800000 AND 900000). Which is a pretty close call (50 vs. 32), so
91
+ # the planner could get it wrong with an unlucky set of samples. This
92
+ # case happens to work, but others ("b BETWEEN 40 AND 44" for example)
93
+ # will fail.
94
+ #
95
+ do_execsql_test 3.0 {
96
+ SELECT count(*) FROM t1 WHERE b BETWEEN 30 AND 34;
97
+ SELECT count(*) FROM t1 WHERE c BETWEEN 0 AND 100000;
98
+ SELECT count(*) FROM t1 WHERE c BETWEEN 800000 AND 900000;
99
+ } {50 376 32}
100
+ do_test 3.1 {
101
+ eqp {SELECT * FROM t1 WHERE b BETWEEN 30 AND 34 AND c BETWEEN 0 AND 100000}
102
+ } {/*SEARCH t1 USING INDEX t1b (b>? AND b<?)*/}
103
+ do_test 3.2 {
104
+ eqp {SELECT * FROM t1
105
+ WHERE b BETWEEN 30 AND 34 AND c BETWEEN 800000 AND 900000}
106
+ } {/*SEARCH t1 USING INDEX t1c (c>? AND c<?)*/}
107
+ do_test 3.3 {
108
+ eqp {SELECT * FROM t1 WHERE a=100 AND c BETWEEN 0 AND 100000}
109
+ } {/*SEARCH t1 USING INDEX t1a (a=?)*/}
110
+ do_test 3.4 {
111
+ eqp {SELECT * FROM t1
112
+ WHERE a=100 AND c BETWEEN 800000 AND 900000}
113
+ } {/*SEARCH t1 USING INDEX t1c (c>? AND c<?)*/}
114
+
115
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyze9.test ADDED
@@ -0,0 +1,1235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2013 August 3
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # This file contains automated tests used to verify that the sqlite_stat4
13
+ # functionality is working.
14
+ #
15
+
16
+ set testdir [file dirname $argv0]
17
+ source $testdir/tester.tcl
18
+ set testprefix analyze9
19
+
20
+ ifcapable !stat4 {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ proc s {blob} {
26
+ set ret ""
27
+ binary scan $blob c* bytes
28
+ foreach b $bytes {
29
+ set t [binary format c $b]
30
+ if {[string is print $t]} {
31
+ append ret $t
32
+ } else {
33
+ append ret .
34
+ }
35
+ }
36
+ return $ret
37
+ }
38
+ db function s s
39
+
40
+ do_execsql_test 1.0 {
41
+ CREATE TABLE t1(a TEXT, b TEXT);
42
+ INSERT INTO t1 VALUES('(0)', '(0)');
43
+ INSERT INTO t1 VALUES('(1)', '(1)');
44
+ INSERT INTO t1 VALUES('(2)', '(2)');
45
+ INSERT INTO t1 VALUES('(3)', '(3)');
46
+ INSERT INTO t1 VALUES('(4)', '(4)');
47
+ CREATE INDEX i1 ON t1(a, b);
48
+ } {}
49
+
50
+
51
+ do_execsql_test 1.1 {
52
+ ANALYZE;
53
+ } {}
54
+
55
+ do_execsql_test 1.2 {
56
+ SELECT tbl,idx,nEq,nLt,nDLt,test_decode(sample) FROM sqlite_stat4;
57
+ } {
58
+ t1 i1 {1 1 1} {0 0 0} {0 0 0} {(0) (0) 1}
59
+ t1 i1 {1 1 1} {1 1 1} {1 1 1} {(1) (1) 2}
60
+ t1 i1 {1 1 1} {2 2 2} {2 2 2} {(2) (2) 3}
61
+ t1 i1 {1 1 1} {3 3 3} {3 3 3} {(3) (3) 4}
62
+ t1 i1 {1 1 1} {4 4 4} {4 4 4} {(4) (4) 5}
63
+ }
64
+
65
+ if {[permutation] != "utf16"} {
66
+ do_execsql_test 1.3 {
67
+ SELECT tbl,idx,nEq,nLt,nDLt,s(sample) FROM sqlite_stat4;
68
+ } {
69
+ t1 i1 {1 1 1} {0 0 0} {0 0 0} ....(0)(0)
70
+ t1 i1 {1 1 1} {1 1 1} {1 1 1} ....(1)(1).
71
+ t1 i1 {1 1 1} {2 2 2} {2 2 2} ....(2)(2).
72
+ t1 i1 {1 1 1} {3 3 3} {3 3 3} ....(3)(3).
73
+ t1 i1 {1 1 1} {4 4 4} {4 4 4} ....(4)(4).
74
+ }
75
+ }
76
+
77
+
78
+ #-------------------------------------------------------------------------
79
+ # This is really just to test SQL user function "test_decode".
80
+ #
81
+ reset_db
82
+ do_execsql_test 2.1 {
83
+ CREATE TABLE t1(a, b, c);
84
+ INSERT INTO t1 VALUES('some text', 14, NULL);
85
+ INSERT INTO t1 VALUES(22.0, NULL, x'656667');
86
+ CREATE INDEX i1 ON t1(a, b, c);
87
+ ANALYZE;
88
+ SELECT test_decode(sample) FROM sqlite_stat4;
89
+ } {
90
+ {22.0 NULL x'656667' 2}
91
+ {{some text} 14 NULL 1}
92
+ }
93
+
94
+ #-------------------------------------------------------------------------
95
+ #
96
+ reset_db
97
+ do_execsql_test 3.1 {
98
+ CREATE TABLE t2(a, b);
99
+ CREATE INDEX i2 ON t2(a, b);
100
+ BEGIN;
101
+ }
102
+
103
+ do_test 3.2 {
104
+ for {set i 0} {$i < 1000} {incr i} {
105
+ set a [expr $i / 10]
106
+ set b [expr int(rand() * 15.0)]
107
+ execsql { INSERT INTO t2 VALUES($a, $b) }
108
+ }
109
+ execsql COMMIT
110
+ } {}
111
+
112
+ db func lindex lindex
113
+
114
+ # Each value of "a" occurs exactly 10 times in the table.
115
+ #
116
+ do_execsql_test 3.3.1 {
117
+ SELECT count(*) FROM t2 GROUP BY a;
118
+ } [lrange [string repeat "10 " 100] 0 99]
119
+
120
+ # The first element in the "nEq" list of all samples should therefore be 10.
121
+ #
122
+ do_execsql_test 3.3.2 {
123
+ ANALYZE;
124
+ SELECT lindex(nEq, 0) FROM sqlite_stat4;
125
+ } [lrange [string repeat "10 " 100] 0 23]
126
+
127
+ #-------------------------------------------------------------------------
128
+ #
129
+ do_execsql_test 3.4 {
130
+ DROP TABLE IF EXISTS t1;
131
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
132
+ INSERT INTO t1 VALUES(1, 1, 'one-a');
133
+ INSERT INTO t1 VALUES(11, 1, 'one-b');
134
+ INSERT INTO t1 VALUES(21, 1, 'one-c');
135
+ INSERT INTO t1 VALUES(31, 1, 'one-d');
136
+ INSERT INTO t1 VALUES(41, 1, 'one-e');
137
+ INSERT INTO t1 VALUES(51, 1, 'one-f');
138
+ INSERT INTO t1 VALUES(61, 1, 'one-g');
139
+ INSERT INTO t1 VALUES(71, 1, 'one-h');
140
+ INSERT INTO t1 VALUES(81, 1, 'one-i');
141
+ INSERT INTO t1 VALUES(91, 1, 'one-j');
142
+ INSERT INTO t1 SELECT a+1,2,'two' || substr(c,4) FROM t1;
143
+ INSERT INTO t1 SELECT a+2,3,'three'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
144
+ INSERT INTO t1 SELECT a+3,4,'four'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
145
+ INSERT INTO t1 SELECT a+4,5,'five'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
146
+ INSERT INTO t1 SELECT a+5,6,'six'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
147
+ CREATE INDEX t1b ON t1(b);
148
+ ANALYZE;
149
+ SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND 60;
150
+ } {three-d three-e three-f}
151
+
152
+
153
+ #-------------------------------------------------------------------------
154
+ # These tests verify that the sample selection for stat4 appears to be
155
+ # working as designed.
156
+ #
157
+
158
+ reset_db
159
+ db func lindex lindex
160
+ db func lrange lrange
161
+
162
+ do_execsql_test 4.0 {
163
+ DROP TABLE IF EXISTS t1;
164
+ CREATE TABLE t1(a, b, c);
165
+ CREATE INDEX i1 ON t1(c, b, a);
166
+ }
167
+
168
+
169
+ proc insert_filler_rows_n {iStart args} {
170
+ set A(-ncopy) 1
171
+ set A(-nval) 1
172
+
173
+ foreach {k v} $args {
174
+ if {[info exists A($k)]==0} { error "no such option: $k" }
175
+ set A($k) $v
176
+ }
177
+ if {[llength $args] % 2} {
178
+ error "option requires an argument: [lindex $args end]"
179
+ }
180
+
181
+ for {set i 0} {$i < $A(-nval)} {incr i} {
182
+ set iVal [expr $iStart+$i]
183
+ for {set j 0} {$j < $A(-ncopy)} {incr j} {
184
+ execsql { INSERT INTO t1 VALUES($iVal, $iVal, $iVal) }
185
+ }
186
+ }
187
+ }
188
+
189
+ do_test 4.1 {
190
+ execsql { BEGIN }
191
+ insert_filler_rows_n 0 -ncopy 10 -nval 19
192
+ insert_filler_rows_n 20 -ncopy 1 -nval 100
193
+
194
+ execsql {
195
+ INSERT INTO t1(c, b, a) VALUES(200, 1, 'a');
196
+ INSERT INTO t1(c, b, a) VALUES(200, 1, 'b');
197
+ INSERT INTO t1(c, b, a) VALUES(200, 1, 'c');
198
+
199
+ INSERT INTO t1(c, b, a) VALUES(200, 2, 'e');
200
+ INSERT INTO t1(c, b, a) VALUES(200, 2, 'f');
201
+
202
+ INSERT INTO t1(c, b, a) VALUES(201, 3, 'g');
203
+ INSERT INTO t1(c, b, a) VALUES(201, 4, 'h');
204
+
205
+ ANALYZE;
206
+ SELECT count(*) FROM sqlite_stat4;
207
+ SELECT count(*) FROM t1;
208
+ }
209
+ } {24 297}
210
+
211
+ do_execsql_test 4.2 {
212
+ SELECT
213
+ neq,
214
+ lrange(nlt, 0, 2),
215
+ lrange(ndlt, 0, 2),
216
+ lrange(test_decode(sample), 0, 2)
217
+ FROM sqlite_stat4
218
+ ORDER BY rowid LIMIT 16;
219
+ } {
220
+ {10 10 10 1} {0 0 0} {0 0 0} {0 0 0}
221
+ {10 10 10 1} {10 10 10} {1 1 1} {1 1 1}
222
+ {10 10 10 1} {20 20 20} {2 2 2} {2 2 2}
223
+ {10 10 10 1} {30 30 30} {3 3 3} {3 3 3}
224
+ {10 10 10 1} {40 40 40} {4 4 4} {4 4 4}
225
+ {10 10 10 1} {50 50 50} {5 5 5} {5 5 5}
226
+ {10 10 10 1} {60 60 60} {6 6 6} {6 6 6}
227
+ {10 10 10 1} {70 70 70} {7 7 7} {7 7 7}
228
+ {10 10 10 1} {80 80 80} {8 8 8} {8 8 8}
229
+ {10 10 10 1} {90 90 90} {9 9 9} {9 9 9}
230
+ {10 10 10 1} {100 100 100} {10 10 10} {10 10 10}
231
+ {10 10 10 1} {110 110 110} {11 11 11} {11 11 11}
232
+ {10 10 10 1} {120 120 120} {12 12 12} {12 12 12}
233
+ {10 10 10 1} {130 130 130} {13 13 13} {13 13 13}
234
+ {10 10 10 1} {140 140 140} {14 14 14} {14 14 14}
235
+ {10 10 10 1} {150 150 150} {15 15 15} {15 15 15}
236
+ }
237
+
238
+ do_execsql_test 4.3 {
239
+ SELECT
240
+ neq,
241
+ lrange(nlt, 0, 2),
242
+ lrange(ndlt, 0, 2),
243
+ lrange(test_decode(sample), 0, 1)
244
+ FROM sqlite_stat4
245
+ ORDER BY rowid DESC LIMIT 2;
246
+ } {
247
+ {2 1 1 1} {295 296 296} {120 122 125} {201 4}
248
+ {5 3 1 1} {290 290 290} {119 119 119} {200 1}
249
+ }
250
+
251
+ do_execsql_test 4.4 { SELECT count(DISTINCT c) FROM t1 WHERE c<201 } 120
252
+ do_execsql_test 4.5 { SELECT count(DISTINCT c) FROM t1 WHERE c<200 } 119
253
+
254
+ # Check that the perioidic samples are present.
255
+ do_execsql_test 4.6 {
256
+ SELECT count(*) FROM sqlite_stat4
257
+ WHERE lindex(test_decode(sample), 3) IN
258
+ ('34', '68', '102', '136', '170', '204', '238', '272')
259
+ } {8}
260
+
261
+ reset_db
262
+ do_test 4.7 {
263
+ execsql {
264
+ BEGIN;
265
+ CREATE TABLE t1(o,t INTEGER PRIMARY KEY);
266
+ CREATE INDEX i1 ON t1(o);
267
+ }
268
+ for {set i 0} {$i<10000} {incr i [expr (($i<1000)?1:10)]} {
269
+ execsql { INSERT INTO t1 VALUES('x', $i) }
270
+ }
271
+ execsql {
272
+ COMMIT;
273
+ ANALYZE;
274
+ SELECT count(*) FROM sqlite_stat4;
275
+ }
276
+ } {8}
277
+ do_execsql_test 4.8 {
278
+ SELECT test_decode(sample) FROM sqlite_stat4;
279
+ } {
280
+ {x 211} {x 423} {x 635} {x 847}
281
+ {x 1590} {x 3710} {x 5830} {x 7950}
282
+ }
283
+
284
+
285
+ #-------------------------------------------------------------------------
286
+ # The following would cause a crash at one point.
287
+ #
288
+ reset_db
289
+ do_execsql_test 5.1 {
290
+ PRAGMA encoding = 'utf-16';
291
+ CREATE TABLE t0(v);
292
+ ANALYZE;
293
+ }
294
+
295
+ #-------------------------------------------------------------------------
296
+ # This was also crashing (corrupt sqlite_stat4 table).
297
+ #
298
+ reset_db
299
+ do_execsql_test 6.1 {
300
+ CREATE TABLE t1(a, b);
301
+ CREATE INDEX i1 ON t1(a);
302
+ CREATE INDEX i2 ON t1(b);
303
+ INSERT INTO t1 VALUES(1, 1);
304
+ INSERT INTO t1 VALUES(2, 2);
305
+ INSERT INTO t1 VALUES(3, 3);
306
+ INSERT INTO t1 VALUES(4, 4);
307
+ INSERT INTO t1 VALUES(5, 5);
308
+ ANALYZE;
309
+ PRAGMA writable_schema = 1;
310
+ CREATE TEMP TABLE x1 AS
311
+ SELECT tbl,idx,neq,nlt,ndlt,sample FROM sqlite_stat4
312
+ ORDER BY (rowid%5), rowid;
313
+ DELETE FROM sqlite_stat4;
314
+ INSERT INTO sqlite_stat4 SELECT * FROM x1;
315
+ PRAGMA writable_schema = 0;
316
+ ANALYZE sqlite_master;
317
+ }
318
+ do_execsql_test 6.2 {
319
+ SELECT * FROM t1 WHERE a = 'abc';
320
+ }
321
+
322
+ #-------------------------------------------------------------------------
323
+ # The following tests experiment with adding corrupted records to the
324
+ # 'sample' column of the sqlite_stat4 table.
325
+ #
326
+ reset_db
327
+ sqlite3_db_config_lookaside db 0 0 0
328
+
329
+ database_may_be_corrupt
330
+ do_execsql_test 7.1 {
331
+ CREATE TABLE t1(a, b);
332
+ CREATE INDEX i1 ON t1(a, b);
333
+ INSERT INTO t1 VALUES(1, 1);
334
+ INSERT INTO t1 VALUES(2, 2);
335
+ INSERT INTO t1 VALUES(3, 3);
336
+ INSERT INTO t1 VALUES(4, 4);
337
+ INSERT INTO t1 VALUES(5, 5);
338
+ ANALYZE;
339
+ UPDATE sqlite_stat4 SET sample = X'' WHERE rowid = 1;
340
+ ANALYZE sqlite_master;
341
+ }
342
+
343
+ do_execsql_test 7.2 {
344
+ UPDATE sqlite_stat4 SET sample = X'FFFF';
345
+ ANALYZE sqlite_master;
346
+ SELECT * FROM t1 WHERE a = 1;
347
+ } {1 1}
348
+
349
+ do_execsql_test 7.3 {
350
+ ANALYZE;
351
+ UPDATE sqlite_stat4 SET neq = '0 0 0';
352
+ ANALYZE sqlite_master;
353
+ SELECT * FROM t1 WHERE a = 1;
354
+ } {1 1}
355
+
356
+ do_execsql_test 7.4 {
357
+ ANALYZE;
358
+ UPDATE sqlite_stat4 SET ndlt = '0 0 0';
359
+ ANALYZE sqlite_master;
360
+ SELECT * FROM t1 WHERE a = 3;
361
+ } {3 3}
362
+
363
+ do_execsql_test 7.5 {
364
+ ANALYZE;
365
+ UPDATE sqlite_stat4 SET nlt = '0 0 0';
366
+ ANALYZE sqlite_master;
367
+ SELECT * FROM t1 WHERE a = 5;
368
+ } {5 5}
369
+
370
+ database_never_corrupt
371
+
372
+ #-------------------------------------------------------------------------
373
+ #
374
+ reset_db
375
+ do_execsql_test 8.1 {
376
+ CREATE TABLE t1(x TEXT);
377
+ CREATE INDEX i1 ON t1(x);
378
+ INSERT INTO t1 VALUES('1');
379
+ INSERT INTO t1 VALUES('2');
380
+ INSERT INTO t1 VALUES('3');
381
+ INSERT INTO t1 VALUES('4');
382
+ ANALYZE;
383
+ }
384
+ do_execsql_test 8.2 {
385
+ SELECT * FROM t1 WHERE x = 3;
386
+ } {3}
387
+
388
+ #-------------------------------------------------------------------------
389
+ # Check that the bug fixed by [91733bc485] really is fixed.
390
+ #
391
+ reset_db
392
+ do_execsql_test 9.1 {
393
+ CREATE TABLE t1(a, b, c, d, e);
394
+ CREATE INDEX i1 ON t1(a, b, c, d);
395
+ CREATE INDEX i2 ON t1(e);
396
+ }
397
+ do_test 9.2 {
398
+ execsql BEGIN;
399
+ for {set i 0} {$i < 100} {incr i} {
400
+ execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])"
401
+ }
402
+ for {set i 0} {$i < 21} {incr i} {
403
+ execsql "INSERT INTO t1 VALUES('x', 'y', 'z', 101, $i)"
404
+ }
405
+ for {set i 102} {$i < 200} {incr i} {
406
+ execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])"
407
+ }
408
+ execsql COMMIT
409
+ execsql ANALYZE
410
+ } {}
411
+
412
+ do_eqp_test 9.3.1 {
413
+ SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=101 AND e=5;
414
+ } {/t1 USING INDEX i2/}
415
+ do_eqp_test 9.3.2 {
416
+ SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=99 AND e=5;
417
+ } {/t1 USING INDEX i1/}
418
+
419
+ set value_d [expr 101]
420
+ do_eqp_test 9.4.1 {
421
+ SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5
422
+ } {/t1 USING INDEX i2/}
423
+ set value_d [expr 99]
424
+ do_eqp_test 9.4.2 {
425
+ SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5
426
+ } {/t1 USING INDEX i1/}
427
+
428
+ #-------------------------------------------------------------------------
429
+ # Check that the planner takes stat4 data into account when considering
430
+ # "IS NULL" and "IS NOT NULL" constraints.
431
+ #
432
+ do_execsql_test 10.1.1 {
433
+ DROP TABLE IF EXISTS t3;
434
+ CREATE TABLE t3(a, b);
435
+ CREATE INDEX t3a ON t3(a);
436
+ CREATE INDEX t3b ON t3(b);
437
+ }
438
+ do_test 10.1.2 {
439
+ for {set i 1} {$i < 100} {incr i} {
440
+ if {$i>90} { set a $i } else { set a NULL }
441
+ set b [expr $i % 5]
442
+ execsql "INSERT INTO t3 VALUES($a, $b)"
443
+ }
444
+ execsql ANALYZE
445
+ } {}
446
+ do_eqp_test 10.1.3 {
447
+ SELECT * FROM t3 WHERE a IS NULL AND b = 2
448
+ } {/t3 USING INDEX t3b/}
449
+ do_eqp_test 10.1.4 {
450
+ SELECT * FROM t3 WHERE a IS NOT NULL AND b = 2
451
+ } {/t3 USING INDEX t3a/}
452
+
453
+ do_execsql_test 10.2.1 {
454
+ DROP TABLE IF EXISTS t3;
455
+ CREATE TABLE t3(x, a, b);
456
+ CREATE INDEX t3a ON t3(x, a);
457
+ CREATE INDEX t3b ON t3(x, b);
458
+ }
459
+ do_test 10.2.2 {
460
+ for {set i 1} {$i < 100} {incr i} {
461
+ if {$i>90} { set a $i } else { set a NULL }
462
+ set b [expr $i % 5]
463
+ execsql "INSERT INTO t3 VALUES('xyz', $a, $b)"
464
+ }
465
+ execsql ANALYZE
466
+ } {}
467
+ do_eqp_test 10.2.3 {
468
+ SELECT * FROM t3 WHERE x = 'xyz' AND a IS NULL AND b = 2
469
+ } {/t3 USING INDEX t3b/}
470
+ do_eqp_test 10.2.4 {
471
+ SELECT * FROM t3 WHERE x = 'xyz' AND a IS NOT NULL AND b = 2
472
+ } {/t3 USING INDEX t3a/}
473
+
474
+ #-------------------------------------------------------------------------
475
+ # Check that stat4 data is used correctly with non-default collation
476
+ # sequences.
477
+ #
478
+ foreach {tn schema} {
479
+ 1 {
480
+ CREATE TABLE t4(a COLLATE nocase, b);
481
+ CREATE INDEX t4a ON t4(a);
482
+ CREATE INDEX t4b ON t4(b);
483
+ }
484
+ 2 {
485
+ CREATE TABLE t4(a, b);
486
+ CREATE INDEX t4a ON t4(a COLLATE nocase);
487
+ CREATE INDEX t4b ON t4(b);
488
+ }
489
+ } {
490
+ drop_all_tables
491
+ do_test 11.$tn.1 { execsql $schema } {}
492
+
493
+ do_test 11.$tn.2 {
494
+ for {set i 0} {$i < 100} {incr i} {
495
+ if { ($i % 10)==0 } { set a ABC } else { set a DEF }
496
+ set b [expr $i % 5]
497
+ execsql { INSERT INTO t4 VALUES($a, $b) }
498
+ }
499
+ execsql ANALYZE
500
+ } {}
501
+
502
+ do_eqp_test 11.$tn.3 {
503
+ SELECT * FROM t4 WHERE a = 'def' AND b = 3;
504
+ } {/t4 USING INDEX t4b/}
505
+
506
+ if {$tn==1} {
507
+ set sql "SELECT * FROM t4 WHERE a = 'abc' AND b = 3;"
508
+ do_eqp_test 11.$tn.4 $sql {/t4 USING INDEX t4a/}
509
+ } else {
510
+
511
+ set sql "SELECT * FROM t4 WHERE a = 'abc' COLLATE nocase AND b = 3;"
512
+ do_eqp_test 11.$tn.5 $sql {/t4 USING INDEX t4a/}
513
+
514
+ set sql "SELECT * FROM t4 WHERE a COLLATE nocase = 'abc' AND b = 3;"
515
+ do_eqp_test 11.$tn.6 $sql {/t4 USING INDEX t4a/}
516
+ }
517
+ }
518
+
519
+ foreach {tn schema} {
520
+ 1 {
521
+ CREATE TABLE t4(x, a COLLATE nocase, b);
522
+ CREATE INDEX t4a ON t4(x, a);
523
+ CREATE INDEX t4b ON t4(x, b);
524
+ }
525
+ 2 {
526
+ CREATE TABLE t4(x, a, b);
527
+ CREATE INDEX t4a ON t4(x, a COLLATE nocase);
528
+ CREATE INDEX t4b ON t4(x, b);
529
+ }
530
+ } {
531
+ drop_all_tables
532
+ do_test 12.$tn.1 { execsql $schema } {}
533
+
534
+ do_test 12.$tn.2 {
535
+ for {set i 0} {$i < 100} {incr i} {
536
+ if { ($i % 10)==0 } { set a ABC } else { set a DEF }
537
+ set b [expr $i % 5]
538
+ execsql { INSERT INTO t4 VALUES(X'abcdef', $a, $b) }
539
+ }
540
+ execsql ANALYZE
541
+ } {}
542
+
543
+ do_eqp_test 12.$tn.3 {
544
+ SELECT * FROM t4 WHERE x=X'abcdef' AND a = 'def' AND b = 3;
545
+ } {/t4 USING INDEX t4b/}
546
+
547
+ if {$tn==1} {
548
+ set sql "SELECT * FROM t4 WHERE x=X'abcdef' AND a = 'abc' AND b = 3;"
549
+ do_eqp_test 12.$tn.4 $sql {/t4 USING INDEX t4a/}
550
+ } else {
551
+ set sql {
552
+ SELECT * FROM t4 WHERE x=X'abcdef' AND a = 'abc' COLLATE nocase AND b = 3
553
+ }
554
+ do_eqp_test 12.$tn.5 $sql {/t4 USING INDEX t4a/}
555
+ set sql {
556
+ SELECT * FROM t4 WHERE x=X'abcdef' AND a COLLATE nocase = 'abc' AND b = 3
557
+ }
558
+ do_eqp_test 12.$tn.6 $sql {/t4 USING INDEX t4a/}
559
+ }
560
+ }
561
+
562
+ #-------------------------------------------------------------------------
563
+ # Check that affinities are taken into account when using stat4 data to
564
+ # estimate the number of rows scanned by a rowid constraint.
565
+ #
566
+ drop_all_tables
567
+ do_test 13.1 {
568
+ execsql {
569
+ CREATE TABLE t1(a, b, c, d);
570
+ CREATE INDEX i1 ON t1(a);
571
+ CREATE INDEX i2 ON t1(b, c);
572
+ }
573
+ for {set i 0} {$i<100} {incr i} {
574
+ if {$i %2} {set a abc} else {set a def}
575
+ execsql { INSERT INTO t1(rowid, a, b, c) VALUES($i, $a, $i, $i) }
576
+ }
577
+ execsql ANALYZE
578
+ } {}
579
+ do_eqp_test 13.2.1 {
580
+ SELECT * FROM t1 WHERE a='abc' AND rowid<15 AND b<12
581
+ } {/SEARCH t1 USING INDEX i1/}
582
+ do_eqp_test 13.2.2 {
583
+ SELECT * FROM t1 WHERE a='abc' AND rowid<'15' AND b<12
584
+ } {/SEARCH t1 USING INDEX i1/}
585
+ do_eqp_test 13.3.1 {
586
+ SELECT * FROM t1 WHERE a='abc' AND rowid<100 AND b<12
587
+ } {/SEARCH t1 USING INDEX i2/}
588
+ do_eqp_test 13.3.2 {
589
+ SELECT * FROM t1 WHERE a='abc' AND rowid<'100' AND b<12
590
+ } {/SEARCH t1 USING INDEX i2/}
591
+
592
+ #-------------------------------------------------------------------------
593
+ # Check also that affinities are taken into account when using stat4 data
594
+ # to estimate the number of rows scanned by any other constraint on a
595
+ # column other than the leftmost.
596
+ #
597
+ drop_all_tables
598
+ do_test 14.1 {
599
+ execsql { CREATE TABLE t1(a, b INTEGER, c) }
600
+ for {set i 0} {$i<100} {incr i} {
601
+ set c [expr $i % 3]
602
+ execsql { INSERT INTO t1 VALUES('ott', $i, $c) }
603
+ }
604
+ execsql {
605
+ CREATE INDEX i1 ON t1(a, b);
606
+ CREATE INDEX i2 ON t1(c);
607
+ ANALYZE;
608
+ }
609
+ } {}
610
+ do_eqp_test 13.2.1 {
611
+ SELECT * FROM t1 WHERE a='ott' AND b<10 AND c=1
612
+ } {/SEARCH t1 USING INDEX i1/}
613
+ do_eqp_test 13.2.2 {
614
+ SELECT * FROM t1 WHERE a='ott' AND b<'10' AND c=1
615
+ } {/SEARCH t1 USING INDEX i1/}
616
+
617
+ #-------------------------------------------------------------------------
618
+ # By default, 16 non-periodic samples are collected for the stat4 table.
619
+ # The following tests attempt to verify that the most common keys are
620
+ # being collected.
621
+ #
622
+ proc check_stat4 {tn} {
623
+ db eval ANALYZE
624
+ db eval {SELECT a, b, c, d FROM t1} {
625
+ incr k($a)
626
+ incr k([list $a $b])
627
+ incr k([list $a $b $c])
628
+ if { [info exists k([list $a $b $c $d])]==0 } { incr nRow }
629
+ incr k([list $a $b $c $d])
630
+ }
631
+
632
+ set L [list]
633
+ foreach key [array names k] {
634
+ lappend L [list $k($key) $key]
635
+ }
636
+
637
+ set nSample $nRow
638
+ if {$nSample>16} {set nSample 16}
639
+
640
+ set nThreshold [lindex [lsort -decr -integer -index 0 $L] [expr $nSample-1] 0]
641
+ foreach key [array names k] {
642
+ if {$k($key)>$nThreshold} {
643
+ set expect($key) 1
644
+ }
645
+ if {$k($key)==$nThreshold} {
646
+ set possible($key) 1
647
+ }
648
+ }
649
+
650
+
651
+ set nPossible [expr $nSample - [llength [array names expect]]]
652
+
653
+ #puts "EXPECT: [array names expect]"
654
+ #puts "POSSIBLE($nPossible/[array size possible]): [array names possible]"
655
+ #puts "HAVE: [db eval {SELECT test_decode(sample) FROM sqlite_stat4 WHERE idx='i1'}]"
656
+
657
+ db eval {SELECT test_decode(sample) AS s FROM sqlite_stat4 WHERE idx='i1'} {
658
+ set seen 0
659
+ for {set i 0} {$i<4} {incr i} {
660
+ unset -nocomplain expect([lrange $s 0 $i])
661
+ if {[info exists possible([lrange $s 0 $i])]} {
662
+ set seen 1
663
+ unset -nocomplain possible([lrange $s 0 $i])
664
+ }
665
+ }
666
+ if {$seen} {incr nPossible -1}
667
+ }
668
+ if {$nPossible<0} {set nPossible 0}
669
+
670
+ set res [list [llength [array names expect]] $nPossible]
671
+ uplevel [list do_test $tn [list set {} $res] {0 0}]
672
+ }
673
+
674
+ drop_all_tables
675
+ do_test 14.1.1 {
676
+ execsql {
677
+ CREATE TABLE t1(a,b,c,d);
678
+ CREATE INDEX i1 ON t1(a,b,c,d);
679
+ }
680
+ for {set i 0} {$i < 160} {incr i} {
681
+ execsql { INSERT INTO t1 VALUES($i,$i,$i,$i) }
682
+ if {($i % 10)==0} { execsql { INSERT INTO t1 VALUES($i,$i,$i,$i) } }
683
+ }
684
+ } {}
685
+ check_stat4 14.1.2
686
+
687
+ do_test 14.2.1 {
688
+ execsql { DELETE FROM t1 }
689
+ for {set i 0} {$i < 1600} {incr i} {
690
+ execsql { INSERT INTO t1 VALUES($i/10,$i/17,$i/27,$i/37) }
691
+ }
692
+ } {}
693
+ check_stat4 14.2.2
694
+
695
+ do_test 14.3.1 {
696
+ for {set i 0} {$i < 10} {incr i} {
697
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
698
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
699
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
700
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
701
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
702
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
703
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
704
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
705
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
706
+ execsql { INSERT INTO t1 VALUES($i*50,$i*50,$i*50,$i*50) }
707
+ }
708
+ } {}
709
+ check_stat4 14.3.2
710
+
711
+ do_test 14.4.1 {
712
+ execsql {DELETE FROM t1}
713
+ for {set i 1} {$i < 160} {incr i} {
714
+ set b [expr $i % 10]
715
+ if {$b==0 || $b==2} {set b 1}
716
+ execsql { INSERT INTO t1 VALUES($i/10,$b,$i,$i) }
717
+ }
718
+ } {}
719
+ check_stat4 14.4.2
720
+ db func lrange lrange
721
+ db func lindex lindex
722
+ do_execsql_test 14.4.3 {
723
+ SELECT lrange(test_decode(sample), 0, 1) AS s FROM sqlite_stat4
724
+ WHERE lindex(s, 1)=='1' ORDER BY rowid
725
+ } {
726
+ {0 1} {1 1} {2 1} {3 1}
727
+ {4 1} {5 1} {6 1} {7 1}
728
+ {8 1} {9 1} {10 1} {11 1}
729
+ {12 1} {13 1} {14 1} {15 1}
730
+ }
731
+
732
+ #-------------------------------------------------------------------------
733
+ # Test that nothing untoward happens if the stat4 table contains entries
734
+ # for indexes that do not exist. Or NULL values in the idx column.
735
+ # Or NULL values in any of the other columns.
736
+ #
737
+ drop_all_tables
738
+ do_execsql_test 15.1 {
739
+ CREATE TABLE x1(a, b, UNIQUE(a, b));
740
+ INSERT INTO x1 VALUES(1, 2);
741
+ INSERT INTO x1 VALUES(3, 4);
742
+ INSERT INTO x1 VALUES(5, 6);
743
+ ANALYZE;
744
+ INSERT INTO sqlite_stat4 VALUES(NULL, NULL, NULL, NULL, NULL, NULL);
745
+ }
746
+ db close
747
+ sqlite3 db test.db
748
+ do_execsql_test 15.2 { SELECT * FROM x1 } {1 2 3 4 5 6}
749
+
750
+ do_execsql_test 15.3 {
751
+ INSERT INTO sqlite_stat4 VALUES(42, 42, 42, 42, 42, 42);
752
+ }
753
+ db close
754
+ sqlite3 db test.db
755
+ do_execsql_test 15.4 { SELECT * FROM x1 } {1 2 3 4 5 6}
756
+
757
+ do_execsql_test 15.5 {
758
+ UPDATE sqlite_stat1 SET stat = NULL;
759
+ }
760
+ db close
761
+ sqlite3 db test.db
762
+ do_execsql_test 15.6 { SELECT * FROM x1 } {1 2 3 4 5 6}
763
+
764
+ do_execsql_test 15.7 {
765
+ ANALYZE;
766
+ UPDATE sqlite_stat1 SET tbl = 'no such tbl';
767
+ }
768
+ db close
769
+ sqlite3 db test.db
770
+ do_execsql_test 15.8 { SELECT * FROM x1 } {1 2 3 4 5 6}
771
+
772
+ do_execsql_test 15.9 {
773
+ ANALYZE;
774
+ UPDATE sqlite_stat4 SET neq = NULL, nlt=NULL, ndlt=NULL;
775
+ }
776
+ db close
777
+ sqlite3 db test.db
778
+ do_execsql_test 15.10 { SELECT * FROM x1 } {1 2 3 4 5 6}
779
+
780
+ # This is just for coverage....
781
+ do_execsql_test 15.11 {
782
+ ANALYZE;
783
+ UPDATE sqlite_stat1 SET stat = stat || ' unordered';
784
+ }
785
+ db close
786
+ sqlite3 db test.db
787
+ do_execsql_test 15.12 { SELECT * FROM x1 } {1 2 3 4 5 6}
788
+
789
+ #-------------------------------------------------------------------------
790
+ # Test that allocations used for sqlite_stat4 samples are included in
791
+ # the quantity returned by SQLITE_DBSTATUS_SCHEMA_USED.
792
+ #
793
+ set one [string repeat x 1000]
794
+ set two [string repeat x 2000]
795
+ do_test 16.1 {
796
+ reset_db
797
+ execsql {
798
+ CREATE TABLE t1(a, UNIQUE(a));
799
+ INSERT INTO t1 VALUES($one);
800
+ ANALYZE;
801
+ }
802
+ set nByte [lindex [sqlite3_db_status db SCHEMA_USED 0] 1]
803
+
804
+ reset_db
805
+ execsql {
806
+ CREATE TABLE t1(a, UNIQUE(a));
807
+ INSERT INTO t1 VALUES($two);
808
+ ANALYZE;
809
+ }
810
+ set nByte2 [lindex [sqlite3_db_status db SCHEMA_USED 0] 1]
811
+ puts -nonewline " (nByte=$nByte nByte2=$nByte2)"
812
+
813
+ expr {$nByte2 > $nByte+900 && $nByte2 < $nByte+1100}
814
+ } {1}
815
+
816
+ #-------------------------------------------------------------------------
817
+ # Test that stat4 data may be used with partial indexes.
818
+ #
819
+ do_test 17.1 {
820
+ reset_db
821
+ execsql {
822
+ CREATE TABLE t1(a, b, c, d);
823
+ CREATE INDEX i1 ON t1(a, b) WHERE d IS NOT NULL;
824
+ INSERT INTO t1 VALUES(-1, -1, -1, NULL);
825
+ INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
826
+ INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
827
+ INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
828
+ INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
829
+ INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
830
+ INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
831
+ }
832
+
833
+ for {set i 0} {$i < 32} {incr i} {
834
+ if {$i<8} {set b 0} else { set b $i }
835
+ execsql { INSERT INTO t1 VALUES($i%2, $b, $i/2, 'abc') }
836
+ }
837
+ execsql {ANALYZE main.t1}
838
+ } {}
839
+
840
+ do_catchsql_test 17.1.2 {
841
+ ANALYZE temp.t1;
842
+ } {1 {no such table: temp.t1}}
843
+
844
+ do_eqp_test 17.2 {
845
+ SELECT * FROM t1 WHERE d IS NOT NULL AND a=0 AND b=10 AND c=10;
846
+ } {/USING INDEX i1/}
847
+ do_eqp_test 17.3 {
848
+ SELECT * FROM t1 WHERE d IS NOT NULL AND a=0 AND b=0 AND c=10;
849
+ } {/USING INDEX i1/}
850
+
851
+ do_execsql_test 17.4 {
852
+ CREATE INDEX i2 ON t1(c, d);
853
+ ANALYZE main.i2;
854
+ }
855
+ do_eqp_test 17.5 {
856
+ SELECT * FROM t1 WHERE d IS NOT NULL AND a=0 AND b=10 AND c=10;
857
+ } {/USING INDEX i1/}
858
+ do_eqp_test 17.6 {
859
+ SELECT * FROM t1 WHERE d IS NOT NULL AND a=0 AND b=0 AND c=10;
860
+ } {/USING INDEX i2/}
861
+
862
+ #-------------------------------------------------------------------------
863
+ #
864
+ do_test 18.1 {
865
+ reset_db
866
+ execsql {
867
+ CREATE TABLE t1(a, b);
868
+ CREATE INDEX i1 ON t1(a, b);
869
+ }
870
+ for {set i 0} {$i < 9} {incr i} {
871
+ execsql {
872
+ INSERT INTO t1 VALUES($i, 0);
873
+ INSERT INTO t1 VALUES($i, 0);
874
+ INSERT INTO t1 VALUES($i, 0);
875
+ INSERT INTO t1 VALUES($i, 0);
876
+ INSERT INTO t1 VALUES($i, 0);
877
+ INSERT INTO t1 VALUES($i, 0);
878
+ INSERT INTO t1 VALUES($i, 0);
879
+ INSERT INTO t1 VALUES($i, 0);
880
+ INSERT INTO t1 VALUES($i, 0);
881
+ INSERT INTO t1 VALUES($i, 0);
882
+ INSERT INTO t1 VALUES($i, 0);
883
+ INSERT INTO t1 VALUES($i, 0);
884
+ INSERT INTO t1 VALUES($i, 0);
885
+ INSERT INTO t1 VALUES($i, 0);
886
+ INSERT INTO t1 VALUES($i, 0);
887
+ }
888
+ }
889
+ execsql ANALYZE
890
+ execsql { SELECT count(*) FROM sqlite_stat4 }
891
+ } {9}
892
+
893
+ #-------------------------------------------------------------------------
894
+ # For coverage.
895
+ #
896
+ ifcapable view {
897
+ do_test 19.1 {
898
+ reset_db
899
+ execsql {
900
+ CREATE TABLE t1(x, y);
901
+ CREATE INDEX i1 ON t1(x, y);
902
+ CREATE VIEW v1 AS SELECT * FROM t1;
903
+ ANALYZE;
904
+ }
905
+ } {}
906
+ }
907
+ ifcapable auth {
908
+ proc authproc {op args} {
909
+ if {$op == "SQLITE_ANALYZE"} { return "SQLITE_DENY" }
910
+ return "SQLITE_OK"
911
+ }
912
+ do_test 19.2 {
913
+ reset_db
914
+ db auth authproc
915
+ execsql {
916
+ CREATE TABLE t1(x, y);
917
+ CREATE VIEW v1 AS SELECT * FROM t1;
918
+ }
919
+ catchsql ANALYZE
920
+ } {1 {not authorized}}
921
+ }
922
+
923
+ #-------------------------------------------------------------------------
924
+ #
925
+ reset_db
926
+ proc r {args} { expr rand() }
927
+ db func r r
928
+ db func lrange lrange
929
+ do_test 20.1 {
930
+ execsql {
931
+ CREATE TABLE t1(a,b,c,d);
932
+ CREATE INDEX i1 ON t1(a,b,c,d);
933
+ }
934
+ for {set i 0} {$i < 16} {incr i} {
935
+ execsql {
936
+ INSERT INTO t1 VALUES($i, r(), r(), r());
937
+ INSERT INTO t1 VALUES($i, $i, r(), r());
938
+ INSERT INTO t1 VALUES($i, $i, $i, r());
939
+ INSERT INTO t1 VALUES($i, $i, $i, $i);
940
+ INSERT INTO t1 VALUES($i, $i, $i, $i);
941
+ INSERT INTO t1 VALUES($i, $i, $i, r());
942
+ INSERT INTO t1 VALUES($i, $i, r(), r());
943
+ INSERT INTO t1 VALUES($i, r(), r(), r());
944
+ }
945
+ }
946
+ } {}
947
+ do_execsql_test 20.2 { ANALYZE }
948
+ for {set i 0} {$i<16} {incr i} {
949
+ set val "$i $i $i $i"
950
+ do_execsql_test 20.3.$i {
951
+ SELECT count(*) FROM sqlite_stat4
952
+ WHERE lrange(test_decode(sample), 0, 3)=$val
953
+ } {1}
954
+ }
955
+
956
+ #-------------------------------------------------------------------------
957
+ #
958
+ reset_db
959
+
960
+ do_execsql_test 21.0 {
961
+ CREATE TABLE t2(a, b);
962
+ CREATE INDEX i2 ON t2(a);
963
+ }
964
+
965
+ do_test 21.1 {
966
+ for {set i 1} {$i < 100} {incr i} {
967
+ execsql {
968
+ INSERT INTO t2 VALUES(CASE WHEN $i < 80 THEN 'one' ELSE 'two' END, $i)
969
+ }
970
+ }
971
+ execsql ANALYZE
972
+ } {}
973
+
974
+ # Condition (a='one') matches 80% of the table. (rowid<10) reduces this to
975
+ # 10%, but (rowid<50) only reduces it to 50%. So in the first case below
976
+ # the index is used. In the second, it is not.
977
+ #
978
+ do_eqp_test 21.2 {
979
+ SELECT * FROM t2 WHERE a='one' AND rowid < 10
980
+ } {/*USING INDEX i2 (a=? AND rowid<?)*/}
981
+ do_eqp_test 21.3 {
982
+ SELECT * FROM t2 WHERE a='one' AND rowid < 50
983
+ } {/*USING INTEGER PRIMARY KEY*/}
984
+
985
+ #-------------------------------------------------------------------------
986
+ #
987
+ reset_db
988
+ do_execsql_test 22.0 {
989
+ CREATE TABLE t3(a, b, c, d, PRIMARY KEY(a, b)) WITHOUT ROWID;
990
+ SELECT * FROM t3;
991
+ } {}
992
+ do_execsql_test 22.1 {
993
+ WITH r(x) AS (
994
+ SELECT 1
995
+ UNION ALL
996
+ SELECT x+1 FROM r WHERE x<=100
997
+ )
998
+
999
+ INSERT INTO t3 SELECT
1000
+ CASE WHEN (x>45 AND x<96) THEN 'B' ELSE 'A' END, /* Column "a" */
1001
+ x, /* Column "b" */
1002
+ CASE WHEN (x<51) THEN 'one' ELSE 'two' END, /* Column "c" */
1003
+ x /* Column "d" */
1004
+ FROM r;
1005
+
1006
+ CREATE INDEX i3 ON t3(c);
1007
+ CREATE INDEX i4 ON t3(d);
1008
+ ANALYZE;
1009
+ }
1010
+
1011
+ # Expression (c='one' AND a='B') matches 5 table rows. But (c='one' AND a=A')
1012
+ # matches 45. Expression (d<?) matches 20. Neither index is a covering index.
1013
+ #
1014
+ # Therefore, with stat4 data, SQLite prefers (c='one' AND a='B') over (d<20),
1015
+ # and (d<20) over (c='one' AND a='A').
1016
+ foreach {tn where res} {
1017
+ 1 "c='one' AND a='B' AND d < 20" {/*INDEX i3 (c=? AND a=?)*/}
1018
+ 2 "c='one' AND a='A' AND d < 20" {/*INDEX i4 (d<?)*/}
1019
+ } {
1020
+ do_eqp_test 22.2.$tn "SELECT * FROM t3 WHERE $where" $res
1021
+ }
1022
+
1023
+ proc int_to_char {i} {
1024
+ set ret ""
1025
+ set char [list a b c d e f g h i j]
1026
+ foreach {div} {1000 100 10 1} {
1027
+ append ret [lindex $char [expr ($i / $div) % 10]]
1028
+ }
1029
+ set ret
1030
+ }
1031
+ db func int_to_char int_to_char
1032
+
1033
+ do_execsql_test 23.0 {
1034
+ CREATE TABLE t4(
1035
+ a COLLATE nocase, b, c,
1036
+ d, e, f,
1037
+ PRIMARY KEY(c, b, a)
1038
+ ) WITHOUT ROWID;
1039
+ CREATE INDEX i41 ON t4(e);
1040
+ CREATE INDEX i42 ON t4(f);
1041
+
1042
+ WITH data(a, b, c, d, e, f) AS (
1043
+ SELECT int_to_char(0), 'xyz', 'zyx', '*', 0, 0
1044
+ UNION ALL
1045
+ SELECT
1046
+ int_to_char(f+1), b, c, d, (e+1) % 2, f+1
1047
+ FROM data WHERE f<1024
1048
+ )
1049
+ INSERT INTO t4 SELECT a, b, c, d, e, f FROM data;
1050
+ ANALYZE;
1051
+ } {}
1052
+
1053
+ do_eqp_test 23.1 {
1054
+ SELECT * FROM t4 WHERE
1055
+ (e=1 AND b='xyz' AND c='zyx' AND a<'AEA') AND f<300
1056
+ -- Formerly used index i41. But i41 is not a covering index whereas
1057
+ -- the PRIMARY KEY is a covering index, and so as of 2017-10-15, the
1058
+ -- PRIMARY KEY is preferred.
1059
+ } {SEARCH t4 USING PRIMARY KEY (c=? AND b=? AND a<?)}
1060
+ do_eqp_test 23.2 {
1061
+ SELECT * FROM t4 WHERE
1062
+ (e=1 AND b='xyz' AND c='zyx' AND a<'JJJ') AND f<300
1063
+ } {SEARCH t4 USING INDEX i42 (f<?)}
1064
+
1065
+ do_execsql_test 24.0 {
1066
+ CREATE TABLE t5(c, d, b, e, a, PRIMARY KEY(a, b, c)) WITHOUT ROWID;
1067
+ WITH data(a, b, c, d, e) AS (
1068
+ SELECT 'z', 'y', 0, 0, 0
1069
+ UNION ALL
1070
+ SELECT
1071
+ a, CASE WHEN b='y' THEN 'n' ELSE 'y' END, c+1, e/250, e+1
1072
+ FROM data
1073
+ WHERE e<1000
1074
+ )
1075
+ INSERT INTO t5(a, b, c, d, e) SELECT * FROM data;
1076
+ CREATE INDEX t5d ON t5(d);
1077
+ CREATE INDEX t5e ON t5(e);
1078
+ ANALYZE;
1079
+ }
1080
+
1081
+ foreach {tn where eqp} {
1082
+ 1 "d=0 AND a='z' AND b='n' AND e<200" {/*t5d (d=? AND a=? AND b=?)*/}
1083
+ 2 "d=0 AND a='z' AND b='n' AND e<100" {/*t5e (e<?)*/}
1084
+
1085
+ 3 "d=0 AND e<300" {/*t5d (d=?)*/}
1086
+ 4 "d=0 AND e<200" {/*t5e (e<?)*/}
1087
+ } {
1088
+ do_eqp_test 24.$tn "SeLeCt * FROM t5 WHERE $where" $eqp
1089
+ }
1090
+
1091
+ #-------------------------------------------------------------------------
1092
+ # Test that if stat4 data is available but cannot be used because the
1093
+ # rhs of a range constraint is a complex expression, the default estimates
1094
+ # are used instead.
1095
+ ifcapable stat4&&cte {
1096
+ do_execsql_test 25.1 {
1097
+ CREATE TABLE t6(a, b);
1098
+ WITH ints(i,j) AS (
1099
+ SELECT 1,1 UNION ALL SELECT i+1,j+1 FROM ints WHERE i<100
1100
+ ) INSERT INTO t6 SELECT * FROM ints;
1101
+ CREATE INDEX aa ON t6(a);
1102
+ CREATE INDEX bb ON t6(b);
1103
+ ANALYZE;
1104
+ }
1105
+
1106
+ # Term (b<?) is estimated at 25%. Better than (a<30) but not as
1107
+ # good as (a<20).
1108
+ do_eqp_test 25.2.1 { SELECT * FROM t6 WHERE a<30 AND b<? } \
1109
+ {SEARCH t6 USING INDEX bb (b<?)}
1110
+ do_eqp_test 25.2.2 { SELECT * FROM t6 WHERE a<20 AND b<? } \
1111
+ {SEARCH t6 USING INDEX aa (a<?)}
1112
+
1113
+ # Term (b BETWEEN ? AND ?) is estimated at 1/64.
1114
+ do_eqp_test 25.3.1 {
1115
+ SELECT * FROM t6 WHERE a BETWEEN 5 AND 10 AND b BETWEEN ? AND ?
1116
+ } {SEARCH t6 USING INDEX bb (b>? AND b<?)}
1117
+
1118
+ # Term (b BETWEEN ? AND 60) is estimated to return roughly 15 rows -
1119
+ # 60 from (b<=60) multiplied by 0.25 for the b>=? term. Better than
1120
+ # (a<20) but not as good as (a<10).
1121
+ do_eqp_test 25.4.1 {
1122
+ SELECT * FROM t6 WHERE a < 10 AND (b BETWEEN ? AND 60)
1123
+ } {SEARCH t6 USING INDEX aa (a<?)}
1124
+
1125
+ do_eqp_test 25.4.2 {
1126
+ SELECT * FROM t6 WHERE a < 20 AND (b BETWEEN ? AND 60)
1127
+ } {SEARCH t6 USING INDEX bb (b>? AND b<?)}
1128
+ }
1129
+
1130
+ #-------------------------------------------------------------------------
1131
+ # Check that a problem in they way stat4 data is used has been
1132
+ # resolved (see below).
1133
+ #
1134
+ reset_db
1135
+ do_test 26.1.1 {
1136
+ db transaction {
1137
+ execsql {
1138
+ CREATE TABLE t1(x, y, z);
1139
+ CREATE INDEX t1xy ON t1(x, y);
1140
+ CREATE INDEX t1z ON t1(z);
1141
+ }
1142
+ for {set i 0} {$i < 10000} {incr i} {
1143
+ execsql { INSERT INTO t1(x, y) VALUES($i, $i) }
1144
+ }
1145
+ for {set i 0} {$i < 10} {incr i} {
1146
+ execsql {
1147
+ WITH cnt(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM cnt WHERE x<100)
1148
+ INSERT INTO t1(x, y) SELECT 10000+$i, x FROM cnt;
1149
+ INSERT INTO t1(x, y) SELECT 10000+$i, 100;
1150
+ }
1151
+ }
1152
+ execsql {
1153
+ UPDATE t1 SET z = rowid / 20;
1154
+ ANALYZE;
1155
+ }
1156
+ }
1157
+ } {}
1158
+
1159
+ do_execsql_test 26.1.2 {
1160
+ SELECT count(*) FROM t1 WHERE x = 10000 AND y < 50;
1161
+ } {49}
1162
+ do_execsql_test 26.1.3 {
1163
+ SELECT count(*) FROM t1 WHERE z = 444;
1164
+ } {20}
1165
+
1166
+ # The analyzer knows that any (z=?) expression matches 20 rows. So it
1167
+ # will use index "t1z" if the estimate of hits for (x=10000 AND y<50)
1168
+ # is greater than 20 rows.
1169
+ #
1170
+ # And it should be. The analyzer has a stat4 sample as follows:
1171
+ #
1172
+ # sample=(x=10000, y=100) nLt=(10000 10099)
1173
+ #
1174
+ # There should be no other samples that start with (x=10000). So it knows
1175
+ # that (x=10000 AND y<50) must match somewhere between 0 and 99 rows, but
1176
+ # no more than that. Guessing less than 20 is therefore unreasonable.
1177
+ #
1178
+ # At one point though, due to a problem in whereKeyStats(), the planner was
1179
+ # estimating that (x=10000 AND y<50) would match only 2 rows.
1180
+ #
1181
+ do_eqp_test 26.1.4 {
1182
+ SELECT * FROM t1 WHERE x = 10000 AND y < 50 AND z = 444;
1183
+ } {SEARCH t1 USING INDEX t1z (z=?)}
1184
+
1185
+
1186
+ # This test - 26.2.* - tests that another manifestation of the same problem
1187
+ # is no longer present in the library. Assuming:
1188
+ #
1189
+ # CREATE INDEX t1xy ON t1(x, y)
1190
+ #
1191
+ # and that have samples for index t1xy as follows:
1192
+ #
1193
+ #
1194
+ # sample=('A', 70) nEq=(100, 2) nLt=(900, 970)
1195
+ # sample=('B', 70) nEq=(100, 2) nLt=(1000, 1070)
1196
+ #
1197
+ # the planner should estimate that (x = 'B' AND y > 25) matches 76 rows
1198
+ # (70 * 2/3 + 30). Before, due to the problem, the planner was estimating
1199
+ # that this matched 100 rows.
1200
+ #
1201
+ reset_db
1202
+ do_execsql_test 26.2.1 {
1203
+ BEGIN;
1204
+ CREATE TABLE t1(x, y, z);
1205
+ CREATE INDEX i1 ON t1(x, y);
1206
+ CREATE INDEX i2 ON t1(z);
1207
+
1208
+ WITH
1209
+ cnt(y) AS (SELECT 0 UNION ALL SELECT y+1 FROM cnt WHERE y<99),
1210
+ letters(x) AS (
1211
+ SELECT 'A' UNION SELECT 'B' UNION SELECT 'C' UNION SELECT 'D'
1212
+ )
1213
+ INSERT INTO t1(x, y) SELECT x, y FROM letters, cnt;
1214
+
1215
+ WITH
1216
+ letters(x) AS (
1217
+ SELECT 'A' UNION SELECT 'B' UNION SELECT 'C' UNION SELECT 'D'
1218
+ )
1219
+ INSERT INTO t1(x, y) SELECT x, 70 FROM letters;
1220
+
1221
+ WITH
1222
+ cnt(i) AS (SELECT 0 UNION ALL SELECT i+1 FROM cnt WHERE i<9999)
1223
+ INSERT INTO t1(x, y) SELECT i, i FROM cnt;
1224
+
1225
+ UPDATE t1 SET z = (rowid / 95);
1226
+ ANALYZE;
1227
+ COMMIT;
1228
+ }
1229
+
1230
+ do_eqp_test 26.2.2 {
1231
+ SELECT * FROM t1 WHERE x='B' AND y>25 AND z=?;
1232
+ } {SEARCH t1 USING INDEX i1 (x=? AND y>?)}
1233
+
1234
+
1235
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyzeC.test ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2014-07-22
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # This file contains automated tests used to verify that the text terms
13
+ # at the end of sqlite_stat1.stat are processed correctly.
14
+ #
15
+ # (1) "unordered" means that the index cannot be used for ORDER BY
16
+ # or for range queries
17
+ #
18
+ # (2) "sz=NNN" sets the relative size of the index entries
19
+ #
20
+ # (3) All other fields are silently ignored
21
+ #
22
+
23
+ set testdir [file dirname $argv0]
24
+ source $testdir/tester.tcl
25
+ set testprefix analyzeC
26
+
27
+ # Baseline case. Range queries work OK. Indexes can be used for
28
+ # ORDER BY.
29
+ #
30
+ do_execsql_test 1.0 {
31
+ CREATE TABLE t1(a,b,c);
32
+ INSERT INTO t1(a,b,c)
33
+ VALUES(1,2,3),(7,8,9),(4,5,6),(10,11,12),(4,8,12),(1,11,111);
34
+ CREATE INDEX t1a ON t1(a);
35
+ CREATE INDEX t1b ON t1(b);
36
+ ANALYZE;
37
+ DELETE FROM sqlite_stat1;
38
+ INSERT INTO sqlite_stat1(tbl,idx,stat)
39
+ VALUES('t1','t1a','12345 2'),('t1','t1b','12345 4');
40
+ ANALYZE sqlite_master;
41
+ SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
42
+ } {4 5 6 # 7 8 9 # 4 8 12 #}
43
+ do_execsql_test 1.1 {
44
+ EXPLAIN QUERY PLAN
45
+ SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
46
+ } {/.* USING INDEX t1a .a>. AND a<...*/}
47
+ do_execsql_test 1.2 {
48
+ SELECT c FROM t1 ORDER BY a;
49
+ } {3 111 6 12 9 12}
50
+ do_execsql_test 1.3 {
51
+ EXPLAIN QUERY PLAN
52
+ SELECT c FROM t1 ORDER BY a;
53
+ } {/.*SCAN t1 USING INDEX t1a.*/}
54
+ do_execsql_test 1.3x {
55
+ EXPLAIN QUERY PLAN
56
+ SELECT c FROM t1 ORDER BY a;
57
+ } {~/.*B-TREE FOR ORDER BY.*/}
58
+
59
+ # Now mark the t1a index as "unordered". Range queries and ORDER BY no
60
+ # longer use the index, but equality queries do.
61
+ #
62
+ do_execsql_test 2.0 {
63
+ UPDATE sqlite_stat1 SET stat='12345 2 unordered' WHERE idx='t1a';
64
+ ANALYZE sqlite_master;
65
+ SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
66
+ } {4 5 6 # 7 8 9 # 4 8 12 #}
67
+ do_execsql_test 2.1 {
68
+ EXPLAIN QUERY PLAN
69
+ SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
70
+ } {~/.*USING INDEX.*/}
71
+ do_execsql_test 2.2 {
72
+ SELECT c FROM t1 ORDER BY a;
73
+ } {3 111 6 12 9 12}
74
+ do_execsql_test 2.3 {
75
+ EXPLAIN QUERY PLAN
76
+ SELECT c FROM t1 ORDER BY a;
77
+ } {~/.*USING INDEX.*/}
78
+ do_execsql_test 2.3x {
79
+ EXPLAIN QUERY PLAN
80
+ SELECT c FROM t1 ORDER BY a;
81
+ } {/.*B-TREE FOR ORDER BY.*/}
82
+
83
+ # Ignore extraneous text parameters in the sqlite_stat1.stat field.
84
+ #
85
+ do_execsql_test 3.0 {
86
+ UPDATE sqlite_stat1 SET stat='12345 2 whatever=5 unordered xyzzy=11'
87
+ WHERE idx='t1a';
88
+ ANALYZE sqlite_master;
89
+ SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
90
+ } {4 5 6 # 7 8 9 # 4 8 12 #}
91
+ do_execsql_test 3.1 {
92
+ EXPLAIN QUERY PLAN
93
+ SELECT *, '#' FROM t1 WHERE a BETWEEN 3 AND 8 ORDER BY c;
94
+ } {~/.*USING INDEX.*/}
95
+ do_execsql_test 3.2 {
96
+ SELECT c FROM t1 ORDER BY a;
97
+ } {3 111 6 12 9 12}
98
+ do_execsql_test 3.3 {
99
+ EXPLAIN QUERY PLAN
100
+ SELECT c FROM t1 ORDER BY a;
101
+ } {~/.*USING INDEX.*/}
102
+ do_execsql_test 3.3x {
103
+ EXPLAIN QUERY PLAN
104
+ SELECT c FROM t1 ORDER BY a;
105
+ } {/.*B-TREE FOR ORDER BY.*/}
106
+
107
+ # The sz=NNN parameter determines which index to scan
108
+ #
109
+ do_execsql_test 4.0 {
110
+ DROP INDEX t1a;
111
+ CREATE INDEX t1ab ON t1(a,b);
112
+ CREATE INDEX t1ca ON t1(c,a);
113
+ DELETE FROM sqlite_stat1;
114
+ INSERT INTO sqlite_stat1(tbl,idx,stat)
115
+ VALUES('t1','t1ab','12345 3 2 sz=10'),('t1','t1ca','12345 3 2 sz=20');
116
+ ANALYZE sqlite_master;
117
+ SELECT count(a) FROM t1;
118
+ } {6}
119
+ do_execsql_test 4.1 {
120
+ EXPLAIN QUERY PLAN
121
+ SELECT count(a) FROM t1;
122
+ } {/.*INDEX t1ab.*/}
123
+ do_execsql_test 4.2 {
124
+ DELETE FROM sqlite_stat1;
125
+ INSERT INTO sqlite_stat1(tbl,idx,stat)
126
+ VALUES('t1','t1ab','12345 3 2 sz=20'),('t1','t1ca','12345 3 2 sz=10');
127
+ ANALYZE sqlite_master;
128
+ SELECT count(a) FROM t1;
129
+ } {6}
130
+ do_execsql_test 4.3 {
131
+ EXPLAIN QUERY PLAN
132
+ SELECT count(a) FROM t1;
133
+ } {/.*INDEX t1ca.*/}
134
+
135
+ # 2019-08-15.
136
+ # Ticket https://www.sqlite.org/src/tktview/e4598ecbdd18bd82945f602901
137
+ # The sz=N parameter in the sqlite_stat1 table needs to have a value of
138
+ # 2 or more to avoid a division by zero in the query planner.
139
+ #
140
+ do_execsql_test 4.4 {
141
+ DROP TABLE IF EXISTS t44;
142
+ CREATE TABLE t44(a PRIMARY KEY);
143
+ INSERT INTO sqlite_stat1 VALUES('t44',null,'sz=0');
144
+ ANALYZE sqlite_master;
145
+ SELECT 0 FROM t44 WHERE a IN(1,2,3);
146
+ } {}
147
+
148
+
149
+
150
+ # The sz=NNN parameter works even if there is other extraneous text
151
+ # in the sqlite_stat1.stat column.
152
+ #
153
+ do_execsql_test 5.0 {
154
+ DELETE FROM sqlite_stat1;
155
+ INSERT INTO sqlite_stat1(tbl,idx,stat)
156
+ VALUES('t1','t1ab','12345 3 2 x=5 sz=10 y=10'),
157
+ ('t1','t1ca','12345 3 2 whatever sz=20 junk');
158
+ ANALYZE sqlite_master;
159
+ SELECT count(a) FROM t1;
160
+ } {6}
161
+ do_execsql_test 5.1 {
162
+ EXPLAIN QUERY PLAN
163
+ SELECT count(a) FROM t1;
164
+ } {/.*INDEX t1ab.*/}
165
+ do_execsql_test 5.2 {
166
+ DELETE FROM sqlite_stat1;
167
+ INSERT INTO sqlite_stat1(tbl,idx,stat)
168
+ VALUES('t1','t1ca','12345 3 2 x=5 sz=10 y=10'),
169
+ ('t1','t1ab','12345 3 2 whatever sz=20 junk');
170
+ ANALYZE sqlite_master;
171
+ SELECT count(a) FROM t1;
172
+ } {6}
173
+ do_execsql_test 5.3 {
174
+ EXPLAIN QUERY PLAN
175
+ SELECT count(a) FROM t1;
176
+ } {/.*INDEX t1ca.*/}
177
+
178
+
179
+
180
+
181
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyzeD.test ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2014-10-04
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library.
12
+ # This file implements tests for the ANALYZE command.
13
+ #
14
+
15
+ set testdir [file dirname $argv0]
16
+ source $testdir/tester.tcl
17
+ set ::testprefix analyzeD
18
+
19
+ ifcapable {!stat4} {
20
+ finish_test
21
+ return
22
+ }
23
+
24
+
25
+ # Set up a table with the following properties:
26
+ #
27
+ # * Contains 1000 rows.
28
+ # * Column a contains even integers between 0 and 18, inclusive (so that
29
+ # a=? for any such integer matches 100 rows).
30
+ # * Column b contains integers between 0 and 9, inclusive.
31
+ # * Column c contains integers between 0 and 199, inclusive (so that
32
+ # for any such integer, c=? matches 5 rows).
33
+ # * Then add 7 rows with a new value for "a" - 3001. The stat4 table will
34
+ # not contain any samples with a=3001.
35
+ #
36
+ do_execsql_test 1.0 {
37
+ CREATE TABLE t1(a, b, c);
38
+ }
39
+ do_test 1.1 {
40
+ for {set i 1} {$i < 1000} {incr i} {
41
+ set c [expr $i % 200]
42
+ execsql { INSERT INTO t1(a, b, c) VALUES( 2*($i/100), $i%10, $c ) }
43
+ }
44
+
45
+ execsql {
46
+ INSERT INTO t1 VALUES(3001, 3001, 3001);
47
+ INSERT INTO t1 VALUES(3001, 3001, 3002);
48
+ INSERT INTO t1 VALUES(3001, 3001, 3003);
49
+ INSERT INTO t1 VALUES(3001, 3001, 3004);
50
+ INSERT INTO t1 VALUES(3001, 3001, 3005);
51
+ INSERT INTO t1 VALUES(3001, 3001, 3006);
52
+ INSERT INTO t1 VALUES(3001, 3001, 3007);
53
+
54
+ CREATE INDEX t1_ab ON t1(a, b);
55
+ CREATE INDEX t1_c ON t1(c);
56
+
57
+ ANALYZE;
58
+ }
59
+ } {}
60
+
61
+ # With full ANALYZE data, SQLite sees that c=150 (5 rows) is better than
62
+ # a=3001 (7 rows).
63
+ #
64
+ do_eqp_test 1.2 {
65
+ SELECT * FROM t1 WHERE a=3001 AND c=150;
66
+ } {SEARCH t1 USING INDEX t1_c (c=?)}
67
+
68
+ do_test 1.3 {
69
+ execsql { DELETE FROM sqlite_stat1 }
70
+ db close
71
+ sqlite3 db test.db
72
+ } {}
73
+
74
+ # Without stat1, because 3001 is larger than all samples in the stat4
75
+ # table, SQLite thinks that a=3001 matches just 1 row. So it (incorrectly)
76
+ # chooses it over the c=150 index (5 rows). Even with stat1 data, things
77
+ # worked this way before commit [e6f7f97dbc].
78
+ #
79
+ do_eqp_test 1.4 {
80
+ SELECT * FROM t1 WHERE a=3001 AND c=150;
81
+ } {SEARCH t1 USING INDEX t1_ab (a=?)}
82
+
83
+ do_test 1.5 {
84
+ execsql {
85
+ UPDATE t1 SET a=13 WHERE a = 3001;
86
+ ANALYZE;
87
+ }
88
+ } {}
89
+
90
+ do_eqp_test 1.6 {
91
+ SELECT * FROM t1 WHERE a=13 AND c=150;
92
+ } {SEARCH t1 USING INDEX t1_c (c=?)}
93
+
94
+ do_test 1.7 {
95
+ execsql { DELETE FROM sqlite_stat1 }
96
+ db close
97
+ sqlite3 db test.db
98
+ } {}
99
+
100
+ # Same test as 1.4, except this time the 7 rows that match the a=? condition
101
+ # do not feature larger values than all rows in the stat4 table. So SQLite
102
+ # gets this right, even without stat1 data.
103
+ do_eqp_test 1.8 {
104
+ SELECT * FROM t1 WHERE a=13 AND c=150;
105
+ } {SEARCH t1 USING INDEX t1_c (c=?)}
106
+
107
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyzeE.test ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2014-10-08
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements tests for using STAT4 information
12
+ # on a descending index in a range query.
13
+ #
14
+
15
+ set testdir [file dirname $argv0]
16
+ source $testdir/tester.tcl
17
+ set ::testprefix analyzeE
18
+
19
+ ifcapable {!stat4} {
20
+ finish_test
21
+ return
22
+ }
23
+
24
+ # Verify that range queries on an ASCENDING index will use the
25
+ # index only if the range covers only a small fraction of the
26
+ # entries.
27
+ #
28
+ do_execsql_test analyzeE-1.0 {
29
+ CREATE TABLE t1(a,b);
30
+ WITH RECURSIVE
31
+ cnt(x) AS (VALUES(1000) UNION ALL SELECT x+1 FROM cnt WHERE x<2000)
32
+ INSERT INTO t1(a,b) SELECT x, x FROM cnt;
33
+ CREATE INDEX t1a ON t1(a);
34
+ ANALYZE;
35
+ } {}
36
+ do_execsql_test analyzeE-1.1 {
37
+ EXPLAIN QUERY PLAN
38
+ SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500;
39
+ } {/SCAN t1/}
40
+ do_execsql_test analyzeE-1.2 {
41
+ EXPLAIN QUERY PLAN
42
+ SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000;
43
+ } {/SEARCH t1 USING INDEX t1a/}
44
+ do_execsql_test analyzeE-1.3 {
45
+ EXPLAIN QUERY PLAN
46
+ SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750;
47
+ } {/SEARCH t1 USING INDEX t1a/}
48
+ do_execsql_test analyzeE-1.4 {
49
+ EXPLAIN QUERY PLAN
50
+ SELECT * FROM t1 WHERE a BETWEEN 1 AND 500
51
+ } {/SEARCH t1 USING INDEX t1a/}
52
+ do_execsql_test analyzeE-1.5 {
53
+ EXPLAIN QUERY PLAN
54
+ SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000
55
+ } {/SEARCH t1 USING INDEX t1a/}
56
+ do_execsql_test analyzeE-1.6 {
57
+ EXPLAIN QUERY PLAN
58
+ SELECT * FROM t1 WHERE a<500
59
+ } {/SEARCH t1 USING INDEX t1a/}
60
+ do_execsql_test analyzeE-1.7 {
61
+ EXPLAIN QUERY PLAN
62
+ SELECT * FROM t1 WHERE a>2500
63
+ } {/SEARCH t1 USING INDEX t1a/}
64
+ do_execsql_test analyzeE-1.8 {
65
+ EXPLAIN QUERY PLAN
66
+ SELECT * FROM t1 WHERE a>1900
67
+ } {/SEARCH t1 USING INDEX t1a/}
68
+ do_execsql_test analyzeE-1.9 {
69
+ EXPLAIN QUERY PLAN
70
+ SELECT * FROM t1 WHERE a>1100
71
+ } {/SCAN t1/}
72
+ do_execsql_test analyzeE-1.10 {
73
+ EXPLAIN QUERY PLAN
74
+ SELECT * FROM t1 WHERE a<1100
75
+ } {/SEARCH t1 USING INDEX t1a/}
76
+ do_execsql_test analyzeE-1.11 {
77
+ EXPLAIN QUERY PLAN
78
+ SELECT * FROM t1 WHERE a<1900
79
+ } {/SCAN t1/}
80
+
81
+ # Verify that everything works the same on a DESCENDING index.
82
+ #
83
+ do_execsql_test analyzeE-2.0 {
84
+ DROP INDEX t1a;
85
+ CREATE INDEX t1a ON t1(a DESC);
86
+ ANALYZE;
87
+ } {}
88
+ do_execsql_test analyzeE-2.1 {
89
+ EXPLAIN QUERY PLAN
90
+ SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500;
91
+ } {/SCAN t1/}
92
+ do_execsql_test analyzeE-2.2 {
93
+ EXPLAIN QUERY PLAN
94
+ SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000;
95
+ } {/SEARCH t1 USING INDEX t1a/}
96
+ do_execsql_test analyzeE-2.3 {
97
+ EXPLAIN QUERY PLAN
98
+ SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750;
99
+ } {/SEARCH t1 USING INDEX t1a/}
100
+ do_execsql_test analyzeE-2.4 {
101
+ EXPLAIN QUERY PLAN
102
+ SELECT * FROM t1 WHERE a BETWEEN 1 AND 500
103
+ } {/SEARCH t1 USING INDEX t1a/}
104
+ do_execsql_test analyzeE-2.5 {
105
+ EXPLAIN QUERY PLAN
106
+ SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000
107
+ } {/SEARCH t1 USING INDEX t1a/}
108
+ do_execsql_test analyzeE-2.6 {
109
+ EXPLAIN QUERY PLAN
110
+ SELECT * FROM t1 WHERE a<500
111
+ } {/SEARCH t1 USING INDEX t1a/}
112
+ do_execsql_test analyzeE-2.7 {
113
+ EXPLAIN QUERY PLAN
114
+ SELECT * FROM t1 WHERE a>2500
115
+ } {/SEARCH t1 USING INDEX t1a/}
116
+ do_execsql_test analyzeE-2.8 {
117
+ EXPLAIN QUERY PLAN
118
+ SELECT * FROM t1 WHERE a>1900
119
+ } {/SEARCH t1 USING INDEX t1a/}
120
+ do_execsql_test analyzeE-2.9 {
121
+ EXPLAIN QUERY PLAN
122
+ SELECT * FROM t1 WHERE a>1100
123
+ } {/SCAN t1/}
124
+ do_execsql_test analyzeE-2.10 {
125
+ EXPLAIN QUERY PLAN
126
+ SELECT * FROM t1 WHERE a<1100
127
+ } {/SEARCH t1 USING INDEX t1a/}
128
+ do_execsql_test analyzeE-2.11 {
129
+ EXPLAIN QUERY PLAN
130
+ SELECT * FROM t1 WHERE a<1900
131
+ } {/SCAN t1/}
132
+
133
+ # Now do a range query on the second term of an ASCENDING index
134
+ # where the first term is constrained by equality.
135
+ #
136
+ do_execsql_test analyzeE-3.0 {
137
+ DROP TABLE t1;
138
+ CREATE TABLE t1(a,b,c);
139
+ WITH RECURSIVE
140
+ cnt(x) AS (VALUES(1000) UNION ALL SELECT x+1 FROM cnt WHERE x<2000)
141
+ INSERT INTO t1(a,b,c) SELECT x, x, 123 FROM cnt;
142
+ CREATE INDEX t1ca ON t1(c,a);
143
+ ANALYZE;
144
+ } {}
145
+ do_execsql_test analyzeE-3.1 {
146
+ EXPLAIN QUERY PLAN
147
+ SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500 AND c=123;
148
+ } {/SCAN t1/}
149
+ do_execsql_test analyzeE-3.2 {
150
+ EXPLAIN QUERY PLAN
151
+ SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000 AND c=123;
152
+ } {/SEARCH t1 USING INDEX t1ca/}
153
+ do_execsql_test analyzeE-3.3 {
154
+ EXPLAIN QUERY PLAN
155
+ SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750 AND c=123;
156
+ } {/SEARCH t1 USING INDEX t1ca/}
157
+ do_execsql_test analyzeE-3.4 {
158
+ EXPLAIN QUERY PLAN
159
+ SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 AND c=123
160
+ } {/SEARCH t1 USING INDEX t1ca/}
161
+ do_execsql_test analyzeE-3.5 {
162
+ EXPLAIN QUERY PLAN
163
+ SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 AND c=123
164
+ } {/SEARCH t1 USING INDEX t1ca/}
165
+ do_execsql_test analyzeE-3.6 {
166
+ EXPLAIN QUERY PLAN
167
+ SELECT * FROM t1 WHERE a<500 AND c=123
168
+ } {/SEARCH t1 USING INDEX t1ca/}
169
+ do_execsql_test analyzeE-3.7 {
170
+ EXPLAIN QUERY PLAN
171
+ SELECT * FROM t1 WHERE a>2500 AND c=123
172
+ } {/SEARCH t1 USING INDEX t1ca/}
173
+ do_execsql_test analyzeE-3.8 {
174
+ EXPLAIN QUERY PLAN
175
+ SELECT * FROM t1 WHERE a>1900 AND c=123
176
+ } {/SEARCH t1 USING INDEX t1ca/}
177
+ do_execsql_test analyzeE-3.9 {
178
+ EXPLAIN QUERY PLAN
179
+ SELECT * FROM t1 WHERE a>1100 AND c=123
180
+ } {/SCAN t1/}
181
+ do_execsql_test analyzeE-3.10 {
182
+ EXPLAIN QUERY PLAN
183
+ SELECT * FROM t1 WHERE a<1100 AND c=123
184
+ } {/SEARCH t1 USING INDEX t1ca/}
185
+ do_execsql_test analyzeE-3.11 {
186
+ EXPLAIN QUERY PLAN
187
+ SELECT * FROM t1 WHERE a<1900 AND c=123
188
+ } {/SCAN t1/}
189
+
190
+ # Repeat the 3.x tests using a DESCENDING index
191
+ #
192
+ do_execsql_test analyzeE-4.0 {
193
+ DROP INDEX t1ca;
194
+ CREATE INDEX t1ca ON t1(c ASC,a DESC);
195
+ ANALYZE;
196
+ } {}
197
+ do_execsql_test analyzeE-4.1 {
198
+ EXPLAIN QUERY PLAN
199
+ SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500 AND c=123;
200
+ } {/SCAN t1/}
201
+ do_execsql_test analyzeE-4.2 {
202
+ EXPLAIN QUERY PLAN
203
+ SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000 AND c=123;
204
+ } {/SEARCH t1 USING INDEX t1ca/}
205
+ do_execsql_test analyzeE-4.3 {
206
+ EXPLAIN QUERY PLAN
207
+ SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750 AND c=123;
208
+ } {/SEARCH t1 USING INDEX t1ca/}
209
+ do_execsql_test analyzeE-4.4 {
210
+ EXPLAIN QUERY PLAN
211
+ SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 AND c=123
212
+ } {/SEARCH t1 USING INDEX t1ca/}
213
+ do_execsql_test analyzeE-4.5 {
214
+ EXPLAIN QUERY PLAN
215
+ SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 AND c=123
216
+ } {/SEARCH t1 USING INDEX t1ca/}
217
+ do_execsql_test analyzeE-4.6 {
218
+ EXPLAIN QUERY PLAN
219
+ SELECT * FROM t1 WHERE a<500 AND c=123
220
+ } {/SEARCH t1 USING INDEX t1ca/}
221
+ do_execsql_test analyzeE-4.7 {
222
+ EXPLAIN QUERY PLAN
223
+ SELECT * FROM t1 WHERE a>2500 AND c=123
224
+ } {/SEARCH t1 USING INDEX t1ca/}
225
+ do_execsql_test analyzeE-4.8 {
226
+ EXPLAIN QUERY PLAN
227
+ SELECT * FROM t1 WHERE a>1900 AND c=123
228
+ } {/SEARCH t1 USING INDEX t1ca/}
229
+ do_execsql_test analyzeE-4.9 {
230
+ EXPLAIN QUERY PLAN
231
+ SELECT * FROM t1 WHERE a>1100 AND c=123
232
+ } {/SCAN t1/}
233
+ do_execsql_test analyzeE-4.10 {
234
+ EXPLAIN QUERY PLAN
235
+ SELECT * FROM t1 WHERE a<1100 AND c=123
236
+ } {/SEARCH t1 USING INDEX t1ca/}
237
+ do_execsql_test analyzeE-4.11 {
238
+ EXPLAIN QUERY PLAN
239
+ SELECT * FROM t1 WHERE a<1900 AND c=123
240
+ } {/SCAN t1/}
241
+
242
+ # 2023-03-23 https://sqlite.org/forum/forumpost/dc4854437b
243
+ #
244
+ reset_db
245
+ do_execsql_test analyzeE-5.0 {
246
+ PRAGMA encoding = 'UTF-16';
247
+ CREATE TABLE t0 (c1 TEXT);
248
+ INSERT INTO t0 VALUES ('');
249
+ CREATE INDEX i0 ON t0(c1);
250
+ ANALYZE;
251
+ SELECT * FROM t0 WHERE t0.c1 BETWEEN '' AND (ABS(''));
252
+ } {{}}
253
+
254
+ # 2023-03-24 https://sqlite.org/forum/forumpost/bc39e531e5
255
+ #
256
+ reset_db
257
+ do_execsql_test analyzeE-6.0 {
258
+ CREATE TABLE t1(x);
259
+ CREATE INDEX i1 ON t1(x,x,x,x,x||2);
260
+ CREATE INDEX i2 ON t1(1<2);
261
+ WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
262
+ INSERT INTO t1(x) SELECT x FROM c;
263
+ ANALYZE;
264
+ } {}
265
+ do_execsql_test analyzeE-6.1 {
266
+ SELECT count(*)>1 FROM sqlite_stat4 WHERE idx='i2' AND neq='1000 1';
267
+ } 1
268
+ do_execsql_test analyzeE-6.2 {
269
+ SELECT count(*) FROM sqlite_stat4 WHERE idx='i2' AND neq<>'1000 1';
270
+ } 0
271
+ do_execsql_test analyzeE-6.3 {
272
+ SELECT count(*)>1 FROM sqlite_stat4 WHERE idx='i1' AND neq='1 1 1 1 1 1';
273
+ } 1
274
+ do_execsql_test analyzeE-6.4 {
275
+ SELECT count(*) FROM sqlite_stat4 WHERE idx='i1' AND neq<>'1 1 1 1 1 1';
276
+ } 0
277
+
278
+ # 2023-03-25 https://sqlite.org/forum/forumpost/5275207102
279
+ # Correctly expand zeroblobs while processing STAT4 information
280
+ # during query planning.
281
+ #
282
+ reset_db
283
+ do_execsql_test analyzeE-7.0 {
284
+ CREATE TABLE t1(a TEXT COLLATE binary);
285
+ CREATE INDEX t1x ON t1(a);
286
+ INSERT INTO t1(a) VALUES(0),('apple'),(NULL),(''),('banana');
287
+ ANALYZE;
288
+ SELECT format('(%s)',a) FROM t1 WHERE t1.a > CAST(zeroblob(5) AS TEXT);
289
+ } {(0) (apple) (banana)}
290
+ do_execsql_test analyzeE-7.1 {
291
+ SELECT format('(%s)',a) FROM t1 WHERE t1.a <= CAST(zeroblob(5) AS TEXT);
292
+ } {()}
293
+
294
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/analyzer1.test ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2015-05-11
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # Quick tests for the sqlite3_analyzer tool
13
+ #
14
+ set testdir [file dirname $argv0]
15
+ source $testdir/tester.tcl
16
+
17
+ ifcapable !vtab {
18
+ finish_test
19
+ return
20
+ }
21
+
22
+ if {$tcl_platform(platform)=="windows"} {
23
+ set PROG "sqlite3_analyzer.exe"
24
+ } else {
25
+ set PROG "./sqlite3_analyzer"
26
+ }
27
+ if {![file exe $PROG]} {
28
+ set PROG [file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $PROG]]
29
+ if {![file exe $PROG]} {
30
+ puts "analyzer1 cannot run because $PROG is not available"
31
+ finish_test
32
+ return
33
+ }
34
+ }
35
+ db close
36
+ forcedelete test.db test.db-journal test.db-wal
37
+ sqlite3 db test.db
38
+
39
+ do_test analyzer1-1.0 {
40
+ db eval {
41
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
42
+ CREATE TABLE t2(a INT PRIMARY KEY, b) WITHOUT ROWID;
43
+ WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<250)
44
+ INSERT INTO t1(a,b) SELECT x, randomblob(200) FROM c;
45
+ INSERT INTO t2(a,b) SELECT a, b FROM t1;
46
+ }
47
+ set line "exec $PROG test.db"
48
+ unset -nocomplain ::MSG
49
+ catch {eval $line} ::MSG
50
+ } {0}
51
+ do_test analyzer1-1.1 {
52
+ regexp {^/\*\* Disk-Space Utilization.*COMMIT;\W*$} $::MSG
53
+ } {1}
54
+
55
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/atomic2.test ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2018-07-15
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is testing that if an IO error is encountered
13
+ # as part of an atomic F2FS commit, an attempt is made to commit the
14
+ # transaction using a legacy journal commit.
15
+ #
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+ source $testdir/malloc_common.tcl
20
+ set ::testprefix atomic2
21
+
22
+ db close
23
+ if {[atomic_batch_write test.db]==0} {
24
+ puts "No f2fs atomic-batch-write support. Skipping tests..."
25
+ finish_test
26
+ return
27
+ }
28
+
29
+ reset_db
30
+
31
+ do_execsql_test 1.0 {
32
+ CREATE TABLE t1(x, y);
33
+ CREATE INDEX i1x ON t1(x);
34
+ CREATE INDEX i2x ON t1(y);
35
+
36
+ WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 )
37
+ INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s;
38
+ }
39
+
40
+ set setup [list \
41
+ -injectstart at_injectstart \
42
+ -injectstop at_injectstop \
43
+ ]
44
+
45
+ set ::at_fail 0
46
+ set ::at_nfail 0
47
+
48
+ proc at_injectstart {iFail} {
49
+ set ::at_fail $iFail
50
+ set ::at_nfail 0
51
+ }
52
+ proc at_injectstop {} {
53
+ set ::at_fail 0
54
+ return $::at_nfail
55
+ }
56
+
57
+ proc at_vfs_callback {method file z args} {
58
+ if {$::at_fail>0} {
59
+ incr ::at_fail -1
60
+ if {$::at_fail==0} {
61
+ incr ::at_nfail
62
+ return SQLITE_IOERR
63
+ } elseif {$method=="xFileControl" && $z=="COMMIT_ATOMIC_WRITE"} {
64
+ set ::at_fail 0
65
+ }
66
+ }
67
+ return SQLITE_OK
68
+ }
69
+
70
+ testvfs tvfs -default 1
71
+ tvfs script at_vfs_callback
72
+ tvfs filter {xFileControl xWrite}
73
+
74
+ faultsim_save_and_close
75
+
76
+ do_one_faultsim_test 2.0 {*}$setup -prep {
77
+ faultsim_restore_and_reopen
78
+ } -body {
79
+ execsql {
80
+ WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 )
81
+ INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s;
82
+ }
83
+ } -test {
84
+ faultsim_test_result {0 {}}
85
+
86
+ set res [execsql {SELECT count(*) FROM t1; PRAGMA integrity_check}]
87
+ if {$res!="200 ok"} {
88
+ error "expected {200 ok}, got $res"
89
+ }
90
+ }
91
+
92
+ db close
93
+ tvfs delete
94
+
95
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/atrc.c ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ ** This program generates a script that stresses the ALTER TABLE statement.
3
+ ** Compile like this:
4
+ **
5
+ ** gcc -g -c sqlite3.c
6
+ ** gcc -g -o atrc atrc.c sqlite3.o -ldl -lpthread
7
+ **
8
+ ** Run the program this way:
9
+ **
10
+ ** ./atrc DATABASE | ./sqlite3 DATABASE
11
+ **
12
+ ** This program "atrc" generates a script that can be fed into an ordinary
13
+ ** command-line shell. The script performs many ALTER TABLE statements,
14
+ ** runs ".schema --indent" and "PRAGMA integrity_check;", does more
15
+ ** ALTER TABLE statements to restore the original schema, and then
16
+ ** runs "PRAGMA integrity_check" again. Every table and column has its
17
+ ** name changed. The entire script is contained within BEGIN...ROLLBACK
18
+ ** so that no changes are ever actually made to the database.
19
+ */
20
+ #include "sqlite3.h"
21
+ #include <stdio.h>
22
+
23
+ /*
24
+ ** Generate the text of ALTER TABLE statements that will rename
25
+ ** every column in table zTable to a generic name composed from
26
+ ** zColPrefix and a sequential number. The generated text is
27
+ ** appended pConvert. If pUndo is not NULL, then SQL text that
28
+ ** will undo the change is appended to pUndo.
29
+ **
30
+ ** The table to be converted must be in the "main" schema.
31
+ */
32
+ int rename_all_columns_of_table(
33
+ sqlite3 *db, /* Database connection */
34
+ const char *zTab, /* Table whose columns should all be renamed */
35
+ const char *zColPrefix, /* Prefix for new column names */
36
+ sqlite3_str *pConvert, /* Append ALTER TABLE statements here */
37
+ sqlite3_str *pUndo /* SQL to undo the change, if not NULL */
38
+ ){
39
+ sqlite3_stmt *pStmt;
40
+ int rc;
41
+ int cnt = 0;
42
+
43
+ rc = sqlite3_prepare_v2(db,
44
+ "SELECT name FROM pragma_table_info(?1);",
45
+ -1, &pStmt, 0);
46
+ if( rc ) return rc;
47
+ sqlite3_bind_text(pStmt, 1, zTab, -1, SQLITE_STATIC);
48
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
49
+ const char *zCol = (const char*)sqlite3_column_text(pStmt, 0);
50
+ cnt++;
51
+ sqlite3_str_appendf(pConvert,
52
+ "ALTER TABLE \"%w\" RENAME COLUMN \"%w\" TO \"%w%d\";\n",
53
+ zTab, zCol, zColPrefix, cnt
54
+ );
55
+ if( pUndo ){
56
+ sqlite3_str_appendf(pUndo,
57
+ "ALTER TABLE \"%w\" RENAME COLUMN \"%w%d\" TO \"%w\";\n",
58
+ zTab, zColPrefix, cnt, zCol
59
+ );
60
+ }
61
+ }
62
+ sqlite3_finalize(pStmt);
63
+ return SQLITE_OK;
64
+ }
65
+
66
+ /* Rename all tables and their columns in the main database
67
+ */
68
+ int rename_all_tables(
69
+ sqlite3 *db, /* Database connection */
70
+ sqlite3_str *pConvert, /* Append SQL to do the rename here */
71
+ sqlite3_str *pUndo /* Append SQL to undo the rename here */
72
+ ){
73
+ sqlite3_stmt *pStmt;
74
+ int rc;
75
+ int cnt = 0;
76
+
77
+ rc = sqlite3_prepare_v2(db,
78
+ "SELECT name FROM sqlite_schema WHERE type='table'"
79
+ " AND name NOT LIKE 'sqlite_%';",
80
+ -1, &pStmt, 0);
81
+ if( rc ) return rc;
82
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
83
+ const char *zTab = (const char*)sqlite3_column_text(pStmt, 0);
84
+ char *zNewTab;
85
+ char zPrefix[2];
86
+
87
+ zPrefix[0] = (cnt%26) + 'a';
88
+ zPrefix[1] = 0;
89
+ zNewTab = sqlite3_mprintf("tx%d", ++cnt);
90
+ if( pUndo ){
91
+ sqlite3_str_appendf(pUndo,
92
+ "ALTER TABLE \"%s\" RENAME TO \"%w\";\n",
93
+ zNewTab, zTab
94
+ );
95
+ }
96
+ rename_all_columns_of_table(db, zTab, zPrefix, pConvert, pUndo);
97
+ sqlite3_str_appendf(pConvert,
98
+ "ALTER TABLE \"%w\" RENAME TO \"%s\";\n",
99
+ zTab, zNewTab
100
+ );
101
+ sqlite3_free(zNewTab);
102
+ }
103
+ sqlite3_finalize(pStmt);
104
+ return SQLITE_OK;
105
+ }
106
+
107
+ /*
108
+ ** Generate a script that does this:
109
+ **
110
+ ** (1) Start a transaction
111
+ ** (2) Rename all tables and columns to use generic names.
112
+ ** (3) Print the schema after this rename
113
+ ** (4) Run pragma integrity_check
114
+ ** (5) Do more ALTER TABLE statements to change the names back
115
+ ** (6) Run pragma integrity_check again
116
+ ** (7) Rollback the transaction
117
+ */
118
+ int main(int argc, char **argv){
119
+ sqlite3 *db;
120
+ int rc;
121
+ sqlite3_str *pConvert;
122
+ sqlite3_str *pUndo;
123
+ char *zDbName;
124
+ char *zSql1, *zSql2;
125
+ if( argc!=2 ){
126
+ fprintf(stderr, "Usage: %s DATABASE\n", argv[0]);
127
+ }
128
+ zDbName = argv[1];
129
+ rc = sqlite3_open(zDbName, &db);
130
+ if( rc ){
131
+ fprintf(stderr, "sqlite3_open() returns %d\n", rc);
132
+ return 1;
133
+ }
134
+ pConvert = sqlite3_str_new(db);
135
+ pUndo = sqlite3_str_new(db);
136
+ rename_all_tables(db, pConvert, pUndo);
137
+ zSql1 = sqlite3_str_finish(pConvert);
138
+ zSql2 = sqlite3_str_finish(pUndo);
139
+ sqlite3_close(db);
140
+ printf("BEGIN;\n");
141
+ printf("%s", zSql1);
142
+ sqlite3_free(zSql1);
143
+ printf(".schema --indent\n");
144
+ printf("PRAGMA integrity_check;\n");
145
+ printf("%s", zSql2);
146
+ sqlite3_free(zSql2);
147
+ printf("PRAGMA integrity_check;\n");
148
+ printf("ROLLBACK;\n");
149
+ return 0;
150
+ }
local-test-sqlite3-delta-03/afc-sqlite3/test/attach.test ADDED
@@ -0,0 +1,928 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2003 April 4
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing the ATTACH and DETACH commands
13
+ # and related functionality.
14
+ #
15
+ # $Id: attach.test,v 1.52 2009/05/29 14:39:08 drh Exp $
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+ ifcapable !attach {
22
+ finish_test
23
+ return
24
+ }
25
+
26
+ for {set i 2} {$i<=15} {incr i} {
27
+ forcedelete test$i.db
28
+ forcedelete test$i.db-journal
29
+ }
30
+
31
+ do_test attach-1.1 {
32
+ execsql {
33
+ CREATE TABLE t1(a,b);
34
+ INSERT INTO t1 VALUES(1,2);
35
+ INSERT INTO t1 VALUES(3,4);
36
+ SELECT * FROM t1;
37
+ }
38
+ } {1 2 3 4}
39
+ do_test attach-1.2 {
40
+ sqlite3 db2 test2.db
41
+ execsql {
42
+ CREATE TABLE t2(x,y);
43
+ INSERT INTO t2 VALUES(1,'x');
44
+ INSERT INTO t2 VALUES(2,'y');
45
+ SELECT * FROM t2;
46
+ } db2
47
+ } {1 x 2 y}
48
+ do_test attach-1.3 {
49
+ execsql {
50
+ ATTACH DATABASE 'test2.db' AS two;
51
+ SELECT * FROM two.t2;
52
+ }
53
+ } {1 x 2 y}
54
+
55
+ # Tests for the sqlite3_db_filename interface
56
+ #
57
+ do_test attach-1.3.1 {
58
+ file tail [sqlite3_db_filename db main]
59
+ } {test.db}
60
+ do_test attach-1.3.2 {
61
+ file tail [sqlite3_db_filename db MAIN]
62
+ } {test.db}
63
+ do_test attach-1.3.3 {
64
+ file tail [sqlite3_db_filename db temp]
65
+ } {}
66
+ do_test attach-1.3.4 {
67
+ file tail [sqlite3_db_filename db two]
68
+ } {test2.db}
69
+ do_test attach-1.3.5 {
70
+ file tail [sqlite3_db_filename db three]
71
+ } {}
72
+
73
+ do_test attach-1.4 {
74
+ execsql {
75
+ SELECT * FROM t2;
76
+ }
77
+ } {1 x 2 y}
78
+ do_test attach-1.5 {
79
+ execsql {
80
+ DETACH DATABASE two;
81
+ SELECT * FROM t1;
82
+ }
83
+ } {1 2 3 4}
84
+ do_test attach-1.6 {
85
+ catchsql {
86
+ SELECT * FROM t2;
87
+ }
88
+ } {1 {no such table: t2}}
89
+ do_test attach-1.7 {
90
+ catchsql {
91
+ SELECT * FROM two.t2;
92
+ }
93
+ } {1 {no such table: two.t2}}
94
+ do_test attach-1.8 {
95
+ catchsql {
96
+ ATTACH DATABASE 'test3.db' AS three;
97
+ }
98
+ } {0 {}}
99
+ do_test attach-1.9 {
100
+ catchsql {
101
+ SELECT * FROM three.sqlite_master;
102
+ }
103
+ } {0 {}}
104
+ do_test attach-1.10 {
105
+ catchsql {
106
+ DETACH DATABASE [three];
107
+ }
108
+ } {0 {}}
109
+ do_test attach-1.11 {
110
+ execsql {
111
+ ATTACH 'test.db' AS db2;
112
+ ATTACH 'test.db' AS db3;
113
+ ATTACH 'test.db' AS db4;
114
+ ATTACH 'test.db' AS db5;
115
+ ATTACH 'test.db' AS db6;
116
+ ATTACH 'test.db' AS db7;
117
+ ATTACH 'test.db' AS db8;
118
+ ATTACH 'test.db' AS db9;
119
+ }
120
+ } {}
121
+ proc db_list {db} {
122
+ set list {}
123
+ foreach {idx name file} [execsql {PRAGMA database_list} $db] {
124
+ lappend list $idx $name
125
+ }
126
+ return $list
127
+ }
128
+ ifcapable schema_pragmas {
129
+ do_test attach-1.11b {
130
+ db_list db
131
+ } {0 main 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9}
132
+ } ;# ifcapable schema_pragmas
133
+ do_test attach-1.12 {
134
+ catchsql {
135
+ ATTACH 'test.db' as db2;
136
+ }
137
+ } {1 {database db2 is already in use}}
138
+ do_test attach-1.12.2 {
139
+ db errorcode
140
+ } {1}
141
+ do_test attach-1.13 {
142
+ catchsql {
143
+ ATTACH 'test.db' as db5;
144
+ }
145
+ } {1 {database db5 is already in use}}
146
+ do_test attach-1.14 {
147
+ catchsql {
148
+ ATTACH 'test.db' as db9;
149
+ }
150
+ } {1 {database db9 is already in use}}
151
+ do_catchsql_test attach-1.15 {
152
+ ATTACH 'test.db' as main;
153
+ } {1 {database main is already in use}}
154
+ ifcapable tempdb {
155
+ do_test attach-1.16 {
156
+ catchsql {
157
+ ATTACH 'test.db' as temp;
158
+ }
159
+ } {1 {database temp is already in use}}
160
+ }
161
+ do_catchsql_test attach-1.17 {
162
+ ATTACH 'test.db' as MAIN;
163
+ } {1 {database MAIN is already in use}}
164
+ do_test attach-1.18 {
165
+ catchsql {
166
+ ATTACH 'test.db' as db10;
167
+ ATTACH 'test.db' as db11;
168
+ }
169
+ } {0 {}}
170
+ if {$SQLITE_MAX_ATTACHED==10} {
171
+ do_test attach-1.19 {
172
+ catchsql {
173
+ ATTACH 'test.db' as db12;
174
+ }
175
+ } {1 {too many attached databases - max 10}}
176
+ do_test attach-1.19.1 {
177
+ db errorcode
178
+ } {1}
179
+ }
180
+ do_test attach-1.20.1 {
181
+ execsql {
182
+ DETACH db5;
183
+ }
184
+ } {}
185
+ ifcapable schema_pragmas {
186
+ do_test attach-1.20.2 {
187
+ db_list db
188
+ } {0 main 2 db2 3 db3 4 db4 5 db6 6 db7 7 db8 8 db9 9 db10 10 db11}
189
+ } ;# ifcapable schema_pragmas
190
+ integrity_check attach-1.20.3
191
+ ifcapable tempdb {
192
+ execsql {select * from temp.sqlite_master}
193
+ }
194
+ do_test attach-1.21 {
195
+ catchsql {
196
+ ATTACH 'test.db' as db12;
197
+ }
198
+ } {0 {}}
199
+ if {$SQLITE_MAX_ATTACHED==10} {
200
+ do_test attach-1.22 {
201
+ catchsql {
202
+ ATTACH 'test.db' as db13;
203
+ }
204
+ } {1 {too many attached databases - max 10}}
205
+ do_test attach-1.22.1 {
206
+ db errorcode
207
+ } {1}
208
+ }
209
+ do_test attach-1.23 {
210
+ catchsql {
211
+ DETACH "db14";
212
+ }
213
+ } {1 {no such database: db14}}
214
+ do_test attach-1.24 {
215
+ catchsql {
216
+ DETACH db12;
217
+ }
218
+ } {0 {}}
219
+ do_test attach-1.25 {
220
+ catchsql {
221
+ DETACH db12;
222
+ }
223
+ } {1 {no such database: db12}}
224
+ do_test attach-1.26 {
225
+ catchsql {
226
+ DETACH main;
227
+ }
228
+ } {1 {cannot detach database main}}
229
+
230
+
231
+ ifcapable tempdb {
232
+ do_test attach-1.27 {
233
+ catchsql {
234
+ DETACH Temp;
235
+ }
236
+ } {1 {cannot detach database Temp}}
237
+ } else {
238
+ do_test attach-1.27 {
239
+ catchsql {
240
+ DETACH Temp;
241
+ }
242
+ } {1 {no such database: Temp}}
243
+ }
244
+
245
+ do_test attach-1.28 {
246
+ catchsql {
247
+ DETACH db11;
248
+ DETACH db10;
249
+ DETACH db9;
250
+ DETACH db8;
251
+ DETACH db7;
252
+ DETACH db6;
253
+ DETACH db4;
254
+ DETACH db3;
255
+ DETACH db2;
256
+ }
257
+ } {0 {}}
258
+ ifcapable schema_pragmas {
259
+ ifcapable tempdb {
260
+ do_test attach-1.29 {
261
+ db_list db
262
+ } {0 main 1 temp}
263
+ } else {
264
+ do_test attach-1.29 {
265
+ db_list db
266
+ } {0 main}
267
+ }
268
+ } ;# ifcapable schema_pragmas
269
+
270
+ ifcapable {trigger} { # Only do the following tests if triggers are enabled
271
+ do_test attach-2.1 {
272
+ execsql {
273
+ CREATE TABLE tx(x1,x2,y1,y2);
274
+ CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
275
+ INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y);
276
+ END;
277
+ SELECT * FROM tx;
278
+ } db2;
279
+ } {}
280
+ do_test attach-2.2 {
281
+ execsql {
282
+ UPDATE t2 SET x=x+10;
283
+ SELECT * FROM tx;
284
+ } db2;
285
+ } {1 11 x x 2 12 y y}
286
+ do_test attach-2.3 {
287
+ execsql {
288
+ CREATE TABLE tx(x1,x2,y1,y2);
289
+ SELECT * FROM tx;
290
+ }
291
+ } {}
292
+ do_test attach-2.4 {
293
+ execsql {
294
+ ATTACH 'test2.db' AS db2;
295
+ }
296
+ } {}
297
+ do_test attach-2.5 {
298
+ execsql {
299
+ UPDATE db2.t2 SET x=x+10;
300
+ SELECT * FROM db2.tx;
301
+ }
302
+ } {1 11 x x 2 12 y y 11 21 x x 12 22 y y}
303
+ do_test attach-2.6 {
304
+ execsql {
305
+ SELECT * FROM main.tx;
306
+ }
307
+ } {}
308
+ do_test attach-2.7 {
309
+ execsql {
310
+ SELECT type, name, tbl_name FROM db2.sqlite_master;
311
+ }
312
+ } {table t2 t2 table tx tx trigger r1 t2}
313
+
314
+ ifcapable schema_pragmas&&tempdb {
315
+ do_test attach-2.8 {
316
+ db_list db
317
+ } {0 main 1 temp 2 db2}
318
+ } ;# ifcapable schema_pragmas&&tempdb
319
+ ifcapable schema_pragmas&&!tempdb {
320
+ do_test attach-2.8 {
321
+ db_list db
322
+ } {0 main 2 db2}
323
+ } ;# ifcapable schema_pragmas&&!tempdb
324
+
325
+ do_test attach-2.9 {
326
+ execsql {
327
+ CREATE INDEX i2 ON t2(x);
328
+ SELECT * FROM t2 WHERE x>5;
329
+ } db2
330
+ } {21 x 22 y}
331
+ do_test attach-2.10 {
332
+ execsql {
333
+ SELECT type, name, tbl_name FROM sqlite_master;
334
+ } db2
335
+ } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
336
+ #do_test attach-2.11 {
337
+ # catchsql {
338
+ # SELECT * FROM t2 WHERE x>5;
339
+ # }
340
+ #} {1 {database schema has changed}}
341
+ ifcapable schema_pragmas {
342
+ ifcapable tempdb {
343
+ do_test attach-2.12 {
344
+ db_list db
345
+ } {0 main 1 temp 2 db2}
346
+ } else {
347
+ do_test attach-2.12 {
348
+ db_list db
349
+ } {0 main 2 db2}
350
+ }
351
+ } ;# ifcapable schema_pragmas
352
+ do_test attach-2.13 {
353
+ catchsql {
354
+ SELECT * FROM t2 WHERE x>5;
355
+ }
356
+ } {0 {21 x 22 y}}
357
+ do_test attach-2.14 {
358
+ execsql {
359
+ SELECT type, name, tbl_name FROM sqlite_master;
360
+ }
361
+ } {table t1 t1 table tx tx}
362
+ do_test attach-2.15 {
363
+ execsql {
364
+ SELECT type, name, tbl_name FROM db2.sqlite_master;
365
+ }
366
+ } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
367
+ do_test attach-2.16 {
368
+ db close
369
+ sqlite3 db test.db
370
+ execsql {
371
+ ATTACH 'test2.db' AS db2;
372
+ SELECT type, name, tbl_name FROM db2.sqlite_master;
373
+ }
374
+ } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
375
+ } ;# End of ifcapable {trigger}
376
+
377
+ do_test attach-3.1 {
378
+ db close
379
+ db2 close
380
+ sqlite3 db test.db
381
+ sqlite3 db2 test2.db
382
+ execsql {
383
+ SELECT * FROM t1
384
+ }
385
+ } {1 2 3 4}
386
+
387
+ # If we are testing a version of the code that lacks trigger support,
388
+ # adjust the database contents so that they are the same if triggers
389
+ # had been enabled.
390
+ ifcapable {!trigger} {
391
+ db2 eval {
392
+ DELETE FROM t2;
393
+ INSERT INTO t2 VALUES(21, 'x');
394
+ INSERT INTO t2 VALUES(22, 'y');
395
+ CREATE TABLE tx(x1,x2,y1,y2);
396
+ INSERT INTO tx VALUES(1, 11, 'x', 'x');
397
+ INSERT INTO tx VALUES(2, 12, 'y', 'y');
398
+ INSERT INTO tx VALUES(11, 21, 'x', 'x');
399
+ INSERT INTO tx VALUES(12, 22, 'y', 'y');
400
+ CREATE INDEX i2 ON t2(x);
401
+ }
402
+ }
403
+
404
+ do_test attach-3.2 {
405
+ catchsql {
406
+ SELECT * FROM t2
407
+ }
408
+ } {1 {no such table: t2}}
409
+ do_test attach-3.3 {
410
+ catchsql {
411
+ ATTACH DATABASE 'test2.db' AS db2;
412
+ SELECT * FROM t2
413
+ }
414
+ } {0 {21 x 22 y}}
415
+
416
+ # Even though 'db' has started a transaction, it should not yet have
417
+ # a lock on test2.db so 'db2' should be readable.
418
+ do_test attach-3.4 {
419
+ execsql BEGIN
420
+ catchsql {
421
+ SELECT * FROM t2;
422
+ } db2;
423
+ } {0 {21 x 22 y}}
424
+
425
+ # Reading from test2.db from db within a transaction should not
426
+ # prevent test2.db from being read by db2.
427
+ do_test attach-3.5 {
428
+ execsql {SELECT * FROM t2}
429
+ catchsql {
430
+ SELECT * FROM t2;
431
+ } db2;
432
+ } {0 {21 x 22 y}}
433
+
434
+ # Making a change to test2.db through db causes test2.db to get
435
+ # a reserved lock. It should still be accessible through db2.
436
+ do_test attach-3.6 {
437
+ execsql {
438
+ UPDATE t2 SET x=x+1 WHERE x=50;
439
+ }
440
+ catchsql {
441
+ SELECT * FROM t2;
442
+ } db2;
443
+ } {0 {21 x 22 y}}
444
+
445
+ do_test attach-3.7 {
446
+ execsql ROLLBACK
447
+ execsql {SELECT * FROM t2} db2
448
+ } {21 x 22 y}
449
+
450
+ # Start transactions on both db and db2. Once again, just because
451
+ # we make a change to test2.db using db2, only a RESERVED lock is
452
+ # obtained, so test2.db should still be readable using db.
453
+ #
454
+ do_test attach-3.8 {
455
+ execsql BEGIN
456
+ execsql BEGIN db2
457
+ execsql {UPDATE t2 SET x=0 WHERE 0} db2
458
+ catchsql {SELECT * FROM t2}
459
+ } {0 {21 x 22 y}}
460
+
461
+ # It is also still accessible from db2.
462
+ do_test attach-3.9 {
463
+ catchsql {SELECT * FROM t2} db2
464
+ } {0 {21 x 22 y}}
465
+
466
+ do_test attach-3.10 {
467
+ execsql {SELECT * FROM t1}
468
+ } {1 2 3 4}
469
+
470
+ do_test attach-3.11 {
471
+ catchsql {UPDATE t1 SET a=a+1}
472
+ } {0 {}}
473
+ do_test attach-3.12 {
474
+ execsql {SELECT * FROM t1}
475
+ } {2 2 4 4}
476
+
477
+ # db2 has a RESERVED lock on test2.db, so db cannot write to any tables
478
+ # in test2.db.
479
+ do_test attach-3.13 {
480
+ catchsql {UPDATE t2 SET x=x+1 WHERE x=50}
481
+ } {1 {database is locked}}
482
+
483
+ # Change for version 3. Transaction is no longer rolled back
484
+ # for a locked database.
485
+ execsql {ROLLBACK}
486
+
487
+ # db is able to reread its schema because db2 still only holds a
488
+ # reserved lock.
489
+ do_test attach-3.14 {
490
+ catchsql {SELECT * FROM t1}
491
+ } {0 {1 2 3 4}}
492
+ do_test attach-3.15 {
493
+ execsql COMMIT db2
494
+ execsql {SELECT * FROM t1}
495
+ } {1 2 3 4}
496
+
497
+ # Ticket #323
498
+ do_test attach-4.1 {
499
+ execsql {DETACH db2}
500
+ db2 close
501
+ sqlite3 db2 test2.db
502
+ execsql {
503
+ CREATE TABLE t3(x,y);
504
+ CREATE UNIQUE INDEX t3i1 ON t3(x);
505
+ INSERT INTO t3 VALUES(1,2);
506
+ SELECT * FROM t3;
507
+ } db2;
508
+ } {1 2}
509
+ do_test attach-4.2 {
510
+ execsql {
511
+ CREATE TABLE t3(a,b);
512
+ CREATE UNIQUE INDEX t3i1b ON t3(a);
513
+ INSERT INTO t3 VALUES(9,10);
514
+ SELECT * FROM t3;
515
+ }
516
+ } {9 10}
517
+ do_test attach-4.3 {
518
+ execsql {
519
+ ATTACH DATABASE 'test2.db' AS db2;
520
+ SELECT * FROM db2.t3;
521
+ }
522
+ } {1 2}
523
+ do_test attach-4.4 {
524
+ execsql {
525
+ SELECT * FROM main.t3;
526
+ }
527
+ } {9 10}
528
+ do_test attach-4.5 {
529
+ execsql {
530
+ INSERT INTO db2.t3 VALUES(9,10);
531
+ SELECT * FROM db2.t3;
532
+ }
533
+ } {1 2 9 10}
534
+ execsql {
535
+ DETACH db2;
536
+ }
537
+ ifcapable {trigger} {
538
+ do_test attach-4.6 {
539
+ execsql {
540
+ CREATE TABLE t4(x);
541
+ CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
542
+ INSERT INTO t4 VALUES('db2.' || NEW.x);
543
+ END;
544
+ INSERT INTO t3 VALUES(6,7);
545
+ SELECT * FROM t4;
546
+ } db2
547
+ } {db2.6}
548
+ do_test attach-4.7 {
549
+ execsql {
550
+ CREATE TABLE t4(y);
551
+ CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
552
+ INSERT INTO t4 VALUES('main.' || NEW.a);
553
+ END;
554
+ INSERT INTO main.t3 VALUES(11,12);
555
+ SELECT * FROM main.t4;
556
+ }
557
+ } {main.11}
558
+ }
559
+ ifcapable {!trigger} {
560
+ # When we do not have trigger support, set up the table like they
561
+ # would have been had triggers been there. The tests that follow need
562
+ # this setup.
563
+ execsql {
564
+ CREATE TABLE t4(x);
565
+ INSERT INTO t3 VALUES(6,7);
566
+ INSERT INTO t4 VALUES('db2.6');
567
+ INSERT INTO t4 VALUES('db2.13');
568
+ } db2
569
+ execsql {
570
+ CREATE TABLE t4(y);
571
+ INSERT INTO main.t3 VALUES(11,12);
572
+ INSERT INTO t4 VALUES('main.11');
573
+ }
574
+ }
575
+
576
+
577
+ # This one is tricky. On the UNION ALL select, we have to make sure
578
+ # the schema for both main and db2 is valid before starting to execute
579
+ # the first query of the UNION ALL. If we wait to test the validity of
580
+ # the schema for main until after the first query has run, that test will
581
+ # fail and the query will abort but we will have already output some
582
+ # results. When the query is retried, the results will be repeated.
583
+ #
584
+ ifcapable compound {
585
+ do_test attach-4.8 {
586
+ execsql {
587
+ ATTACH DATABASE 'test2.db' AS db2;
588
+ INSERT INTO db2.t3 VALUES(13,14);
589
+ SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
590
+ }
591
+ } {db2.6 db2.13 main.11}
592
+
593
+ do_test attach-4.9 {
594
+ ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
595
+ execsql {
596
+ INSERT INTO main.t3 VALUES(15,16);
597
+ SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
598
+ }
599
+ } {db2.6 db2.13 main.11 main.15}
600
+ } ;# ifcapable compound
601
+
602
+ ifcapable !compound {
603
+ ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
604
+ execsql {
605
+ ATTACH DATABASE 'test2.db' AS db2;
606
+ INSERT INTO db2.t3 VALUES(13,14);
607
+ INSERT INTO main.t3 VALUES(15,16);
608
+ }
609
+ } ;# ifcapable !compound
610
+
611
+ ifcapable view {
612
+ do_test attach-4.10 {
613
+ execsql {
614
+ DETACH DATABASE db2;
615
+ }
616
+ execsql {
617
+ CREATE VIEW v3 AS SELECT x*100+y FROM t3;
618
+ SELECT * FROM v3;
619
+ } db2
620
+ } {102 910 607 1314}
621
+ do_test attach-4.11 {
622
+ execsql {
623
+ CREATE VIEW v3 AS SELECT a*100+b FROM t3;
624
+ SELECT * FROM v3;
625
+ }
626
+ } {910 1112 1516}
627
+ do_test attach-4.12 {
628
+ execsql {
629
+ ATTACH DATABASE 'test2.db' AS db2;
630
+ SELECT * FROM db2.v3;
631
+ }
632
+ } {102 910 607 1314}
633
+ do_test attach-4.13 {
634
+ execsql {
635
+ SELECT * FROM main.v3;
636
+ }
637
+ } {910 1112 1516}
638
+ } ;# ifcapable view
639
+
640
+ # Tests for the sqliteFix...() routines in attach.c
641
+ #
642
+ ifcapable {trigger} {
643
+ do_test attach-5.1 {
644
+ db close
645
+ sqlite3 db test.db
646
+ db2 close
647
+ forcedelete test2.db
648
+ sqlite3 db2 test2.db
649
+ catchsql {
650
+ ATTACH DATABASE 'test.db' AS orig;
651
+ CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN
652
+ SELECT 'no-op';
653
+ END;
654
+ } db2
655
+ } {1 {trigger r1 cannot reference objects in database orig}}
656
+ do_test attach-5.2 {
657
+ catchsql {
658
+ CREATE TABLE t5(x,y);
659
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
660
+ SELECT 'no-op';
661
+ END;
662
+ } db2
663
+ } {0 {}}
664
+ do_test attach-5.3 {
665
+ catchsql {
666
+ DROP TRIGGER r5;
667
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
668
+ SELECT 'no-op' FROM orig.t1;
669
+ END;
670
+ } db2
671
+ } {1 {trigger r5 cannot reference objects in database orig}}
672
+ ifcapable tempdb {
673
+ do_test attach-5.4 {
674
+ catchsql {
675
+ CREATE TEMP TABLE t6(p,q,r);
676
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
677
+ SELECT 'no-op' FROM temp.t6;
678
+ END;
679
+ } db2
680
+ } {1 {trigger r5 cannot reference objects in database temp}}
681
+ }
682
+ ifcapable subquery {
683
+ do_test attach-5.5 {
684
+ catchsql {
685
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
686
+ SELECT 'no-op' || (SELECT * FROM temp.t6);
687
+ END;
688
+ } db2
689
+ } {1 {trigger r5 cannot reference objects in database temp}}
690
+ do_test attach-5.6 {
691
+ catchsql {
692
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
693
+ SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
694
+ END;
695
+ } db2
696
+ } {1 {trigger r5 cannot reference objects in database temp}}
697
+ do_test attach-5.7 {
698
+ catchsql {
699
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
700
+ SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6);
701
+ END;
702
+ } db2
703
+ } {1 {trigger r5 cannot reference objects in database temp}}
704
+ do_test attach-5.7 {
705
+ catchsql {
706
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
707
+ SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1;
708
+ END;
709
+ } db2
710
+ } {1 {trigger r5 cannot reference objects in database temp}}
711
+ do_test attach-5.8 {
712
+ catchsql {
713
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
714
+ INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5);
715
+ END;
716
+ } db2
717
+ } {1 {trigger r5 cannot reference objects in database temp}}
718
+ do_test attach-5.9 {
719
+ catchsql {
720
+ CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
721
+ DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
722
+ END;
723
+ } db2
724
+ } {1 {trigger r5 cannot reference objects in database temp}}
725
+ } ;# endif subquery
726
+ ifcapable json1&&vtab {
727
+ do_test attach-5.10 {
728
+ db close
729
+ catch {db2 close}
730
+ forcedelete test.db
731
+ sqlite3 db test.db
732
+ db eval {
733
+ CREATE TABLE t1(x);
734
+ CREATE TABLE t2(a,b);
735
+ CREATE TRIGGER x1 AFTER INSERT ON t1 BEGIN
736
+ INSERT INTO t2(a,b) SELECT key, value FROM json_each(NEW.x);
737
+ END;
738
+ INSERT INTO t1(x) VALUES('{"a":1}');
739
+ SELECT * FROM t2;
740
+ }
741
+ } {a 1}
742
+ do_test attach-5.11 {
743
+ sqlite3 db2 :memory:
744
+ db2 eval {
745
+ CREATE TABLE t3(y);
746
+ ATTACH 'test.db' AS aux;
747
+ INSERT INTO aux.t1(x) VALUES('{"b":2}');
748
+ SELECT * FROM aux.t2;
749
+ }
750
+ } {a 1 b 2}
751
+ } ;# endif json1
752
+ } ;# endif trigger
753
+
754
+ # Check to make sure we get a sensible error if unable to open
755
+ # the file that we are trying to attach.
756
+ #
757
+ do_test attach-6.1 {
758
+ catchsql {
759
+ ATTACH DATABASE 'no-such-file' AS nosuch;
760
+ }
761
+ } {0 {}}
762
+ if {$tcl_platform(platform)=="unix"} {
763
+ do_test attach-6.2 {
764
+ sqlite3 dbx cannot-read
765
+ dbx eval {CREATE TABLE t1(a,b,c)}
766
+ dbx close
767
+ file attributes cannot-read -permission 0000
768
+ if {[file writable cannot-read]} {
769
+ puts "\n**** Tests do not work when run as root ****"
770
+ forcedelete cannot-read
771
+ exit 1
772
+ }
773
+ catchsql {
774
+ ATTACH DATABASE 'cannot-read' AS noread;
775
+ }
776
+ } {1 {unable to open database: cannot-read}}
777
+ do_test attach-6.2.2 {
778
+ db errorcode
779
+ } {14}
780
+ forcedelete cannot-read
781
+ }
782
+
783
+ # Check the error message if we try to access a database that has
784
+ # not been attached.
785
+ do_test attach-6.3 {
786
+ catchsql {
787
+ CREATE TABLE no_such_db.t1(a, b, c);
788
+ }
789
+ } {1 {unknown database no_such_db}}
790
+ for {set i 2} {$i<=15} {incr i} {
791
+ catch {db$i close}
792
+ }
793
+ db close
794
+ forcedelete test2.db
795
+ forcedelete no-such-file
796
+
797
+ ifcapable subquery {
798
+ do_test attach-7.1 {
799
+ forcedelete test.db test.db-journal
800
+ sqlite3 db test.db
801
+ catchsql {
802
+ DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY
803
+ REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL )
804
+ }
805
+ } {1 {no such table: AAAAAA}}
806
+ }
807
+
808
+ # Create a malformed file (a file that is not a valid database)
809
+ # and try to attach it
810
+ #
811
+ do_test attach-8.1 {
812
+ set fd [open test2.db w]
813
+ puts $fd "This file is not a valid SQLite database"
814
+ close $fd
815
+ catchsql {
816
+ ATTACH 'test2.db' AS t2;
817
+ }
818
+ } {1 {file is not a database}}
819
+ do_test attach-8.2 {
820
+ db errorcode
821
+ } {26}
822
+ forcedelete test2.db
823
+ do_test attach-8.3 {
824
+ sqlite3 db2 test2.db
825
+ db2 eval {CREATE TABLE t1(x); BEGIN EXCLUSIVE}
826
+ catchsql {
827
+ ATTACH 'test2.db' AS t2;
828
+ }
829
+ } {1 {database is locked}}
830
+ do_test attach-8.4 {
831
+ db errorcode
832
+ } {5}
833
+ db2 close
834
+ forcedelete test2.db
835
+
836
+ # Test that it is possible to attach the same database more than
837
+ # once when not in shared-cache mode. That this is not possible in
838
+ # shared-cache mode is tested in shared7.test.
839
+ do_test attach-9.1 {
840
+ forcedelete test4.db
841
+ execsql {
842
+ ATTACH 'test4.db' AS aux1;
843
+ CREATE TABLE aux1.t1(a, b);
844
+ INSERT INTO aux1.t1 VALUES(1, 2);
845
+ ATTACH 'test4.db' AS aux2;
846
+ SELECT * FROM aux2.t1;
847
+ }
848
+ } {1 2}
849
+ do_test attach-9.2 {
850
+ catchsql {
851
+ BEGIN;
852
+ INSERT INTO aux1.t1 VALUES(3, 4);
853
+ INSERT INTO aux2.t1 VALUES(5, 6);
854
+ }
855
+ } {1 {database is locked}}
856
+ do_test attach-9.3 {
857
+ execsql {
858
+ COMMIT;
859
+ SELECT * FROM aux2.t1;
860
+ }
861
+ } {1 2 3 4}
862
+
863
+ # Ticket [abe728bbc311d81334dae9762f0db87c07a98f79].
864
+ # Multi-database commit on an attached TEMP database.
865
+ #
866
+ do_test attach-10.1 {
867
+ execsql {
868
+ ATTACH '' AS noname;
869
+ ATTACH ':memory:' AS inmem;
870
+ BEGIN;
871
+ CREATE TABLE noname.noname(x);
872
+ CREATE TABLE inmem.inmem(y);
873
+ CREATE TABLE main.main(z);
874
+ COMMIT;
875
+ SELECT name FROM noname.sqlite_master;
876
+ SELECT name FROM inmem.sqlite_master;
877
+ }
878
+ } {noname inmem}
879
+ do_test attach-10.2 {
880
+ lrange [execsql {
881
+ PRAGMA database_list;
882
+ }] 9 end
883
+ } {4 noname {} 5 inmem {}}
884
+
885
+ # Attach with a very long URI filename.
886
+ #
887
+ db close
888
+ sqlite3 db test.db -uri 1
889
+ do_execsql_test attach-11.1 {
890
+ ATTACH printf('file:%09000x/x.db?mode=memory&cache=shared',1) AS aux1;
891
+ CREATE TABLE aux1.t1(x,y);
892
+ INSERT INTO aux1.t1(x,y) VALUES(1,2),(3,4);
893
+ SELECT * FROM aux1.t1;
894
+ } {1 2 3 4}
895
+
896
+ # Ticket https://sqlite.org/src/tktview/a4e06e75a9ab61a1 2017-07-15
897
+ # False positive when running integrity_check on a connection with
898
+ # attached databases.
899
+ #
900
+ db close
901
+ sqlite3 db :memory:
902
+ do_execsql_test attach-12.1 {
903
+ CREATE TABLE Table1 (col TEXT NOT NULL PRIMARY KEY);
904
+ ATTACH ':memory:' AS db2;
905
+ CREATE TABLE db2.Table2(col1 INTEGER, col2 INTEGER, col3 INTEGER, col4);
906
+ CREATE UNIQUE INDEX db2.idx_col1_unique ON Table2 (col1);
907
+ CREATE UNIQUE INDEX db2.idx_col23_unique ON Table2 (col2, col3);
908
+ CREATE INDEX db2.idx_col2 ON Table2 (col2);
909
+ INSERT INTO Table2 VALUES(1,2,3,4);
910
+ PRAGMA integrity_check;
911
+ } {ok}
912
+
913
+ # 2021-03-10 Forum post https://sqlite.org/forum/forumpost/a006d86f72
914
+ #
915
+ reset_db
916
+ do_test attach-13.1 {
917
+ sqlite3 db :memory:
918
+ db eval {CREATE TABLE base(x);}
919
+ for {set i 0} {$i<$SQLITE_MAX_ATTACHED} {incr i} {
920
+ db eval "ATTACH ':memory:' AS a$i"
921
+ }
922
+ set m "a[expr {$SQLITE_MAX_ATTACHED-1}]"
923
+ db eval "CREATE TABLE $m.t1(a INTEGER PRIMARY KEY, b);"
924
+ db eval "CREATE TABLE $m.t2(a INTEGER PRIMARY KEY, b);"
925
+ db eval {SELECT a FROM t1 WHERE b IN (SELECT a FROM t2);}
926
+ } {}
927
+
928
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/attach3.test ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2003 July 1
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing the ATTACH and DETACH commands
13
+ # and schema changes to attached databases.
14
+ #
15
+ # $Id: attach3.test,v 1.18 2007/10/09 08:29:32 danielk1977 Exp $
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+ ifcapable !attach {
22
+ finish_test
23
+ return
24
+ }
25
+
26
+ # The tests in this file were written before SQLite supported recursive
27
+ # trigger invocation, and some tests depend on that to pass. So disable
28
+ # recursive triggers for this file.
29
+ catchsql { pragma recursive_triggers = off }
30
+
31
+ # Create tables t1 and t2 in the main database
32
+ execsql {
33
+ CREATE TABLE t1(a, b);
34
+ CREATE TABLE t2(c, d);
35
+ }
36
+
37
+ # Create tables t1 and t2 in database file test2.db
38
+ forcedelete test2.db
39
+ forcedelete test2.db-journal
40
+ sqlite3 db2 test2.db
41
+ execsql {
42
+ CREATE TABLE t1(a, b);
43
+ CREATE TABLE t2(c, d);
44
+ } db2
45
+ db2 close
46
+
47
+ # Create a table in the auxilary database.
48
+ do_test attach3-1.1 {
49
+ execsql {
50
+ ATTACH 'test2.db' AS aux;
51
+ }
52
+ } {}
53
+ do_test attach3-1.2 {
54
+ execsql {
55
+ CREATE TABLE aux.t3(e, f);
56
+ }
57
+ } {}
58
+ do_test attach3-1.3 {
59
+ execsql {
60
+ SELECT * FROM sqlite_master WHERE name = 't3';
61
+ }
62
+ } {}
63
+ do_test attach3-1.4 {
64
+ execsql {
65
+ SELECT * FROM aux.sqlite_master WHERE name = 't3';
66
+ }
67
+ } "table t3 t3 [expr $AUTOVACUUM?5:4] {CREATE TABLE t3(e, f)}"
68
+ do_test attach3-1.5 {
69
+ execsql {
70
+ INSERT INTO t3 VALUES(1, 2);
71
+ SELECT * FROM t3;
72
+ }
73
+ } {1 2}
74
+
75
+ # Create an index on the auxilary database table.
76
+ do_test attach3-2.1 {
77
+ execsql {
78
+ CREATE INDEX aux.i1 on t3(e);
79
+ }
80
+ } {}
81
+ do_test attach3-2.2 {
82
+ execsql {
83
+ SELECT * FROM sqlite_master WHERE name = 'i1';
84
+ }
85
+ } {}
86
+ do_test attach3-2.3 {
87
+ execsql {
88
+ SELECT * FROM aux.sqlite_master WHERE name = 'i1';
89
+ }
90
+ } "index i1 t3 [expr $AUTOVACUUM?6:5] {CREATE INDEX i1 on t3(e)}"
91
+
92
+ # Drop the index on the aux database table.
93
+ do_test attach3-3.1 {
94
+ execsql {
95
+ DROP INDEX aux.i1;
96
+ SELECT * FROM aux.sqlite_master WHERE name = 'i1';
97
+ }
98
+ } {}
99
+ do_test attach3-3.2 {
100
+ execsql {
101
+ CREATE INDEX aux.i1 on t3(e);
102
+ SELECT * FROM aux.sqlite_master WHERE name = 'i1';
103
+ }
104
+ } "index i1 t3 [expr $AUTOVACUUM?6:5] {CREATE INDEX i1 on t3(e)}"
105
+ do_test attach3-3.3 {
106
+ execsql {
107
+ DROP INDEX i1;
108
+ SELECT * FROM aux.sqlite_master WHERE name = 'i1';
109
+ }
110
+ } {}
111
+
112
+ # Drop tables t1 and t2 in the auxilary database.
113
+ do_test attach3-4.1 {
114
+ execsql {
115
+ DROP TABLE aux.t1;
116
+ SELECT name FROM aux.sqlite_master;
117
+ }
118
+ } {t2 t3}
119
+ do_test attach3-4.2 {
120
+ # This will drop main.t2
121
+ execsql {
122
+ DROP TABLE t2;
123
+ SELECT name FROM aux.sqlite_master;
124
+ }
125
+ } {t2 t3}
126
+ do_test attach3-4.3 {
127
+ execsql {
128
+ DROP TABLE t2;
129
+ SELECT name FROM aux.sqlite_master;
130
+ }
131
+ } {t3}
132
+
133
+ # Create a view in the auxilary database.
134
+ ifcapable view {
135
+ do_test attach3-5.1 {
136
+ execsql {
137
+ CREATE VIEW aux.v1 AS SELECT * FROM t3;
138
+ }
139
+ } {}
140
+ do_test attach3-5.2 {
141
+ execsql {
142
+ SELECT * FROM aux.sqlite_master WHERE name = 'v1';
143
+ }
144
+ } {view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t3}}
145
+ do_test attach3-5.3 {
146
+ execsql {
147
+ INSERT INTO aux.t3 VALUES('hello', 'world');
148
+ SELECT * FROM v1;
149
+ }
150
+ } {1 2 hello world}
151
+
152
+ # Drop the view
153
+ do_test attach3-6.1 {
154
+ execsql {
155
+ DROP VIEW aux.v1;
156
+ }
157
+ } {}
158
+ do_test attach3-6.2 {
159
+ execsql {
160
+ SELECT * FROM aux.sqlite_master WHERE name = 'v1';
161
+ }
162
+ } {}
163
+ } ;# ifcapable view
164
+
165
+ ifcapable {trigger} {
166
+ # Create a trigger in the auxilary database.
167
+ do_test attach3-7.1 {
168
+ execsql {
169
+ CREATE TRIGGER aux.tr1 AFTER INSERT ON t3 BEGIN
170
+ INSERT INTO t3 VALUES(new.e*2, new.f*2);
171
+ END;
172
+ }
173
+ } {}
174
+ do_test attach3-7.2 {
175
+ execsql {
176
+ DELETE FROM t3;
177
+ INSERT INTO t3 VALUES(10, 20);
178
+ SELECT * FROM t3;
179
+ }
180
+ } {10 20 20 40}
181
+ do_test attach3-5.3 {
182
+ execsql {
183
+ SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
184
+ }
185
+ } {trigger tr1 t3 0 {CREATE TRIGGER tr1 AFTER INSERT ON t3 BEGIN
186
+ INSERT INTO t3 VALUES(new.e*2, new.f*2);
187
+ END}}
188
+
189
+ # Drop the trigger
190
+ do_test attach3-8.1 {
191
+ execsql {
192
+ DROP TRIGGER aux.tr1;
193
+ }
194
+ } {}
195
+ do_test attach3-8.2 {
196
+ execsql {
197
+ SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
198
+ }
199
+ } {}
200
+
201
+ ifcapable tempdb {
202
+ # Try to trick SQLite into dropping the wrong temp trigger.
203
+ do_test attach3-9.0 {
204
+ execsql {
205
+ CREATE TABLE main.t4(a, b, c);
206
+ CREATE TABLE aux.t4(a, b, c);
207
+ CREATE TEMP TRIGGER tst_trigger BEFORE INSERT ON aux.t4 BEGIN
208
+ SELECT 'hello world';
209
+ END;
210
+ SELECT count(*) FROM temp.sqlite_master;
211
+ }
212
+ } {1}
213
+ do_test attach3-9.1 {
214
+ execsql {
215
+ DROP TABLE main.t4;
216
+ SELECT count(*) FROM sqlite_temp_master;
217
+ }
218
+ } {1}
219
+ do_test attach3-9.2 {
220
+ execsql {
221
+ DROP TABLE aux.t4;
222
+ SELECT count(*) FROM temp.sqlite_master;
223
+ }
224
+ } {0}
225
+ }
226
+ } ;# endif trigger
227
+
228
+ # Make sure the aux.sqlite_master table is read-only
229
+ do_test attach3-10.0 {
230
+ catchsql {
231
+ INSERT INTO aux.sqlite_master VALUES(1, 2, 3, 4, 5);
232
+ }
233
+ } {1 {table sqlite_master may not be modified}}
234
+
235
+ # Failure to attach leaves us in a workable state.
236
+ # Ticket #811
237
+ #
238
+ do_test attach3-11.0 {
239
+ catchsql {
240
+ ATTACH DATABASE '/nodir/nofile.x' AS notadb;
241
+ }
242
+ } {1 {unable to open database: /nodir/nofile.x}}
243
+ do_test attach3-11.1 {
244
+ catchsql {
245
+ ATTACH DATABASE ':memory:' AS notadb;
246
+ }
247
+ } {0 {}}
248
+ do_test attach3-11.2 {
249
+ catchsql {
250
+ DETACH DATABASE notadb;
251
+ }
252
+ } {0 {}}
253
+
254
+ # Return a list of attached databases
255
+ #
256
+ proc db_list {} {
257
+ set x [execsql {
258
+ PRAGMA database_list;
259
+ }]
260
+ set y {}
261
+ foreach {n id file} $x {lappend y $id}
262
+ return $y
263
+ }
264
+
265
+ ifcapable schema_pragmas&&tempdb {
266
+
267
+ ifcapable !trigger {
268
+ execsql {create temp table dummy(dummy)}
269
+ }
270
+
271
+ # Ticket #1825
272
+ #
273
+ do_test attach3-12.1 {
274
+ db_list
275
+ } {main temp aux}
276
+ do_test attach3-12.2 {
277
+ execsql {
278
+ ATTACH DATABASE ? AS ?
279
+ }
280
+ db_list
281
+ } {main temp aux {}}
282
+ do_test attach3-12.3 {
283
+ execsql {
284
+ DETACH aux
285
+ }
286
+ db_list
287
+ } {main temp {}}
288
+ do_test attach3-12.4 {
289
+ execsql {
290
+ DETACH ?
291
+ }
292
+ db_list
293
+ } {main temp}
294
+ do_test attach3-12.5 {
295
+ execsql {
296
+ ATTACH DATABASE '' AS ''
297
+ }
298
+ db_list
299
+ } {main temp {}}
300
+ do_test attach3-12.6 {
301
+ execsql {
302
+ DETACH ''
303
+ }
304
+ db_list
305
+ } {main temp}
306
+ do_test attach3-12.7 {
307
+ execsql {
308
+ ATTACH DATABASE '' AS ?
309
+ }
310
+ db_list
311
+ } {main temp {}}
312
+ do_test attach3-12.8 {
313
+ execsql {
314
+ DETACH ''
315
+ }
316
+ db_list
317
+ } {main temp}
318
+ do_test attach3-12.9 {
319
+ execsql {
320
+ ATTACH DATABASE '' AS NULL
321
+ }
322
+ db_list
323
+ } {main temp {}}
324
+ do_test attach3-12.10 {
325
+ execsql {
326
+ DETACH ?
327
+ }
328
+ db_list
329
+ } {main temp}
330
+ do_test attach3-12.11 {
331
+ catchsql {
332
+ DETACH NULL
333
+ }
334
+ } {1 {no such database: }}
335
+ do_test attach3-12.12 {
336
+ catchsql {
337
+ ATTACH null AS null;
338
+ ATTACH '' AS '';
339
+ }
340
+ } {1 {database is already in use}}
341
+ do_test attach3-12.13 {
342
+ db_list
343
+ } {main temp {}}
344
+ do_test attach3-12.14 {
345
+ execsql {
346
+ DETACH '';
347
+ }
348
+ db_list
349
+ } {main temp}
350
+
351
+ } ;# ifcapable pragma
352
+
353
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/attachmalloc.test ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2005 September 19
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing the ATTACH statement and
13
+ # specifically out-of-memory conditions within that command.
14
+ #
15
+ # $Id: attachmalloc.test,v 1.10 2008/10/22 10:45:38 danielk1977 Exp $
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+ ifcapable !attach {
22
+ finish_test
23
+ return
24
+ }
25
+
26
+ source $testdir/malloc_common.tcl
27
+
28
+ do_malloc_test attachmalloc-1 -tclprep {
29
+ catch { db close }
30
+ for {set i 2} {$i<=4} {incr i} {
31
+ catch { db$i close }
32
+ forcedelete test$i.db
33
+ forcedelete test$i.db-journal
34
+ }
35
+ } -tclbody {
36
+ if {[catch {sqlite3 db test.db}]} {
37
+ error "out of memory"
38
+ }
39
+ sqlite3_db_config_lookaside db 0 0 0
40
+ sqlite3_extended_result_codes db 1
41
+ } -sqlbody {
42
+ ATTACH 'test2.db' AS two;
43
+ CREATE TABLE two.t1(x);
44
+ ATTACH 'test3.db' AS three;
45
+ CREATE TABLE three.t1(x);
46
+ ATTACH 'test4.db' AS four;
47
+ CREATE TABLE four.t1(x);
48
+ }
49
+
50
+ do_malloc_test attachmalloc-2 -tclprep {
51
+ forcedelete test2.db
52
+ forcedelete test2.db-journal
53
+ sqlite3 db2 test2.db
54
+ db2 eval {
55
+ CREATE TABLE t1(a, b, c);
56
+ CREATE INDEX i1 ON t1(a, b);
57
+ }
58
+ db2 close
59
+ } -sqlbody {
60
+ CREATE TABLE t1(d, e, f);
61
+ ATTACH 'test2.db' AS db1;
62
+ }
63
+
64
+ ifcapable shared_cache {
65
+ set enable_shared_cache [sqlite3_enable_shared_cache 1]
66
+ sqlite3 dbaux test3.db
67
+ dbaux eval {SELECT * FROM sqlite_master}
68
+ do_malloc_test attachmalloc-3 -sqlbody {
69
+ SELECT * FROM sqlite_master;
70
+ ATTACH 'test3.db' AS three;
71
+ } -cleanup {
72
+ db eval { DETACH three }
73
+ }
74
+ dbaux close
75
+ sqlite3_enable_shared_cache $enable_shared_cache
76
+ }
77
+
78
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/auth.test ADDED
@@ -0,0 +1,2678 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2003 April 4
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing the sqlite3_set_authorizer() API
13
+ # and related functionality.
14
+ #
15
+ # $Id: auth.test,v 1.46 2009/07/02 18:40:35 danielk1977 Exp $
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+ # disable this test if the SQLITE_OMIT_AUTHORIZATION macro is
22
+ # defined during compilation.
23
+ if {[catch {db auth {}} msg]} {
24
+ finish_test
25
+ return
26
+ }
27
+
28
+ rename proc proc_real
29
+ proc_real proc {name arguments script} {
30
+ proc_real $name $arguments $script
31
+ if {$name=="auth"} {
32
+ db authorizer ::auth
33
+ }
34
+ }
35
+
36
+ do_test auth-1.1.1 {
37
+ db close
38
+ set ::DB [sqlite3 db test.db]
39
+ proc authx {code arg1 arg2 arg3 arg4 args} {return SQLITE_DENY}
40
+ proc auth {code arg1 arg2 arg3 arg4 args} {
41
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
42
+ return SQLITE_DENY
43
+ }
44
+ return SQLITE_OK
45
+ }
46
+ db authorizer ::authx
47
+ # EVIDENCE-OF: R-03993-24285 Only a single authorizer can be in place on
48
+ # a database connection at a time. Each call to sqlite3_set_authorizer
49
+ # overrides the previous call.
50
+ #
51
+ # The authx authorizer above is overridden by the auth authorizer below
52
+ # authx is never invoked.
53
+ db authorizer ::auth
54
+ catchsql {CREATE TABLE t1(a,b,c)}
55
+ } {1 {not authorized}}
56
+ do_test auth-1.1.2 {
57
+ db errorcode
58
+ } {23}
59
+ do_test auth-1.1.3 {
60
+ db authorizer
61
+ } {::auth}
62
+ do_test auth-1.1.4 {
63
+ # Ticket #896.
64
+ catchsql {
65
+ SELECT x;
66
+ }
67
+ } {1 {no such column: x}}
68
+ do_test auth-1.2 {
69
+ execsql {SELECT name FROM sqlite_master}
70
+ } {}
71
+ # EVIDENCE-OF: R-04452-49349 When the callback returns SQLITE_DENY, the
72
+ # sqlite3_prepare_v2() or equivalent call that triggered the authorizer
73
+ # will fail with an error message explaining that access is denied.
74
+ do_test auth-1.3.1 {
75
+ proc auth {code arg1 arg2 arg3 arg4 args} {
76
+ if {$code=="SQLITE_CREATE_TABLE"} {
77
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
78
+ return SQLITE_DENY
79
+ }
80
+ return SQLITE_OK
81
+ }
82
+ catchsql {CREATE TABLE t1(a,b,c)}
83
+ } {1 {not authorized}}
84
+ do_test auth-1.3.2 {
85
+ db errorcode
86
+ } {23}
87
+ do_test auth-1.3.3 {
88
+ set ::authargs
89
+ } {t1 {} main {}}
90
+ do_test auth-1.4 {
91
+ execsql {SELECT name FROM sqlite_master}
92
+ } {}
93
+
94
+ ifcapable tempdb {
95
+ do_test auth-1.5 {
96
+ proc auth {code arg1 arg2 arg3 arg4 args} {
97
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
98
+ return SQLITE_DENY
99
+ }
100
+ return SQLITE_OK
101
+ }
102
+ catchsql {CREATE TEMP TABLE t1(a,b,c)}
103
+ } {1 {not authorized}}
104
+ do_test auth-1.6 {
105
+ execsql {SELECT name FROM temp.sqlite_master}
106
+ } {}
107
+ do_test auth-1.7.1 {
108
+ proc auth {code arg1 arg2 arg3 arg4 args} {
109
+ if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
110
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
111
+ return SQLITE_DENY
112
+ }
113
+ return SQLITE_OK
114
+ }
115
+ catchsql {CREATE TEMP TABLE t1(a,b,c)}
116
+ } {1 {not authorized}}
117
+ do_test auth-1.7.2 {
118
+ set ::authargs
119
+ } {t1 {} temp {}}
120
+ do_test auth-1.8 {
121
+ execsql {SELECT name FROM sqlite_temp_master}
122
+ } {}
123
+ }
124
+
125
+ do_test auth-1.9 {
126
+ proc auth {code arg1 arg2 arg3 arg4 args} {
127
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
128
+ return SQLITE_IGNORE
129
+ }
130
+ return SQLITE_OK
131
+ }
132
+ catchsql {CREATE TABLE t1(a,b,c)}
133
+ } {0 {}}
134
+ do_test auth-1.10 {
135
+ execsql {SELECT name FROM sqlite_master}
136
+ } {}
137
+ do_test auth-1.11 {
138
+ proc auth {code arg1 arg2 arg3 arg4 args} {
139
+ if {$code=="SQLITE_CREATE_TABLE"} {
140
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
141
+ return SQLITE_IGNORE
142
+ }
143
+ return SQLITE_OK
144
+ }
145
+ catchsql {CREATE TABLE t1(a,b,c)}
146
+ } {0 {}}
147
+ do_test auth-1.12 {
148
+ execsql {SELECT name FROM sqlite_master}
149
+ } {}
150
+
151
+ ifcapable tempdb {
152
+ do_test auth-1.13 {
153
+ proc auth {code arg1 arg2 arg3 arg4 args} {
154
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
155
+ return SQLITE_IGNORE
156
+ }
157
+ return SQLITE_OK
158
+ }
159
+ catchsql {CREATE TEMP TABLE t1(a,b,c)}
160
+ } {0 {}}
161
+ do_test auth-1.14 {
162
+ execsql {SELECT name FROM temp.sqlite_master}
163
+ } {}
164
+ do_test auth-1.15 {
165
+ proc auth {code arg1 arg2 arg3 arg4 args} {
166
+ if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
167
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
168
+ return SQLITE_IGNORE
169
+ }
170
+ return SQLITE_OK
171
+ }
172
+ catchsql {CREATE TEMP TABLE t1(a,b,c)}
173
+ } {0 {}}
174
+ do_test auth-1.16 {
175
+ execsql {SELECT name FROM sqlite_temp_master}
176
+ } {}
177
+
178
+ do_test auth-1.17 {
179
+ proc auth {code arg1 arg2 arg3 arg4 args} {
180
+ if {$code=="SQLITE_CREATE_TABLE"} {
181
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
182
+ return SQLITE_DENY
183
+ }
184
+ return SQLITE_OK
185
+ }
186
+ catchsql {CREATE TEMP TABLE t1(a,b,c)}
187
+ } {0 {}}
188
+ do_test auth-1.18 {
189
+ execsql {SELECT name FROM sqlite_temp_master}
190
+ } {t1}
191
+ }
192
+
193
+ do_test auth-1.19.1 {
194
+ set ::authargs {}
195
+ proc auth {code arg1 arg2 arg3 arg4 args} {
196
+ if {$code=="SQLITE_CREATE_TEMP_TABLE"} {
197
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
198
+ return SQLITE_DENY
199
+ }
200
+ return SQLITE_OK
201
+ }
202
+ catchsql {CREATE TABLE t2(a,b,c)}
203
+ } {0 {}}
204
+ do_test auth-1.19.2 {
205
+ set ::authargs
206
+ } {}
207
+ do_test auth-1.20 {
208
+ execsql {SELECT name FROM sqlite_master}
209
+ } {t2}
210
+
211
+ do_test auth-1.21.1 {
212
+ proc auth {code arg1 arg2 arg3 arg4 args} {
213
+ if {$code=="SQLITE_DROP_TABLE"} {
214
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
215
+ return SQLITE_DENY
216
+ }
217
+ return SQLITE_OK
218
+ }
219
+ catchsql {DROP TABLE t2}
220
+ } {1 {not authorized}}
221
+ do_test auth-1.21.2 {
222
+ set ::authargs
223
+ } {t2 {} main {}}
224
+ do_test auth-1.22 {
225
+ execsql {SELECT name FROM sqlite_master}
226
+ } {t2}
227
+ do_test auth-1.23.1 {
228
+ proc auth {code arg1 arg2 arg3 arg4 args} {
229
+ if {$code=="SQLITE_DROP_TABLE"} {
230
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
231
+ return SQLITE_IGNORE
232
+ }
233
+ return SQLITE_OK
234
+ }
235
+ catchsql {DROP TABLE t2}
236
+ } {0 {}}
237
+ do_test auth-1.23.2 {
238
+ set ::authargs
239
+ } {t2 {} main {}}
240
+ do_test auth-1.24 {
241
+ execsql {SELECT name FROM sqlite_master}
242
+ } {t2}
243
+
244
+ ifcapable tempdb {
245
+ do_test auth-1.25 {
246
+ proc auth {code arg1 arg2 arg3 arg4 args} {
247
+ if {$code=="SQLITE_DROP_TEMP_TABLE"} {
248
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
249
+ return SQLITE_DENY
250
+ }
251
+ return SQLITE_OK
252
+ }
253
+ catchsql {DROP TABLE t1}
254
+ } {1 {not authorized}}
255
+ do_test auth-1.26 {
256
+ execsql {SELECT name FROM sqlite_temp_master}
257
+ } {t1}
258
+ do_test auth-1.27 {
259
+ proc auth {code arg1 arg2 arg3 arg4 args} {
260
+ if {$code=="SQLITE_DROP_TEMP_TABLE"} {
261
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
262
+ return SQLITE_IGNORE
263
+ }
264
+ return SQLITE_OK
265
+ }
266
+ catchsql {DROP TABLE t1}
267
+ } {0 {}}
268
+ do_test auth-1.28 {
269
+ execsql {SELECT name FROM sqlite_temp_master}
270
+ } {t1}
271
+ }
272
+
273
+ do_test auth-1.29 {
274
+ proc auth {code arg1 arg2 arg3 arg4 args} {
275
+ if {$code=="SQLITE_INSERT" && $arg1=="t2"} {
276
+ return SQLITE_DENY
277
+ }
278
+ return SQLITE_OK
279
+ }
280
+ catchsql {INSERT INTO t2 VALUES(1,2,3)}
281
+ } {1 {not authorized}}
282
+ do_test auth-1.30 {
283
+ execsql {SELECT * FROM t2}
284
+ } {}
285
+ do_test auth-1.31 {
286
+ proc auth {code arg1 arg2 arg3 arg4 args} {
287
+ if {$code=="SQLITE_INSERT" && $arg1=="t2"} {
288
+ return SQLITE_IGNORE
289
+ }
290
+ return SQLITE_OK
291
+ }
292
+ catchsql {INSERT INTO t2 VALUES(1,2,3)}
293
+ } {0 {}}
294
+ do_test auth-1.32 {
295
+ execsql {SELECT * FROM t2}
296
+ } {}
297
+ do_test auth-1.33 {
298
+ proc auth {code arg1 arg2 arg3 arg4 args} {
299
+ if {$code=="SQLITE_INSERT" && $arg1=="t1"} {
300
+ return SQLITE_IGNORE
301
+ }
302
+ return SQLITE_OK
303
+ }
304
+ catchsql {INSERT INTO t2 VALUES(1,2,3)}
305
+ } {0 {}}
306
+ do_test auth-1.34 {
307
+ execsql {SELECT * FROM t2}
308
+ } {1 2 3}
309
+
310
+ do_test auth-1.35.1 {
311
+ proc auth {code arg1 arg2 arg3 arg4 args} {
312
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
313
+ return SQLITE_DENY
314
+ }
315
+ return SQLITE_OK
316
+ }
317
+ catchsql {SELECT * FROM t2}
318
+ } {1 {access to t2.b is prohibited}}
319
+ ifcapable attach {
320
+ do_test auth-1.35.2 {
321
+ execsql {ATTACH DATABASE 'test.db' AS two}
322
+ catchsql {SELECT * FROM two.t2}
323
+ } {1 {access to two.t2.b is prohibited}}
324
+ execsql {DETACH DATABASE two}
325
+ }
326
+ # EVIDENCE-OF: R-38392-49970 If the action code is SQLITE_READ and the
327
+ # callback returns SQLITE_IGNORE then the prepared statement statement
328
+ # is constructed to substitute a NULL value in place of the table column
329
+ # that would have been read if SQLITE_OK had been returned.
330
+ do_test auth-1.36 {
331
+ proc auth {code arg1 arg2 arg3 arg4 args} {
332
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
333
+ return SQLITE_IGNORE
334
+ }
335
+ return SQLITE_OK
336
+ }
337
+ catchsql {SELECT * FROM t2}
338
+ } {0 {1 {} 3}}
339
+ do_test auth-1.37 {
340
+ proc auth {code arg1 arg2 arg3 arg4 args} {
341
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
342
+ return SQLITE_IGNORE
343
+ }
344
+ return SQLITE_OK
345
+ }
346
+ catchsql {SELECT * FROM t2 WHERE b=2}
347
+ } {0 {}}
348
+ do_test auth-1.38 {
349
+ proc auth {code arg1 arg2 arg3 arg4 args} {
350
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="a"} {
351
+ return SQLITE_IGNORE
352
+ }
353
+ return SQLITE_OK
354
+ }
355
+ catchsql {SELECT * FROM t2 WHERE b=2}
356
+ } {0 {{} 2 3}}
357
+ do_test auth-1.39 {
358
+ proc auth {code arg1 arg2 arg3 arg4 args} {
359
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
360
+ return SQLITE_IGNORE
361
+ }
362
+ return SQLITE_OK
363
+ }
364
+ catchsql {SELECT * FROM t2 WHERE b IS NULL}
365
+ } {0 {1 {} 3}}
366
+ do_test auth-1.40 {
367
+ proc auth {code arg1 arg2 arg3 arg4 args} {
368
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="b"} {
369
+ return SQLITE_DENY
370
+ }
371
+ return SQLITE_OK
372
+ }
373
+ catchsql {SELECT a,c FROM t2 WHERE b IS NULL}
374
+ } {1 {access to t2.b is prohibited}}
375
+
376
+ do_test auth-1.41 {
377
+ proc auth {code arg1 arg2 arg3 arg4 args} {
378
+ if {$code=="SQLITE_UPDATE" && $arg1=="t2" && $arg2=="b"} {
379
+ return SQLITE_DENY
380
+ }
381
+ return SQLITE_OK
382
+ }
383
+ catchsql {UPDATE t2 SET a=11}
384
+ } {0 {}}
385
+ do_test auth-1.42 {
386
+ execsql {SELECT * FROM t2}
387
+ } {11 2 3}
388
+ do_test auth-1.43 {
389
+ proc auth {code arg1 arg2 arg3 arg4 args} {
390
+ if {$code=="SQLITE_UPDATE" && $arg1=="t2" && $arg2=="b"} {
391
+ return SQLITE_DENY
392
+ }
393
+ return SQLITE_OK
394
+ }
395
+ catchsql {UPDATE t2 SET b=22, c=33}
396
+ } {1 {not authorized}}
397
+ do_test auth-1.44 {
398
+ execsql {SELECT * FROM t2}
399
+ } {11 2 3}
400
+ do_test auth-1.45 {
401
+ proc auth {code arg1 arg2 arg3 arg4 args} {
402
+ if {$code=="SQLITE_UPDATE" && $arg1=="t2" && $arg2=="b"} {
403
+ return SQLITE_IGNORE
404
+ }
405
+ return SQLITE_OK
406
+ }
407
+ catchsql {UPDATE t2 SET b=22, c=33}
408
+ } {0 {}}
409
+ do_test auth-1.46 {
410
+ execsql {SELECT * FROM t2}
411
+ } {11 2 33}
412
+
413
+ do_test auth-1.47 {
414
+ proc auth {code arg1 arg2 arg3 arg4 args} {
415
+ if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
416
+ return SQLITE_DENY
417
+ }
418
+ return SQLITE_OK
419
+ }
420
+ catchsql {DELETE FROM t2 WHERE a=11}
421
+ } {1 {not authorized}}
422
+ do_test auth-1.48 {
423
+ execsql {SELECT * FROM t2}
424
+ } {11 2 33}
425
+ do_test auth-1.49 {
426
+ proc auth {code arg1 arg2 arg3 arg4 args} {
427
+ if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
428
+ return SQLITE_IGNORE
429
+ }
430
+ return SQLITE_OK
431
+ }
432
+ catchsql {DELETE FROM t2 WHERE a=11}
433
+ } {0 {}}
434
+ do_test auth-1.50 {
435
+ execsql {SELECT * FROM t2}
436
+ } {}
437
+ do_test auth-1.50.2 {
438
+ execsql {INSERT INTO t2 VALUES(11, 2, 33)}
439
+ } {}
440
+
441
+ do_test auth-1.51 {
442
+ proc auth {code arg1 arg2 arg3 arg4 args} {
443
+ if {$code=="SQLITE_SELECT"} {
444
+ return SQLITE_DENY
445
+ }
446
+ return SQLITE_OK
447
+ }
448
+ catchsql {SELECT * FROM t2}
449
+ } {1 {not authorized}}
450
+ do_test auth-1.52 {
451
+ proc auth {code arg1 arg2 arg3 arg4 args} {
452
+ if {$code=="SQLITE_SELECT"} {
453
+ return SQLITE_IGNORE
454
+ }
455
+ return SQLITE_OK
456
+ }
457
+ catchsql {SELECT * FROM t2}
458
+ } {0 {}}
459
+ do_test auth-1.53 {
460
+ proc auth {code arg1 arg2 arg3 arg4 args} {
461
+ if {$code=="SQLITE_SELECT"} {
462
+ return SQLITE_OK
463
+ }
464
+ return SQLITE_OK
465
+ }
466
+ catchsql {SELECT * FROM t2}
467
+ } {0 {11 2 33}}
468
+
469
+ # Update for version 3: There used to be a handful of test here that
470
+ # tested the authorisation callback with the COPY command. The following
471
+ # test makes the same database modifications as they used to.
472
+ do_test auth-1.54 {
473
+ execsql {INSERT INTO t2 VALUES(7, 8, 9);}
474
+ } {}
475
+ do_test auth-1.55 {
476
+ execsql {SELECT * FROM t2}
477
+ } {11 2 33 7 8 9}
478
+
479
+ do_test auth-1.63 {
480
+ proc auth {code arg1 arg2 arg3 arg4 args} {
481
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
482
+ return SQLITE_DENY
483
+ }
484
+ return SQLITE_OK
485
+ }
486
+ catchsql {DROP TABLE t2}
487
+ } {1 {not authorized}}
488
+ do_test auth-1.64 {
489
+ execsql {SELECT name FROM sqlite_master}
490
+ } {t2}
491
+ do_test auth-1.65 {
492
+ proc auth {code arg1 arg2 arg3 arg4 args} {
493
+ if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
494
+ return SQLITE_DENY
495
+ }
496
+ return SQLITE_OK
497
+ }
498
+ catchsql {DROP TABLE t2}
499
+ } {1 {not authorized}}
500
+ do_test auth-1.66 {
501
+ execsql {SELECT name FROM sqlite_master}
502
+ } {t2}
503
+
504
+ ifcapable tempdb {
505
+ do_test auth-1.67 {
506
+ proc auth {code arg1 arg2 arg3 arg4 args} {
507
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
508
+ return SQLITE_DENY
509
+ }
510
+ return SQLITE_OK
511
+ }
512
+ catchsql {DROP TABLE t1}
513
+ } {1 {not authorized}}
514
+ do_test auth-1.68 {
515
+ execsql {SELECT name FROM sqlite_temp_master}
516
+ } {t1}
517
+ do_test auth-1.69 {
518
+ proc auth {code arg1 arg2 arg3 arg4 args} {
519
+ if {$code=="SQLITE_DELETE" && $arg1=="t1"} {
520
+ return SQLITE_DENY
521
+ }
522
+ return SQLITE_OK
523
+ }
524
+ catchsql {DROP TABLE t1}
525
+ } {1 {not authorized}}
526
+ do_test auth-1.70 {
527
+ execsql {SELECT name FROM sqlite_temp_master}
528
+ } {t1}
529
+ }
530
+
531
+ do_test auth-1.71 {
532
+ proc auth {code arg1 arg2 arg3 arg4 args} {
533
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
534
+ return SQLITE_IGNORE
535
+ }
536
+ return SQLITE_OK
537
+ }
538
+ catchsql {DROP TABLE t2}
539
+ } {0 {}}
540
+ do_test auth-1.72 {
541
+ execsql {SELECT name FROM sqlite_master}
542
+ } {t2}
543
+ do_test auth-1.73 {
544
+ proc auth {code arg1 arg2 arg3 arg4 args} {
545
+ if {$code=="SQLITE_DELETE" && $arg1=="t2"} {
546
+ return SQLITE_IGNORE
547
+ }
548
+ return SQLITE_OK
549
+ }
550
+ catchsql {DROP TABLE t2}
551
+ } {0 {}}
552
+ do_test auth-1.74 {
553
+ execsql {SELECT name FROM sqlite_master}
554
+ } {t2}
555
+
556
+ ifcapable tempdb {
557
+ do_test auth-1.75 {
558
+ proc auth {code arg1 arg2 arg3 arg4 args} {
559
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
560
+ return SQLITE_IGNORE
561
+ }
562
+ return SQLITE_OK
563
+ }
564
+ catchsql {DROP TABLE t1}
565
+ } {0 {}}
566
+ do_test auth-1.76 {
567
+ execsql {SELECT name FROM sqlite_temp_master}
568
+ } {t1}
569
+ do_test auth-1.77 {
570
+ proc auth {code arg1 arg2 arg3 arg4 args} {
571
+ if {$code=="SQLITE_DELETE" && $arg1=="t1"} {
572
+ return SQLITE_IGNORE
573
+ }
574
+ return SQLITE_OK
575
+ }
576
+ catchsql {DROP TABLE t1}
577
+ } {0 {}}
578
+ do_test auth-1.78 {
579
+ execsql {SELECT name FROM temp.sqlite_master}
580
+ } {t1}
581
+ }
582
+
583
+ # Test cases auth-1.79 to auth-1.124 test creating and dropping views.
584
+ # Omit these if the library was compiled with views omitted.
585
+ ifcapable view {
586
+ do_test auth-1.79 {
587
+ proc auth {code arg1 arg2 arg3 arg4 args} {
588
+ if {$code=="SQLITE_CREATE_VIEW"} {
589
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
590
+ return SQLITE_DENY
591
+ }
592
+ return SQLITE_OK
593
+ }
594
+ catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
595
+ } {1 {not authorized}}
596
+ do_test auth-1.80 {
597
+ set ::authargs
598
+ } {v1 {} main {}}
599
+ do_test auth-1.81 {
600
+ execsql {SELECT name FROM sqlite_master}
601
+ } {t2}
602
+ do_test auth-1.82 {
603
+ proc auth {code arg1 arg2 arg3 arg4 args} {
604
+ if {$code=="SQLITE_CREATE_VIEW"} {
605
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
606
+ return SQLITE_IGNORE
607
+ }
608
+ return SQLITE_OK
609
+ }
610
+ catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
611
+ } {0 {}}
612
+ do_test auth-1.83 {
613
+ set ::authargs
614
+ } {v1 {} main {}}
615
+ do_test auth-1.84 {
616
+ execsql {SELECT name FROM sqlite_master}
617
+ } {t2}
618
+
619
+ ifcapable tempdb {
620
+ do_test auth-1.85 {
621
+ proc auth {code arg1 arg2 arg3 arg4 args} {
622
+ if {$code=="SQLITE_CREATE_TEMP_VIEW"} {
623
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
624
+ return SQLITE_DENY
625
+ }
626
+ return SQLITE_OK
627
+ }
628
+ catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
629
+ } {1 {not authorized}}
630
+ do_test auth-1.86 {
631
+ set ::authargs
632
+ } {v1 {} temp {}}
633
+ do_test auth-1.87 {
634
+ execsql {SELECT name FROM sqlite_temp_master}
635
+ } {t1}
636
+ do_test auth-1.88 {
637
+ proc auth {code arg1 arg2 arg3 arg4 args} {
638
+ if {$code=="SQLITE_CREATE_TEMP_VIEW"} {
639
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
640
+ return SQLITE_IGNORE
641
+ }
642
+ return SQLITE_OK
643
+ }
644
+ catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
645
+ } {0 {}}
646
+ do_test auth-1.89 {
647
+ set ::authargs
648
+ } {v1 {} temp {}}
649
+ do_test auth-1.90 {
650
+ execsql {SELECT name FROM temp.sqlite_master}
651
+ } {t1}
652
+ }
653
+
654
+ do_test auth-1.91 {
655
+ proc auth {code arg1 arg2 arg3 arg4 args} {
656
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
657
+ return SQLITE_DENY
658
+ }
659
+ return SQLITE_OK
660
+ }
661
+ catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
662
+ } {1 {not authorized}}
663
+ do_test auth-1.92 {
664
+ execsql {SELECT name FROM sqlite_master}
665
+ } {t2}
666
+ do_test auth-1.93 {
667
+ proc auth {code arg1 arg2 arg3 arg4 args} {
668
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
669
+ return SQLITE_IGNORE
670
+ }
671
+ return SQLITE_OK
672
+ }
673
+ catchsql {CREATE VIEW v1 AS SELECT a+1,b+1 FROM t2}
674
+ } {0 {}}
675
+ do_test auth-1.94 {
676
+ execsql {SELECT name FROM sqlite_master}
677
+ } {t2}
678
+
679
+ ifcapable tempdb {
680
+ do_test auth-1.95 {
681
+ proc auth {code arg1 arg2 arg3 arg4 args} {
682
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
683
+ return SQLITE_DENY
684
+ }
685
+ return SQLITE_OK
686
+ }
687
+ catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
688
+ } {1 {not authorized}}
689
+ do_test auth-1.96 {
690
+ execsql {SELECT name FROM sqlite_temp_master}
691
+ } {t1}
692
+ do_test auth-1.97 {
693
+ proc auth {code arg1 arg2 arg3 arg4 args} {
694
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
695
+ return SQLITE_IGNORE
696
+ }
697
+ return SQLITE_OK
698
+ }
699
+ catchsql {CREATE TEMPORARY VIEW v1 AS SELECT a+1,b+1 FROM t2}
700
+ } {0 {}}
701
+ do_test auth-1.98 {
702
+ execsql {SELECT name FROM sqlite_temp_master}
703
+ } {t1}
704
+ }
705
+
706
+ do_test auth-1.99 {
707
+ proc auth {code arg1 arg2 arg3 arg4 args} {
708
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
709
+ return SQLITE_DENY
710
+ }
711
+ return SQLITE_OK
712
+ }
713
+ catchsql {
714
+ CREATE VIEW v2 AS SELECT a+1,b+1 FROM t2;
715
+ DROP VIEW v2
716
+ }
717
+ } {1 {not authorized}}
718
+ do_test auth-1.100 {
719
+ execsql {SELECT name FROM sqlite_master}
720
+ } {t2 v2}
721
+ do_test auth-1.101 {
722
+ proc auth {code arg1 arg2 arg3 arg4 args} {
723
+ if {$code=="SQLITE_DROP_VIEW"} {
724
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
725
+ return SQLITE_DENY
726
+ }
727
+ return SQLITE_OK
728
+ }
729
+ catchsql {DROP VIEW v2}
730
+ } {1 {not authorized}}
731
+ do_test auth-1.102 {
732
+ set ::authargs
733
+ } {v2 {} main {}}
734
+ do_test auth-1.103 {
735
+ execsql {SELECT name FROM sqlite_master}
736
+ } {t2 v2}
737
+ do_test auth-1.104 {
738
+ proc auth {code arg1 arg2 arg3 arg4 args} {
739
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
740
+ return SQLITE_IGNORE
741
+ }
742
+ return SQLITE_OK
743
+ }
744
+ catchsql {DROP VIEW v2}
745
+ } {0 {}}
746
+ do_test auth-1.105 {
747
+ execsql {SELECT name FROM sqlite_master}
748
+ } {t2 v2}
749
+ do_test auth-1.106 {
750
+ proc auth {code arg1 arg2 arg3 arg4 args} {
751
+ if {$code=="SQLITE_DROP_VIEW"} {
752
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
753
+ return SQLITE_IGNORE
754
+ }
755
+ return SQLITE_OK
756
+ }
757
+ catchsql {DROP VIEW v2}
758
+ } {0 {}}
759
+ do_test auth-1.107 {
760
+ set ::authargs
761
+ } {v2 {} main {}}
762
+ do_test auth-1.108 {
763
+ execsql {SELECT name FROM sqlite_master}
764
+ } {t2 v2}
765
+ do_test auth-1.109 {
766
+ proc auth {code arg1 arg2 arg3 arg4 args} {
767
+ if {$code=="SQLITE_DROP_VIEW"} {
768
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
769
+ return SQLITE_OK
770
+ }
771
+ return SQLITE_OK
772
+ }
773
+ catchsql {DROP VIEW v2}
774
+ } {0 {}}
775
+ do_test auth-1.110 {
776
+ set ::authargs
777
+ } {v2 {} main {}}
778
+ do_test auth-1.111 {
779
+ execsql {SELECT name FROM sqlite_master}
780
+ } {t2}
781
+
782
+
783
+ ifcapable tempdb {
784
+ do_test auth-1.112 {
785
+ proc auth {code arg1 arg2 arg3 arg4 args} {
786
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
787
+ return SQLITE_DENY
788
+ }
789
+ return SQLITE_OK
790
+ }
791
+ catchsql {
792
+ CREATE TEMP VIEW v1 AS SELECT a+1,b+1 FROM t1;
793
+ DROP VIEW v1
794
+ }
795
+ } {1 {not authorized}}
796
+ do_test auth-1.113 {
797
+ execsql {SELECT name FROM temp.sqlite_master}
798
+ } {t1 v1}
799
+ do_test auth-1.114 {
800
+ proc auth {code arg1 arg2 arg3 arg4 args} {
801
+ if {$code=="SQLITE_DROP_TEMP_VIEW"} {
802
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
803
+ return SQLITE_DENY
804
+ }
805
+ return SQLITE_OK
806
+ }
807
+ catchsql {DROP VIEW v1}
808
+ } {1 {not authorized}}
809
+ do_test auth-1.115 {
810
+ set ::authargs
811
+ } {v1 {} temp {}}
812
+ do_test auth-1.116 {
813
+ execsql {SELECT name FROM sqlite_temp_master}
814
+ } {t1 v1}
815
+ do_test auth-1.117 {
816
+ proc auth {code arg1 arg2 arg3 arg4 args} {
817
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
818
+ return SQLITE_IGNORE
819
+ }
820
+ return SQLITE_OK
821
+ }
822
+ catchsql {DROP VIEW v1}
823
+ } {0 {}}
824
+ do_test auth-1.118 {
825
+ execsql {SELECT name FROM sqlite_temp_master}
826
+ } {t1 v1}
827
+ do_test auth-1.119 {
828
+ proc auth {code arg1 arg2 arg3 arg4 args} {
829
+ if {$code=="SQLITE_DROP_TEMP_VIEW"} {
830
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
831
+ return SQLITE_IGNORE
832
+ }
833
+ return SQLITE_OK
834
+ }
835
+ catchsql {DROP VIEW v1}
836
+ } {0 {}}
837
+ do_test auth-1.120 {
838
+ set ::authargs
839
+ } {v1 {} temp {}}
840
+ do_test auth-1.121 {
841
+ execsql {SELECT name FROM temp.sqlite_master}
842
+ } {t1 v1}
843
+ do_test auth-1.122 {
844
+ proc auth {code arg1 arg2 arg3 arg4 args} {
845
+ if {$code=="SQLITE_DROP_TEMP_VIEW"} {
846
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
847
+ return SQLITE_OK
848
+ }
849
+ return SQLITE_OK
850
+ }
851
+ catchsql {DROP VIEW v1}
852
+ } {0 {}}
853
+ do_test auth-1.123 {
854
+ set ::authargs
855
+ } {v1 {} temp {}}
856
+ do_test auth-1.124 {
857
+ execsql {SELECT name FROM sqlite_temp_master}
858
+ } {t1}
859
+ }
860
+ } ;# ifcapable view
861
+
862
+ # Test cases auth-1.125 to auth-1.176 test creating and dropping triggers.
863
+ # Omit these if the library was compiled with triggers omitted.
864
+ #
865
+ ifcapable trigger&&tempdb {
866
+ do_test auth-1.125 {
867
+ proc auth {code arg1 arg2 arg3 arg4 args} {
868
+ if {$code=="SQLITE_CREATE_TRIGGER"} {
869
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
870
+ return SQLITE_DENY
871
+ }
872
+ return SQLITE_OK
873
+ }
874
+ catchsql {
875
+ CREATE TRIGGER r2 DELETE on t2 BEGIN
876
+ SELECT NULL;
877
+ END;
878
+ }
879
+ } {1 {not authorized}}
880
+ do_test auth-1.126 {
881
+ set ::authargs
882
+ } {r2 t2 main {}}
883
+ do_test auth-1.127 {
884
+ execsql {SELECT name FROM sqlite_master}
885
+ } {t2}
886
+ do_test auth-1.128 {
887
+ proc auth {code arg1 arg2 arg3 arg4 args} {
888
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
889
+ return SQLITE_DENY
890
+ }
891
+ return SQLITE_OK
892
+ }
893
+ catchsql {
894
+ CREATE TRIGGER r2 DELETE on t2 BEGIN
895
+ SELECT NULL;
896
+ END;
897
+ }
898
+ } {1 {not authorized}}
899
+ do_test auth-1.129 {
900
+ execsql {SELECT name FROM sqlite_master}
901
+ } {t2}
902
+ do_test auth-1.130 {
903
+ proc auth {code arg1 arg2 arg3 arg4 args} {
904
+ if {$code=="SQLITE_CREATE_TRIGGER"} {
905
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
906
+ return SQLITE_IGNORE
907
+ }
908
+ return SQLITE_OK
909
+ }
910
+ catchsql {
911
+ CREATE TRIGGER r2 DELETE on t2 BEGIN
912
+ SELECT NULL;
913
+ END;
914
+ }
915
+ } {0 {}}
916
+ do_test auth-1.131 {
917
+ set ::authargs
918
+ } {r2 t2 main {}}
919
+ do_test auth-1.132 {
920
+ execsql {SELECT name FROM sqlite_master}
921
+ } {t2}
922
+ do_test auth-1.133 {
923
+ proc auth {code arg1 arg2 arg3 arg4 args} {
924
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
925
+ return SQLITE_IGNORE
926
+ }
927
+ return SQLITE_OK
928
+ }
929
+ catchsql {
930
+ CREATE TRIGGER r2 DELETE on t2 BEGIN
931
+ SELECT NULL;
932
+ END;
933
+ }
934
+ } {0 {}}
935
+ do_test auth-1.134 {
936
+ execsql {SELECT name FROM sqlite_master}
937
+ } {t2}
938
+ do_test auth-1.135 {
939
+ proc auth {code arg1 arg2 arg3 arg4 args} {
940
+ if {$code=="SQLITE_CREATE_TRIGGER"} {
941
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
942
+ return SQLITE_OK
943
+ }
944
+ return SQLITE_OK
945
+ }
946
+ catchsql {
947
+ CREATE TABLE tx(id);
948
+ CREATE TRIGGER r2 AFTER INSERT ON t2 BEGIN
949
+ INSERT INTO tx VALUES(NEW.rowid);
950
+ END;
951
+ }
952
+ } {0 {}}
953
+ do_test auth-1.136.1 {
954
+ set ::authargs
955
+ } {r2 t2 main {}}
956
+ do_test auth-1.136.2 {
957
+ execsql {
958
+ SELECT name FROM sqlite_master WHERE type='trigger'
959
+ }
960
+ } {r2}
961
+ do_test auth-1.136.3 {
962
+ proc auth {code arg1 arg2 arg3 arg4 args} {
963
+ lappend ::authargs $code $arg1 $arg2 $arg3 $arg4
964
+ return SQLITE_OK
965
+ }
966
+ set ::authargs {}
967
+ execsql {
968
+ INSERT INTO t2 VALUES(1,2,3);
969
+ }
970
+ set ::authargs
971
+ } {SQLITE_INSERT t2 {} main {} SQLITE_INSERT tx {} main r2 SQLITE_READ t2 ROWID main r2}
972
+ do_test auth-1.136.4 {
973
+ execsql {
974
+ SELECT * FROM tx;
975
+ }
976
+ } {3}
977
+ do_test auth-1.137 {
978
+ execsql {SELECT name FROM sqlite_master}
979
+ } {t2 tx r2}
980
+ do_test auth-1.138 {
981
+ proc auth {code arg1 arg2 arg3 arg4 args} {
982
+ if {$code=="SQLITE_CREATE_TEMP_TRIGGER"} {
983
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
984
+ return SQLITE_DENY
985
+ }
986
+ return SQLITE_OK
987
+ }
988
+ catchsql {
989
+ CREATE TRIGGER r1 DELETE on t1 BEGIN
990
+ SELECT NULL;
991
+ END;
992
+ }
993
+ } {1 {not authorized}}
994
+ do_test auth-1.139 {
995
+ set ::authargs
996
+ } {r1 t1 temp {}}
997
+ do_test auth-1.140 {
998
+ execsql {SELECT name FROM temp.sqlite_master}
999
+ } {t1}
1000
+ do_test auth-1.141 {
1001
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1002
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
1003
+ return SQLITE_DENY
1004
+ }
1005
+ return SQLITE_OK
1006
+ }
1007
+ catchsql {
1008
+ CREATE TRIGGER r1 DELETE on t1 BEGIN
1009
+ SELECT NULL;
1010
+ END;
1011
+ }
1012
+ } {1 {not authorized}}
1013
+ do_test auth-1.142 {
1014
+ execsql {SELECT name FROM sqlite_temp_master}
1015
+ } {t1}
1016
+ do_test auth-1.143 {
1017
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1018
+ if {$code=="SQLITE_CREATE_TEMP_TRIGGER"} {
1019
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1020
+ return SQLITE_IGNORE
1021
+ }
1022
+ return SQLITE_OK
1023
+ }
1024
+ catchsql {
1025
+ CREATE TRIGGER r1 DELETE on t1 BEGIN
1026
+ SELECT NULL;
1027
+ END;
1028
+ }
1029
+ } {0 {}}
1030
+ do_test auth-1.144 {
1031
+ set ::authargs
1032
+ } {r1 t1 temp {}}
1033
+ do_test auth-1.145 {
1034
+ execsql {SELECT name FROM temp.sqlite_master}
1035
+ } {t1}
1036
+ do_test auth-1.146 {
1037
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1038
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
1039
+ return SQLITE_IGNORE
1040
+ }
1041
+ return SQLITE_OK
1042
+ }
1043
+ catchsql {
1044
+ CREATE TRIGGER r1 DELETE on t1 BEGIN
1045
+ SELECT NULL;
1046
+ END;
1047
+ }
1048
+ } {0 {}}
1049
+ do_test auth-1.147 {
1050
+ execsql {SELECT name FROM sqlite_temp_master}
1051
+ } {t1}
1052
+ do_test auth-1.148 {
1053
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1054
+ if {$code=="SQLITE_CREATE_TEMP_TRIGGER"} {
1055
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1056
+ return SQLITE_OK
1057
+ }
1058
+ return SQLITE_OK
1059
+ }
1060
+ catchsql {
1061
+ CREATE TRIGGER r1 DELETE on t1 BEGIN
1062
+ SELECT NULL;
1063
+ END;
1064
+ }
1065
+ } {0 {}}
1066
+ do_test auth-1.149 {
1067
+ set ::authargs
1068
+ } {r1 t1 temp {}}
1069
+ do_test auth-1.150 {
1070
+ execsql {SELECT name FROM temp.sqlite_master}
1071
+ } {t1 r1}
1072
+
1073
+ do_test auth-1.151 {
1074
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1075
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
1076
+ return SQLITE_DENY
1077
+ }
1078
+ return SQLITE_OK
1079
+ }
1080
+ catchsql {DROP TRIGGER r2}
1081
+ } {1 {not authorized}}
1082
+ do_test auth-1.152 {
1083
+ execsql {SELECT name FROM sqlite_master}
1084
+ } {t2 tx r2}
1085
+ do_test auth-1.153 {
1086
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1087
+ if {$code=="SQLITE_DROP_TRIGGER"} {
1088
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1089
+ return SQLITE_DENY
1090
+ }
1091
+ return SQLITE_OK
1092
+ }
1093
+ catchsql {DROP TRIGGER r2}
1094
+ } {1 {not authorized}}
1095
+ do_test auth-1.154 {
1096
+ set ::authargs
1097
+ } {r2 t2 main {}}
1098
+ do_test auth-1.155 {
1099
+ execsql {SELECT name FROM sqlite_master}
1100
+ } {t2 tx r2}
1101
+ do_test auth-1.156 {
1102
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1103
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
1104
+ return SQLITE_IGNORE
1105
+ }
1106
+ return SQLITE_OK
1107
+ }
1108
+ catchsql {DROP TRIGGER r2}
1109
+ } {0 {}}
1110
+ do_test auth-1.157 {
1111
+ execsql {SELECT name FROM sqlite_master}
1112
+ } {t2 tx r2}
1113
+ do_test auth-1.158 {
1114
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1115
+ if {$code=="SQLITE_DROP_TRIGGER"} {
1116
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1117
+ return SQLITE_IGNORE
1118
+ }
1119
+ return SQLITE_OK
1120
+ }
1121
+ catchsql {DROP TRIGGER r2}
1122
+ } {0 {}}
1123
+ do_test auth-1.159 {
1124
+ set ::authargs
1125
+ } {r2 t2 main {}}
1126
+ do_test auth-1.160 {
1127
+ execsql {SELECT name FROM sqlite_master}
1128
+ } {t2 tx r2}
1129
+ do_test auth-1.161 {
1130
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1131
+ if {$code=="SQLITE_DROP_TRIGGER"} {
1132
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1133
+ return SQLITE_OK
1134
+ }
1135
+ return SQLITE_OK
1136
+ }
1137
+ catchsql {DROP TRIGGER r2}
1138
+ } {0 {}}
1139
+ do_test auth-1.162 {
1140
+ set ::authargs
1141
+ } {r2 t2 main {}}
1142
+ do_test auth-1.163 {
1143
+ execsql {
1144
+ DROP TABLE tx;
1145
+ DELETE FROM t2 WHERE a=1 AND b=2 AND c=3;
1146
+ SELECT name FROM sqlite_master;
1147
+ }
1148
+ } {t2}
1149
+
1150
+ do_test auth-1.164 {
1151
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1152
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
1153
+ return SQLITE_DENY
1154
+ }
1155
+ return SQLITE_OK
1156
+ }
1157
+ catchsql {DROP TRIGGER r1}
1158
+ } {1 {not authorized}}
1159
+ do_test auth-1.165 {
1160
+ execsql {SELECT name FROM temp.sqlite_master}
1161
+ } {t1 r1}
1162
+ do_test auth-1.166 {
1163
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1164
+ if {$code=="SQLITE_DROP_TEMP_TRIGGER"} {
1165
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1166
+ return SQLITE_DENY
1167
+ }
1168
+ return SQLITE_OK
1169
+ }
1170
+ catchsql {DROP TRIGGER r1}
1171
+ } {1 {not authorized}}
1172
+ do_test auth-1.167 {
1173
+ set ::authargs
1174
+ } {r1 t1 temp {}}
1175
+ do_test auth-1.168 {
1176
+ execsql {SELECT name FROM sqlite_temp_master}
1177
+ } {t1 r1}
1178
+ do_test auth-1.169 {
1179
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1180
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
1181
+ return SQLITE_IGNORE
1182
+ }
1183
+ return SQLITE_OK
1184
+ }
1185
+ catchsql {DROP TRIGGER r1}
1186
+ } {0 {}}
1187
+ do_test auth-1.170 {
1188
+ execsql {SELECT name FROM temp.sqlite_master}
1189
+ } {t1 r1}
1190
+ do_test auth-1.171 {
1191
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1192
+ if {$code=="SQLITE_DROP_TEMP_TRIGGER"} {
1193
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1194
+ return SQLITE_IGNORE
1195
+ }
1196
+ return SQLITE_OK
1197
+ }
1198
+ catchsql {DROP TRIGGER r1}
1199
+ } {0 {}}
1200
+ do_test auth-1.172 {
1201
+ set ::authargs
1202
+ } {r1 t1 temp {}}
1203
+ do_test auth-1.173 {
1204
+ execsql {SELECT name FROM sqlite_temp_master}
1205
+ } {t1 r1}
1206
+ do_test auth-1.174 {
1207
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1208
+ if {$code=="SQLITE_DROP_TEMP_TRIGGER"} {
1209
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1210
+ return SQLITE_OK
1211
+ }
1212
+ return SQLITE_OK
1213
+ }
1214
+ catchsql {DROP TRIGGER r1}
1215
+ } {0 {}}
1216
+ do_test auth-1.175 {
1217
+ set ::authargs
1218
+ } {r1 t1 temp {}}
1219
+ do_test auth-1.176 {
1220
+ execsql {SELECT name FROM temp.sqlite_master}
1221
+ } {t1}
1222
+ } ;# ifcapable trigger
1223
+
1224
+ do_test auth-1.177 {
1225
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1226
+ if {$code=="SQLITE_CREATE_INDEX"} {
1227
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1228
+ return SQLITE_DENY
1229
+ }
1230
+ return SQLITE_OK
1231
+ }
1232
+ catchsql {CREATE INDEX i2 ON t2(a)}
1233
+ } {1 {not authorized}}
1234
+ do_test auth-1.178 {
1235
+ set ::authargs
1236
+ } {i2 t2 main {}}
1237
+ do_test auth-1.179 {
1238
+ execsql {SELECT name FROM sqlite_master}
1239
+ } {t2}
1240
+ do_test auth-1.180 {
1241
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1242
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
1243
+ return SQLITE_DENY
1244
+ }
1245
+ return SQLITE_OK
1246
+ }
1247
+ catchsql {CREATE INDEX i2 ON t2(a)}
1248
+ } {1 {not authorized}}
1249
+ do_test auth-1.181 {
1250
+ execsql {SELECT name FROM sqlite_master}
1251
+ } {t2}
1252
+ do_test auth-1.182 {
1253
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1254
+ if {$code=="SQLITE_CREATE_INDEX"} {
1255
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1256
+ return SQLITE_IGNORE
1257
+ }
1258
+ return SQLITE_OK
1259
+ }
1260
+ catchsql {CREATE INDEX i2 ON t2(b)}
1261
+ } {0 {}}
1262
+ do_test auth-1.183 {
1263
+ set ::authargs
1264
+ } {i2 t2 main {}}
1265
+ do_test auth-1.184 {
1266
+ execsql {SELECT name FROM sqlite_master}
1267
+ } {t2}
1268
+ do_test auth-1.185 {
1269
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1270
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_master"} {
1271
+ return SQLITE_IGNORE
1272
+ }
1273
+ return SQLITE_OK
1274
+ }
1275
+ catchsql {CREATE INDEX i2 ON t2(b)}
1276
+ } {0 {}}
1277
+ do_test auth-1.186 {
1278
+ execsql {SELECT name FROM sqlite_master}
1279
+ } {t2}
1280
+ do_test auth-1.187 {
1281
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1282
+ if {$code=="SQLITE_CREATE_INDEX"} {
1283
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1284
+ return SQLITE_OK
1285
+ }
1286
+ return SQLITE_OK
1287
+ }
1288
+ catchsql {CREATE INDEX i2 ON t2(a)}
1289
+ } {0 {}}
1290
+ do_test auth-1.188 {
1291
+ set ::authargs
1292
+ } {i2 t2 main {}}
1293
+ do_test auth-1.189 {
1294
+ execsql {SELECT name FROM sqlite_master}
1295
+ } {t2 i2}
1296
+
1297
+ ifcapable tempdb {
1298
+ do_test auth-1.190 {
1299
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1300
+ if {$code=="SQLITE_CREATE_TEMP_INDEX"} {
1301
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1302
+ return SQLITE_DENY
1303
+ }
1304
+ return SQLITE_OK
1305
+ }
1306
+ catchsql {CREATE INDEX i1 ON t1(a)}
1307
+ } {1 {not authorized}}
1308
+ do_test auth-1.191 {
1309
+ set ::authargs
1310
+ } {i1 t1 temp {}}
1311
+ do_test auth-1.192 {
1312
+ execsql {SELECT name FROM sqlite_temp_master}
1313
+ } {t1}
1314
+ do_test auth-1.193 {
1315
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1316
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
1317
+ return SQLITE_DENY
1318
+ }
1319
+ return SQLITE_OK
1320
+ }
1321
+ catchsql {CREATE INDEX i1 ON t1(b)}
1322
+ } {1 {not authorized}}
1323
+ do_test auth-1.194 {
1324
+ execsql {SELECT name FROM temp.sqlite_master}
1325
+ } {t1}
1326
+ do_test auth-1.195 {
1327
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1328
+ if {$code=="SQLITE_CREATE_TEMP_INDEX"} {
1329
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1330
+ return SQLITE_IGNORE
1331
+ }
1332
+ return SQLITE_OK
1333
+ }
1334
+ catchsql {CREATE INDEX i1 ON t1(b)}
1335
+ } {0 {}}
1336
+ do_test auth-1.196 {
1337
+ set ::authargs
1338
+ } {i1 t1 temp {}}
1339
+ do_test auth-1.197 {
1340
+ execsql {SELECT name FROM sqlite_temp_master}
1341
+ } {t1}
1342
+ do_test auth-1.198 {
1343
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1344
+ if {$code=="SQLITE_INSERT" && $arg1=="sqlite_temp_master"} {
1345
+ return SQLITE_IGNORE
1346
+ }
1347
+ return SQLITE_OK
1348
+ }
1349
+ catchsql {CREATE INDEX i1 ON t1(c)}
1350
+ } {0 {}}
1351
+ do_test auth-1.199 {
1352
+ execsql {SELECT name FROM sqlite_temp_master}
1353
+ } {t1}
1354
+ do_test auth-1.200 {
1355
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1356
+ if {$code=="SQLITE_CREATE_TEMP_INDEX"} {
1357
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1358
+ return SQLITE_OK
1359
+ }
1360
+ return SQLITE_OK
1361
+ }
1362
+ catchsql {CREATE INDEX i1 ON t1(a)}
1363
+ } {0 {}}
1364
+ do_test auth-1.201 {
1365
+ set ::authargs
1366
+ } {i1 t1 temp {}}
1367
+ do_test auth-1.202 {
1368
+ execsql {SELECT name FROM temp.sqlite_master}
1369
+ } {t1 i1}
1370
+ }
1371
+
1372
+ do_test auth-1.203 {
1373
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1374
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
1375
+ return SQLITE_DENY
1376
+ }
1377
+ return SQLITE_OK
1378
+ }
1379
+ catchsql {DROP INDEX i2}
1380
+ } {1 {not authorized}}
1381
+ do_test auth-1.204 {
1382
+ execsql {SELECT name FROM sqlite_master}
1383
+ } {t2 i2}
1384
+ do_test auth-1.205 {
1385
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1386
+ if {$code=="SQLITE_DROP_INDEX"} {
1387
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1388
+ return SQLITE_DENY
1389
+ }
1390
+ return SQLITE_OK
1391
+ }
1392
+ catchsql {DROP INDEX i2}
1393
+ } {1 {not authorized}}
1394
+ do_test auth-1.205a {
1395
+ set ::authargs
1396
+ } {i2 t2 main {}}
1397
+ db eval {
1398
+ ATTACH ':memory:' as di205;
1399
+ CREATE TABLE di205.t1(x);
1400
+ CREATE INDEX di205.t1x ON t1(x);
1401
+ }
1402
+ do_catchsql_test auth-1.205b {
1403
+ DROP INDEX di205.t1x;
1404
+ } {1 {not authorized}}
1405
+ db eval {
1406
+ DETACH di205;
1407
+ }
1408
+ do_test auth-1.206 {
1409
+ set ::authargs
1410
+ } {t1x t1 di205 {}}
1411
+ do_test auth-1.207 {
1412
+ execsql {SELECT name FROM sqlite_master}
1413
+ } {t2 i2}
1414
+ do_test auth-1.208 {
1415
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1416
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_master"} {
1417
+ return SQLITE_IGNORE
1418
+ }
1419
+ return SQLITE_OK
1420
+ }
1421
+ catchsql {DROP INDEX i2}
1422
+ } {0 {}}
1423
+ do_test auth-1.209 {
1424
+ execsql {SELECT name FROM sqlite_master}
1425
+ } {t2 i2}
1426
+ do_test auth-1.210 {
1427
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1428
+ if {$code=="SQLITE_DROP_INDEX"} {
1429
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1430
+ return SQLITE_IGNORE
1431
+ }
1432
+ return SQLITE_OK
1433
+ }
1434
+ catchsql {DROP INDEX i2}
1435
+ } {0 {}}
1436
+ do_test auth-1.211 {
1437
+ set ::authargs
1438
+ } {i2 t2 main {}}
1439
+ do_test auth-1.212 {
1440
+ execsql {SELECT name FROM sqlite_master}
1441
+ } {t2 i2}
1442
+ do_test auth-1.213 {
1443
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1444
+ if {$code=="SQLITE_DROP_INDEX"} {
1445
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1446
+ return SQLITE_OK
1447
+ }
1448
+ return SQLITE_OK
1449
+ }
1450
+ catchsql {DROP INDEX i2}
1451
+ } {0 {}}
1452
+ do_test auth-1.214 {
1453
+ set ::authargs
1454
+ } {i2 t2 main {}}
1455
+ do_test auth-1.215 {
1456
+ execsql {SELECT name FROM sqlite_master}
1457
+ } {t2}
1458
+
1459
+ ifcapable tempdb {
1460
+ do_test auth-1.216 {
1461
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1462
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
1463
+ return SQLITE_DENY
1464
+ }
1465
+ return SQLITE_OK
1466
+ }
1467
+ catchsql {DROP INDEX i1}
1468
+ } {1 {not authorized}}
1469
+ do_test auth-1.217 {
1470
+ execsql {SELECT name FROM sqlite_temp_master}
1471
+ } {t1 i1}
1472
+ do_test auth-1.218 {
1473
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1474
+ if {$code=="SQLITE_DROP_TEMP_INDEX"} {
1475
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1476
+ return SQLITE_DENY
1477
+ }
1478
+ return SQLITE_OK
1479
+ }
1480
+ catchsql {DROP INDEX i1}
1481
+ } {1 {not authorized}}
1482
+ do_test auth-1.219 {
1483
+ set ::authargs
1484
+ } {i1 t1 temp {}}
1485
+ do_test auth-1.220 {
1486
+ execsql {SELECT name FROM sqlite_temp_master}
1487
+ } {t1 i1}
1488
+ do_test auth-1.221 {
1489
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1490
+ if {$code=="SQLITE_DELETE" && $arg1=="sqlite_temp_master"} {
1491
+ return SQLITE_IGNORE
1492
+ }
1493
+ return SQLITE_OK
1494
+ }
1495
+ catchsql {DROP INDEX i1}
1496
+ } {0 {}}
1497
+ do_test auth-1.222 {
1498
+ execsql {SELECT name FROM temp.sqlite_master}
1499
+ } {t1 i1}
1500
+ do_test auth-1.223 {
1501
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1502
+ if {$code=="SQLITE_DROP_TEMP_INDEX"} {
1503
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1504
+ return SQLITE_IGNORE
1505
+ }
1506
+ return SQLITE_OK
1507
+ }
1508
+ catchsql {DROP INDEX i1}
1509
+ } {0 {}}
1510
+ do_test auth-1.224 {
1511
+ set ::authargs
1512
+ } {i1 t1 temp {}}
1513
+ do_test auth-1.225 {
1514
+ execsql {SELECT name FROM temp.sqlite_master}
1515
+ } {t1 i1}
1516
+ do_test auth-1.226 {
1517
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1518
+ if {$code=="SQLITE_DROP_TEMP_INDEX"} {
1519
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1520
+ return SQLITE_OK
1521
+ }
1522
+ return SQLITE_OK
1523
+ }
1524
+ catchsql {DROP INDEX i1}
1525
+ } {0 {}}
1526
+ do_test auth-1.227 {
1527
+ set ::authargs
1528
+ } {i1 t1 temp {}}
1529
+ do_test auth-1.228 {
1530
+ execsql {SELECT name FROM temp.sqlite_master}
1531
+ } {t1}
1532
+ }
1533
+
1534
+ do_test auth-1.229 {
1535
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1536
+ if {$code=="SQLITE_PRAGMA"} {
1537
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1538
+ return SQLITE_DENY
1539
+ }
1540
+ return SQLITE_OK
1541
+ }
1542
+ catchsql {PRAGMA full_column_names=on}
1543
+ } {1 {not authorized}}
1544
+ do_test auth-1.230 {
1545
+ set ::authargs
1546
+ } {full_column_names on {} {}}
1547
+ do_test auth-1.231 {
1548
+ execsql2 {SELECT a FROM t2}
1549
+ } {a 11 a 7}
1550
+ do_test auth-1.232 {
1551
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1552
+ if {$code=="SQLITE_PRAGMA"} {
1553
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1554
+ return SQLITE_IGNORE
1555
+ }
1556
+ return SQLITE_OK
1557
+ }
1558
+ catchsql {PRAGMA full_column_names=on}
1559
+ } {0 {}}
1560
+ do_test auth-1.233 {
1561
+ set ::authargs
1562
+ } {full_column_names on {} {}}
1563
+ do_test auth-1.234 {
1564
+ execsql2 {SELECT a FROM t2}
1565
+ } {a 11 a 7}
1566
+ do_test auth-1.235 {
1567
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1568
+ if {$code=="SQLITE_PRAGMA"} {
1569
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1570
+ return SQLITE_OK
1571
+ }
1572
+ return SQLITE_OK
1573
+ }
1574
+ catchsql {PRAGMA full_column_names=on}
1575
+ } {0 {}}
1576
+ do_test auth-1.236 {
1577
+ execsql2 {SELECT a FROM t2}
1578
+ } {t2.a 11 t2.a 7}
1579
+ do_test auth-1.237 {
1580
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1581
+ if {$code=="SQLITE_PRAGMA"} {
1582
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1583
+ return SQLITE_OK
1584
+ }
1585
+ return SQLITE_OK
1586
+ }
1587
+ catchsql {PRAGMA full_column_names=OFF}
1588
+ } {0 {}}
1589
+ do_test auth-1.238 {
1590
+ set ::authargs
1591
+ } {full_column_names OFF {} {}}
1592
+ do_test auth-1.239 {
1593
+ execsql2 {SELECT a FROM t2}
1594
+ } {a 11 a 7}
1595
+
1596
+ do_test auth-1.240 {
1597
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1598
+ if {$code=="SQLITE_TRANSACTION"} {
1599
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1600
+ return SQLITE_DENY
1601
+ }
1602
+ return SQLITE_OK
1603
+ }
1604
+ catchsql {BEGIN}
1605
+ } {1 {not authorized}}
1606
+ do_test auth-1.241 {
1607
+ set ::authargs
1608
+ } {BEGIN {} {} {}}
1609
+ do_test auth-1.242 {
1610
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1611
+ if {$code=="SQLITE_TRANSACTION" && $arg1!="BEGIN"} {
1612
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1613
+ return SQLITE_DENY
1614
+ }
1615
+ return SQLITE_OK
1616
+ }
1617
+ catchsql {BEGIN; INSERT INTO t2 VALUES(44,55,66); COMMIT}
1618
+ } {1 {not authorized}}
1619
+ do_test auth-1.243 {
1620
+ set ::authargs
1621
+ } {COMMIT {} {} {}}
1622
+ do_test auth-1.244 {
1623
+ execsql {SELECT * FROM t2}
1624
+ } {11 2 33 7 8 9 44 55 66}
1625
+ do_test auth-1.245 {
1626
+ catchsql {ROLLBACK}
1627
+ } {1 {not authorized}}
1628
+ do_test auth-1.246 {
1629
+ set ::authargs
1630
+ } {ROLLBACK {} {} {}}
1631
+ do_test auth-1.247 {
1632
+ catchsql {END TRANSACTION}
1633
+ } {1 {not authorized}}
1634
+ do_test auth-1.248 {
1635
+ set ::authargs
1636
+ } {COMMIT {} {} {}}
1637
+ do_test auth-1.249 {
1638
+ # EVIDENCE-OF: R-52112-44167 Disable the authorizer by installing a NULL
1639
+ # callback.
1640
+ db authorizer {}
1641
+ catchsql {ROLLBACK}
1642
+ } {0 {}}
1643
+ do_test auth-1.250 {
1644
+ execsql {SELECT * FROM t2}
1645
+ } {11 2 33 7 8 9}
1646
+
1647
+ # ticket #340 - authorization for ATTACH and DETACH.
1648
+ #
1649
+ ifcapable attach {
1650
+ do_test auth-1.251 {
1651
+ db authorizer ::auth
1652
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1653
+ if {$code=="SQLITE_ATTACH"} {
1654
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1655
+ }
1656
+ return SQLITE_OK
1657
+ }
1658
+ catchsql {
1659
+ ATTACH DATABASE ':memory:' AS test1
1660
+ }
1661
+ } {0 {}}
1662
+ do_test auth-1.252a {
1663
+ set ::authargs
1664
+ } {:memory: {} {} {}}
1665
+ do_test auth-1.252b {
1666
+ db eval {DETACH test1}
1667
+ set ::attachfilename :memory:
1668
+ db eval {ATTACH $::attachfilename AS test1}
1669
+ set ::authargs
1670
+ } {{} {} {} {}}
1671
+ do_test auth-1.252c {
1672
+ db eval {DETACH test1}
1673
+ db eval {ATTACH ':mem' || 'ory:' AS test1}
1674
+ set ::authargs
1675
+ } {{} {} {} {}}
1676
+ do_test auth-1.253 {
1677
+ catchsql {DETACH DATABASE test1}
1678
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1679
+ if {$code=="SQLITE_ATTACH"} {
1680
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1681
+ return SQLITE_DENY
1682
+ }
1683
+ return SQLITE_OK
1684
+ }
1685
+ catchsql {
1686
+ ATTACH DATABASE ':memory:' AS test1;
1687
+ }
1688
+ } {1 {not authorized}}
1689
+ do_test auth-1.254 {
1690
+ lindex [execsql {PRAGMA database_list}] 7
1691
+ } {}
1692
+ do_test auth-1.255 {
1693
+ catchsql {DETACH DATABASE test1}
1694
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1695
+ if {$code=="SQLITE_ATTACH"} {
1696
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1697
+ return SQLITE_IGNORE
1698
+ }
1699
+ return SQLITE_OK
1700
+ }
1701
+ catchsql {
1702
+ ATTACH DATABASE ':memory:' AS test1;
1703
+ }
1704
+ } {0 {}}
1705
+ do_test auth-1.256 {
1706
+ lindex [execsql {PRAGMA database_list}] 7
1707
+ } {}
1708
+ do_test auth-1.257 {
1709
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1710
+ if {$code=="SQLITE_DETACH"} {
1711
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1712
+ return SQLITE_OK
1713
+ }
1714
+ return SQLITE_OK
1715
+ }
1716
+ execsql {ATTACH DATABASE ':memory:' AS test1}
1717
+ catchsql {
1718
+ DETACH DATABASE test1;
1719
+ }
1720
+ } {0 {}}
1721
+ do_test auth-1.258 {
1722
+ lindex [execsql {PRAGMA database_list}] 7
1723
+ } {}
1724
+ do_test auth-1.259 {
1725
+ execsql {ATTACH DATABASE ':memory:' AS test1}
1726
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1727
+ if {$code=="SQLITE_DETACH"} {
1728
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1729
+ return SQLITE_IGNORE
1730
+ }
1731
+ return SQLITE_OK
1732
+ }
1733
+ catchsql {
1734
+ DETACH DATABASE test1;
1735
+ }
1736
+ } {0 {}}
1737
+ ifcapable tempdb {
1738
+ ifcapable schema_pragmas {
1739
+ do_test auth-1.260 {
1740
+ lindex [execsql {PRAGMA database_list}] 7
1741
+ } {test1}
1742
+ } ;# ifcapable schema_pragmas
1743
+ do_test auth-1.261 {
1744
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1745
+ if {$code=="SQLITE_DETACH"} {
1746
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1747
+ return SQLITE_DENY
1748
+ }
1749
+ return SQLITE_OK
1750
+ }
1751
+ catchsql {
1752
+ DETACH DATABASE test1;
1753
+ }
1754
+ } {1 {not authorized}}
1755
+ ifcapable schema_pragmas {
1756
+ do_test auth-1.262 {
1757
+ lindex [execsql {PRAGMA database_list}] 7
1758
+ } {test1}
1759
+ } ;# ifcapable schema_pragmas
1760
+ db authorizer {}
1761
+ execsql {DETACH DATABASE test1}
1762
+ db authorizer ::auth
1763
+
1764
+ # Authorization for ALTER TABLE. These tests are omitted if the library
1765
+ # was built without ALTER TABLE support.
1766
+ ifcapable altertable {
1767
+
1768
+ do_test auth-1.263 {
1769
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1770
+ if {$code=="SQLITE_ALTER_TABLE"} {
1771
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1772
+ return SQLITE_OK
1773
+ }
1774
+ return SQLITE_OK
1775
+ }
1776
+ catchsql {
1777
+ ALTER TABLE t1 RENAME TO t1x
1778
+ }
1779
+ } {0 {}}
1780
+ do_test auth-1.264 {
1781
+ execsql {SELECT name FROM sqlite_temp_master WHERE type='table'}
1782
+ } {t1x}
1783
+ do_test auth-1.265 {
1784
+ set authargs
1785
+ } {temp t1 {} {}}
1786
+ do_test auth-1.266 {
1787
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1788
+ if {$code=="SQLITE_ALTER_TABLE"} {
1789
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1790
+ return SQLITE_IGNORE
1791
+ }
1792
+ return SQLITE_OK
1793
+ }
1794
+ catchsql {
1795
+ ALTER TABLE t1x RENAME TO t1
1796
+ }
1797
+ } {0 {}}
1798
+ do_test auth-1.267 {
1799
+ execsql {SELECT name FROM temp.sqlite_master WHERE type='table'}
1800
+ } {t1x}
1801
+ do_test auth-1.268 {
1802
+ set authargs
1803
+ } {temp t1x {} {}}
1804
+ do_test auth-1.269 {
1805
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1806
+ if {$code=="SQLITE_ALTER_TABLE"} {
1807
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1808
+ return SQLITE_DENY
1809
+ }
1810
+ return SQLITE_OK
1811
+ }
1812
+ catchsql {
1813
+ ALTER TABLE t1x RENAME TO t1
1814
+ }
1815
+ } {1 {not authorized}}
1816
+ do_test auth-1.270 {
1817
+ execsql {SELECT name FROM sqlite_temp_master WHERE type='table'}
1818
+ } {t1x}
1819
+
1820
+ do_test auth-1.271 {
1821
+ set authargs
1822
+ } {temp t1x {} {}}
1823
+ } ;# ifcapable altertable
1824
+
1825
+ } else {
1826
+ db authorizer {}
1827
+ db eval {
1828
+ DETACH DATABASE test1;
1829
+ }
1830
+ }
1831
+ }
1832
+
1833
+ ifcapable altertable {
1834
+ db authorizer {}
1835
+ catchsql {ALTER TABLE t1x RENAME TO t1}
1836
+ db authorizer ::auth
1837
+ do_test auth-1.272 {
1838
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1839
+ if {$code=="SQLITE_ALTER_TABLE"} {
1840
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1841
+ return SQLITE_OK
1842
+ }
1843
+ return SQLITE_OK
1844
+ }
1845
+ catchsql {
1846
+ ALTER TABLE t2 RENAME TO t2x
1847
+ }
1848
+ } {0 {}}
1849
+ do_test auth-1.273 {
1850
+ execsql {SELECT name FROM sqlite_master WHERE type='table'}
1851
+ } {t2x}
1852
+ do_test auth-1.274 {
1853
+ set authargs
1854
+ } {main t2 {} {}}
1855
+ do_test auth-1.275 {
1856
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1857
+ if {$code=="SQLITE_ALTER_TABLE"} {
1858
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1859
+ return SQLITE_IGNORE
1860
+ }
1861
+ return SQLITE_OK
1862
+ }
1863
+ catchsql {
1864
+ ALTER TABLE t2x RENAME TO t2
1865
+ }
1866
+ } {0 {}}
1867
+ do_test auth-1.276 {
1868
+ execsql {SELECT name FROM sqlite_master WHERE type='table'}
1869
+ } {t2x}
1870
+ do_test auth-1.277 {
1871
+ set authargs
1872
+ } {main t2x {} {}}
1873
+ do_test auth-1.278 {
1874
+ proc auth {code arg1 arg2 arg3 arg4 args} {
1875
+ if {$code=="SQLITE_ALTER_TABLE"} {
1876
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
1877
+ return SQLITE_DENY
1878
+ }
1879
+ return SQLITE_OK
1880
+ }
1881
+ catchsql {
1882
+ ALTER TABLE t2x RENAME TO t2
1883
+ }
1884
+ } {1 {not authorized}}
1885
+ do_test auth-1.279 {
1886
+ execsql {SELECT name FROM sqlite_master WHERE type='table'}
1887
+ } {t2x}
1888
+ do_test auth-1.280 {
1889
+ set authargs
1890
+ } {main t2x {} {}}
1891
+ db authorizer {}
1892
+ catchsql {ALTER TABLE t2x RENAME TO t2}
1893
+
1894
+ } ;# ifcapable altertable
1895
+
1896
+ # Test the authorization callbacks for the REINDEX command.
1897
+ ifcapable reindex {
1898
+
1899
+ proc auth {code args} {
1900
+ if {$code=="SQLITE_REINDEX"} {
1901
+ set ::authargs [concat $::authargs [lrange $args 0 3]]
1902
+ }
1903
+ return SQLITE_OK
1904
+ }
1905
+ db authorizer auth
1906
+ do_test auth-1.281 {
1907
+ execsql {
1908
+ CREATE TABLE t3(a PRIMARY KEY, b, c);
1909
+ CREATE INDEX t3_idx1 ON t3(c COLLATE BINARY);
1910
+ CREATE INDEX t3_idx2 ON t3(b COLLATE NOCASE);
1911
+ }
1912
+ } {}
1913
+ do_test auth-1.282 {
1914
+ set ::authargs {}
1915
+ execsql {
1916
+ REINDEX t3_idx1;
1917
+ }
1918
+ set ::authargs
1919
+ } {t3_idx1 {} main {}}
1920
+ do_test auth-1.283 {
1921
+ set ::authargs {}
1922
+ execsql {
1923
+ REINDEX BINARY;
1924
+ }
1925
+ set ::authargs
1926
+ } {t3_idx1 {} main {} sqlite_autoindex_t3_1 {} main {}}
1927
+ do_test auth-1.284 {
1928
+ set ::authargs {}
1929
+ execsql {
1930
+ REINDEX NOCASE;
1931
+ }
1932
+ set ::authargs
1933
+ } {t3_idx2 {} main {}}
1934
+ do_test auth-1.285 {
1935
+ set ::authargs {}
1936
+ execsql {
1937
+ REINDEX t3;
1938
+ }
1939
+ set ::authargs
1940
+ } {t3_idx2 {} main {} t3_idx1 {} main {} sqlite_autoindex_t3_1 {} main {}}
1941
+ do_test auth-1.286 {
1942
+ execsql {
1943
+ DROP TABLE t3;
1944
+ }
1945
+ } {}
1946
+ ifcapable tempdb {
1947
+ do_test auth-1.287 {
1948
+ execsql {
1949
+ CREATE TEMP TABLE t3(a PRIMARY KEY, b, c);
1950
+ CREATE INDEX t3_idx1 ON t3(c COLLATE BINARY);
1951
+ CREATE INDEX t3_idx2 ON t3(b COLLATE NOCASE);
1952
+ }
1953
+ } {}
1954
+ do_test auth-1.288 {
1955
+ set ::authargs {}
1956
+ execsql {
1957
+ REINDEX temp.t3_idx1;
1958
+ }
1959
+ set ::authargs
1960
+ } {t3_idx1 {} temp {}}
1961
+ do_test auth-1.289 {
1962
+ set ::authargs {}
1963
+ execsql {
1964
+ REINDEX BINARY;
1965
+ }
1966
+ set ::authargs
1967
+ } {t3_idx1 {} temp {} sqlite_autoindex_t3_1 {} temp {}}
1968
+ do_test auth-1.290 {
1969
+ set ::authargs {}
1970
+ execsql {
1971
+ REINDEX NOCASE;
1972
+ }
1973
+ set ::authargs
1974
+ } {t3_idx2 {} temp {}}
1975
+ do_test auth-1.291 {
1976
+ set ::authargs {}
1977
+ execsql {
1978
+ REINDEX temp.t3;
1979
+ }
1980
+ set ::authargs
1981
+ } {t3_idx2 {} temp {} t3_idx1 {} temp {} sqlite_autoindex_t3_1 {} temp {}}
1982
+ proc auth {code args} {
1983
+ if {$code=="SQLITE_REINDEX"} {
1984
+ set ::authargs [concat $::authargs [lrange $args 0 3]]
1985
+ return SQLITE_DENY
1986
+ }
1987
+ return SQLITE_OK
1988
+ }
1989
+ do_test auth-1.292 {
1990
+ set ::authargs {}
1991
+ catchsql {
1992
+ REINDEX temp.t3;
1993
+ }
1994
+ } {1 {not authorized}}
1995
+ do_test auth-1.293 {
1996
+ execsql {
1997
+ DROP TABLE t3;
1998
+ }
1999
+ } {}
2000
+ }
2001
+
2002
+ } ;# ifcapable reindex
2003
+
2004
+ ifcapable analyze {
2005
+ proc auth {code args} {
2006
+ if {$code=="SQLITE_ANALYZE"} {
2007
+ set ::authargs [concat $::authargs [lrange $args 0 3]]
2008
+ }
2009
+ return SQLITE_OK
2010
+ }
2011
+ do_test auth-1.294 {
2012
+ set ::authargs {}
2013
+ execsql {
2014
+ CREATE TABLE t4(a,b,c);
2015
+ CREATE INDEX t4i1 ON t4(a);
2016
+ CREATE INDEX t4i2 ON t4(b,a,c);
2017
+ INSERT INTO t4 VALUES(1,2,3);
2018
+ ANALYZE;
2019
+ }
2020
+ set ::authargs
2021
+ } {t4 {} main {} t2 {} main {}}
2022
+ do_test auth-1.295 {
2023
+ execsql {
2024
+ SELECT count(*) FROM sqlite_stat1;
2025
+ }
2026
+ } 3
2027
+ proc auth {code args} {
2028
+ if {$code=="SQLITE_ANALYZE"} {
2029
+ set ::authargs [concat $::authargs $args]
2030
+ return SQLITE_DENY
2031
+ }
2032
+ return SQLITE_OK
2033
+ }
2034
+ do_test auth-1.296 {
2035
+ set ::authargs {}
2036
+ catchsql {
2037
+ ANALYZE;
2038
+ }
2039
+ } {1 {not authorized}}
2040
+ do_test auth-1.297 {
2041
+ execsql {
2042
+ SELECT count(*) FROM sqlite_stat1;
2043
+ }
2044
+ } 3
2045
+ } ;# ifcapable analyze
2046
+
2047
+
2048
+ # Authorization for ALTER TABLE ADD COLUMN.
2049
+ # These tests are omitted if the library
2050
+ # was built without ALTER TABLE support.
2051
+ ifcapable {altertable} {
2052
+ do_test auth-1.300 {
2053
+ execsql {CREATE TABLE t5(x)}
2054
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2055
+ if {$code=="SQLITE_ALTER_TABLE"} {
2056
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
2057
+ return SQLITE_OK
2058
+ }
2059
+ return SQLITE_OK
2060
+ }
2061
+ catchsql {
2062
+ ALTER TABLE t5 ADD COLUMN new_col_1;
2063
+ }
2064
+ } {0 {}}
2065
+ do_test auth-1.301 {
2066
+ set x [execsql {SELECT sql FROM sqlite_master WHERE name='t5'}]
2067
+ regexp new_col_1 $x
2068
+ } {1}
2069
+ do_test auth-1.302 {
2070
+ set authargs
2071
+ } {main t5 {} {}}
2072
+ db eval BEGIN
2073
+ set authargs {}
2074
+ do_execsql_test auth-1.302-drop-1 {
2075
+ ALTER TABLE t5 DROP COLUMN new_col_1;
2076
+ } {}
2077
+ db eval ROLLBACK
2078
+ do_test auth-1.302-drop-2 {
2079
+ set authargs
2080
+ } {main t5 new_col_1 {}}
2081
+ do_test auth-1.303 {
2082
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2083
+ if {$code=="SQLITE_ALTER_TABLE"} {
2084
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
2085
+ return SQLITE_IGNORE
2086
+ }
2087
+ return SQLITE_OK
2088
+ }
2089
+ catchsql {
2090
+ ALTER TABLE t5 ADD COLUMN new_col_2;
2091
+ }
2092
+ } {0 {}}
2093
+ do_test auth-1.304 {
2094
+ set x [execsql {SELECT sql FROM sqlite_master WHERE name='t5'}]
2095
+ regexp new_col_2 $x
2096
+ } {0}
2097
+ do_test auth-1.305 {
2098
+ set authargs
2099
+ } {main t5 {} {}}
2100
+ db eval BEGIN
2101
+ set authargs {}
2102
+ do_execsql_test auth-1.305-drop-1 {
2103
+ ALTER TABLE t5 DROP COLUMN new_col_1;
2104
+ SELECT 1 FROM sqlite_schema WHERE name='t5' AND sql LIKE '%new_col_1%';
2105
+ } {1}
2106
+ db eval ROLLBACK
2107
+ do_test auth-1.305-drop-2 {
2108
+ set authargs
2109
+ } {main t5 new_col_1 {}}
2110
+ do_test auth-1.306 {
2111
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2112
+ if {$code=="SQLITE_ALTER_TABLE"} {
2113
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
2114
+ return SQLITE_DENY
2115
+ }
2116
+ return SQLITE_OK
2117
+ }
2118
+ catchsql {
2119
+ ALTER TABLE t5 ADD COLUMN new_col_3
2120
+ }
2121
+ } {1 {not authorized}}
2122
+ do_test auth-1.307 {
2123
+ set x [execsql {SELECT sql FROM temp.sqlite_master WHERE type='t5'}]
2124
+ regexp new_col_3 $x
2125
+ } {0}
2126
+ do_test auth-1.308 {
2127
+ set authargs
2128
+ } {main t5 {} {}}
2129
+ db eval BEGIN
2130
+ set authargs {}
2131
+ do_catchsql_test auth-1.308-drop-1 {
2132
+ ALTER TABLE t5 DROP COLUMN new_col_1;
2133
+ } {1 {not authorized}}
2134
+ do_execsql_test auth-1.308-drop-2 {
2135
+ SELECT 1 FROM sqlite_schema WHERE name='t5' AND sql LIKE '%new_col_1%';
2136
+ } {1}
2137
+ do_test auth-1.308-drop-3 {
2138
+ set authargs
2139
+ } {main t5 new_col_1 {}}
2140
+ db eval ROLLBACK
2141
+
2142
+ execsql {DROP TABLE t5}
2143
+ } ;# ifcapable altertable
2144
+
2145
+ ifcapable {cte} {
2146
+ do_test auth-1.310 {
2147
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2148
+ if {$code=="SQLITE_RECURSIVE"} {
2149
+ return SQLITE_DENY
2150
+ }
2151
+ return SQLITE_OK
2152
+ }
2153
+ db eval {
2154
+ DROP TABLE IF EXISTS t1;
2155
+ CREATE TABLE t1(a,b);
2156
+ INSERT INTO t1 VALUES(1,2),(3,4),(5,6);
2157
+ }
2158
+ } {}
2159
+ do_catchsql_test auth-1.311 {
2160
+ WITH
2161
+ auth1311(x,y) AS (SELECT a+b, b-a FROM t1)
2162
+ SELECT * FROM auth1311 ORDER BY x;
2163
+ } {0 {3 1 7 1 11 1}}
2164
+ do_catchsql_test auth-1.312 {
2165
+ WITH RECURSIVE
2166
+ auth1312(x,y) AS (SELECT a+b, b-a FROM t1)
2167
+ SELECT x, y FROM auth1312 ORDER BY x;
2168
+ } {0 {3 1 7 1 11 1}}
2169
+ do_catchsql_test auth-1.313 {
2170
+ WITH RECURSIVE
2171
+ auth1313(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM auth1313 WHERE x<5)
2172
+ SELECT * FROM t1;
2173
+ } {0 {1 2 3 4 5 6}}
2174
+ do_catchsql_test auth-1.314 {
2175
+ WITH RECURSIVE
2176
+ auth1314(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM auth1314 WHERE x<5)
2177
+ SELECT * FROM t1 LEFT JOIN auth1314;
2178
+ } {1 {not authorized}}
2179
+ } ;# ifcapable cte
2180
+
2181
+ #
2182
+ # db eval {SELECT sql FROM temp.sqlite_master} {puts "TEMP: $sql;"}
2183
+ # db eval {SELECT sql FROM main.sqlite_master} {puts "MAIN: $sql;"}
2184
+ #
2185
+ # MAIN: CREATE TABLE "t2"(a,b,c);
2186
+ # MAIN: CREATE TABLE t4(a,b,c);
2187
+ # MAIN: CREATE INDEX t4i1 ON t4(a);
2188
+ # MAIN: CREATE INDEX t4i2 ON t4(b,a,c);
2189
+ # MAIN: CREATE TABLE sqlite_stat1(tbl,idx,stat);
2190
+ # MAIN: CREATE TABLE t1(a,b);
2191
+ #
2192
+ ifcapable altertable&&vtab {
2193
+ do_test auth-1.350 {
2194
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2195
+ if {$code=="SQLITE_ALTER_TABLE"} {
2196
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
2197
+ return SQLITE_OK
2198
+ }
2199
+ return SQLITE_OK
2200
+ }
2201
+ catchsql {
2202
+ ALTER TABLE t1 RENAME COLUMN b TO bcdefg;
2203
+ }
2204
+ } {0 {}}
2205
+ do_execsql_test auth-1.351 {
2206
+ SELECT name FROM pragma_table_info('t1') ORDER BY cid;
2207
+ } {a bcdefg}
2208
+ do_test auth-1.352 {
2209
+ set authargs
2210
+ } {main t1 {} {}}
2211
+ do_test auth-1.353 {
2212
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2213
+ if {$code=="SQLITE_ALTER_TABLE"} {
2214
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
2215
+ return SQLITE_IGNORE
2216
+ }
2217
+ return SQLITE_OK
2218
+ }
2219
+ catchsql {
2220
+ ALTER TABLE t1 RENAME COLUMN bcdefg TO b;
2221
+ }
2222
+ } {0 {}}
2223
+ do_execsql_test auth-1.354 {
2224
+ SELECT name FROM pragma_table_info('t1') ORDER BY cid;
2225
+ } {a bcdefg}
2226
+ do_test auth-1.355 {
2227
+ set authargs
2228
+ } {main t1 {} {}}
2229
+ do_test auth-1.356 {
2230
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2231
+ if {$code=="SQLITE_ALTER_TABLE"} {
2232
+ set ::authargs [list $arg1 $arg2 $arg3 $arg4]
2233
+ return SQLITE_DENY
2234
+ }
2235
+ return SQLITE_OK
2236
+ }
2237
+ catchsql {
2238
+ ALTER TABLE t1 RENAME COLUMN bcdefg TO b;
2239
+ }
2240
+ } {1 {not authorized}}
2241
+ do_execsql_test auth-1.357 {
2242
+ SELECT name FROM pragma_table_info('t1') ORDER BY cid;
2243
+ } {a bcdefg}
2244
+ do_test auth-1.358 {
2245
+ set authargs
2246
+ } {main t1 {} {}}
2247
+ }
2248
+
2249
+ # 2022-12-28
2250
+ # The sqlite3_declare_vtab() call that occurs during pragma_table_list
2251
+ # should not cause an authentication failure.
2252
+ #
2253
+ ifcapable vtab {
2254
+ do_test auth-1.359 {
2255
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2256
+ if {$code=="SQLITE_UPDATE"} {
2257
+ return SQLITE_DENY
2258
+ }
2259
+ return SQLITE_OK
2260
+ }
2261
+ catchsql {SELECT * FROM pragma_table_list WHERE name='xyzzy';}
2262
+ } {0 {}}
2263
+ }
2264
+
2265
+ do_test auth-2.1 {
2266
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2267
+ if {$code=="SQLITE_READ" && $arg1=="t3" && $arg2=="x"} {
2268
+ return SQLITE_DENY
2269
+ }
2270
+ return SQLITE_OK
2271
+ }
2272
+ db authorizer ::auth
2273
+ execsql {CREATE TABLE t3(x INTEGER PRIMARY KEY, y, z)}
2274
+ catchsql {SELECT * FROM t3}
2275
+ } {1 {access to t3.x is prohibited}}
2276
+ do_test auth-2.1 {
2277
+ catchsql {SELECT y,z FROM t3}
2278
+ } {0 {}}
2279
+ do_test auth-2.2 {
2280
+ catchsql {SELECT ROWID,y,z FROM t3}
2281
+ } {1 {access to t3.x is prohibited}}
2282
+ do_test auth-2.3 {
2283
+ catchsql {SELECT OID,y,z FROM t3}
2284
+ } {1 {access to t3.x is prohibited}}
2285
+ do_test auth-2.4 {
2286
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2287
+ if {$code=="SQLITE_READ" && $arg1=="t3" && $arg2=="x"} {
2288
+ return SQLITE_IGNORE
2289
+ }
2290
+ return SQLITE_OK
2291
+ }
2292
+ execsql {INSERT INTO t3 VALUES(44,55,66)}
2293
+ catchsql {SELECT * FROM t3}
2294
+ } {0 {{} 55 66}}
2295
+ do_test auth-2.5 {
2296
+ catchsql {SELECT rowid,y,z FROM t3}
2297
+ } {0 {{} 55 66}}
2298
+ do_test auth-2.6 {
2299
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2300
+ if {$code=="SQLITE_READ" && $arg1=="t3" && $arg2=="ROWID"} {
2301
+ return SQLITE_IGNORE
2302
+ }
2303
+ return SQLITE_OK
2304
+ }
2305
+ catchsql {SELECT * FROM t3}
2306
+ } {0 {44 55 66}}
2307
+ do_test auth-2.7 {
2308
+ catchsql {SELECT ROWID,y,z FROM t3}
2309
+ } {0 {44 55 66}}
2310
+ do_test auth-2.8 {
2311
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2312
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="ROWID"} {
2313
+ return SQLITE_IGNORE
2314
+ }
2315
+ return SQLITE_OK
2316
+ }
2317
+ catchsql {SELECT ROWID,b,c FROM t2}
2318
+ } {0 {{} 2 33 {} 8 9}}
2319
+ do_test auth-2.9.1 {
2320
+ # We have to flush the cache here in case the Tcl interface tries to
2321
+ # reuse a statement compiled with sqlite3_prepare_v2(). In this case,
2322
+ # the first error encountered is an SQLITE_SCHEMA error. Then, when
2323
+ # trying to recompile the statement, the authorization error is encountered.
2324
+ # If we do not flush the cache, the correct error message is returned, but
2325
+ # the error code is SQLITE_SCHEMA, not SQLITE_ERROR as required by the test
2326
+ # case after this one.
2327
+ #
2328
+ db cache flush
2329
+
2330
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2331
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="ROWID"} {
2332
+ return bogus
2333
+ }
2334
+ return SQLITE_OK
2335
+ }
2336
+ catchsql {SELECT ROWID,b,c FROM t2}
2337
+ } {1 {authorizer malfunction}}
2338
+ do_test auth-2.9.2 {
2339
+ db errorcode
2340
+ } {1}
2341
+ do_test auth-2.10 {
2342
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2343
+ if {$code=="SQLITE_SELECT"} {
2344
+ return bogus
2345
+ }
2346
+ return SQLITE_OK
2347
+ }
2348
+ catchsql {SELECT ROWID,b,c FROM t2}
2349
+ } {1 {authorizer malfunction}}
2350
+ do_test auth-2.11.1 {
2351
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2352
+ if {$code=="SQLITE_READ" && $arg2=="a"} {
2353
+ return SQLITE_IGNORE
2354
+ }
2355
+ return SQLITE_OK
2356
+ }
2357
+ catchsql {SELECT * FROM t2, t3}
2358
+ } {0 {{} 2 33 44 55 66 {} 8 9 44 55 66}}
2359
+ do_test auth-2.11.2 {
2360
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2361
+ if {$code=="SQLITE_READ" && $arg2=="x"} {
2362
+ return SQLITE_IGNORE
2363
+ }
2364
+ return SQLITE_OK
2365
+ }
2366
+ catchsql {SELECT * FROM t2, t3}
2367
+ } {0 {11 2 33 {} 55 66 7 8 9 {} 55 66}}
2368
+
2369
+ # Make sure the OLD and NEW pseudo-tables of a trigger get authorized.
2370
+ #
2371
+ ifcapable trigger {
2372
+ do_test auth-3.1 {
2373
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2374
+ return SQLITE_OK
2375
+ }
2376
+ execsql {
2377
+ CREATE TABLE tx(a1,a2,b1,b2,c1,c2);
2378
+ CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
2379
+ INSERT INTO tx VALUES(OLD.a,NEW.a,OLD.b,NEW.b,OLD.c,NEW.c);
2380
+ END;
2381
+ UPDATE t2 SET a=a+1;
2382
+ SELECT * FROM tx;
2383
+ }
2384
+ } {11 12 2 2 33 33 7 8 8 8 9 9}
2385
+ do_test auth-3.2 {
2386
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2387
+ if {$code=="SQLITE_READ" && $arg1=="t2" && $arg2=="c"} {
2388
+ return SQLITE_IGNORE
2389
+ }
2390
+ return SQLITE_OK
2391
+ }
2392
+ execsql {
2393
+ DELETE FROM tx;
2394
+ UPDATE t2 SET a=a+100;
2395
+ SELECT * FROM tx;
2396
+ }
2397
+ } {12 112 2 2 {} {} 8 108 8 8 {} {}}
2398
+ } ;# ifcapable trigger
2399
+
2400
+ # Make sure the names of views and triggers are passed on on arg4.
2401
+ #
2402
+ ifcapable trigger {
2403
+ do_test auth-4.1 {
2404
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2405
+ lappend ::authargs $code $arg1 $arg2 $arg3 $arg4
2406
+ return SQLITE_OK
2407
+ }
2408
+ set authargs {}
2409
+ execsql {
2410
+ UPDATE t2 SET a=a+1;
2411
+ }
2412
+ set authargs
2413
+ } [list \
2414
+ SQLITE_READ t2 a main {} \
2415
+ SQLITE_UPDATE t2 a main {} \
2416
+ SQLITE_INSERT tx {} main r1 \
2417
+ SQLITE_READ t2 a main r1 \
2418
+ SQLITE_READ t2 a main r1 \
2419
+ SQLITE_READ t2 b main r1 \
2420
+ SQLITE_READ t2 b main r1 \
2421
+ SQLITE_READ t2 c main r1 \
2422
+ SQLITE_READ t2 c main r1]
2423
+ }
2424
+
2425
+ ifcapable {view && trigger} {
2426
+ do_test auth-4.2 {
2427
+ execsql {
2428
+ CREATE VIEW v1 AS SELECT a+b AS x FROM t2;
2429
+ CREATE TABLE v1chng(x1,x2);
2430
+ CREATE TRIGGER r2 INSTEAD OF UPDATE ON v1 BEGIN
2431
+ INSERT INTO v1chng VALUES(OLD.x,NEW.x);
2432
+ END;
2433
+ SELECT * FROM v1;
2434
+ }
2435
+ } {115 117}
2436
+ do_test auth-4.3 {
2437
+ set authargs {}
2438
+ execsql {
2439
+ UPDATE v1 SET x=1 WHERE x=117
2440
+ }
2441
+ set authargs
2442
+ } [list \
2443
+ SQLITE_UPDATE v1 x main {} \
2444
+ SQLITE_SELECT {} {} {} v1 \
2445
+ SQLITE_READ t2 a main v1 \
2446
+ SQLITE_READ t2 b main v1 \
2447
+ SQLITE_READ v1 x main v1 \
2448
+ SQLITE_READ v1 x main v1 \
2449
+ SQLITE_SELECT {} {} {} v1 \
2450
+ SQLITE_READ v1 x main v1 \
2451
+ SQLITE_INSERT v1chng {} main r2 \
2452
+ SQLITE_READ v1 x main r2 \
2453
+ SQLITE_READ v1 x main r2 \
2454
+ ]
2455
+
2456
+ do_test auth-4.4 {
2457
+ execsql {
2458
+ CREATE TRIGGER r3 INSTEAD OF DELETE ON v1 BEGIN
2459
+ INSERT INTO v1chng VALUES(OLD.x,NULL);
2460
+ END;
2461
+ SELECT * FROM v1;
2462
+ }
2463
+ } {115 117}
2464
+ do_test auth-4.5 {
2465
+ set authargs {}
2466
+ execsql {
2467
+ DELETE FROM v1 WHERE x=117
2468
+ }
2469
+ set authargs
2470
+ } [list \
2471
+ SQLITE_DELETE v1 {} main {} \
2472
+ SQLITE_SELECT {} {} {} v1 \
2473
+ SQLITE_READ t2 a main v1 \
2474
+ SQLITE_READ t2 b main v1 \
2475
+ SQLITE_READ v1 x main v1 \
2476
+ SQLITE_READ v1 x main v1 \
2477
+ SQLITE_SELECT {} {} {} v1 \
2478
+ SQLITE_READ v1 x main v1 \
2479
+ SQLITE_INSERT v1chng {} main r3 \
2480
+ SQLITE_READ v1 x main r3 \
2481
+ ]
2482
+
2483
+ } ;# ifcapable view && trigger
2484
+
2485
+ # Ticket #1338: Make sure authentication works in the presence of an AS
2486
+ # clause.
2487
+ #
2488
+ do_test auth-5.1 {
2489
+ proc auth {code arg1 arg2 arg3 arg4 args} {
2490
+ return SQLITE_OK
2491
+ }
2492
+ execsql {
2493
+ SELECT count(a) AS cnt FROM t4 ORDER BY cnt
2494
+ }
2495
+ } {1}
2496
+
2497
+ # Ticket #1607
2498
+ #
2499
+ ifcapable compound&&subquery {
2500
+ ifcapable trigger {
2501
+ execsql {
2502
+ DROP TABLE tx;
2503
+ }
2504
+ ifcapable view {
2505
+ execsql {
2506
+ DROP TABLE v1chng;
2507
+ }
2508
+ }
2509
+ }
2510
+ ifcapable stat4 {
2511
+ set stat4 "sqlite_stat4 "
2512
+ } else {
2513
+ set stat4 ""
2514
+ }
2515
+ do_test auth-5.2 {
2516
+ execsql {
2517
+ SELECT name FROM (
2518
+ SELECT * FROM sqlite_master UNION ALL SELECT * FROM temp.sqlite_master)
2519
+ WHERE type='table'
2520
+ ORDER BY name
2521
+ }
2522
+ } "sqlite_stat1 ${stat4}t1 t2 t3 t4"
2523
+ }
2524
+
2525
+ # Ticket #3944
2526
+ #
2527
+ ifcapable trigger {
2528
+ do_test auth-5.3.1 {
2529
+ execsql {
2530
+ CREATE TABLE t5 ( x );
2531
+ CREATE TRIGGER t5_tr1 AFTER INSERT ON t5 BEGIN
2532
+ UPDATE t5 SET x = 1 WHERE NEW.x = 0;
2533
+ END;
2534
+ }
2535
+ } {}
2536
+ set ::authargs [list]
2537
+ proc auth {args} {
2538
+ eval lappend ::authargs [lrange $args 0 4]
2539
+ return SQLITE_OK
2540
+ }
2541
+ do_test auth-5.3.2 {
2542
+ execsql { INSERT INTO t5 (x) values(0) }
2543
+ set ::authargs
2544
+ } [list SQLITE_INSERT t5 {} main {} \
2545
+ SQLITE_UPDATE t5 x main t5_tr1 \
2546
+ SQLITE_READ t5 x main t5_tr1 \
2547
+ ]
2548
+ do_test auth-5.3.2 {
2549
+ execsql { SELECT * FROM t5 }
2550
+ } {1}
2551
+ }
2552
+
2553
+ # Ticket [0eb70d77cb05bb22720]: Invalid pointer passsed to the authorizer
2554
+ # callback when updating a ROWID.
2555
+ #
2556
+ do_test auth-6.1 {
2557
+ execsql {
2558
+ CREATE TABLE t6(a,b,c,d,e,f,g,h);
2559
+ INSERT INTO t6 VALUES(1,2,3,4,5,6,7,8);
2560
+ }
2561
+ } {}
2562
+ set ::authargs [list]
2563
+ proc auth {args} {
2564
+ eval lappend ::authargs [lrange $args 0 4]
2565
+ return SQLITE_OK
2566
+ }
2567
+ do_test auth-6.2 {
2568
+ execsql {UPDATE t6 SET rowID=rowID+100}
2569
+ set ::authargs
2570
+ } [list SQLITE_READ t6 ROWID main {} \
2571
+ SQLITE_UPDATE t6 ROWID main {} \
2572
+ ]
2573
+ do_test auth-6.3 {
2574
+ execsql {SELECT rowid, * FROM t6}
2575
+ } {101 1 2 3 4 5 6 7 8}
2576
+
2577
+ #-------------------------------------------------------------------------
2578
+ # Test that view names are included as zArg4.
2579
+ #
2580
+ do_execsql_test auth-7.1 {
2581
+ CREATE TABLE t7(a, b, c);
2582
+ CREATE VIEW v7 AS SELECT * FROM t7;
2583
+ } {}
2584
+ set ::authargs [list]
2585
+ proc auth {args} {
2586
+ eval lappend ::authargs [lrange $args 0 4]
2587
+ return SQLITE_OK
2588
+ }
2589
+
2590
+ do_test auth-7.2 {
2591
+ execsql {SELECT a, c FROM v7}
2592
+ set ::authargs
2593
+ } [list \
2594
+ SQLITE_SELECT {} {} {} {} \
2595
+ SQLITE_READ t7 a main v7 \
2596
+ SQLITE_READ t7 b main v7 \
2597
+ SQLITE_READ t7 c main v7 \
2598
+ SQLITE_READ v7 a main {} \
2599
+ SQLITE_READ v7 c main {} \
2600
+ SQLITE_SELECT {} {} {} v7 \
2601
+ ]
2602
+
2603
+ set ::authargs [list]
2604
+ do_test auth-7.3 {
2605
+ execsql {SELECT a, c FROM t7}
2606
+ set ::authargs
2607
+ } [list \
2608
+ SQLITE_SELECT {} {} {} {} \
2609
+ SQLITE_READ t7 a main {} \
2610
+ SQLITE_READ t7 c main {} \
2611
+ ]
2612
+
2613
+ set ::authargs [list]
2614
+ do_test auth-7.4 {
2615
+ execsql {SELECT a, c FROM t7 AS v7}
2616
+ set ::authargs
2617
+ } [list \
2618
+ SQLITE_SELECT {} {} {} {} \
2619
+ SQLITE_READ t7 a main {} \
2620
+ SQLITE_READ t7 c main {} \
2621
+ ]
2622
+
2623
+ # If a table is referenced but no columns are read from the table,
2624
+ # that causes a single SQLITE_READ authorization with a NULL column
2625
+ # name.
2626
+ #
2627
+ # EVIDENCE-OF: R-31520-16302 When a table is referenced by a SELECT but
2628
+ # no column values are extracted from that table (for example in a query
2629
+ # like "SELECT count(*) FROM tab") then the SQLITE_READ authorizer
2630
+ # callback is invoked once for that table with a column name that is an
2631
+ # empty string.
2632
+ #
2633
+ set ::authargs [list]
2634
+ do_test auth-8.1 {
2635
+ execsql {SELECT count(*) FROM t7}
2636
+ set ::authargs
2637
+ } [list \
2638
+ SQLITE_SELECT {} {} {} {} \
2639
+ SQLITE_FUNCTION {} count {} {} \
2640
+ SQLITE_READ t7 {} {} {} \
2641
+ ]
2642
+ set ::authargs [list]
2643
+
2644
+ do_test auth-8.2 {
2645
+ execsql {SELECT t6.a FROM t6, t7}
2646
+ set ::authargs
2647
+ } [list \
2648
+ SQLITE_SELECT {} {} {} {} \
2649
+ SQLITE_READ t6 a main {} \
2650
+ SQLITE_READ t7 {} {} {} \
2651
+ ]
2652
+
2653
+ # Test also that if SQLITE_DENY is returned from an SQLITE_READ authorizer
2654
+ # invocation with no column name specified, compilation fails.
2655
+ #
2656
+ set ::authargs [list]
2657
+ proc auth {op args} {
2658
+ foreach {a b c d} $args break
2659
+ lappend ::authargs $op $a $b $c $d
2660
+ if {$op == "SQLITE_READ"} { return "SQLITE_DENY" }
2661
+ return "SQLITE_OK"
2662
+ }
2663
+ set ::authargs [list]
2664
+ do_catchsql_test auth-8.3 {
2665
+ SELECT count(*) FROM t7
2666
+ } {1 {not authorized}}
2667
+ do_test auth-8.4 {
2668
+ set ::authargs
2669
+ } [list \
2670
+ SQLITE_SELECT {} {} {} {} \
2671
+ SQLITE_FUNCTION {} count {} {} \
2672
+ SQLITE_READ t7 {} {} {} \
2673
+ ]
2674
+
2675
+
2676
+ rename proc {}
2677
+ rename proc_real proc
2678
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/auth3.test ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2008 October 27
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+ # Test that the truncate optimization is disabled if the SQLITE_DELETE
13
+ # authorization callback returns SQLITE_IGNORE.
14
+ #
15
+ # Test that authorizer is disabled during schema parsing.
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ # disable this test if the SQLITE_OMIT_AUTHORIZATION macro is
21
+ # defined during compilation.
22
+ if {[catch {db auth {}} msg]} {
23
+ finish_test
24
+ return
25
+ }
26
+
27
+ # Disable the statement cache for these tests.
28
+ #
29
+ db cache size 0
30
+
31
+ db authorizer ::auth
32
+ proc auth {code arg1 arg2 arg3 arg4 args} {
33
+ if {$code=="SQLITE_DELETE"} {
34
+ return $::authcode
35
+ }
36
+ return SQLITE_OK
37
+ }
38
+
39
+ #--------------------------------------------------------------------------
40
+ # The following tests - auth3-1.* - test that return values of SQLITE_DENY,
41
+ # SQLITE_IGNORE, SQLITE_OK and <invalid> are correctly handled when returned
42
+ # by an SQLITE_DELETE authorization callback triggered by a
43
+ # "DELETE FROM <table-name>" statement.
44
+ #
45
+ do_test auth3-1.1 {
46
+ execsql {
47
+ CREATE TABLE t1(a,b,c);
48
+ INSERT INTO t1 VALUES(1, 2, 3);
49
+ INSERT INTO t1 VALUES(4, 5, 6);
50
+ }
51
+ } {}
52
+ do_test auth3.1.2 {
53
+ set ::authcode SQLITE_DENY
54
+ catchsql { DELETE FROM t1 }
55
+ } {1 {not authorized}}
56
+ # EVIDENCE-OF: R-64962-58611 If the authorizer callback returns any
57
+ # value other than SQLITE_IGNORE, SQLITE_OK, or SQLITE_DENY then the
58
+ # sqlite3_prepare_v2() or equivalent call that triggered the authorizer
59
+ # will fail with an error message.
60
+ do_test auth3.1.3 {
61
+ set ::authcode SQLITE_INVALID
62
+ catchsql { DELETE FROM t1 }
63
+ } {1 {authorizer malfunction}}
64
+ do_test auth3.1.4 {
65
+ execsql { SELECT * FROM t1 }
66
+ } {1 2 3 4 5 6}
67
+ do_test auth3-1.5 {
68
+ set ::authcode SQLITE_IGNORE
69
+ execsql {
70
+ DELETE FROM t1;
71
+ SELECT * FROM t1;
72
+ }
73
+ } {}
74
+ do_test auth3-1.6 {
75
+ set ::authcode SQLITE_OK
76
+ execsql {
77
+ INSERT INTO t1 VALUES(1, 2, 3);
78
+ INSERT INTO t1 VALUES(4, 5, 6);
79
+ DELETE FROM t1;
80
+ SELECT * FROM t1;
81
+ }
82
+ } {}
83
+
84
+ #--------------------------------------------------------------------------
85
+ # These tests - auth3-2.* - test that returning SQLITE_IGNORE really does
86
+ # disable the truncate optimization.
87
+ #
88
+ do_test auth3-2.1 {
89
+ set ::authcode SQLITE_OK
90
+ execsql {
91
+ INSERT INTO t1 VALUES(1, 2, 3);
92
+ INSERT INTO t1 VALUES(4, 5, 6);
93
+ }
94
+ set sqlite_search_count 0
95
+ execsql {
96
+ DELETE FROM t1;
97
+ }
98
+ set sqlite_search_count
99
+ } {0}
100
+
101
+ do_test auth3-2.2 {
102
+ set ::authcode SQLITE_IGNORE
103
+ execsql {
104
+ INSERT INTO t1 VALUES(1, 2, 3);
105
+ INSERT INTO t1 VALUES(4, 5, 6);
106
+ }
107
+ set sqlite_search_count 0
108
+ execsql {
109
+ DELETE FROM t1;
110
+ }
111
+ set sqlite_search_count
112
+ } {1}
113
+
114
+ # 2016-07-28. A problem report from a private client complaining about
115
+ # an authorizer failure during an ALTER TABLE. The solution (I think) is
116
+ # to disable the authorizer during schema parsing.
117
+ #
118
+ ifcapable altertable {
119
+ proc auth {code args} {
120
+ if {$code=="SQLITE_READ" && [regexp {DoNotRead} $args]} {
121
+ return SQLITE_DENY
122
+ }
123
+ return SQLITE_OK
124
+ }
125
+ do_execsql_test auth3-3.0 {
126
+ CREATE TEMPORARY TABLE TempTable (
127
+ key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,
128
+ value TEXT NOT NULL ON CONFLICT FAIL);
129
+ ALTER TABLE TempTable RENAME TO DoNotRead;
130
+ SELECT name FROM temp.sqlite_master;
131
+ } {DoNotRead sqlite_autoindex_DoNotRead_1}
132
+ }
133
+
134
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/autoindex1.test ADDED
@@ -0,0 +1,566 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2010 April 07
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script is testing automatic index creation logic.
13
+ #
14
+ # EVIDENCE-OF: R-34271-33106 PRAGMA automatic_index; PRAGMA
15
+ # automatic_index = boolean; Query, set, or clear the automatic indexing
16
+ # capability.
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+ # If the library is not compiled with automatic index support then
22
+ # skip all tests in this file.
23
+ #
24
+ ifcapable {!autoindex} {
25
+ finish_test
26
+ return
27
+ }
28
+
29
+ # Setup for logging
30
+ db close
31
+ sqlite3_shutdown
32
+ test_sqlite3_log [list lappend ::log]
33
+ set ::log [list]
34
+ sqlite3 db test.db
35
+
36
+
37
+ # With automatic index turned off, we do a full scan of the T2 table
38
+ do_test autoindex1-100 {
39
+ db eval {
40
+ CREATE TABLE t1(a,b);
41
+ INSERT INTO t1 VALUES(1,11);
42
+ INSERT INTO t1 VALUES(2,22);
43
+ INSERT INTO t1 SELECT a+2, b+22 FROM t1;
44
+ INSERT INTO t1 SELECT a+4, b+44 FROM t1;
45
+ CREATE TABLE t2(c,d);
46
+ INSERT INTO t2 SELECT a, 900+b FROM t1;
47
+ }
48
+ db eval {
49
+ PRAGMA automatic_index=OFF;
50
+ SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b;
51
+ }
52
+ } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
53
+ do_test autoindex1-101 {
54
+ db status step
55
+ } {63}
56
+ do_test autoindex1-102 {
57
+ db status autoindex
58
+ } {0}
59
+
60
+ # With autoindex turned on, we build an index once and then use that index
61
+ # to find T2 values.
62
+ do_test autoindex1-110 {
63
+ db eval {
64
+ PRAGMA automatic_index=ON;
65
+ SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b;
66
+ }
67
+ } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
68
+ do_test autoindex1-111 {
69
+ db status step
70
+ } {7}
71
+ do_test autoindex1-112 {
72
+ db status autoindex
73
+ } {7}
74
+ do_test autoindex1-113 {
75
+ set ::log
76
+ } {SQLITE_WARNING_AUTOINDEX {automatic index on t2(c)}}
77
+
78
+ db close
79
+ sqlite3_shutdown
80
+ test_sqlite3_log
81
+ sqlite3_initialize
82
+ sqlite3 db test.db
83
+
84
+ # The same test as above, but this time the T2 query is a subquery rather
85
+ # than a join.
86
+ do_test autoindex1-200 {
87
+ db eval {
88
+ PRAGMA automatic_index=OFF;
89
+ SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1;
90
+ }
91
+ } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
92
+ do_test autoindex1-201 {
93
+ db status step
94
+ } {35}
95
+ do_test autoindex1-202 {
96
+ db status autoindex
97
+ } {0}
98
+ do_test autoindex1-210 {
99
+ db eval {
100
+ PRAGMA automatic_index=ON;
101
+ ANALYZE;
102
+ UPDATE sqlite_stat1 SET stat='10000' WHERE tbl='t1';
103
+ -- Table t2 actually contains 8 rows.
104
+ UPDATE sqlite_stat1 SET stat='16' WHERE tbl='t2';
105
+ ANALYZE sqlite_master;
106
+ SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1;
107
+ }
108
+ } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
109
+ do_test autoindex1-211 {
110
+ db status step
111
+ } {7}
112
+ do_test autoindex1-212 {
113
+ db status autoindex
114
+ } {7}
115
+
116
+
117
+ # Modify the second table of the join while the join is in progress
118
+ #
119
+ do_execsql_test autoindex1-299 {
120
+ UPDATE sqlite_stat1 SET stat='10000' WHERE tbl='t2';
121
+ ANALYZE sqlite_master;
122
+ EXPLAIN QUERY PLAN
123
+ SELECT b, d FROM t1 CROSS JOIN t2 ON (c=a);
124
+ } {/AUTOMATIC COVERING INDEX/}
125
+ do_test autoindex1-300 {
126
+ set r {}
127
+ db eval {SELECT b, d FROM t1 CROSS JOIN t2 ON (c=a)} {
128
+ lappend r $b $d
129
+ db eval {UPDATE t2 SET d=d+1}
130
+ }
131
+ set r
132
+ } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
133
+ do_test autoindex1-310 {
134
+ db eval {SELECT d FROM t2 ORDER BY d}
135
+ } {919 930 941 952 963 974 985 996}
136
+
137
+ # The next test does a 10-way join on unindexed tables. Without
138
+ # automatic indices, the join will take a long time to complete.
139
+ # With automatic indices, it should only take about a second.
140
+ #
141
+ do_test autoindex1-400 {
142
+ db eval {
143
+ CREATE TABLE t4(a, b);
144
+ INSERT INTO t4 VALUES(1,2);
145
+ INSERT INTO t4 VALUES(2,3);
146
+ }
147
+ for {set n 2} {$n<4096} {set n [expr {$n+$n}]} {
148
+ db eval {INSERT INTO t4 SELECT a+$n, b+$n FROM t4}
149
+ }
150
+ db eval {
151
+ SELECT count(*) FROM t4;
152
+ }
153
+ } {4096}
154
+ do_test autoindex1-401 {
155
+ db eval {
156
+ SELECT count(*)
157
+ FROM t4 AS x1
158
+ JOIN t4 AS x2 ON x2.a=x1.b
159
+ JOIN t4 AS x3 ON x3.a=x2.b
160
+ JOIN t4 AS x4 ON x4.a=x3.b
161
+ JOIN t4 AS x5 ON x5.a=x4.b
162
+ JOIN t4 AS x6 ON x6.a=x5.b
163
+ JOIN t4 AS x7 ON x7.a=x6.b
164
+ JOIN t4 AS x8 ON x8.a=x7.b
165
+ JOIN t4 AS x9 ON x9.a=x8.b
166
+ JOIN t4 AS x10 ON x10.a=x9.b;
167
+ }
168
+ } {4087}
169
+
170
+ # Ticket [8011086c85c6c404014c947fcf3eb9f42b184a0d] from 2010-07-08
171
+ # Make sure automatic indices are not created for the RHS of an IN expression
172
+ # that is not a correlated subquery.
173
+ #
174
+ do_execsql_test autoindex1-500 {
175
+ CREATE TABLE t501(a INTEGER PRIMARY KEY, b);
176
+ CREATE TABLE t502(x INTEGER PRIMARY KEY, y);
177
+ INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t501',null,'1000000');
178
+ INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t502',null,'1000');
179
+ ANALYZE sqlite_master;
180
+ }
181
+ do_eqp_test autoindex1-500.1 {
182
+ SELECT b FROM t501
183
+ WHERE t501.a IN (SELECT x FROM t502 WHERE y=?);
184
+ } {
185
+ QUERY PLAN
186
+ |--SEARCH t501 USING INTEGER PRIMARY KEY (rowid=?)
187
+ `--LIST SUBQUERY xxxxxx
188
+ |--SCAN t502
189
+ `--CREATE BLOOM FILTER
190
+ }
191
+ do_eqp_test autoindex1-501 {
192
+ SELECT b FROM t501
193
+ WHERE t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
194
+ } {
195
+ QUERY PLAN
196
+ |--SCAN t501
197
+ `--CORRELATED LIST SUBQUERY xxxxxx
198
+ |--BLOOM FILTER ON t502 (y=?)
199
+ `--SEARCH t502 USING AUTOMATIC COVERING INDEX (y=?)
200
+ }
201
+ do_eqp_test autoindex1-502 {
202
+ SELECT b FROM t501
203
+ WHERE t501.a=123
204
+ AND t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
205
+ } {
206
+ QUERY PLAN
207
+ |--SEARCH t501 USING INTEGER PRIMARY KEY (rowid=?)
208
+ `--CORRELATED LIST SUBQUERY xxxxxx
209
+ `--SCAN t502
210
+ }
211
+
212
+ # The following code checks a performance regression reported on the
213
+ # mailing list on 2010-10-19. The problem is that the nRowEst field
214
+ # of ephermeral tables was not being initialized correctly and so no
215
+ # automatic index was being created for the emphemeral table when it was
216
+ # used as part of a join.
217
+ #
218
+ do_execsql_test autoindex1-600 {
219
+ CREATE TABLE flock_owner(
220
+ owner_rec_id INTEGER CONSTRAINT flock_owner_key PRIMARY KEY,
221
+ flock_no VARCHAR(6) NOT NULL REFERENCES flock (flock_no),
222
+ owner_person_id INTEGER NOT NULL REFERENCES person (person_id),
223
+ owner_change_date TEXT, last_changed TEXT NOT NULL,
224
+ CONSTRAINT fo_owner_date UNIQUE (flock_no, owner_change_date)
225
+ );
226
+ CREATE TABLE sheep (
227
+ Sheep_No char(7) NOT NULL,
228
+ Date_of_Birth char(8),
229
+ Sort_DoB text,
230
+ Flock_Book_Vol char(2),
231
+ Breeder_No char(6),
232
+ Breeder_Person integer,
233
+ Originating_Flock char(6),
234
+ Registering_Flock char(6),
235
+ Tag_Prefix char(9),
236
+ Tag_No char(15),
237
+ Sort_Tag_No integer,
238
+ Breeders_Temp_Tag char(15),
239
+ Sex char(1),
240
+ Sheep_Name char(32),
241
+ Sire_No char(7),
242
+ Dam_No char(7),
243
+ Register_Code char(1),
244
+ Colour char(48),
245
+ Colour_Code char(2),
246
+ Pattern_Code char(8),
247
+ Horns char(1),
248
+ Litter_Size char(1),
249
+ Coeff_of_Inbreeding real,
250
+ Date_of_Registration text,
251
+ Date_Last_Changed text,
252
+ UNIQUE(Sheep_No));
253
+ CREATE INDEX fo_flock_no_index
254
+ ON flock_owner (flock_no);
255
+ CREATE INDEX fo_owner_change_date_index
256
+ ON flock_owner (owner_change_date);
257
+ CREATE INDEX fo_owner_person_id_index
258
+ ON flock_owner (owner_person_id);
259
+ CREATE INDEX sheep_org_flock_index
260
+ ON sheep (originating_flock);
261
+ CREATE INDEX sheep_reg_flock_index
262
+ ON sheep (registering_flock);
263
+ }
264
+ do_eqp_test autoindex1-600a {
265
+ SELECT x.sheep_no, x.registering_flock, x.date_of_registration
266
+ FROM sheep x LEFT JOIN
267
+ (SELECT s.sheep_no, prev.flock_no, prev.owner_person_id,
268
+ s.date_of_registration, prev.owner_change_date
269
+ FROM sheep s JOIN flock_owner prev ON s.registering_flock =
270
+ prev.flock_no
271
+ AND (prev.owner_change_date <= s.date_of_registration || ' 00:00:00')
272
+ WHERE NOT EXISTS
273
+ (SELECT 'x' FROM flock_owner later
274
+ WHERE prev.flock_no = later.flock_no
275
+ AND later.owner_change_date > prev.owner_change_date
276
+ AND later.owner_change_date <= s.date_of_registration||' 00:00:00')
277
+ ) y ON x.sheep_no = y.sheep_no
278
+ WHERE y.sheep_no IS NULL
279
+ ORDER BY x.registering_flock;
280
+ } {
281
+ QUERY PLAN
282
+ |--MATERIALIZE y
283
+ | |--SCAN s
284
+ | |--SEARCH prev USING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date<?)
285
+ | `--CORRELATED SCALAR SUBQUERY xxxxxx
286
+ | `--SEARCH later USING COVERING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date>? AND owner_change_date<?)
287
+ |--SCAN x USING INDEX sheep_reg_flock_index
288
+ `--SEARCH y USING AUTOMATIC COVERING INDEX (sheep_no=?) LEFT-JOIN
289
+ }
290
+
291
+
292
+ do_execsql_test autoindex1-700 {
293
+ CREATE TABLE t5(a, b, c);
294
+ }
295
+ do_eqp_test autoindex1-700a {
296
+ SELECT a FROM t5 WHERE b=10 ORDER BY c;
297
+ } {
298
+ QUERY PLAN
299
+ |--SCAN t5
300
+ `--USE TEMP B-TREE FOR ORDER BY
301
+ }
302
+
303
+ # The following checks a performance issue reported on the sqlite-dev
304
+ # mailing list on 2013-01-10
305
+ #
306
+ do_execsql_test autoindex1-800 {
307
+ CREATE TABLE accounts(
308
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
309
+ account_name TEXT,
310
+ account_type TEXT,
311
+ data_set TEXT
312
+ );
313
+ CREATE TABLE data(
314
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
315
+ package_id INTEGER REFERENCES package(_id),
316
+ mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
317
+ raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
318
+ is_read_only INTEGER NOT NULL DEFAULT 0,
319
+ is_primary INTEGER NOT NULL DEFAULT 0,
320
+ is_super_primary INTEGER NOT NULL DEFAULT 0,
321
+ data_version INTEGER NOT NULL DEFAULT 0,
322
+ data1 TEXT,
323
+ data2 TEXT,
324
+ data3 TEXT,
325
+ data4 TEXT,
326
+ data5 TEXT,
327
+ data6 TEXT,
328
+ data7 TEXT,
329
+ data8 TEXT,
330
+ data9 TEXT,
331
+ data10 TEXT,
332
+ data11 TEXT,
333
+ data12 TEXT,
334
+ data13 TEXT,
335
+ data14 TEXT,
336
+ data15 TEXT,
337
+ data_sync1 TEXT,
338
+ data_sync2 TEXT,
339
+ data_sync3 TEXT,
340
+ data_sync4 TEXT
341
+ );
342
+ CREATE TABLE mimetypes(
343
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
344
+ mimetype TEXT NOT NULL
345
+ );
346
+ CREATE TABLE raw_contacts(
347
+ _id INTEGER PRIMARY KEY AUTOINCREMENT,
348
+ account_id INTEGER REFERENCES accounts(_id),
349
+ sourceid TEXT,
350
+ raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0,
351
+ version INTEGER NOT NULL DEFAULT 1,
352
+ dirty INTEGER NOT NULL DEFAULT 0,
353
+ deleted INTEGER NOT NULL DEFAULT 0,
354
+ contact_id INTEGER REFERENCES contacts(_id),
355
+ aggregation_mode INTEGER NOT NULL DEFAULT 0,
356
+ aggregation_needed INTEGER NOT NULL DEFAULT 1,
357
+ custom_ringtone TEXT,
358
+ send_to_voicemail INTEGER NOT NULL DEFAULT 0,
359
+ times_contacted INTEGER NOT NULL DEFAULT 0,
360
+ last_time_contacted INTEGER,
361
+ starred INTEGER NOT NULL DEFAULT 0,
362
+ display_name TEXT,
363
+ display_name_alt TEXT,
364
+ display_name_source INTEGER NOT NULL DEFAULT 0,
365
+ phonetic_name TEXT,
366
+ phonetic_name_style TEXT,
367
+ sort_key TEXT,
368
+ sort_key_alt TEXT,
369
+ name_verified INTEGER NOT NULL DEFAULT 0,
370
+ sync1 TEXT,
371
+ sync2 TEXT,
372
+ sync3 TEXT,
373
+ sync4 TEXT,
374
+ sync_uid TEXT,
375
+ sync_version INTEGER NOT NULL DEFAULT 1,
376
+ has_calendar_event INTEGER NOT NULL DEFAULT 0,
377
+ modified_time INTEGER,
378
+ is_restricted INTEGER DEFAULT 0,
379
+ yp_source TEXT,
380
+ method_selected INTEGER DEFAULT 0,
381
+ custom_vibration_type INTEGER DEFAULT 0,
382
+ custom_ringtone_path TEXT,
383
+ message_notification TEXT,
384
+ message_notification_path TEXT
385
+ );
386
+ CREATE INDEX data_mimetype_data1_index ON data (mimetype_id,data1);
387
+ CREATE INDEX data_raw_contact_id ON data (raw_contact_id);
388
+ CREATE UNIQUE INDEX mime_type ON mimetypes (mimetype);
389
+ CREATE INDEX raw_contact_sort_key1_index ON raw_contacts (sort_key);
390
+ CREATE INDEX raw_contact_sort_key2_index ON raw_contacts (sort_key_alt);
391
+ CREATE INDEX raw_contacts_contact_id_index ON raw_contacts (contact_id);
392
+ CREATE INDEX raw_contacts_source_id_account_id_index
393
+ ON raw_contacts (sourceid, account_id);
394
+ ANALYZE sqlite_master;
395
+ INSERT INTO sqlite_stat1
396
+ VALUES('raw_contacts','raw_contact_sort_key2_index','1600 4');
397
+ INSERT INTO sqlite_stat1
398
+ VALUES('raw_contacts','raw_contact_sort_key1_index','1600 4');
399
+ INSERT INTO sqlite_stat1
400
+ VALUES('raw_contacts','raw_contacts_source_id_account_id_index',
401
+ '1600 1600 1600');
402
+ INSERT INTO sqlite_stat1
403
+ VALUES('raw_contacts','raw_contacts_contact_id_index','1600 1');
404
+ INSERT INTO sqlite_stat1 VALUES('mimetypes','mime_type','12 1');
405
+ INSERT INTO sqlite_stat1
406
+ VALUES('data','data_mimetype_data1_index','9819 2455 3');
407
+ INSERT INTO sqlite_stat1 VALUES('data','data_raw_contact_id','9819 7');
408
+ INSERT INTO sqlite_stat1 VALUES('accounts',NULL,'1');
409
+ DROP TABLE IF EXISTS sqlite_stat3;
410
+ ANALYZE sqlite_master;
411
+
412
+ EXPLAIN QUERY PLAN
413
+ SELECT * FROM
414
+ data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
415
+ JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
416
+ JOIN accounts ON (raw_contacts.account_id=accounts._id)
417
+ WHERE mimetype_id=10 AND data14 IS NOT NULL;
418
+ } {/SEARCH data .*SEARCH raw_contacts/}
419
+ do_execsql_test autoindex1-801 {
420
+ EXPLAIN QUERY PLAN
421
+ SELECT * FROM
422
+ data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
423
+ JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
424
+ JOIN accounts ON (raw_contacts.account_id=accounts._id)
425
+ WHERE mimetypes._id=10 AND data14 IS NOT NULL;
426
+ } {/SEARCH data .*SEARCH raw_contacts/}
427
+
428
+ # Another test case from an important user of SQLite. The key feature of
429
+ # this test is that the "aggindex" subquery should make use of an
430
+ # automatic index. If it does, the query is fast. If it does not, the
431
+ # query is deathly slow. It worked OK in 3.7.17 but started going slow
432
+ # with version 3.8.0. The problem was fixed for 3.8.7 by reducing the
433
+ # cost estimate for automatic indexes on views and subqueries.
434
+ #
435
+ db close
436
+ forcedelete test.db
437
+ sqlite3 db test.db
438
+ do_execsql_test autoindex1-900 {
439
+ CREATE TABLE messages (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, message_id, document_id BLOB, in_reply_to, remote_id INTEGER, sender INTEGER, subject_prefix, subject INTEGER, date_sent INTEGER, date_received INTEGER, date_created INTEGER, date_last_viewed INTEGER, mailbox INTEGER, remote_mailbox INTEGER, original_mailbox INTEGER, flags INTEGER, read, flagged, size INTEGER, color, encoding, type INTEGER, pad, conversation_id INTEGER DEFAULT -1, snippet TEXT DEFAULT NULL, fuzzy_ancestor INTEGER DEFAULT NULL, automated_conversation INTEGER DEFAULT 0, root_status INTEGER DEFAULT -1, conversation_position INTEGER DEFAULT -1);
440
+ CREATE INDEX date_index ON messages(date_received);
441
+ CREATE INDEX date_last_viewed_index ON messages(date_last_viewed);
442
+ CREATE INDEX date_created_index ON messages(date_created);
443
+ CREATE INDEX message_message_id_mailbox_index ON messages(message_id, mailbox);
444
+ CREATE INDEX message_document_id_index ON messages(document_id);
445
+ CREATE INDEX message_read_index ON messages(read);
446
+ CREATE INDEX message_flagged_index ON messages(flagged);
447
+ CREATE INDEX message_mailbox_index ON messages(mailbox, date_received);
448
+ CREATE INDEX message_remote_mailbox_index ON messages(remote_mailbox, remote_id);
449
+ CREATE INDEX message_type_index ON messages(type);
450
+ CREATE INDEX message_conversation_id_conversation_position_index ON messages(conversation_id, conversation_position);
451
+ CREATE INDEX message_fuzzy_ancestor_index ON messages(fuzzy_ancestor);
452
+ CREATE INDEX message_subject_fuzzy_ancestor_index ON messages(subject, fuzzy_ancestor);
453
+ CREATE INDEX message_sender_subject_automated_conversation_index ON messages(sender, subject, automated_conversation);
454
+ CREATE INDEX message_sender_index ON messages(sender);
455
+ CREATE INDEX message_root_status ON messages(root_status);
456
+ CREATE TABLE subjects (ROWID INTEGER PRIMARY KEY, subject COLLATE RTRIM, normalized_subject COLLATE RTRIM);
457
+ CREATE INDEX subject_subject_index ON subjects(subject);
458
+ CREATE INDEX subject_normalized_subject_index ON subjects(normalized_subject);
459
+ CREATE TABLE addresses (ROWID INTEGER PRIMARY KEY, address COLLATE NOCASE, comment, UNIQUE(address, comment));
460
+ CREATE INDEX addresses_address_index ON addresses(address);
461
+ CREATE TABLE mailboxes (ROWID INTEGER PRIMARY KEY, url UNIQUE, total_count INTEGER DEFAULT 0, unread_count INTEGER DEFAULT 0, unseen_count INTEGER DEFAULT 0, deleted_count INTEGER DEFAULT 0, unread_count_adjusted_for_duplicates INTEGER DEFAULT 0, change_identifier, source INTEGER, alleged_change_identifier);
462
+ CREATE INDEX mailboxes_source_index ON mailboxes(source);
463
+ CREATE TABLE labels (ROWID INTEGER PRIMARY KEY, message_id INTEGER NOT NULL, mailbox_id INTEGER NOT NULL, UNIQUE(message_id, mailbox_id));
464
+ CREATE INDEX labels_message_id_mailbox_id_index ON labels(message_id, mailbox_id);
465
+ CREATE INDEX labels_mailbox_id_index ON labels(mailbox_id);
466
+
467
+ explain query plan
468
+ SELECT messages.ROWID,
469
+ messages.message_id,
470
+ messages.remote_id,
471
+ messages.date_received,
472
+ messages.date_sent,
473
+ messages.flags,
474
+ messages.size,
475
+ messages.color,
476
+ messages.date_last_viewed,
477
+ messages.subject_prefix,
478
+ subjects.subject,
479
+ sender.comment,
480
+ sender.address,
481
+ NULL,
482
+ messages.mailbox,
483
+ messages.original_mailbox,
484
+ NULL,
485
+ NULL,
486
+ messages.type,
487
+ messages.document_id,
488
+ sender,
489
+ NULL,
490
+ messages.conversation_id,
491
+ messages.conversation_position,
492
+ agglabels.labels
493
+ FROM mailboxes AS mailbox
494
+ JOIN messages ON mailbox.ROWID = messages.mailbox
495
+ LEFT OUTER JOIN subjects ON messages.subject = subjects.ROWID
496
+ LEFT OUTER JOIN addresses AS sender ON messages.sender = sender.ROWID
497
+ LEFT OUTER JOIN (
498
+ SELECT message_id, group_concat(mailbox_id) as labels
499
+ FROM labels GROUP BY message_id
500
+ ) AS agglabels ON messages.ROWID = agglabels.message_id
501
+ WHERE (mailbox.url = 'imap://email.app@imap.gmail.com/%5BGmail%5D/All%20Mail')
502
+ AND (messages.ROWID IN (
503
+ SELECT labels.message_id
504
+ FROM labels JOIN mailboxes ON labels.mailbox_id = mailboxes.ROWID
505
+ WHERE mailboxes.url = 'imap://email.app@imap.gmail.com/INBOX'))
506
+ AND messages.mailbox in (6,12,18,24,30,36,42,1,7,13,19,25,31,37,43,2,8,
507
+ 14,20,26,32,38,3,9,15,21,27,33,39,4,10,16,22,28,
508
+ 34,40,5,11,17,23,35,41)
509
+ ORDER BY date_received DESC;
510
+ } {/agglabels USING AUTOMATIC COVERING INDEX/}
511
+
512
+ # A test case for VIEWs
513
+ #
514
+ do_execsql_test autoindex1-901 {
515
+ CREATE TABLE t1(x INTEGER PRIMARY KEY, y, z);
516
+ CREATE TABLE t2(a, b);
517
+ CREATE VIEW agg2 AS SELECT a, sum(b) AS m FROM t2 GROUP BY a;
518
+ EXPLAIN QUERY PLAN
519
+ SELECT t1.z, agg2.m
520
+ FROM t1 JOIN agg2 ON t1.y=agg2.m
521
+ WHERE t1.x IN (1,2,3);
522
+ } {/USING AUTOMATIC COVERING INDEX/}
523
+
524
+ # 2015-04-15: A NULL CollSeq pointer in automatic index creation.
525
+ #
526
+ do_execsql_test autoindex1-920 {
527
+ CREATE TABLE t920(x);
528
+ INSERT INTO t920 VALUES(3),(4),(5);
529
+ SELECT * FROM t920,(SELECT 0 FROM t920),(VALUES(9)) WHERE 5 IN (x);
530
+ } {5 0 9 5 0 9 5 0 9}
531
+
532
+ #-------------------------------------------------------------------------
533
+ # An IS term from the WHERE clause of a LEFT JOIN cannot be used as an
534
+ # index driver for the RHS of a LEFT JOIN. Prior to this being fixed,
535
+ # the following SELECT count(*) would incorrectly return 1.
536
+ #
537
+ do_execsql_test autoindex1-1010 {
538
+ CREATE TABLE t11(w);
539
+ CREATE TABLE t12(y);
540
+ INSERT INTO t11 VALUES(NULL);
541
+ INSERT INTO t12 VALUES('notnull');
542
+ }
543
+ do_execsql_test autoindex1-1020 {
544
+ SELECT count(*) FROM t11 LEFT JOIN t12 WHERE t12.y IS t11.w;
545
+ } 0
546
+
547
+ # 2022-04-25
548
+ # https://sqlite.org/forum/forumpost/0d3200f4f3bcd3a3
549
+ #
550
+ reset_db
551
+ do_execsql_test autoindex-1100 {
552
+ CREATE TABLE t1(a INT, b INT);
553
+ CREATE TABLE t2(c INT, d INT);
554
+ CREATE TABLE t3(e TEXT, f TEXT);
555
+ INSERT INTO t1 VALUES(1, 1);
556
+ INSERT INTO t2 VALUES(1, 2);
557
+ INSERT INTO t3 VALUES('abc', 'def');
558
+ } {}
559
+ do_execsql_test autoindex-1110 {
560
+ SELECT * FROM t1, t2 LEFT JOIN t3 ON (t2.d=1) WHERE t2.c = +t1.a;
561
+ } {1 1 1 2 {} {}}
562
+ do_execsql_test autoindex-1120 {
563
+ SELECT * FROM t1 LEFT JOIN t2 ON (t2.c=+t1.a) LEFT JOIN t3 ON (t2.d IS NULL);
564
+ } {1 1 1 2 {} {}}
565
+
566
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/autoindex5.test ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2014-10-24
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #*************************************************************************
11
+ #
12
+ # This file implements regression tests for SQLite library. The
13
+ # focus of this script is testing automatic index creation logic,
14
+ # and specifically ensuring that automatic indexes can be used with
15
+ # co-routine subqueries.
16
+ #
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+ set testprefix autoindex5
21
+
22
+ # Schema is from the Debian security database
23
+ #
24
+ do_execsql_test autoindex5-1.0 {
25
+ CREATE TABLE source_package_status
26
+ (bug_name TEXT NOT NULL,
27
+ package INTEGER NOT NULL,
28
+ vulnerable INTEGER NOT NULL,
29
+ urgency TEXT NOT NULL,
30
+ PRIMARY KEY (bug_name, package));
31
+ CREATE INDEX source_package_status_package
32
+ ON source_package_status(package);
33
+
34
+ CREATE TABLE source_packages
35
+ (name TEXT NOT NULL,
36
+ release TEXT NOT NULL,
37
+ subrelease TEXT NOT NULL,
38
+ archive TEXT NOT NULL,
39
+ version TEXT NOT NULL,
40
+ version_id INTEGER NOT NULL DEFAULT 0,
41
+ PRIMARY KEY (name, release, subrelease, archive));
42
+
43
+ CREATE TABLE bugs
44
+ (name TEXT NOT NULL PRIMARY KEY,
45
+ cve_status TEXT NOT NULL
46
+ CHECK (cve_status IN
47
+ ('', 'CANDIDATE', 'ASSIGNED', 'RESERVED', 'REJECTED')),
48
+ not_for_us INTEGER NOT NULL CHECK (not_for_us IN (0, 1)),
49
+ description TEXT NOT NULL,
50
+ release_date TEXT NOT NULL,
51
+ source_file TEXT NOT NULL,
52
+ source_line INTEGER NOT NULL);
53
+
54
+ CREATE TABLE package_notes
55
+ (id INTEGER NOT NULL PRIMARY KEY,
56
+ bug_name TEXT NOT NULL,
57
+ package TEXT NOT NULL,
58
+ fixed_version TEXT
59
+ CHECK (fixed_version IS NULL OR fixed_version <> ''),
60
+ fixed_version_id INTEGER NOT NULL DEFAULT 0,
61
+ release TEXT NOT NULL,
62
+ package_kind TEXT NOT NULL DEFAULT 'unknown',
63
+ urgency TEXT NOT NULL,
64
+ bug_origin TEXT NOT NULL DEFAULT '');
65
+ CREATE INDEX package_notes_package
66
+ ON package_notes(package);
67
+ CREATE UNIQUE INDEX package_notes_bug
68
+ ON package_notes(bug_name, package, release);
69
+
70
+ CREATE TABLE debian_bugs
71
+ (bug INTEGER NOT NULL,
72
+ note INTEGER NOT NULL,
73
+ PRIMARY KEY (bug, note));
74
+
75
+
76
+ CREATE VIEW debian_cve AS
77
+ SELECT DISTINCT debian_bugs.bug, st.bug_name
78
+ FROM package_notes, debian_bugs, source_package_status AS st
79
+ WHERE package_notes.bug_name = st.bug_name
80
+ AND debian_bugs.note = package_notes.id
81
+ ORDER BY debian_bugs.bug;
82
+ } {}
83
+
84
+ # The following query should use an automatic index for the view
85
+ # in FROM clause of the subquery of the second result column.
86
+ #
87
+ do_eqp_test autoindex5-1.1 {
88
+ SELECT
89
+ st.bug_name,
90
+ (SELECT ALL debian_cve.bug FROM debian_cve
91
+ WHERE debian_cve.bug_name = st.bug_name
92
+ ORDER BY debian_cve.bug),
93
+ sp.release
94
+ FROM
95
+ source_package_status AS st,
96
+ source_packages AS sp,
97
+ bugs
98
+ WHERE
99
+ sp.rowid = st.package
100
+ AND st.bug_name = bugs.name
101
+ AND ( st.bug_name LIKE 'CVE-%' OR st.bug_name LIKE 'TEMP-%' )
102
+ AND ( sp.release = 'sid' OR sp.release = 'stretch' OR sp.release = 'jessie'
103
+ OR sp.release = 'wheezy' OR sp.release = 'squeeze' )
104
+ ORDER BY sp.name, st.bug_name, sp.release, sp.subrelease;
105
+ } {SEARCH debian_cve USING AUTOMATIC COVERING INDEX (bug_name=?)}
106
+
107
+ #-------------------------------------------------------------------------
108
+ # Test that ticket [8a2adec1] has been fixed.
109
+ #
110
+ do_execsql_test 2.1 {
111
+ CREATE TABLE one(o);
112
+ INSERT INTO one DEFAULT VALUES;
113
+
114
+ CREATE TABLE t1(x, z);
115
+ INSERT INTO t1 VALUES('aaa', 4.0);
116
+ INSERT INTO t1 VALUES('aaa', 4.0);
117
+ CREATE VIEW vvv AS
118
+ SELECT * FROM t1
119
+ UNION ALL
120
+ SELECT 0, 0 WHERE 0;
121
+
122
+ SELECT (
123
+ SELECT sum(z) FROM vvv WHERE x='aaa'
124
+ ) FROM one;
125
+ } {8.0}
126
+
127
+ # At one point the following was returning "no such column: rowid". This
128
+ # was incorrect - "rowid" matches against the rowid of table t1 in this
129
+ # query.
130
+ do_catchsql_test 2.2 {
131
+ DROP TABLE t1;
132
+ CREATE TABLE t1(aaa);
133
+ INSERT INTO t1(aaa) VALUES(9);
134
+ SELECT (
135
+ SELECT aaa FROM t1 GROUP BY (
136
+ SELECT bbb FROM (
137
+ SELECT ccc AS bbb FROM (
138
+ SELECT 1 ccc
139
+ ) WHERE rowid IS NOT 1
140
+ ) WHERE bbb = 1
141
+ )
142
+ );
143
+ } {0 9}
144
+
145
+ # Ticket https://www.sqlite.org/src/info/787fa716be3a7f65
146
+ # Segfault due to multiple uses of the same subquery where the
147
+ # subquery is implemented via coroutine.
148
+ #
149
+ ifcapable windowfunc {
150
+ sqlite3 db :memory:
151
+ do_execsql_test 3.0 {
152
+ -- This is the original test case reported on the mailing list
153
+ CREATE TABLE artists (
154
+ id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
155
+ name varchar(255)
156
+ );
157
+ CREATE TABLE albums (
158
+ id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
159
+ name varchar(255),
160
+ artist_id integer REFERENCES artists
161
+ );
162
+ INSERT INTO artists (name) VALUES ('Ar');
163
+ INSERT INTO albums (name, artist_id) VALUES ('Al', 1);
164
+ SELECT artists.*
165
+ FROM artists
166
+ INNER JOIN artists AS 'b' ON (b.id = artists.id)
167
+ WHERE (artists.id IN (
168
+ SELECT albums.artist_id
169
+ FROM albums
170
+ WHERE ((name = 'Al')
171
+ AND (albums.artist_id IS NOT NULL)
172
+ AND (albums.id IN (
173
+ SELECT id
174
+ FROM (
175
+ SELECT albums.id,
176
+ row_number() OVER (
177
+ PARTITION BY albums.artist_id
178
+ ORDER BY name
179
+ ) AS 'x'
180
+ FROM albums
181
+ WHERE (name = 'Al')
182
+ ) AS 't1'
183
+ WHERE (x = 1)
184
+ ))
185
+ AND (albums.id IN (1, 2)))
186
+ ));
187
+ } {1 Ar}
188
+ } ;# windowfunc
189
+
190
+ # The remaining test cases were discovered (by Dan) during trouble-shooting
191
+ sqlite3 db :memory:
192
+ do_execsql_test 3.1 {
193
+ CREATE TABLE t1 (a); INSERT INTO t1 (a) VALUES (104);
194
+ CREATE TABLE t2 (b); INSERT INTO t2 (b) VALUES (104);
195
+ CREATE TABLE t3 (c); INSERT INTO t3 (c) VALUES (104);
196
+ CREATE TABLE t4 (d); INSERT INTO t4 (d) VALUES (104);
197
+ SELECT *
198
+ FROM t1 CROSS JOIN t2 ON (t1.a = t2.b) WHERE t2.b IN (
199
+ SELECT t3.c
200
+ FROM t3
201
+ WHERE t3.c IN (
202
+ SELECT d FROM (SELECT DISTINCT d FROM t4) AS x WHERE x.d=104
203
+ )
204
+ );
205
+ } {104 104}
206
+ sqlite3 db :memory:
207
+ do_execsql_test 3.2 {
208
+ CREATE TABLE t5(a, b, c, d);
209
+ CREATE INDEX t5a ON t5(a);
210
+ CREATE INDEX t5b ON t5(b);
211
+ CREATE TABLE t6(e);
212
+ INSERT INTO t6 VALUES(1);
213
+ INSERT INTO t5 VALUES(1,1,1,1), (2,2,2,2);
214
+ SELECT * FROM t5 WHERE (a=1 OR b=2) AND c IN (
215
+ SELECT e FROM (SELECT DISTINCT e FROM t6) WHERE e=1
216
+ );
217
+ } {1 1 1 1}
218
+ sqlite3 db :memory:
219
+ do_execsql_test 3.3 {
220
+ CREATE TABLE t1(a1, a2, a3);
221
+ CREATE INDEX t1a2 ON t1(a2, a1);
222
+ CREATE INDEX t1a3 ON t1(a3, a1);
223
+ CREATE TABLE t2(d);
224
+ INSERT INTO t1 VALUES(3, 1, 1), (3, 2, 2);
225
+ INSERT INTO t2 VALUES(3);
226
+ SELECT *, 'x' FROM t1 WHERE (a2=1 OR a3=2) AND a1 = (
227
+ SELECT d FROM (SELECT DISTINCT d FROM t2) WHERE d=3
228
+ );
229
+ } {3 1 1 x 3 2 2 x}
230
+
231
+
232
+
233
+
234
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/autovacuum.test ADDED
@@ -0,0 +1,715 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2001 September 15
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is testing the autovacuum feature.
13
+ #
14
+
15
+ set testdir [file dirname $argv0]
16
+ source $testdir/tester.tcl
17
+
18
+ # If this build of the library does not support auto-vacuum, omit this
19
+ # whole file.
20
+ ifcapable {!autovacuum || !pragma} {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ # Return a string $len characters long. The returned string is $char repeated
26
+ # over and over. For example, [make_str abc 8] returns "abcabcab".
27
+ proc make_str {char len} {
28
+ set str [string repeat $char. $len]
29
+ return [string range $str 0 [expr $len-1]]
30
+ }
31
+
32
+ # Return the number of pages in the file test.db by looking at the file system.
33
+ proc file_pages {} {
34
+ return [expr [file size test.db] / 1024]
35
+ }
36
+
37
+ #-------------------------------------------------------------------------
38
+ # Test cases autovacuum-1.* work as follows:
39
+ #
40
+ # 1. A table with a single indexed field is created.
41
+ # 2. Approximately 20 rows are inserted into the table. Each row is long
42
+ # enough such that it uses at least 2 overflow pages for both the table
43
+ # and index entry.
44
+ # 3. The rows are deleted in a psuedo-random order. Sometimes only one row
45
+ # is deleted per transaction, sometimes more than one.
46
+ # 4. After each transaction the table data is checked to ensure it is correct
47
+ # and a "PRAGMA integrity_check" is executed.
48
+ # 5. Once all the rows are deleted the file is checked to make sure it
49
+ # consists of exactly 4 pages.
50
+ #
51
+ # Steps 2-5 are repeated for a few different psuedo-random delete patterns
52
+ # (defined by the $delete_orders list).
53
+ set delete_orders [list]
54
+ lappend delete_orders {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
55
+ lappend delete_orders {20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1}
56
+ lappend delete_orders {8 18 2 4 14 11 13 3 10 7 9 5 12 17 19 15 20 6 16 1}
57
+ lappend delete_orders {10 3 11 17 19 20 7 4 13 6 1 14 16 12 9 18 8 15 5 2}
58
+ lappend delete_orders {{1 2 3 4 5 6 7 8 9 10} {11 12 13 14 15 16 17 18 19 20}}
59
+ lappend delete_orders {{19 8 17 15} {16 11 9 14} {18 5 3 1} {13 20 7 2} {6 12}}
60
+
61
+ # The length of each table entry.
62
+ # set ENTRY_LEN 3500
63
+ set ENTRY_LEN 3500
64
+
65
+ do_test autovacuum-1.1 {
66
+ execsql {
67
+ PRAGMA auto_vacuum = 1;
68
+ CREATE TABLE av1(a);
69
+ CREATE INDEX av1_idx ON av1(a);
70
+ }
71
+ } {}
72
+
73
+ set tn 0
74
+ foreach delete_order $delete_orders {
75
+ incr tn
76
+
77
+ # Set up the table.
78
+ set ::tbl_data [list]
79
+ foreach i [lsort -integer [eval concat $delete_order]] {
80
+ execsql "INSERT INTO av1 (oid, a) VALUES($i, '[make_str $i $ENTRY_LEN]')"
81
+ lappend ::tbl_data [make_str $i $ENTRY_LEN]
82
+ }
83
+
84
+ # Make sure the integrity check passes with the initial data.
85
+ ifcapable {integrityck} {
86
+ do_test autovacuum-1.$tn.1 {
87
+ execsql {
88
+ pragma integrity_check
89
+ }
90
+ } {ok}
91
+ }
92
+
93
+ foreach delete $delete_order {
94
+ # Delete one set of rows from the table.
95
+ do_test autovacuum-1.$tn.($delete).1 {
96
+ execsql "
97
+ DELETE FROM av1 WHERE oid = [join $delete " OR oid = "]
98
+ "
99
+ } {}
100
+
101
+ # Do the integrity check.
102
+ ifcapable {integrityck} {
103
+ do_test autovacuum-1.$tn.($delete).2 {
104
+ execsql {
105
+ pragma integrity_check
106
+ }
107
+ } {ok}
108
+ }
109
+ # Ensure the data remaining in the table is what was expected.
110
+ foreach d $delete {
111
+ set idx [lsearch $::tbl_data [make_str $d $ENTRY_LEN]]
112
+ set ::tbl_data [lreplace $::tbl_data $idx $idx]
113
+ }
114
+ do_test autovacuum-1.$tn.($delete).3 {
115
+ execsql {
116
+ select a from av1 order by rowid
117
+ }
118
+ } $::tbl_data
119
+ }
120
+
121
+ # All rows have been deleted. Ensure the file has shrunk to 4 pages.
122
+ do_test autovacuum-1.$tn.3 {
123
+ file_pages
124
+ } {4}
125
+ }
126
+
127
+ #---------------------------------------------------------------------------
128
+ # Tests cases autovacuum-2.* test that root pages are allocated
129
+ # and deallocated correctly at the start of the file. Operation is roughly as
130
+ # follows:
131
+ #
132
+ # autovacuum-2.1.*: Drop the tables that currently exist in the database.
133
+ # autovacuum-2.2.*: Create some tables. Ensure that data pages can be
134
+ # moved correctly to make space for new root-pages.
135
+ # autovacuum-2.3.*: Drop one of the tables just created (not the last one),
136
+ # and check that one of the other tables is moved to
137
+ # the free root-page location.
138
+ # autovacuum-2.4.*: Check that a table can be created correctly when the
139
+ # root-page it requires is on the free-list.
140
+ # autovacuum-2.5.*: Check that a table with indices can be dropped. This
141
+ # is slightly tricky because dropping one of the
142
+ # indices/table btrees could move the root-page of another.
143
+ # The code-generation layer of SQLite overcomes this problem
144
+ # by dropping the btrees in descending order of root-pages.
145
+ # This test ensures that this actually happens.
146
+ #
147
+ do_test autovacuum-2.1.1 {
148
+ execsql {
149
+ DROP TABLE av1;
150
+ }
151
+ } {}
152
+ do_test autovacuum-2.1.2 {
153
+ file_pages
154
+ } {1}
155
+
156
+ # Create a table and put some data in it.
157
+ do_test autovacuum-2.2.1 {
158
+ execsql {
159
+ CREATE TABLE av1(x);
160
+ SELECT rootpage FROM sqlite_master ORDER BY rootpage;
161
+ }
162
+ } {3}
163
+ do_test autovacuum-2.2.2 {
164
+ execsql "
165
+ INSERT INTO av1 VALUES('[make_str abc 3000]');
166
+ INSERT INTO av1 VALUES('[make_str def 3000]');
167
+ INSERT INTO av1 VALUES('[make_str ghi 3000]');
168
+ INSERT INTO av1 VALUES('[make_str jkl 3000]');
169
+ "
170
+ set ::av1_data [db eval {select * from av1}]
171
+ file_pages
172
+ } {15}
173
+
174
+ # Create another table. Check it is located immediately after the first.
175
+ # This test case moves the second page in an over-flow chain.
176
+ do_test autovacuum-2.2.3 {
177
+ execsql {
178
+ CREATE TABLE av2(x);
179
+ SELECT rootpage FROM sqlite_master ORDER BY rootpage;
180
+ }
181
+ } {3 4}
182
+ do_test autovacuum-2.2.4 {
183
+ file_pages
184
+ } {16}
185
+
186
+ # Create another table. Check it is located immediately after the second.
187
+ # This test case moves the first page in an over-flow chain.
188
+ do_test autovacuum-2.2.5 {
189
+ execsql {
190
+ CREATE TABLE av3(x);
191
+ SELECT rootpage FROM sqlite_master ORDER BY rootpage;
192
+ }
193
+ } {3 4 5}
194
+ do_test autovacuum-2.2.6 {
195
+ file_pages
196
+ } {17}
197
+
198
+ # Create another table. Check it is located immediately after the second.
199
+ # This test case moves a btree leaf page.
200
+ do_test autovacuum-2.2.7 {
201
+ execsql {
202
+ CREATE TABLE av4(x);
203
+ SELECT rootpage FROM sqlite_master ORDER BY rootpage;
204
+ }
205
+ } {3 4 5 6}
206
+ do_test autovacuum-2.2.8 {
207
+ file_pages
208
+ } {18}
209
+ do_test autovacuum-2.2.9 {
210
+ execsql {
211
+ select * from av1
212
+ }
213
+ } $av1_data
214
+
215
+ do_test autovacuum-2.3.1 {
216
+ execsql {
217
+ INSERT INTO av2 SELECT 'av1' || x FROM av1;
218
+ INSERT INTO av3 SELECT 'av2' || x FROM av1;
219
+ INSERT INTO av4 SELECT 'av3' || x FROM av1;
220
+ }
221
+ set ::av2_data [execsql {select x from av2}]
222
+ set ::av3_data [execsql {select x from av3}]
223
+ set ::av4_data [execsql {select x from av4}]
224
+ file_pages
225
+ } {54}
226
+ do_test autovacuum-2.3.2 {
227
+ execsql {
228
+ DROP TABLE av2;
229
+ SELECT rootpage FROM sqlite_master ORDER BY rootpage;
230
+ }
231
+ } {3 4 5}
232
+ do_test autovacuum-2.3.3 {
233
+ file_pages
234
+ } {41}
235
+ do_test autovacuum-2.3.4 {
236
+ execsql {
237
+ SELECT x FROM av3;
238
+ }
239
+ } $::av3_data
240
+ do_test autovacuum-2.3.5 {
241
+ execsql {
242
+ SELECT x FROM av4;
243
+ }
244
+ } $::av4_data
245
+
246
+ # Drop all the tables in the file. This puts all pages except the first 2
247
+ # (the sqlite_master root-page and the first pointer map page) on the
248
+ # free-list.
249
+ do_test autovacuum-2.4.1 {
250
+ execsql {
251
+ DROP TABLE av1;
252
+ DROP TABLE av3;
253
+ BEGIN;
254
+ DROP TABLE av4;
255
+ }
256
+ file_pages
257
+ } {15}
258
+ do_test autovacuum-2.4.2 {
259
+ for {set i 3} {$i<=10} {incr i} {
260
+ execsql "CREATE TABLE av$i (x)"
261
+ }
262
+ file_pages
263
+ } {15}
264
+ do_test autovacuum-2.4.3 {
265
+ execsql {
266
+ SELECT rootpage FROM sqlite_master ORDER by rootpage
267
+ }
268
+ } {3 4 5 6 7 8 9 10}
269
+
270
+ # Right now there are 5 free pages in the database. Consume and then free
271
+ # all 520 pages. Then create 520 tables. This ensures that at least some of the
272
+ # desired root-pages reside on the second free-list trunk page, and that the
273
+ # trunk itself is required at some point.
274
+ do_test autovacuum-2.4.4 {
275
+ execsql "
276
+ INSERT INTO av3 VALUES ('[make_str abcde [expr 1020*520 + 500]]');
277
+ DELETE FROM av3;
278
+ "
279
+ } {}
280
+ set root_page_list [list]
281
+ set pending_byte_page [expr ($::sqlite_pending_byte / 1024) + 1]
282
+
283
+ # unusable_pages
284
+ # These are either the pending_byte page or the pointer map pages
285
+ #
286
+ unset -nocomplain unusable_page
287
+ if {[sqlite3 -has-codec]} {
288
+ array set unusable_page {205 1 408 1}
289
+ } else {
290
+ array set unusable_page {207 1 412 1}
291
+ }
292
+ set unusable_page($pending_byte_page) 1
293
+
294
+ for {set i 3} {$i<=532} {incr i} {
295
+ if {![info exists unusable_page($i)]} {
296
+ lappend root_page_list $i
297
+ }
298
+ }
299
+ if {$i >= $pending_byte_page} {
300
+ lappend root_page_list $i
301
+ }
302
+ do_test autovacuum-2.4.5 {
303
+ for {set i 11} {$i<=530} {incr i} {
304
+ execsql "CREATE TABLE av$i (x)"
305
+ }
306
+ execsql {
307
+ SELECT rootpage FROM sqlite_master ORDER by rootpage
308
+ }
309
+ } $root_page_list
310
+
311
+ # Just for fun, delete all those tables and see if the database is 1 page.
312
+ do_test autovacuum-2.4.6 {
313
+ execsql COMMIT;
314
+ file_pages
315
+ } [expr 561 + (($i >= $pending_byte_page)?1:0)]
316
+ integrity_check autovacuum-2.4.6
317
+ do_test autovacuum-2.4.7 {
318
+ execsql BEGIN
319
+ for {set i 3} {$i<=530} {incr i} {
320
+ execsql "DROP TABLE av$i"
321
+ }
322
+ execsql COMMIT
323
+ file_pages
324
+ } 1
325
+
326
+ # Create some tables with indices to drop.
327
+ do_test autovacuum-2.5.1 {
328
+ execsql {
329
+ CREATE TABLE av1(a PRIMARY KEY, b, c);
330
+ INSERT INTO av1 VALUES('av1 a', 'av1 b', 'av1 c');
331
+
332
+ CREATE TABLE av2(a PRIMARY KEY, b, c);
333
+ CREATE INDEX av2_i1 ON av2(b);
334
+ CREATE INDEX av2_i2 ON av2(c);
335
+ INSERT INTO av2 VALUES('av2 a', 'av2 b', 'av2 c');
336
+
337
+ CREATE TABLE av3(a PRIMARY KEY, b, c);
338
+ CREATE INDEX av3_i1 ON av3(b);
339
+ INSERT INTO av3 VALUES('av3 a', 'av3 b', 'av3 c');
340
+
341
+ CREATE TABLE av4(a, b, c);
342
+ CREATE INDEX av4_i1 ON av4(a);
343
+ CREATE INDEX av4_i2 ON av4(b);
344
+ CREATE INDEX av4_i3 ON av4(c);
345
+ CREATE INDEX av4_i4 ON av4(a, b, c);
346
+ INSERT INTO av4 VALUES('av4 a', 'av4 b', 'av4 c');
347
+ }
348
+ } {}
349
+
350
+ do_test autovacuum-2.5.2 {
351
+ execsql {
352
+ SELECT name, rootpage FROM sqlite_master;
353
+ }
354
+ } [list av1 3 sqlite_autoindex_av1_1 4 \
355
+ av2 5 sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
356
+ av3 9 sqlite_autoindex_av3_1 10 av3_i1 11 \
357
+ av4 12 av4_i1 13 av4_i2 14 av4_i3 15 av4_i4 16 \
358
+ ]
359
+
360
+ # The following 4 tests are SELECT queries that use the indices created.
361
+ # If the root-pages in the internal schema are not updated correctly when
362
+ # a table or indice is moved, these queries will fail. They are repeated
363
+ # after each table is dropped (i.e. as test cases 2.5.*.[1..4]).
364
+ do_test autovacuum-2.5.2.1 {
365
+ execsql {
366
+ SELECT * FROM av1 WHERE a = 'av1 a';
367
+ }
368
+ } {{av1 a} {av1 b} {av1 c}}
369
+ do_test autovacuum-2.5.2.2 {
370
+ execsql {
371
+ SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
372
+ }
373
+ } {{av2 a} {av2 b} {av2 c}}
374
+ do_test autovacuum-2.5.2.3 {
375
+ execsql {
376
+ SELECT * FROM av3 WHERE a = 'av3 a' AND b = 'av3 b';
377
+ }
378
+ } {{av3 a} {av3 b} {av3 c}}
379
+ do_test autovacuum-2.5.2.4 {
380
+ execsql {
381
+ SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
382
+ }
383
+ } {{av4 a} {av4 b} {av4 c}}
384
+
385
+ # Drop table av3. Indices av4_i2, av4_i3 and av4_i4 are moved to fill the two
386
+ # root pages vacated. The operation proceeds as:
387
+ # Step 1: Delete av3_i1 (root-page 11). Move root-page of av4_i4 to page 11.
388
+ # Step 2: Delete av3 (root-page 10). Move root-page of av4_i3 to page 10.
389
+ # Step 3: Delete sqlite_autoindex_av1_3 (root-page 9). Move av4_i2 to page 9.
390
+ do_test autovacuum-2.5.3 {
391
+ execsql {
392
+ DROP TABLE av3;
393
+ SELECT name, rootpage FROM sqlite_master;
394
+ }
395
+ } [list av1 3 sqlite_autoindex_av1_1 4 \
396
+ av2 5 sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
397
+ av4 12 av4_i1 13 av4_i2 9 av4_i3 10 av4_i4 11 \
398
+ ]
399
+ do_test autovacuum-2.5.3.1 {
400
+ execsql {
401
+ SELECT * FROM av1 WHERE a = 'av1 a';
402
+ }
403
+ } {{av1 a} {av1 b} {av1 c}}
404
+ do_test autovacuum-2.5.3.2 {
405
+ execsql {
406
+ SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
407
+ }
408
+ } {{av2 a} {av2 b} {av2 c}}
409
+ do_test autovacuum-2.5.3.3 {
410
+ execsql {
411
+ SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
412
+ }
413
+ } {{av4 a} {av4 b} {av4 c}}
414
+
415
+ # Drop table av1:
416
+ # Step 1: Delete av1 (root page 4). Root-page of av4_i1 fills the gap.
417
+ # Step 2: Delete sqlite_autoindex_av1_1 (root page 3). Move av4 to the gap.
418
+ do_test autovacuum-2.5.4 {
419
+ execsql {
420
+ DROP TABLE av1;
421
+ SELECT name, rootpage FROM sqlite_master;
422
+ }
423
+ } [list av2 5 sqlite_autoindex_av2_1 6 av2_i1 7 av2_i2 8 \
424
+ av4 3 av4_i1 4 av4_i2 9 av4_i3 10 av4_i4 11 \
425
+ ]
426
+ do_test autovacuum-2.5.4.2 {
427
+ execsql {
428
+ SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
429
+ }
430
+ } {{av2 a} {av2 b} {av2 c}}
431
+ do_test autovacuum-2.5.4.4 {
432
+ execsql {
433
+ SELECT * FROM av4 WHERE a = 'av4 a' AND b = 'av4 b' AND c = 'av4 c';
434
+ }
435
+ } {{av4 a} {av4 b} {av4 c}}
436
+
437
+ # Drop table av4:
438
+ # Step 1: Delete av4_i4.
439
+ # Step 2: Delete av4_i3.
440
+ # Step 3: Delete av4_i2.
441
+ # Step 4: Delete av4_i1. av2_i2 replaces it.
442
+ # Step 5: Delete av4. av2_i1 replaces it.
443
+ do_test autovacuum-2.5.5 {
444
+ execsql {
445
+ DROP TABLE av4;
446
+ SELECT name, rootpage FROM sqlite_master;
447
+ }
448
+ } [list av2 5 sqlite_autoindex_av2_1 6 av2_i1 3 av2_i2 4]
449
+ do_test autovacuum-2.5.5.2 {
450
+ execsql {
451
+ SELECT * FROM av2 WHERE a = 'av2 a' AND b = 'av2 b' AND c = 'av2 c'
452
+ }
453
+ } {{av2 a} {av2 b} {av2 c}}
454
+
455
+ #--------------------------------------------------------------------------
456
+ # Test cases autovacuum-3.* test the operation of the "PRAGMA auto_vacuum"
457
+ # command.
458
+ #
459
+ do_test autovacuum-3.1 {
460
+ execsql {
461
+ PRAGMA auto_vacuum;
462
+ }
463
+ } {1}
464
+ do_test autovacuum-3.2 {
465
+ db close
466
+ sqlite3 db test.db
467
+ execsql {
468
+ PRAGMA auto_vacuum;
469
+ }
470
+ } {1}
471
+ do_test autovacuum-3.3 {
472
+ execsql {
473
+ PRAGMA auto_vacuum = 0;
474
+ PRAGMA auto_vacuum;
475
+ }
476
+ } {1}
477
+
478
+ do_test autovacuum-3.4 {
479
+ db close
480
+ forcedelete test.db
481
+ sqlite3 db test.db
482
+ execsql {
483
+ PRAGMA auto_vacuum;
484
+ }
485
+ } $AUTOVACUUM
486
+ do_test autovacuum-3.5 {
487
+ execsql {
488
+ CREATE TABLE av1(x);
489
+ PRAGMA auto_vacuum;
490
+ }
491
+ } $AUTOVACUUM
492
+ do_test autovacuum-3.6 {
493
+ execsql {
494
+ PRAGMA auto_vacuum = 1;
495
+ PRAGMA auto_vacuum;
496
+ }
497
+ } [expr $AUTOVACUUM ? 1 : 0]
498
+ do_test autovacuum-3.7 {
499
+ execsql {
500
+ DROP TABLE av1;
501
+ }
502
+ file_pages
503
+ } [expr $AUTOVACUUM?1:2]
504
+
505
+
506
+ #-----------------------------------------------------------------------
507
+ # Test that if a statement transaction around a CREATE INDEX statement is
508
+ # rolled back no corruption occurs.
509
+ #
510
+ do_test autovacuum-4.0 {
511
+ # The last round of tests may have left the db in non-autovacuum mode.
512
+ # Reset everything just in case.
513
+ #
514
+ db close
515
+ forcedelete test.db test.db-journal
516
+ sqlite3 db test.db
517
+ execsql {
518
+ PRAGMA auto_vacuum = 1;
519
+ PRAGMA auto_vacuum;
520
+ }
521
+ } {1}
522
+ do_test autovacuum-4.1 {
523
+ execsql {
524
+ CREATE TABLE av1(a, b);
525
+ BEGIN;
526
+ }
527
+ for {set i 0} {$i<100} {incr i} {
528
+ execsql "INSERT INTO av1 VALUES($i, '[string repeat X 200]');"
529
+ }
530
+ execsql "INSERT INTO av1 VALUES(99, '[string repeat X 200]');"
531
+ execsql {
532
+ SELECT sum(a) FROM av1;
533
+ }
534
+ } {5049}
535
+ do_test autovacuum-4.2 {
536
+ catchsql {
537
+ CREATE UNIQUE INDEX av1_i ON av1(a);
538
+ }
539
+ } {1 {UNIQUE constraint failed: av1.a}}
540
+ do_test autovacuum-4.3 {
541
+ execsql {
542
+ SELECT sum(a) FROM av1;
543
+ }
544
+ } {5049}
545
+ do_test autovacuum-4.4 {
546
+ execsql {
547
+ COMMIT;
548
+ }
549
+ } {}
550
+
551
+ ifcapable integrityck {
552
+
553
+ # Ticket #1727
554
+ do_test autovacuum-5.1 {
555
+ db close
556
+ sqlite3 db :memory:
557
+ db eval {
558
+ PRAGMA auto_vacuum=1;
559
+ CREATE TABLE t1(a);
560
+ CREATE TABLE t2(a);
561
+ DROP TABLE t1;
562
+ PRAGMA integrity_check;
563
+ }
564
+ } ok
565
+
566
+ }
567
+
568
+ # Ticket #1728.
569
+ #
570
+ # In autovacuum mode, when tables or indices are deleted, the rootpage
571
+ # values in the symbol table have to be updated. There was a bug in this
572
+ # logic so that if an index/table was moved twice, the second move might
573
+ # not occur. This would leave the internal symbol table in an inconsistent
574
+ # state causing subsequent statements to fail.
575
+ #
576
+ # The problem is difficult to reproduce. The sequence of statements in
577
+ # the following test are carefully designed make it occur and thus to
578
+ # verify that this very obscure bug has been resolved.
579
+ #
580
+ ifcapable integrityck&&memorydb {
581
+
582
+ do_test autovacuum-6.1 {
583
+ db close
584
+ sqlite3 db :memory:
585
+ db eval {
586
+ PRAGMA auto_vacuum=1;
587
+ CREATE TABLE t1(a, b);
588
+ CREATE INDEX i1 ON t1(a);
589
+ CREATE TABLE t2(a);
590
+ CREATE INDEX i2 ON t2(a);
591
+ CREATE TABLE t3(a);
592
+ CREATE INDEX i3 ON t2(a);
593
+ CREATE INDEX x ON t1(b);
594
+ DROP TABLE t3;
595
+ PRAGMA integrity_check;
596
+ DROP TABLE t2;
597
+ PRAGMA integrity_check;
598
+ DROP TABLE t1;
599
+ PRAGMA integrity_check;
600
+ }
601
+ } {ok ok ok}
602
+
603
+ }
604
+
605
+ #---------------------------------------------------------------------
606
+ # Test cases autovacuum-7.X test the case where a page must be moved
607
+ # and the destination location collides with at least one other
608
+ # entry in the page hash-table (internal to the pager.c module.
609
+ #
610
+ do_test autovacuum-7.1 {
611
+ db close
612
+ forcedelete test.db
613
+ forcedelete test.db-journal
614
+ sqlite3 db test.db
615
+
616
+ execsql {
617
+ PRAGMA auto_vacuum=1;
618
+ CREATE TABLE t1(a, b, PRIMARY KEY(a, b));
619
+ INSERT INTO t1 VALUES(randstr(400,400),randstr(400,400));
620
+ INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
621
+ INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 4
622
+ INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 8
623
+ INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 16
624
+ INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 32
625
+ }
626
+
627
+ expr {[file size test.db] / 1024}
628
+ } {73}
629
+
630
+ do_test autovacuum-7.2 {
631
+ execsql {
632
+ CREATE TABLE t2(a, b, PRIMARY KEY(a, b));
633
+ INSERT INTO t2 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
634
+ CREATE TABLE t3(a, b, PRIMARY KEY(a, b));
635
+ INSERT INTO t3 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
636
+ CREATE TABLE t4(a, b, PRIMARY KEY(a, b));
637
+ INSERT INTO t4 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
638
+ CREATE TABLE t5(a, b, PRIMARY KEY(a, b));
639
+ INSERT INTO t5 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
640
+ }
641
+ expr {[file size test.db] / 1024}
642
+ } {354}
643
+
644
+ do_test autovacuum-7.3 {
645
+ db close
646
+ sqlite3 db test.db
647
+ execsql {
648
+ BEGIN;
649
+ DELETE FROM t4;
650
+ COMMIT;
651
+ SELECT count(*) FROM t1;
652
+ }
653
+ expr {[file size test.db] / 1024}
654
+ } {286}
655
+
656
+ #------------------------------------------------------------------------
657
+ # Additional tests.
658
+ #
659
+ # Try to determine the autovacuum setting for a database that is locked.
660
+ #
661
+ do_test autovacuum-8.1 {
662
+ db close
663
+ sqlite3 db test.db
664
+ sqlite3 db2 test.db
665
+ db eval {PRAGMA auto_vacuum}
666
+ } {1}
667
+ if {[permutation] == ""} {
668
+ do_test autovacuum-8.2 {
669
+ db eval {BEGIN EXCLUSIVE}
670
+ catchsql {PRAGMA auto_vacuum} db2
671
+ } {1 {database is locked}}
672
+ catch {db2 close}
673
+ catch {db eval {COMMIT}}
674
+ }
675
+
676
+ do_test autovacuum-9.1 {
677
+ execsql {
678
+ DROP TABLE t1;
679
+ DROP TABLE t2;
680
+ DROP TABLE t3;
681
+ DROP TABLE t4;
682
+ DROP TABLE t5;
683
+ PRAGMA page_count;
684
+ }
685
+ } {1}
686
+ do_test autovacuum-9.2 {
687
+ file size test.db
688
+ } 1024
689
+ do_test autovacuum-9.3 {
690
+ execsql {
691
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
692
+ INSERT INTO t1 VALUES(NULL, randstr(50,50));
693
+ }
694
+ for {set ii 0} {$ii < 10} {incr ii} {
695
+ db eval { INSERT INTO t1 SELECT NULL, randstr(50,50) FROM t1 }
696
+ }
697
+ file size test.db
698
+ } $::sqlite_pending_byte
699
+ do_test autovacuum-9.4 {
700
+ execsql { INSERT INTO t1 SELECT NULL, randstr(50,50) FROM t1 }
701
+ } {}
702
+ do_test autovacuum-9.5 {
703
+ execsql { DELETE FROM t1 WHERE rowid > (SELECT max(a)/2 FROM t1) }
704
+ file size test.db
705
+ } $::sqlite_pending_byte
706
+
707
+ do_execsql_test autovacuum-10.1 {
708
+ DROP TABLE t1;
709
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
710
+ INSERT INTO t1 VALUES(25, randomblob(104));
711
+ REPLACE INTO t1 VALUES(25, randomblob(1117));
712
+ PRAGMA integrity_check;
713
+ } {ok}
714
+
715
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/autovacuum2.test ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2021-10-15
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is testing the sqlite3_autovacuum_pages() interface
13
+ #
14
+
15
+ set testdir [file dirname $argv0]
16
+ source $testdir/tester.tcl
17
+
18
+ # If this build of the library does not support auto-vacuum, omit this
19
+ # whole file.
20
+ ifcapable {!autovacuum || !pragma} {
21
+ finish_test
22
+ return
23
+ }
24
+
25
+ # Demonstrate basic sqlite3_autovacuum_pages functionality
26
+ #
27
+ do_execsql_test autovacuum2-1.0 {
28
+ PRAGMA page_size=1024;
29
+ PRAGMA auto_vacuum=FULL;
30
+ CREATE TABLE t1(x);
31
+ VACUUM;
32
+ INSERT INTO t1(x) VALUES(zeroblob(10000));
33
+ PRAGMA page_count;
34
+ } {12}
35
+ proc autovac_page_callback {schema filesize freesize pagesize} {
36
+ global autovac_callback_data
37
+ lappend autovac_callback_data $schema $filesize $freesize $pagesize
38
+ return [expr {$freesize/2}]
39
+ }
40
+ sqlite3_autovacuum_pages db autovac_page_callback
41
+ set autovac_callback_data {}
42
+ do_execsql_test autovacuum2-1.1 {
43
+ BEGIN;
44
+ DELETE FROM t1;
45
+ PRAGMA freelist_count;
46
+ PRAGMA page_count;
47
+ } {9 12}
48
+ do_execsql_test autovacuum2-1.2 {
49
+ COMMIT;
50
+ } {}
51
+ do_test autovacuum2-1.3 {
52
+ set autovac_callback_data
53
+ } {main 12 9 1024}
54
+ do_execsql_test autovacuum2-1.4 {
55
+ PRAGMA freelist_count;
56
+ PRAGMA page_count;
57
+ } {5 8}
58
+ do_execsql_test autovacuum2-1.5 {
59
+ PRAGMA integrity_check;
60
+ } {ok}
61
+
62
+ # Disable the autovacuum-pages callback. Then do any transaction.
63
+ # The database should shrink to minimal size
64
+ #
65
+ sqlite3_autovacuum_pages db
66
+ do_execsql_test autovacuum2-1.10 {
67
+ CREATE TABLE t2(x);
68
+ PRAGMA freelist_count;
69
+ } {0}
70
+
71
+ # Rig the autovacuum-pages callback to always return zero. No
72
+ # autovacuum will happen.
73
+ #
74
+ proc autovac_page_callback_off {schema filesize freesize pagesize} {
75
+ return 0
76
+ }
77
+ sqlite3_autovacuum_pages db autovac_page_callback_off
78
+ do_execsql_test autovacuum2-1.20 {
79
+ BEGIN;
80
+ INSERT INTO t1(x) VALUES(zeroblob(10000));
81
+ DELETE FROM t1;
82
+ PRAGMA freelist_count;
83
+ COMMIT;
84
+ PRAGMA freelist_count;
85
+ } {9 9}
86
+
87
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/autovacuum_ioerr2.test ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2001 October 12
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is testing for correct handling of I/O errors
13
+ # such as writes failing because the disk is full.
14
+ #
15
+ # The tests in this file use special facilities that are only
16
+ # available in the SQLite test fixture.
17
+ #
18
+ # $Id: autovacuum_ioerr2.test,v 1.7 2008/07/12 14:52:20 drh Exp $
19
+
20
+ set testdir [file dirname $argv0]
21
+ source $testdir/tester.tcl
22
+
23
+ # If this build of the library does not support auto-vacuum, omit this
24
+ # whole file.
25
+ ifcapable {!autovacuum} {
26
+ finish_test
27
+ return
28
+ }
29
+
30
+ do_ioerr_test autovacuum-ioerr2-1 -sqlprep {
31
+ PRAGMA auto_vacuum = 1;
32
+ CREATE TABLE abc(a);
33
+ INSERT INTO abc VALUES(randstr(1500,1500));
34
+ } -sqlbody {
35
+ CREATE TABLE abc2(a);
36
+ BEGIN;
37
+ DELETE FROM abc;
38
+ INSERT INTO abc VALUES(randstr(1500,1500));
39
+ CREATE TABLE abc3(a);
40
+ COMMIT;
41
+ }
42
+
43
+ do_ioerr_test autovacuum-ioerr2-2 -tclprep {
44
+ execsql {
45
+ PRAGMA auto_vacuum = 1;
46
+ PRAGMA cache_size = 10;
47
+ BEGIN;
48
+ CREATE TABLE abc(a);
49
+ INSERT INTO abc VALUES(randstr(1100,1100)); -- Page 4 is overflow
50
+ INSERT INTO abc VALUES(randstr(1100,1100)); -- Page 5 is overflow
51
+ }
52
+ for {set i 0} {$i<150} {incr i} {
53
+ execsql {
54
+ INSERT INTO abc VALUES(randstr(100,100));
55
+ }
56
+ }
57
+ execsql COMMIT
58
+ } -sqlbody {
59
+ BEGIN;
60
+ DELETE FROM abc WHERE length(a)>100;
61
+ UPDATE abc SET a = randstr(90,90);
62
+ CREATE TABLE abc3(a);
63
+ COMMIT;
64
+ }
65
+
66
+ do_ioerr_test autovacuum-ioerr2-3 -sqlprep {
67
+ PRAGMA auto_vacuum = 1;
68
+ CREATE TABLE abc(a);
69
+ CREATE TABLE abc2(b);
70
+ } -sqlbody {
71
+ BEGIN;
72
+ INSERT INTO abc2 VALUES(10);
73
+ DROP TABLE abc;
74
+ COMMIT;
75
+ DROP TABLE abc2;
76
+ }
77
+
78
+ forcedelete backup.db
79
+ ifcapable subquery {
80
+ do_ioerr_test autovacuum-ioerr2-4 -tclprep {
81
+ if {![file exists backup.db]} {
82
+ sqlite3 dbb backup.db
83
+ execsql {
84
+ PRAGMA auto_vacuum = 1;
85
+ BEGIN;
86
+ CREATE TABLE abc(a);
87
+ INSERT INTO abc VALUES(randstr(1100,1100)); -- Page 4 is overflow
88
+ INSERT INTO abc VALUES(randstr(1100,1100)); -- Page 5 is overflow
89
+ } dbb
90
+ for {set i 0} {$i<2500} {incr i} {
91
+ execsql {
92
+ INSERT INTO abc VALUES(randstr(100,100));
93
+ } dbb
94
+ }
95
+ execsql {
96
+ COMMIT;
97
+ PRAGMA cache_size = 10;
98
+ } dbb
99
+ dbb close
100
+ }
101
+ db close
102
+ forcedelete test.db
103
+ forcedelete test.db-journal
104
+ forcecopy backup.db test.db
105
+ set ::DB [sqlite3 db test.db]
106
+ execsql {
107
+ PRAGMA cache_size = 10;
108
+ }
109
+ } -sqlbody {
110
+ BEGIN;
111
+ DELETE FROM abc WHERE oid < 3;
112
+ UPDATE abc SET a = randstr(100,100) WHERE oid > 2300;
113
+ UPDATE abc SET a = randstr(1100,1100) WHERE oid =
114
+ (select max(oid) from abc);
115
+ COMMIT;
116
+ }
117
+ }
118
+
119
+ do_ioerr_test autovacuum-ioerr2-1 -sqlprep {
120
+ PRAGMA auto_vacuum = 1;
121
+ CREATE TABLE abc(a);
122
+ INSERT INTO abc VALUES(randstr(1500,1500));
123
+ } -sqlbody {
124
+ CREATE TABLE abc2(a);
125
+ BEGIN;
126
+ DELETE FROM abc;
127
+ INSERT INTO abc VALUES(randstr(1500,1500));
128
+ CREATE TABLE abc3(a);
129
+ COMMIT;
130
+ }
131
+
132
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/avtrans.test ADDED
@@ -0,0 +1,927 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2001 September 15
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. This
12
+ # file is a copy of "trans.test" modified to run under autovacuum mode.
13
+ # the point is to stress the autovacuum logic and try to get it to fail.
14
+ #
15
+ # $Id: avtrans.test,v 1.6 2007/09/12 17:01:45 danielk1977 Exp $
16
+
17
+
18
+ set testdir [file dirname $argv0]
19
+ source $testdir/tester.tcl
20
+
21
+
22
+ # Create several tables to work with.
23
+ #
24
+ do_test avtrans-1.0 {
25
+ execsql { PRAGMA auto_vacuum=full }
26
+ wal_set_journal_mode
27
+ execsql {
28
+ CREATE TABLE one(a int PRIMARY KEY, b text);
29
+ INSERT INTO one VALUES(1,'one');
30
+ INSERT INTO one VALUES(2,'two');
31
+ INSERT INTO one VALUES(3,'three');
32
+ SELECT b FROM one ORDER BY a;
33
+ }
34
+ } {one two three}
35
+ do_test avtrans-1.0.1 { execsql { PRAGMA auto_vacuum } } 1
36
+ do_test avtrans-1.1 {
37
+ execsql {
38
+ CREATE TABLE two(a int PRIMARY KEY, b text);
39
+ INSERT INTO two VALUES(1,'I');
40
+ INSERT INTO two VALUES(5,'V');
41
+ INSERT INTO two VALUES(10,'X');
42
+ SELECT b FROM two ORDER BY a;
43
+ }
44
+ } {I V X}
45
+ do_test avtrans-1.9 {
46
+ sqlite3 altdb test.db
47
+ execsql {SELECT b FROM one ORDER BY a} altdb
48
+ } {one two three}
49
+ do_test avtrans-1.10 {
50
+ execsql {SELECT b FROM two ORDER BY a} altdb
51
+ } {I V X}
52
+ integrity_check avtrans-1.11
53
+ wal_check_journal_mode avtrans-1.12
54
+
55
+ # Basic transactions
56
+ #
57
+ do_test avtrans-2.1 {
58
+ set v [catch {execsql {BEGIN}} msg]
59
+ lappend v $msg
60
+ } {0 {}}
61
+ do_test avtrans-2.2 {
62
+ set v [catch {execsql {END}} msg]
63
+ lappend v $msg
64
+ } {0 {}}
65
+ do_test avtrans-2.3 {
66
+ set v [catch {execsql {BEGIN TRANSACTION}} msg]
67
+ lappend v $msg
68
+ } {0 {}}
69
+ do_test avtrans-2.4 {
70
+ set v [catch {execsql {COMMIT TRANSACTION}} msg]
71
+ lappend v $msg
72
+ } {0 {}}
73
+ do_test avtrans-2.5 {
74
+ set v [catch {execsql {BEGIN TRANSACTION 'foo'}} msg]
75
+ lappend v $msg
76
+ } {0 {}}
77
+ do_test avtrans-2.6 {
78
+ set v [catch {execsql {ROLLBACK TRANSACTION 'foo'}} msg]
79
+ lappend v $msg
80
+ } {0 {}}
81
+ do_test avtrans-2.10 {
82
+ execsql {
83
+ BEGIN;
84
+ SELECT a FROM one ORDER BY a;
85
+ SELECT a FROM two ORDER BY a;
86
+ END;
87
+ }
88
+ } {1 2 3 1 5 10}
89
+ integrity_check avtrans-2.11
90
+ wal_check_journal_mode avtrans-2.12
91
+
92
+ # Check the locking behavior
93
+ #
94
+ sqlite3_soft_heap_limit 0
95
+ do_test avtrans-3.1 {
96
+ execsql {
97
+ BEGIN;
98
+ UPDATE one SET a = 0 WHERE 0;
99
+ SELECT a FROM one ORDER BY a;
100
+ }
101
+ } {1 2 3}
102
+ do_test avtrans-3.2 {
103
+ catchsql {
104
+ SELECT a FROM two ORDER BY a;
105
+ } altdb
106
+ } {0 {1 5 10}}
107
+ do_test avtrans-3.3 {
108
+ catchsql {
109
+ SELECT a FROM one ORDER BY a;
110
+ } altdb
111
+ } {0 {1 2 3}}
112
+ do_test avtrans-3.4 {
113
+ catchsql {
114
+ INSERT INTO one VALUES(4,'four');
115
+ }
116
+ } {0 {}}
117
+ do_test avtrans-3.5 {
118
+ catchsql {
119
+ SELECT a FROM two ORDER BY a;
120
+ } altdb
121
+ } {0 {1 5 10}}
122
+ do_test avtrans-3.6 {
123
+ catchsql {
124
+ SELECT a FROM one ORDER BY a;
125
+ } altdb
126
+ } {0 {1 2 3}}
127
+ do_test avtrans-3.7 {
128
+ catchsql {
129
+ INSERT INTO two VALUES(4,'IV');
130
+ }
131
+ } {0 {}}
132
+ do_test avtrans-3.8 {
133
+ catchsql {
134
+ SELECT a FROM two ORDER BY a;
135
+ } altdb
136
+ } {0 {1 5 10}}
137
+ do_test avtrans-3.9 {
138
+ catchsql {
139
+ SELECT a FROM one ORDER BY a;
140
+ } altdb
141
+ } {0 {1 2 3}}
142
+ do_test avtrans-3.10 {
143
+ execsql {END TRANSACTION}
144
+ } {}
145
+ do_test avtrans-3.11 {
146
+ set v [catch {execsql {
147
+ SELECT a FROM two ORDER BY a;
148
+ } altdb} msg]
149
+ lappend v $msg
150
+ } {0 {1 4 5 10}}
151
+ do_test avtrans-3.12 {
152
+ set v [catch {execsql {
153
+ SELECT a FROM one ORDER BY a;
154
+ } altdb} msg]
155
+ lappend v $msg
156
+ } {0 {1 2 3 4}}
157
+ do_test avtrans-3.13 {
158
+ set v [catch {execsql {
159
+ SELECT a FROM two ORDER BY a;
160
+ } db} msg]
161
+ lappend v $msg
162
+ } {0 {1 4 5 10}}
163
+ do_test avtrans-3.14 {
164
+ set v [catch {execsql {
165
+ SELECT a FROM one ORDER BY a;
166
+ } db} msg]
167
+ lappend v $msg
168
+ } {0 {1 2 3 4}}
169
+ sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
170
+ integrity_check avtrans-3.15
171
+
172
+ do_test avtrans-4.1 {
173
+ set v [catch {execsql {
174
+ COMMIT;
175
+ } db} msg]
176
+ lappend v $msg
177
+ } {1 {cannot commit - no transaction is active}}
178
+ do_test avtrans-4.2 {
179
+ set v [catch {execsql {
180
+ ROLLBACK;
181
+ } db} msg]
182
+ lappend v $msg
183
+ } {1 {cannot rollback - no transaction is active}}
184
+ do_test avtrans-4.3 {
185
+ catchsql {
186
+ BEGIN TRANSACTION;
187
+ UPDATE two SET a = 0 WHERE 0;
188
+ SELECT a FROM two ORDER BY a;
189
+ } db
190
+ } {0 {1 4 5 10}}
191
+ do_test avtrans-4.4 {
192
+ catchsql {
193
+ SELECT a FROM two ORDER BY a;
194
+ } altdb
195
+ } {0 {1 4 5 10}}
196
+ do_test avtrans-4.5 {
197
+ catchsql {
198
+ SELECT a FROM one ORDER BY a;
199
+ } altdb
200
+ } {0 {1 2 3 4}}
201
+ do_test avtrans-4.6 {
202
+ catchsql {
203
+ BEGIN TRANSACTION;
204
+ SELECT a FROM one ORDER BY a;
205
+ } db
206
+ } {1 {cannot start a transaction within a transaction}}
207
+ do_test avtrans-4.7 {
208
+ catchsql {
209
+ SELECT a FROM two ORDER BY a;
210
+ } altdb
211
+ } {0 {1 4 5 10}}
212
+ do_test avtrans-4.8 {
213
+ catchsql {
214
+ SELECT a FROM one ORDER BY a;
215
+ } altdb
216
+ } {0 {1 2 3 4}}
217
+ do_test avtrans-4.9 {
218
+ set v [catch {execsql {
219
+ END TRANSACTION;
220
+ SELECT a FROM two ORDER BY a;
221
+ } db} msg]
222
+ lappend v $msg
223
+ } {0 {1 4 5 10}}
224
+ do_test avtrans-4.10 {
225
+ set v [catch {execsql {
226
+ SELECT a FROM two ORDER BY a;
227
+ } altdb} msg]
228
+ lappend v $msg
229
+ } {0 {1 4 5 10}}
230
+ do_test avtrans-4.11 {
231
+ set v [catch {execsql {
232
+ SELECT a FROM one ORDER BY a;
233
+ } altdb} msg]
234
+ lappend v $msg
235
+ } {0 {1 2 3 4}}
236
+ integrity_check avtrans-4.12
237
+ do_test avtrans-4.98 {
238
+ altdb close
239
+ execsql {
240
+ DROP TABLE one;
241
+ DROP TABLE two;
242
+ }
243
+ } {}
244
+ integrity_check avtrans-4.99
245
+
246
+ # Check out the commit/rollback behavior of the database
247
+ #
248
+ do_test avtrans-5.1 {
249
+ execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
250
+ } {}
251
+ do_test avtrans-5.2 {
252
+ execsql {BEGIN TRANSACTION}
253
+ execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
254
+ } {}
255
+ do_test avtrans-5.3 {
256
+ execsql {CREATE TABLE one(a text, b int)}
257
+ execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
258
+ } {one}
259
+ do_test avtrans-5.4 {
260
+ execsql {SELECT a,b FROM one ORDER BY b}
261
+ } {}
262
+ do_test avtrans-5.5 {
263
+ execsql {INSERT INTO one(a,b) VALUES('hello', 1)}
264
+ execsql {SELECT a,b FROM one ORDER BY b}
265
+ } {hello 1}
266
+ do_test avtrans-5.6 {
267
+ execsql {ROLLBACK}
268
+ execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
269
+ } {}
270
+ do_test avtrans-5.7 {
271
+ set v [catch {
272
+ execsql {SELECT a,b FROM one ORDER BY b}
273
+ } msg]
274
+ lappend v $msg
275
+ } {1 {no such table: one}}
276
+
277
+ # Test commits and rollbacks of table CREATE TABLEs, CREATE INDEXs
278
+ # DROP TABLEs and DROP INDEXs
279
+ #
280
+ do_test avtrans-5.8 {
281
+ execsql {
282
+ SELECT name fROM sqlite_master
283
+ WHERE type='table' OR type='index'
284
+ ORDER BY name
285
+ }
286
+ } {}
287
+ do_test avtrans-5.9 {
288
+ execsql {
289
+ BEGIN TRANSACTION;
290
+ CREATE TABLE t1(a int, b int, c int);
291
+ SELECT name fROM sqlite_master
292
+ WHERE type='table' OR type='index'
293
+ ORDER BY name;
294
+ }
295
+ } {t1}
296
+ do_test avtrans-5.10 {
297
+ execsql {
298
+ CREATE INDEX i1 ON t1(a);
299
+ SELECT name fROM sqlite_master
300
+ WHERE type='table' OR type='index'
301
+ ORDER BY name;
302
+ }
303
+ } {i1 t1}
304
+ do_test avtrans-5.11 {
305
+ execsql {
306
+ COMMIT;
307
+ SELECT name fROM sqlite_master
308
+ WHERE type='table' OR type='index'
309
+ ORDER BY name;
310
+ }
311
+ } {i1 t1}
312
+ do_test avtrans-5.12 {
313
+ execsql {
314
+ BEGIN TRANSACTION;
315
+ CREATE TABLE t2(a int, b int, c int);
316
+ CREATE INDEX i2a ON t2(a);
317
+ CREATE INDEX i2b ON t2(b);
318
+ DROP TABLE t1;
319
+ SELECT name fROM sqlite_master
320
+ WHERE type='table' OR type='index'
321
+ ORDER BY name;
322
+ }
323
+ } {i2a i2b t2}
324
+ do_test avtrans-5.13 {
325
+ execsql {
326
+ ROLLBACK;
327
+ SELECT name fROM sqlite_master
328
+ WHERE type='table' OR type='index'
329
+ ORDER BY name;
330
+ }
331
+ } {i1 t1}
332
+ do_test avtrans-5.14 {
333
+ execsql {
334
+ BEGIN TRANSACTION;
335
+ DROP INDEX i1;
336
+ SELECT name fROM sqlite_master
337
+ WHERE type='table' OR type='index'
338
+ ORDER BY name;
339
+ }
340
+ } {t1}
341
+ do_test avtrans-5.15 {
342
+ execsql {
343
+ ROLLBACK;
344
+ SELECT name fROM sqlite_master
345
+ WHERE type='table' OR type='index'
346
+ ORDER BY name;
347
+ }
348
+ } {i1 t1}
349
+ do_test avtrans-5.16 {
350
+ execsql {
351
+ BEGIN TRANSACTION;
352
+ DROP INDEX i1;
353
+ CREATE TABLE t2(x int, y int, z int);
354
+ CREATE INDEX i2x ON t2(x);
355
+ CREATE INDEX i2y ON t2(y);
356
+ INSERT INTO t2 VALUES(1,2,3);
357
+ SELECT name fROM sqlite_master
358
+ WHERE type='table' OR type='index'
359
+ ORDER BY name;
360
+ }
361
+ } {i2x i2y t1 t2}
362
+ do_test avtrans-5.17 {
363
+ execsql {
364
+ COMMIT;
365
+ SELECT name fROM sqlite_master
366
+ WHERE type='table' OR type='index'
367
+ ORDER BY name;
368
+ }
369
+ } {i2x i2y t1 t2}
370
+ do_test avtrans-5.18 {
371
+ execsql {
372
+ SELECT * FROM t2;
373
+ }
374
+ } {1 2 3}
375
+ do_test avtrans-5.19 {
376
+ execsql {
377
+ SELECT x FROM t2 WHERE y=2;
378
+ }
379
+ } {1}
380
+ do_test avtrans-5.20 {
381
+ execsql {
382
+ BEGIN TRANSACTION;
383
+ DROP TABLE t1;
384
+ DROP TABLE t2;
385
+ SELECT name fROM sqlite_master
386
+ WHERE type='table' OR type='index'
387
+ ORDER BY name;
388
+ }
389
+ } {}
390
+ do_test avtrans-5.21 {
391
+ set r [catch {execsql {
392
+ SELECT * FROM t2
393
+ }} msg]
394
+ lappend r $msg
395
+ } {1 {no such table: t2}}
396
+ do_test avtrans-5.22 {
397
+ execsql {
398
+ ROLLBACK;
399
+ SELECT name fROM sqlite_master
400
+ WHERE type='table' OR type='index'
401
+ ORDER BY name;
402
+ }
403
+ } {i2x i2y t1 t2}
404
+ do_test avtrans-5.23 {
405
+ execsql {
406
+ SELECT * FROM t2;
407
+ }
408
+ } {1 2 3}
409
+ integrity_check avtrans-5.23
410
+
411
+
412
+ # Try to DROP and CREATE tables and indices with the same name
413
+ # within a transaction. Make sure ROLLBACK works.
414
+ #
415
+ do_test avtrans-6.1 {
416
+ execsql2 {
417
+ INSERT INTO t1 VALUES(1,2,3);
418
+ BEGIN TRANSACTION;
419
+ DROP TABLE t1;
420
+ CREATE TABLE t1(p,q,r);
421
+ ROLLBACK;
422
+ SELECT * FROM t1;
423
+ }
424
+ } {a 1 b 2 c 3}
425
+ do_test avtrans-6.2 {
426
+ execsql2 {
427
+ INSERT INTO t1 VALUES(1,2,3);
428
+ BEGIN TRANSACTION;
429
+ DROP TABLE t1;
430
+ CREATE TABLE t1(p,q,r);
431
+ COMMIT;
432
+ SELECT * FROM t1;
433
+ }
434
+ } {}
435
+ do_test avtrans-6.3 {
436
+ execsql2 {
437
+ INSERT INTO t1 VALUES(1,2,3);
438
+ SELECT * FROM t1;
439
+ }
440
+ } {p 1 q 2 r 3}
441
+ do_test avtrans-6.4 {
442
+ execsql2 {
443
+ BEGIN TRANSACTION;
444
+ DROP TABLE t1;
445
+ CREATE TABLE t1(a,b,c);
446
+ INSERT INTO t1 VALUES(4,5,6);
447
+ SELECT * FROM t1;
448
+ DROP TABLE t1;
449
+ }
450
+ } {a 4 b 5 c 6}
451
+ do_test avtrans-6.5 {
452
+ execsql2 {
453
+ ROLLBACK;
454
+ SELECT * FROM t1;
455
+ }
456
+ } {p 1 q 2 r 3}
457
+ do_test avtrans-6.6 {
458
+ execsql2 {
459
+ BEGIN TRANSACTION;
460
+ DROP TABLE t1;
461
+ CREATE TABLE t1(a,b,c);
462
+ INSERT INTO t1 VALUES(4,5,6);
463
+ SELECT * FROM t1;
464
+ DROP TABLE t1;
465
+ }
466
+ } {a 4 b 5 c 6}
467
+ do_test avtrans-6.7 {
468
+ catchsql {
469
+ COMMIT;
470
+ SELECT * FROM t1;
471
+ }
472
+ } {1 {no such table: t1}}
473
+
474
+ # Repeat on a table with an automatically generated index.
475
+ #
476
+ do_test avtrans-6.10 {
477
+ execsql2 {
478
+ CREATE TABLE t1(a unique,b,c);
479
+ INSERT INTO t1 VALUES(1,2,3);
480
+ BEGIN TRANSACTION;
481
+ DROP TABLE t1;
482
+ CREATE TABLE t1(p unique,q,r);
483
+ ROLLBACK;
484
+ SELECT * FROM t1;
485
+ }
486
+ } {a 1 b 2 c 3}
487
+ do_test avtrans-6.11 {
488
+ execsql2 {
489
+ BEGIN TRANSACTION;
490
+ DROP TABLE t1;
491
+ CREATE TABLE t1(p unique,q,r);
492
+ COMMIT;
493
+ SELECT * FROM t1;
494
+ }
495
+ } {}
496
+ do_test avtrans-6.12 {
497
+ execsql2 {
498
+ INSERT INTO t1 VALUES(1,2,3);
499
+ SELECT * FROM t1;
500
+ }
501
+ } {p 1 q 2 r 3}
502
+ do_test avtrans-6.13 {
503
+ execsql2 {
504
+ BEGIN TRANSACTION;
505
+ DROP TABLE t1;
506
+ CREATE TABLE t1(a unique,b,c);
507
+ INSERT INTO t1 VALUES(4,5,6);
508
+ SELECT * FROM t1;
509
+ DROP TABLE t1;
510
+ }
511
+ } {a 4 b 5 c 6}
512
+ do_test avtrans-6.14 {
513
+ execsql2 {
514
+ ROLLBACK;
515
+ SELECT * FROM t1;
516
+ }
517
+ } {p 1 q 2 r 3}
518
+ do_test avtrans-6.15 {
519
+ execsql2 {
520
+ BEGIN TRANSACTION;
521
+ DROP TABLE t1;
522
+ CREATE TABLE t1(a unique,b,c);
523
+ INSERT INTO t1 VALUES(4,5,6);
524
+ SELECT * FROM t1;
525
+ DROP TABLE t1;
526
+ }
527
+ } {a 4 b 5 c 6}
528
+ do_test avtrans-6.16 {
529
+ catchsql {
530
+ COMMIT;
531
+ SELECT * FROM t1;
532
+ }
533
+ } {1 {no such table: t1}}
534
+
535
+ do_test avtrans-6.20 {
536
+ execsql {
537
+ CREATE TABLE t1(a integer primary key,b,c);
538
+ INSERT INTO t1 VALUES(1,-2,-3);
539
+ INSERT INTO t1 VALUES(4,-5,-6);
540
+ SELECT * FROM t1;
541
+ }
542
+ } {1 -2 -3 4 -5 -6}
543
+ do_test avtrans-6.21 {
544
+ execsql {
545
+ CREATE INDEX i1 ON t1(b);
546
+ SELECT * FROM t1 WHERE b<1;
547
+ }
548
+ } {4 -5 -6 1 -2 -3}
549
+ do_test avtrans-6.22 {
550
+ execsql {
551
+ BEGIN TRANSACTION;
552
+ DROP INDEX i1;
553
+ SELECT * FROM t1 WHERE b<1;
554
+ ROLLBACK;
555
+ }
556
+ } {1 -2 -3 4 -5 -6}
557
+ do_test avtrans-6.23 {
558
+ execsql {
559
+ SELECT * FROM t1 WHERE b<1;
560
+ }
561
+ } {4 -5 -6 1 -2 -3}
562
+ do_test avtrans-6.24 {
563
+ execsql {
564
+ BEGIN TRANSACTION;
565
+ DROP TABLE t1;
566
+ ROLLBACK;
567
+ SELECT * FROM t1 WHERE b<1;
568
+ }
569
+ } {4 -5 -6 1 -2 -3}
570
+
571
+ do_test avtrans-6.25 {
572
+ execsql {
573
+ BEGIN TRANSACTION;
574
+ DROP INDEX i1;
575
+ CREATE INDEX i1 ON t1(c);
576
+ SELECT * FROM t1 WHERE b<1;
577
+ }
578
+ } {1 -2 -3 4 -5 -6}
579
+ do_test avtrans-6.26 {
580
+ execsql {
581
+ SELECT * FROM t1 WHERE c<1;
582
+ }
583
+ } {4 -5 -6 1 -2 -3}
584
+ do_test avtrans-6.27 {
585
+ execsql {
586
+ ROLLBACK;
587
+ SELECT * FROM t1 WHERE b<1;
588
+ }
589
+ } {4 -5 -6 1 -2 -3}
590
+ do_test avtrans-6.28 {
591
+ execsql {
592
+ SELECT * FROM t1 WHERE c<1;
593
+ }
594
+ } {1 -2 -3 4 -5 -6}
595
+
596
+ # The following repeats steps 6.20 through 6.28, but puts a "unique"
597
+ # constraint the first field of the table in order to generate an
598
+ # automatic index.
599
+ #
600
+ do_test avtrans-6.30 {
601
+ execsql {
602
+ BEGIN TRANSACTION;
603
+ DROP TABLE t1;
604
+ CREATE TABLE t1(a int unique,b,c);
605
+ COMMIT;
606
+ INSERT INTO t1 VALUES(1,-2,-3);
607
+ INSERT INTO t1 VALUES(4,-5,-6);
608
+ SELECT * FROM t1 ORDER BY a;
609
+ }
610
+ } {1 -2 -3 4 -5 -6}
611
+ do_test avtrans-6.31 {
612
+ execsql {
613
+ CREATE INDEX i1 ON t1(b);
614
+ SELECT * FROM t1 WHERE b<1;
615
+ }
616
+ } {4 -5 -6 1 -2 -3}
617
+ do_test avtrans-6.32 {
618
+ execsql {
619
+ BEGIN TRANSACTION;
620
+ DROP INDEX i1;
621
+ SELECT * FROM t1 WHERE b<1;
622
+ ROLLBACK;
623
+ }
624
+ } {1 -2 -3 4 -5 -6}
625
+ do_test avtrans-6.33 {
626
+ execsql {
627
+ SELECT * FROM t1 WHERE b<1;
628
+ }
629
+ } {4 -5 -6 1 -2 -3}
630
+ do_test avtrans-6.34 {
631
+ execsql {
632
+ BEGIN TRANSACTION;
633
+ DROP TABLE t1;
634
+ ROLLBACK;
635
+ SELECT * FROM t1 WHERE b<1;
636
+ }
637
+ } {4 -5 -6 1 -2 -3}
638
+
639
+ do_test avtrans-6.35 {
640
+ execsql {
641
+ BEGIN TRANSACTION;
642
+ DROP INDEX i1;
643
+ CREATE INDEX i1 ON t1(c);
644
+ SELECT * FROM t1 WHERE b<1;
645
+ }
646
+ } {1 -2 -3 4 -5 -6}
647
+ do_test avtrans-6.36 {
648
+ execsql {
649
+ SELECT * FROM t1 WHERE c<1;
650
+ }
651
+ } {4 -5 -6 1 -2 -3}
652
+ do_test avtrans-6.37 {
653
+ execsql {
654
+ DROP INDEX i1;
655
+ SELECT * FROM t1 WHERE c<1;
656
+ }
657
+ } {1 -2 -3 4 -5 -6}
658
+ do_test avtrans-6.38 {
659
+ execsql {
660
+ ROLLBACK;
661
+ SELECT * FROM t1 WHERE b<1;
662
+ }
663
+ } {4 -5 -6 1 -2 -3}
664
+ do_test avtrans-6.39 {
665
+ execsql {
666
+ SELECT * FROM t1 WHERE c<1;
667
+ }
668
+ } {1 -2 -3 4 -5 -6}
669
+ integrity_check avtrans-6.40
670
+
671
+ ifcapable !floatingpoint {
672
+ finish_test
673
+ return
674
+ }
675
+
676
+ # Test to make sure rollback restores the database back to its original
677
+ # state.
678
+ #
679
+ do_test avtrans-7.1 {
680
+ execsql {BEGIN}
681
+ for {set i 0} {$i<1000} {incr i} {
682
+ set r1 [expr {rand()}]
683
+ set r2 [expr {rand()}]
684
+ set r3 [expr {rand()}]
685
+ execsql "INSERT INTO t2 VALUES($r1,$r2,$r3)"
686
+ }
687
+ execsql {COMMIT}
688
+ set ::checksum [execsql {SELECT md5sum(x,y,z) FROM t2}]
689
+ set ::checksum2 [
690
+ execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
691
+ ]
692
+ execsql {SELECT count(*) FROM t2}
693
+ } {1001}
694
+ do_test avtrans-7.2 {
695
+ execsql {SELECT md5sum(x,y,z) FROM t2}
696
+ } $checksum
697
+ do_test avtrans-7.2.1 {
698
+ execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
699
+ } $checksum2
700
+ do_test avtrans-7.3 {
701
+ execsql {
702
+ BEGIN;
703
+ DELETE FROM t2;
704
+ ROLLBACK;
705
+ SELECT md5sum(x,y,z) FROM t2;
706
+ }
707
+ } $checksum
708
+ do_test avtrans-7.4 {
709
+ execsql {
710
+ BEGIN;
711
+ INSERT INTO t2 SELECT * FROM t2;
712
+ ROLLBACK;
713
+ SELECT md5sum(x,y,z) FROM t2;
714
+ }
715
+ } $checksum
716
+ do_test avtrans-7.5 {
717
+ execsql {
718
+ BEGIN;
719
+ DELETE FROM t2;
720
+ ROLLBACK;
721
+ SELECT md5sum(x,y,z) FROM t2;
722
+ }
723
+ } $checksum
724
+ do_test avtrans-7.6 {
725
+ execsql {
726
+ BEGIN;
727
+ INSERT INTO t2 SELECT * FROM t2;
728
+ ROLLBACK;
729
+ SELECT md5sum(x,y,z) FROM t2;
730
+ }
731
+ } $checksum
732
+ do_test avtrans-7.7 {
733
+ execsql {
734
+ BEGIN;
735
+ CREATE TABLE t3 AS SELECT * FROM t2;
736
+ INSERT INTO t2 SELECT * FROM t3;
737
+ ROLLBACK;
738
+ SELECT md5sum(x,y,z) FROM t2;
739
+ }
740
+ } $checksum
741
+ do_test avtrans-7.8 {
742
+ execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
743
+ } $checksum2
744
+ ifcapable tempdb {
745
+ do_test avtrans-7.9 {
746
+ execsql {
747
+ BEGIN;
748
+ CREATE TEMP TABLE t3 AS SELECT * FROM t2;
749
+ INSERT INTO t2 SELECT * FROM t3;
750
+ ROLLBACK;
751
+ SELECT md5sum(x,y,z) FROM t2;
752
+ }
753
+ } $checksum
754
+ }
755
+ do_test avtrans-7.10 {
756
+ execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
757
+ } $checksum2
758
+ ifcapable tempdb {
759
+ do_test avtrans-7.11 {
760
+ execsql {
761
+ BEGIN;
762
+ CREATE TEMP TABLE t3 AS SELECT * FROM t2;
763
+ INSERT INTO t2 SELECT * FROM t3;
764
+ DROP INDEX i2x;
765
+ DROP INDEX i2y;
766
+ CREATE INDEX i3a ON t3(x);
767
+ ROLLBACK;
768
+ SELECT md5sum(x,y,z) FROM t2;
769
+ }
770
+ } $checksum
771
+ }
772
+ do_test avtrans-7.12 {
773
+ execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
774
+ } $checksum2
775
+ ifcapable tempdb {
776
+ do_test avtrans-7.13 {
777
+ execsql {
778
+ BEGIN;
779
+ DROP TABLE t2;
780
+ ROLLBACK;
781
+ SELECT md5sum(x,y,z) FROM t2;
782
+ }
783
+ } $checksum
784
+ }
785
+ do_test avtrans-7.14 {
786
+ execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
787
+ } $checksum2
788
+ integrity_check avtrans-7.15
789
+
790
+ # Arrange for another process to begin modifying the database but abort
791
+ # and die in the middle of the modification. Then have this process read
792
+ # the database. This process should detect the journal file and roll it
793
+ # back. Verify that this happens correctly.
794
+ #
795
+ set fd [open test.tcl w]
796
+ puts $fd {
797
+ sqlite3 db test.db
798
+ db eval {
799
+ PRAGMA default_cache_size=20;
800
+ BEGIN;
801
+ CREATE TABLE t3 AS SELECT * FROM t2;
802
+ DELETE FROM t2;
803
+ }
804
+ sqlite_abort
805
+ }
806
+ close $fd
807
+ do_test avtrans-8.1 {
808
+ catch {exec [info nameofexec] test.tcl}
809
+ execsql {SELECT md5sum(x,y,z) FROM t2}
810
+ } $checksum
811
+ do_test avtrans-8.2 {
812
+ execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
813
+ } $checksum2
814
+ integrity_check avtrans-8.3
815
+
816
+ # In the following sequence of tests, compute the MD5 sum of the content
817
+ # of a table, make lots of modifications to that table, then do a rollback.
818
+ # Verify that after the rollback, the MD5 checksum is unchanged.
819
+ #
820
+ do_test avtrans-9.1 {
821
+ execsql {
822
+ PRAGMA default_cache_size=10;
823
+ }
824
+ db close
825
+ sqlite3 db test.db
826
+ execsql {
827
+ BEGIN;
828
+ CREATE TABLE t3(x TEXT);
829
+ INSERT INTO t3 VALUES(randstr(10,400));
830
+ INSERT INTO t3 VALUES(randstr(10,400));
831
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
832
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
833
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
834
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
835
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
836
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
837
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
838
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
839
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3;
840
+ COMMIT;
841
+ SELECT count(*) FROM t3;
842
+ }
843
+ } {1024}
844
+
845
+ # The following procedure computes a "signature" for table "t3". If
846
+ # T3 changes in any way, the signature should change.
847
+ #
848
+ # This is used to test ROLLBACK. We gather a signature for t3, then
849
+ # make lots of changes to t3, then rollback and take another signature.
850
+ # The two signatures should be the same.
851
+ #
852
+ proc signature {} {
853
+ return [db eval {SELECT count(*), md5sum(x) FROM t3}]
854
+ }
855
+
856
+ # Repeat the following group of tests 20 times for quick testing and
857
+ # 40 times for full testing. Each iteration of the test makes table
858
+ # t3 a little larger, and thus takes a little longer, so doing 40 tests
859
+ # is more than 2.0 times slower than doing 20 tests. Considerably more.
860
+ #
861
+ if {[info exists G(isquick)]} {
862
+ set limit 20
863
+ } else {
864
+ set limit 40
865
+ }
866
+
867
+ # Do rollbacks. Make sure the signature does not change.
868
+ #
869
+ for {set i 2} {$i<=$limit} {incr i} {
870
+ set ::sig [signature]
871
+ set cnt [lindex $::sig 0]
872
+ if {$i%2==0} {
873
+ execsql {PRAGMA fullfsync=ON}
874
+ } else {
875
+ execsql {PRAGMA fullfsync=OFF}
876
+ }
877
+ set sqlite_sync_count 0
878
+ set sqlite_fullsync_count 0
879
+ do_test avtrans-9.$i.1-$cnt {
880
+ execsql {
881
+ BEGIN;
882
+ DELETE FROM t3 WHERE random()%10!=0;
883
+ INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
884
+ INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
885
+ ROLLBACK;
886
+ }
887
+ signature
888
+ } $sig
889
+ do_test avtrans-9.$i.2-$cnt {
890
+ execsql {
891
+ BEGIN;
892
+ DELETE FROM t3 WHERE random()%10!=0;
893
+ INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
894
+ DELETE FROM t3 WHERE random()%10!=0;
895
+ INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
896
+ ROLLBACK;
897
+ }
898
+ signature
899
+ } $sig
900
+ if {$i<$limit} {
901
+ do_test avtrans-9.$i.3-$cnt {
902
+ execsql {
903
+ INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
904
+ }
905
+ } {}
906
+ if {$tcl_platform(platform)=="unix"} {
907
+ do_test avtrans-9.$i.4-$cnt {
908
+ expr {$sqlite_sync_count>0}
909
+ } 1
910
+ ifcapable pager_pragmas {
911
+ do_test avtrans-9.$i.5-$cnt {
912
+ expr {$sqlite_fullsync_count>0}
913
+ } [expr {$i%2==0}]
914
+ } else {
915
+ do_test avtrans-9.$i.5-$cnt {
916
+ expr {$sqlite_fullsync_count==0}
917
+ } {1}
918
+ }
919
+ }
920
+ wal_check_journal_mode avtrans-9.$i-6.$cnt
921
+ }
922
+ set ::pager_old_format 0
923
+ }
924
+ integrity_check avtrans-10.1
925
+ wal_check_journal_mode avtrans-10.2
926
+
927
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/backcompat.test ADDED
@@ -0,0 +1,524 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2010 August 19
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is testing that the current version of SQLite
13
+ # is capable of reading and writing databases created by previous
14
+ # versions, and vice-versa.
15
+ #
16
+ # To use this test, old versions of the testfixture process should be
17
+ # copied into the working directory alongside the new version. The old
18
+ # versions should be named "testfixtureXXX" (or testfixtureXXX.exe on
19
+ # windows), where XXX can be any string.
20
+ #
21
+ # This test file uses the tcl code for controlling a second testfixture
22
+ # process located in lock_common.tcl. See the commments in lock_common.tcl
23
+ # for documentation of the available commands.
24
+ #
25
+
26
+ set testdir [file dirname $argv0]
27
+ source $testdir/tester.tcl
28
+ source $testdir/lock_common.tcl
29
+ source $testdir/malloc_common.tcl
30
+ source $testdir/bc_common.tcl
31
+ db close
32
+
33
+ if {"" == [bc_find_binaries backcompat.test]} {
34
+ finish_test
35
+ return
36
+ }
37
+
38
+ proc do_backcompat_test {rv bin1 bin2 script} {
39
+
40
+ forcedelete test.db
41
+
42
+ if {$bin1 != ""} { set ::bc_chan1 [launch_testfixture $bin1] }
43
+ set ::bc_chan2 [launch_testfixture $bin2]
44
+
45
+ if { $rv } {
46
+ proc code2 {tcl} { uplevel #0 $tcl }
47
+ if {$bin1 != ""} { proc code2 {tcl} { testfixture $::bc_chan1 $tcl } }
48
+ proc code1 {tcl} { testfixture $::bc_chan2 $tcl }
49
+ } else {
50
+ proc code1 {tcl} { uplevel #0 $tcl }
51
+ if {$bin1 != ""} { proc code1 {tcl} { testfixture $::bc_chan1 $tcl } }
52
+ proc code2 {tcl} { testfixture $::bc_chan2 $tcl }
53
+ }
54
+
55
+ proc sql1 sql { code1 [list db eval $sql] }
56
+ proc sql2 sql { code2 [list db eval $sql] }
57
+
58
+ code1 { sqlite3 db test.db }
59
+ code2 { sqlite3 db test.db }
60
+
61
+ foreach c {code1 code2} {
62
+ $c {
63
+ set v [split [db version] .]
64
+ if {[llength $v]==3} {lappend v 0}
65
+ set ::sqlite_libversion [format \
66
+ "%d%.2d%.2d%.2d" [lindex $v 0] [lindex $v 1] [lindex $v 2] [lindex $v 3]
67
+ ]
68
+ }
69
+ }
70
+
71
+ uplevel $script
72
+
73
+ catch { code1 { db close } }
74
+ catch { code2 { db close } }
75
+ catch { close $::bc_chan2 }
76
+ catch { close $::bc_chan1 }
77
+
78
+
79
+ }
80
+
81
+ array set ::incompatible [list]
82
+ proc do_allbackcompat_test {script} {
83
+
84
+ foreach bin $::BC(binaries) {
85
+ set nErr [set_test_counter errors]
86
+ foreach dir {0 1} {
87
+
88
+ set bintag $bin
89
+ regsub {.*testfixture\.} $bintag {} bintag
90
+ set bintag [string map {\.exe {}} $bintag]
91
+ if {$bintag == ""} {set bintag self}
92
+ set ::bcname ".$bintag.$dir."
93
+
94
+ rename do_test _do_test
95
+ proc do_test {nm sql res} {
96
+ set nm [regsub {\.} $nm $::bcname]
97
+ uplevel [list _do_test $nm $sql $res]
98
+ }
99
+
100
+ do_backcompat_test $dir {} $bin $script
101
+
102
+ rename do_test {}
103
+ rename _do_test do_test
104
+ }
105
+ if { $nErr < [set_test_counter errors] } {
106
+ set ::incompatible([get_version $bin]) 1
107
+ }
108
+ }
109
+ }
110
+
111
+ proc read_file {zFile} {
112
+ set zData {}
113
+ if {[file exists $zFile]} {
114
+ set fd [open $zFile]
115
+ fconfigure $fd -translation binary
116
+
117
+ if {[file size $zFile]<=$::sqlite_pending_byte || $zFile != "test.db"} {
118
+ set zData [read $fd]
119
+ } else {
120
+ set zData [read $fd $::sqlite_pending_byte]
121
+ append zData [string repeat x 512]
122
+ seek $fd [expr $::sqlite_pending_byte+512] start
123
+ append zData [read $fd]
124
+ }
125
+
126
+ close $fd
127
+ }
128
+ return $zData
129
+ }
130
+ proc write_file {zFile zData} {
131
+ set fd [open $zFile w]
132
+ fconfigure $fd -translation binary
133
+ puts -nonewline $fd $zData
134
+ close $fd
135
+ }
136
+ proc read_file_system {} {
137
+ set ret [list]
138
+ foreach f {test.db test.db-journal test.db-wal} { lappend ret [read_file $f] }
139
+ set ret
140
+ }
141
+ proc write_file_system {data} {
142
+ foreach f {test.db test.db-journal test.db-wal} d $data {
143
+ if {[string length $d] == 0} {
144
+ forcedelete $f
145
+ } else {
146
+ write_file $f $d
147
+ }
148
+ }
149
+ }
150
+
151
+ #-------------------------------------------------------------------------
152
+ # Actual tests begin here.
153
+ #
154
+ # This first block of tests checks to see that the same database and
155
+ # journal files can be used by old and new versions. WAL and wal-index
156
+ # files are tested separately below.
157
+ #
158
+ do_allbackcompat_test {
159
+
160
+ # Test that database files are backwards compatible.
161
+ #
162
+ do_test backcompat-1.1.1 { sql1 {
163
+ CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
164
+ INSERT INTO t1 VALUES('abc', 'def');
165
+ } } {}
166
+ do_test backcompat-1.1.2 { sql2 { SELECT * FROM t1; } } {abc def}
167
+ do_test backcompat-1.1.3 { sql2 { INSERT INTO t1 VALUES('ghi', 'jkl'); } } {}
168
+ do_test backcompat-1.1.4 { sql1 { SELECT * FROM t1; } } {abc def ghi jkl}
169
+ do_test backcompat-1.1.5 { sql1 { PRAGMA integrity_check } } {ok}
170
+ do_test backcompat-1.1.6 { sql2 { PRAGMA integrity_check } } {ok}
171
+
172
+ # Test that one version can roll back a hot-journal file left in the
173
+ # file-system by the other version.
174
+ #
175
+ # Each test case is named "backcompat-1.X...", where X is either 0 or
176
+ # 1. If it is 0, then the current version creates a journal file that
177
+ # the old versions try to read. Otherwise, if X is 1, then the old version
178
+ # creates the journal file and we try to read it with the current version.
179
+ #
180
+ do_test backcompat-1.2.1 { sql1 {
181
+ PRAGMA cache_size = 10;
182
+ BEGIN;
183
+ INSERT INTO t1 VALUES(randomblob(400), randomblob(400));
184
+ INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
185
+ INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
186
+ INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
187
+ INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM t1;
188
+ COMMIT;
189
+ } } {}
190
+ set cksum1 [sql1 {SELECT md5sum(a), md5sum(b) FROM t1}]
191
+ set cksum2 [sql2 {SELECT md5sum(a), md5sum(b) FROM t1}]
192
+ do_test backcompat-1.2.2 [list string compare $cksum1 $cksum2] 0
193
+
194
+ do_test backcompat-1.2.3 { sql1 {
195
+ BEGIN;
196
+ UPDATE t1 SET a = randomblob(500);
197
+ } } {}
198
+ set data [read_file_system]
199
+
200
+ do_test backcompat-1.2.4 { sql1 { COMMIT } } {}
201
+
202
+ set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}]
203
+ do_test backcompat-1.2.5 [list set {} $same] 0
204
+
205
+ code1 { db close }
206
+ code2 { db close }
207
+ write_file_system $data
208
+ code1 { sqlite3 db test.db }
209
+ code2 { sqlite3 db test.db }
210
+
211
+ set same [expr {[sql2 {SELECT md5sum(a), md5sum(b) FROM t1}] == $cksum2}]
212
+ do_test backcompat-1.2.6 [list set {} $same] 1
213
+
214
+ do_test backcompat-1.2.7 { sql1 { PRAGMA integrity_check } } {ok}
215
+ do_test backcompat-1.2.8 { sql2 { PRAGMA integrity_check } } {ok}
216
+
217
+ do_test backcompat-2.1 {
218
+ sql1 {
219
+ CREATE TABLE t2(a UNIQUE, b PRIMARY KEY, c UNIQUE);
220
+ INSERT INTO t2 VALUES(1,9,5);
221
+ INSERT INTO t2 VALUES(5,5,1);
222
+ INSERT INTO t2 VALUES(9,1,9);
223
+ SELECT * FROM t2 ORDER BY a;
224
+ }
225
+ } {1 9 5 5 5 1 9 1 9}
226
+ do_test backcompat-2.2 {
227
+ sql2 {
228
+ SELECT * FROM sqlite_master WHERE rootpage=-1;
229
+ SELECT * FROM t2 ORDER BY a;
230
+ }
231
+ } {1 9 5 5 5 1 9 1 9}
232
+ do_test backcompat-2.3 {
233
+ sql1 {
234
+ SELECT * FROM t2 ORDER BY b;
235
+ }
236
+ } {9 1 9 5 5 1 1 9 5}
237
+ do_test backcompat-2.4 {
238
+ sql2 {
239
+ SELECT * FROM t2 ORDER BY b;
240
+ }
241
+ } {9 1 9 5 5 1 1 9 5}
242
+ do_test backcompat-2.5 {
243
+ sql1 {
244
+ SELECT * FROM t2 ORDER BY c;
245
+ }
246
+ } {5 5 1 1 9 5 9 1 9}
247
+ do_test backcompat-2.6 {
248
+ sql2 {
249
+ SELECT * FROM t2 ORDER BY c;
250
+ }
251
+ } {5 5 1 1 9 5 9 1 9}
252
+ }
253
+ foreach k [lsort [array names ::incompatible]] {
254
+ puts "ERROR: Detected journal incompatibility with version $k"
255
+ }
256
+ unset ::incompatible
257
+
258
+
259
+ #-------------------------------------------------------------------------
260
+ # Test that WAL and wal-index files may be shared between different
261
+ # SQLite versions.
262
+ #
263
+ do_allbackcompat_test {
264
+ if {[code1 {sqlite3 -version}] >= "3.7.0"
265
+ && [code1 {set ::sqlite_options(wal)}]
266
+ && [code2 {sqlite3 -version}] >= "3.7.0"
267
+ && [code2 {set ::sqlite_options(wal)}]
268
+ } {
269
+
270
+ do_test backcompat-2.1.1 { sql1 {
271
+ PRAGMA journal_mode = WAL;
272
+ CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
273
+ INSERT INTO t1 VALUES('I', 1);
274
+ INSERT INTO t1 VALUES('II', 2);
275
+ INSERT INTO t1 VALUES('III', 3);
276
+ SELECT * FROM t1;
277
+ } } {wal I 1 II 2 III 3}
278
+ do_test backcompat-2.1.2 { sql2 {
279
+ SELECT * FROM t1;
280
+ } } {I 1 II 2 III 3}
281
+
282
+ set data [read_file_system]
283
+ code1 {db close}
284
+ code2 {db close}
285
+ write_file_system $data
286
+ code1 {sqlite3 db test.db}
287
+ code2 {sqlite3 db test.db}
288
+
289
+ # The WAL file now in the file-system was created by the [code1]
290
+ # process. Check that the [code2] process can recover the log.
291
+ #
292
+ do_test backcompat-2.1.3 { sql2 {
293
+ SELECT * FROM t1;
294
+ } } {I 1 II 2 III 3}
295
+ do_test backcompat-2.1.4 { sql1 {
296
+ SELECT * FROM t1;
297
+ } } {I 1 II 2 III 3}
298
+ }
299
+ }
300
+
301
+ #-------------------------------------------------------------------------
302
+ # Test that FTS3 tables may be read/written by different versions of
303
+ # SQLite.
304
+ #
305
+ ifcapable fts3 {
306
+ set contents {
307
+ CREATE VIRTUAL TABLE t1 USING fts3(a, b);
308
+ }
309
+ foreach {num doc} {
310
+ one "jk zm jk eczkjblu urvysbnykk sk gnl jk ttvgf hmjf"
311
+ two "jk bnhc jjrxpjkb mjpavjuhw fibokdry igju jk zm zm xh"
312
+ three "wxe ogttbykvt uhzq xr iaf zf urvysbnykk aayxpmve oacaxgjoo mjpavjuhw"
313
+ four "gazrt jk ephknonq myjp uenvbm wuvajhwqz jk zm xnxhf nvfasfh"
314
+ five "zm aayxpmve csjqxhgj xnxhf xr jk aayxpmve xnxhf zm zm"
315
+ six "sokcyf zm ogyavjvv jk zm fibokdry zm jk igju igju"
316
+ seven "vgsld bvgimjik xuprtlyle jk akmikrqyt jk aayxpmve hkfoudzftq ddjj"
317
+ eight "zm uhzq ovkyevlgv zk uenvbm csjqxhgj jk vgsld pgybs jk"
318
+ nine "zm agmckuiu zexh fibokdry jk uhzq bu tugflixoex xnxhf sk"
319
+ } {
320
+ append contents "INSERT INTO t1 VALUES('$num', '$doc');"
321
+ }
322
+ do_allbackcompat_test {
323
+ if {[code1 {set ::sqlite_options(fts3)}]
324
+ && [code2 {set ::sqlite_options(fts3)}]
325
+ } {
326
+
327
+ do_test backcompat-3.1 { sql1 $contents } {}
328
+
329
+ foreach {n q} {
330
+ 1 "SELECT * FROM t1 ORDER BY a, b"
331
+ 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'"
332
+ 3 "SELECT * FROM t1 WHERE a MATCH 'five'"
333
+ 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'"
334
+ 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'"
335
+ } {
336
+ do_test backcompat-3.2 [list sql1 $q] [sql2 $q]
337
+ }
338
+
339
+ do_test backcompat-3.3 { sql1 {
340
+ INSERT INTO t1 SELECT * FROM t1;
341
+ INSERT INTO t1 SELECT * FROM t1;
342
+ INSERT INTO t1 SELECT * FROM t1;
343
+ INSERT INTO t1 SELECT * FROM t1;
344
+ INSERT INTO t1 SELECT * FROM t1;
345
+ INSERT INTO t1 SELECT * FROM t1;
346
+ INSERT INTO t1 SELECT * FROM t1;
347
+ INSERT INTO t1 SELECT * FROM t1;
348
+ } } {}
349
+
350
+ foreach {n q} {
351
+ 1 "SELECT * FROM t1 ORDER BY a, b"
352
+ 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'"
353
+ 3 "SELECT * FROM t1 WHERE a MATCH 'five'"
354
+ 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'"
355
+ 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'"
356
+ } {
357
+ do_test backcompat-3.4 [list sql1 $q] [sql2 $q]
358
+ }
359
+
360
+ set alphabet "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4"
361
+ for {set i 0} {$i < 900} {incr i} {
362
+ set term "[lindex $alphabet [expr $i/30]][lindex $alphabet [expr $i%30]] "
363
+ sql1 "INSERT INTO t1 VALUES($i, '[string repeat $term 14]')"
364
+ }
365
+
366
+ foreach {n q} {
367
+ 1 "SELECT * FROM t1 ORDER BY a, b"
368
+ 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'"
369
+ 3 "SELECT * FROM t1 WHERE a MATCH 'five'"
370
+ 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'"
371
+ 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'"
372
+
373
+ 6 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'aa'"
374
+ 7 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH '44'"
375
+ 8 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'a*'"
376
+ } {
377
+ do_test backcompat-3.5 [list sql1 $q] [sql2 $q]
378
+ }
379
+
380
+ do_test backcompat-3.6 {
381
+ sql1 "SELECT optimize(t1) FROM t1 LIMIT 1"
382
+ } {{Index optimized}}
383
+
384
+ foreach {n q} {
385
+ 1 "SELECT * FROM t1 ORDER BY a, b"
386
+ 2 "SELECT rowid FROM t1 WHERE a MATCH 'five'"
387
+ 3 "SELECT * FROM t1 WHERE a MATCH 'five'"
388
+ 4 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'jk'"
389
+ 5 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'tug* OR eight'"
390
+
391
+ 6 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'aa'"
392
+ 7 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH '44'"
393
+ 8 "SELECT offsets(t1) FROM t1 WHERE t1 MATCH 'a*'"
394
+ } {
395
+ do_test backcompat-3.7 [list sql1 $q] [sql2 $q]
396
+ }
397
+
398
+ # Now test that an incremental merge can be started by one version
399
+ # and finished by another. And that the integrity-check still
400
+ # passes.
401
+ do_test backcompat-3.8 {
402
+ sql1 {
403
+ DROP TABLE IF EXISTS t1;
404
+ DROP TABLE IF EXISTS t2;
405
+ CREATE TABLE t1(docid, words);
406
+ CREATE VIRTUAL TABLE t2 USING fts3(words);
407
+ }
408
+ code1 [list source $testdir/genesis.tcl]
409
+ code1 { fts_kjv_genesis }
410
+ sql1 {
411
+ INSERT INTO t2 SELECT words FROM t1;
412
+ INSERT INTO t2 SELECT words FROM t1;
413
+ INSERT INTO t2 SELECT words FROM t1;
414
+ INSERT INTO t2 SELECT words FROM t1;
415
+ INSERT INTO t2 SELECT words FROM t1;
416
+ INSERT INTO t2 SELECT words FROM t1;
417
+ SELECT level, group_concat(idx, ' ') FROM t2_segdir GROUP BY level;
418
+ }
419
+ } {0 {0 1 2 3 4 5}}
420
+
421
+ if {[code1 { set ::sqlite_libversion }] >=3071200
422
+ && [code2 { set ::sqlite_libversion }] >=3071200
423
+ } {
424
+ if {[code1 { set ::sqlite_libversion }]<3120000} {
425
+ set res {0 {0 1} 1 0}
426
+ } else {
427
+ set res {1 0}
428
+ }
429
+
430
+ do_test backcompat-3.9 {
431
+ sql1 { INSERT INTO t2(t2) VALUES('merge=100,4'); }
432
+ sql2 { INSERT INTO t2(t2) VALUES('merge=100,4'); }
433
+ sql1 { INSERT INTO t2(t2) VALUES('merge=100,4'); }
434
+ sql2 { INSERT INTO t2(t2) VALUES('merge=2500,4'); }
435
+ sql2 {
436
+ SELECT level, group_concat(idx, ' ') FROM t2_segdir GROUP BY level;
437
+ }
438
+ } $res
439
+
440
+ do_test backcompat-3.10 {
441
+ sql1 { INSERT INTO t2(t2) VALUES('integrity-check') }
442
+ sql2 { INSERT INTO t2(t2) VALUES('integrity-check') }
443
+ } {}
444
+ }
445
+ }
446
+ }
447
+ }
448
+
449
+ #-------------------------------------------------------------------------
450
+ # Test that Rtree tables may be read/written by different versions of
451
+ # SQLite.
452
+ #
453
+ ifcapable rtree {
454
+ set contents {
455
+ CREATE VIRTUAL TABLE t1 USING rtree(id, x1, x2, y1, y2);
456
+ }
457
+ foreach {id x1 x2 y1 y2} {
458
+ 1 -47.64 43.87 33.86 34.42 2 -21.51 17.32 2.05 31.04
459
+ 3 -43.67 -38.33 -19.79 3.43 4 32.41 35.16 9.12 19.82
460
+ 5 33.28 34.87 14.78 28.26 6 49.31 116.59 -9.87 75.09
461
+ 7 -14.93 34.51 -17.64 64.09 8 -43.05 23.43 -1.19 69.44
462
+ 9 44.79 133.56 28.09 80.30 10 -2.66 81.47 -41.38 -10.46
463
+ 11 -42.89 -3.54 15.76 71.63 12 -3.50 84.96 -11.64 64.95
464
+ 13 -45.69 26.25 11.14 55.06 14 -44.09 11.23 17.52 44.45
465
+ 15 36.23 133.49 -19.38 53.67 16 -17.89 81.54 14.64 50.61
466
+ 17 -41.97 -24.04 -39.43 28.95 18 -5.85 7.76 -6.38 47.02
467
+ 19 18.82 27.10 42.82 100.09 20 39.17 113.45 26.14 73.47
468
+ 21 22.31 103.17 49.92 106.05 22 -43.06 40.38 -1.75 76.08
469
+ 23 2.43 57.27 -14.19 -3.83 24 -47.57 -4.35 8.93 100.06
470
+ 25 -37.47 49.14 -29.11 8.81 26 -7.86 75.72 49.34 107.42
471
+ 27 1.53 45.49 20.36 49.74 28 -48.48 32.54 28.81 54.45
472
+ 29 2.67 39.77 -4.05 13.67 30 4.11 62.88 -47.44 -5.72
473
+ 31 -21.47 51.75 37.25 116.09 32 45.59 111.37 -6.43 43.64
474
+ 33 35.23 48.29 23.54 113.33 34 16.61 68.35 -14.69 65.97
475
+ 35 13.98 16.60 48.66 102.87 36 19.74 23.84 31.15 77.27
476
+ 37 -27.61 24.43 7.96 94.91 38 -34.77 12.05 -22.60 -6.29
477
+ 39 -25.83 8.71 -13.48 -12.53 40 -17.11 -1.01 18.06 67.89
478
+ 41 14.13 71.72 -3.78 39.25 42 23.75 76.00 -16.30 8.23
479
+ 43 -39.15 28.63 38.12 125.88 44 48.62 86.09 36.49 102.95
480
+ 45 -31.39 -21.98 2.52 89.78 46 5.65 56.04 15.94 89.10
481
+ 47 18.28 95.81 46.46 143.08 48 30.93 102.82 -20.08 37.36
482
+ 49 -20.78 -3.48 -5.58 35.46 50 49.85 90.58 -24.48 46.29
483
+ } {
484
+ if {$x1 >= $x2 || $y1 >= $y2} { error "$x1 $x2 $y1 $y2" }
485
+ append contents "INSERT INTO t1 VALUES($id, $x1, $x2, $y1, $y2);"
486
+ }
487
+ set queries {
488
+ 1 "SELECT id FROM t1 WHERE x1>10 AND x2<44"
489
+ 2 "SELECT id FROM t1 WHERE y1<100"
490
+ 3 "SELECT id FROM t1 WHERE y1<100 AND x1>0"
491
+ 4 "SELECT id FROM t1 WHERE y1>10 AND x1>0 AND x2<50 AND y2<550"
492
+ }
493
+ do_allbackcompat_test {
494
+ if {[code1 {set ::sqlite_options(fts3)}]
495
+ && [code2 {set ::sqlite_options(fts3)}]
496
+ } {
497
+
498
+ do_test backcompat-4.1 { sql1 $contents } {}
499
+
500
+ foreach {n q} $::queries {
501
+ do_test backcompat-4.2.$n [list sql1 $q] [sql2 $q]
502
+ }
503
+
504
+ do_test backcompat-4.3 { sql1 {
505
+ INSERT INTO t1 SELECT id+100, x1+10.0, x2+10.0, y1-10.0, y2-10.0 FROM t1;
506
+ } } {}
507
+
508
+ foreach {n q} $::queries {
509
+ do_test backcompat-4.4.$n [list sql1 $q] [sql2 $q]
510
+ }
511
+
512
+ do_test backcompat-4.5 { sql2 {
513
+ INSERT INTO t1 SELECT id+200, x1+20.0, x2+20.0, y1-20.0, y2-20.0 FROM t1;
514
+ } } {}
515
+
516
+ foreach {n q} $::queries {
517
+ do_test backcompat-4.6.$n [list sql1 $q] [sql2 $q]
518
+ }
519
+
520
+ }
521
+ }
522
+ }
523
+
524
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/backup.test ADDED
@@ -0,0 +1,1004 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2009 January 30
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is testing the sqlite3_backup_XXX API.
13
+ #
14
+ # $Id: backup.test,v 1.11 2009/06/05 17:09:12 drh Exp $
15
+
16
+ set testdir [file dirname $argv0]
17
+ source $testdir/tester.tcl
18
+
19
+ do_not_use_codec
20
+
21
+ #---------------------------------------------------------------------
22
+ # Test organization:
23
+ #
24
+ # backup-1.*: Warm-body tests.
25
+ #
26
+ # backup-2.*: Test backup under various conditions. To and from in-memory
27
+ # databases. To and from empty/populated databases. etc.
28
+ #
29
+ # backup-3.*: Verify that the locking-page (pending byte page) is handled.
30
+ #
31
+ # backup-4.*: Test various error conditions.
32
+ #
33
+ # backup-5.*: Test the source database being modified during a backup.
34
+ #
35
+ # backup-6.*: Test the backup_remaining() and backup_pagecount() APIs.
36
+ #
37
+ # backup-7.*: Test SQLITE_BUSY and SQLITE_LOCKED errors.
38
+ #
39
+ # backup-8.*: Test multiple simultaneous backup operations.
40
+ #
41
+ # backup-9.*: Test that passing a negative argument to backup_step() is
42
+ # interpreted as "copy the whole file".
43
+ #
44
+ # backup-10.*: Test writing the source database mid backup.
45
+ #
46
+
47
+ proc data_checksum {db file} { $db one "SELECT md5sum(a, b) FROM ${file}.t1" }
48
+ proc test_contents {name db1 file1 db2 file2} {
49
+ $db2 eval {select * from sqlite_master}
50
+ $db1 eval {select * from sqlite_master}
51
+ set checksum [data_checksum $db2 $file2]
52
+ uplevel [list do_test $name [list data_checksum $db1 $file1] $checksum]
53
+ }
54
+
55
+ do_test backup-1.1 {
56
+ execsql {
57
+ BEGIN;
58
+ CREATE TABLE t1(a, b);
59
+ CREATE INDEX i1 ON t1(a, b);
60
+ INSERT INTO t1 VALUES(1, randstr(1000,1000));
61
+ INSERT INTO t1 VALUES(2, randstr(1000,1000));
62
+ INSERT INTO t1 VALUES(3, randstr(1000,1000));
63
+ INSERT INTO t1 VALUES(4, randstr(1000,1000));
64
+ INSERT INTO t1 VALUES(5, randstr(1000,1000));
65
+ COMMIT;
66
+ }
67
+ } {}
68
+
69
+ # Sanity check to verify that the [test_contents] proc works.
70
+ #
71
+ test_contents backup-1.2 db main db main
72
+
73
+ # Check that it is possible to create and finish backup operations.
74
+ #
75
+ do_test backup-1.3.1 {
76
+ delete_file test2.db
77
+ sqlite3 db2 test2.db
78
+ sqlite3_backup B db2 main db main
79
+ } {B}
80
+ do_test backup-1.3.2 {
81
+ B finish
82
+ } {SQLITE_OK}
83
+ do_test backup-1.3.3 {
84
+ info commands B
85
+ } {}
86
+
87
+ # Simplest backup operation. Backup test.db to test2.db. test2.db is
88
+ # initially empty. test.db uses the default page size.
89
+ #
90
+ do_test backup-1.4.1 {
91
+ sqlite3_backup B db2 main db main
92
+ } {B}
93
+ do_test backup-1.4.2 {
94
+ B step 200
95
+ } {SQLITE_DONE}
96
+ do_test backup-1.4.3 {
97
+ B finish
98
+ } {SQLITE_OK}
99
+ do_test backup-1.4.4 {
100
+ info commands B
101
+ } {}
102
+ test_contents backup-1.4.5 db2 main db main
103
+ db close
104
+ db2 close
105
+ #
106
+ # End of backup-1.* tests.
107
+ #---------------------------------------------------------------------
108
+
109
+
110
+ #---------------------------------------------------------------------
111
+ # The following tests, backup-2.*, are based on the following procedure:
112
+ #
113
+ # 1) Populate the source database.
114
+ # 2) Populate the destination database.
115
+ # 3) Run the backup to completion. (backup-2.*.1)
116
+ # 4) Integrity check the destination db. (backup-2.*.2)
117
+ # 5) Check that the contents of the destination db is the same as that
118
+ # of the source db. (backup-2.*.3)
119
+ #
120
+ # The test is run with all possible combinations of the following
121
+ # input parameters, except that if the destination is an in-memory
122
+ # database, the only page size tested is 1024 bytes (the same as the
123
+ # source page-size).
124
+ #
125
+ # * Source database is an in-memory database, OR
126
+ # * Source database is a file-backed database.
127
+ #
128
+ # * Target database is an in-memory database, OR
129
+ # * Target database is a file-backed database.
130
+ #
131
+ # * Destination database is a main file, OR
132
+ # * Destination database is an attached file, OR
133
+ # * Destination database is a temp database.
134
+ #
135
+ # * Target database is empty (zero bytes), OR
136
+ # * Target database is larger than the source, OR
137
+ # * Target database is smaller than the source.
138
+ #
139
+ # * Target database page-size is the same as the source, OR
140
+ # * Target database page-size is larger than the source, OR
141
+ # * Target database page-size is smaller than the source.
142
+ #
143
+ # * Each call to step copies a single page, OR
144
+ # * A single call to step copies the entire source database.
145
+ #
146
+ set iTest 1
147
+ foreach zSrcFile {test.db :memory:} {
148
+ foreach zDestFile {test2.db :memory:} {
149
+ foreach zOpenScript [list {
150
+ sqlite3 db $zSrcFile
151
+ sqlite3 db2 $zSrcFile
152
+ db2 eval "ATTACH '$zDestFile' AS bak"
153
+ set db_dest db2
154
+ set file_dest bak
155
+ } {
156
+ sqlite3 db $zSrcFile
157
+ sqlite3 db2 $zDestFile
158
+ set db_dest db2
159
+ set file_dest main
160
+ } {
161
+ sqlite3 db $zSrcFile
162
+ sqlite3 db2 $zDestFile
163
+ set db_dest db2
164
+ set file_dest temp
165
+ }] {
166
+ foreach rows_dest {0 3 10} {
167
+ foreach pgsz_dest {512 1024 2048 4096} {
168
+ foreach nPagePerStep {1 200} {
169
+
170
+ # Open the databases.
171
+ catch { delete_file test.db }
172
+ catch { delete_file test2.db }
173
+ eval $zOpenScript
174
+
175
+ # Set to true if copying to an in-memory destination. Copying to an
176
+ # in-memory destination is only possible if the initial destination
177
+ # page size is the same as the source page size (in this case 1024 bytes).
178
+ #
179
+ set isMemDest [expr { $zDestFile eq ":memory:" || $file_dest eq "temp" }]
180
+
181
+ if 0 {
182
+ puts -nonewline "Test $iTest: src=$zSrcFile dest=$zDestFile"
183
+ puts -nonewline " (as $db_dest.$file_dest)"
184
+ puts -nonewline " rows_dest=$rows_dest pgsz_dest=$pgsz_dest"
185
+ puts ""
186
+ }
187
+
188
+ if { $isMemDest==0 || $pgsz_dest==1024 || $rows_dest==0 } {
189
+
190
+ # Set up the content of the source database.
191
+ execsql {
192
+ PRAGMA page_size = 1024;
193
+ BEGIN;
194
+ CREATE TABLE t1(a, b);
195
+ CREATE INDEX i1 ON t1(a, b);
196
+ INSERT INTO t1 VALUES(1, randstr(1000,1000));
197
+ INSERT INTO t1 VALUES(2, randstr(1000,1000));
198
+ INSERT INTO t1 VALUES(3, randstr(1000,1000));
199
+ INSERT INTO t1 VALUES(4, randstr(1000,1000));
200
+ INSERT INTO t1 VALUES(5, randstr(1000,1000));
201
+ COMMIT;
202
+ }
203
+
204
+
205
+
206
+ # Set up the content of the target database.
207
+ execsql "PRAGMA ${file_dest}.page_size = ${pgsz_dest}" $db_dest
208
+ if {$rows_dest != 0} {
209
+ execsql "
210
+ BEGIN;
211
+ CREATE TABLE ${file_dest}.t1(a, b);
212
+ CREATE INDEX ${file_dest}.i1 ON t1(a, b);
213
+ " $db_dest
214
+ for {set ii 0} {$ii < $rows_dest} {incr ii} {
215
+ execsql "
216
+ INSERT INTO ${file_dest}.t1 VALUES(1, randstr(1000,1000))
217
+ " $db_dest
218
+ }
219
+ execsql COMMIT $db_dest
220
+ }
221
+
222
+ # Backup the source database.
223
+ do_test backup-2.$iTest.1 {
224
+ sqlite3_backup B $db_dest $file_dest db main
225
+ while {[B step $nPagePerStep]=="SQLITE_OK"} {}
226
+ B finish
227
+ } {SQLITE_OK}
228
+
229
+ # Run integrity check on the backup.
230
+ do_test backup-2.$iTest.2 {
231
+ execsql "PRAGMA ${file_dest}.integrity_check" $db_dest
232
+ } {ok}
233
+
234
+ test_contents backup-2.$iTest.3 db main $db_dest $file_dest
235
+
236
+ }
237
+
238
+ db close
239
+ catch {db2 close}
240
+ incr iTest
241
+
242
+ } } } } } }
243
+ #
244
+ # End of backup-2.* tests.
245
+ #---------------------------------------------------------------------
246
+
247
+ #---------------------------------------------------------------------
248
+ # These tests, backup-3.*, ensure that nothing goes wrong if either
249
+ # the source or destination database are large enough to include the
250
+ # the locking-page (the page that contains the range of bytes that
251
+ # the locks are applied to). These tests assume that the pending
252
+ # byte is at offset 0x00010000 (64KB offset), as set by tester.tcl,
253
+ # not at the 1GB offset as it usually is.
254
+ #
255
+ # The test procedure is as follows (same procedure as used for
256
+ # the backup-2.* tests):
257
+ #
258
+ # 1) Populate the source database.
259
+ # 2) Populate the destination database.
260
+ # 3) Run the backup to completion. (backup-3.*.1)
261
+ # 4) Integrity check the destination db. (backup-3.*.2)
262
+ # 5) Check that the contents of the destination db is the same as that
263
+ # of the source db. (backup-3.*.3)
264
+ #
265
+ # The test procedure is run with the following parameters varied:
266
+ #
267
+ # * Source database includes pending-byte page.
268
+ # * Source database does not include pending-byte page.
269
+ #
270
+ # * Target database includes pending-byte page.
271
+ # * Target database does not include pending-byte page.
272
+ #
273
+ # * Target database page-size is the same as the source, OR
274
+ # * Target database page-size is larger than the source, OR
275
+ # * Target database page-size is smaller than the source.
276
+ #
277
+ set iTest 1
278
+ foreach nSrcPg {10 64 65 66 100} {
279
+ foreach nDestRow {10 100} {
280
+ foreach nDestPgsz {512 1024 2048 4096} {
281
+
282
+ catch { delete_file test.db }
283
+ catch { delete_file test2.db }
284
+ sqlite3 db test.db
285
+ sqlite3 db2 test2.db
286
+
287
+ # Set up the content of the two databases.
288
+ #
289
+ execsql { PRAGMA page_size = 1024 }
290
+ execsql "PRAGMA page_size = $nDestPgsz" db2
291
+ foreach db {db db2} {
292
+ execsql {
293
+ BEGIN;
294
+ CREATE TABLE t1(a, b);
295
+ CREATE INDEX i1 ON t1(a, b);
296
+ COMMIT;
297
+ } $db
298
+ }
299
+ while {[file size test.db]/1024 < $nSrcPg} {
300
+ execsql { INSERT INTO t1 VALUES($ii, randstr(200,200)) }
301
+ }
302
+
303
+ for {set ii 0} {$ii < $nDestRow} {incr ii} {
304
+ execsql { INSERT INTO t1 VALUES($ii, randstr(1000,1000)) } db2
305
+ }
306
+
307
+ # Backup the source database.
308
+ do_test backup-3.$iTest.1 {
309
+ sqlite3_backup B db main db2 main
310
+ while {[B step 10]=="SQLITE_OK"} {}
311
+ B finish
312
+ } {SQLITE_OK}
313
+
314
+ # Run integrity check on the backup.
315
+ do_test backup-3.$iTest.2 {
316
+ execsql "PRAGMA integrity_check" db2
317
+ } {ok}
318
+
319
+ test_contents backup-3.$iTest.3 db main db2 main
320
+
321
+ db close
322
+ db2 close
323
+ incr iTest
324
+ }
325
+ }
326
+ }
327
+
328
+ #--------------------------------------------------------------------
329
+ do_test backup-3.$iTest.1 {
330
+ catch { forcedelete test.db }
331
+ catch { forcedelete test2.db }
332
+ sqlite3 db test.db
333
+ set iTab 1
334
+
335
+ db eval { PRAGMA page_size = 512 }
336
+ while {[file size test.db] <= $::sqlite_pending_byte} {
337
+ db eval "CREATE TABLE t${iTab}(a, b, c)"
338
+ incr iTab
339
+ }
340
+
341
+ sqlite3 db2 test2.db
342
+ db2 eval { PRAGMA page_size = 4096 }
343
+ while {[file size test2.db] < $::sqlite_pending_byte} {
344
+ db2 eval "CREATE TABLE t${iTab}(a, b, c)"
345
+ incr iTab
346
+ }
347
+
348
+ sqlite3_backup B db2 main db main
349
+ B step -1
350
+ } {SQLITE_DONE}
351
+
352
+ do_test backup-3.$iTest.2 {
353
+ B finish
354
+ } {SQLITE_OK}
355
+
356
+ #
357
+ # End of backup-3.* tests.
358
+ #---------------------------------------------------------------------
359
+
360
+
361
+ #---------------------------------------------------------------------
362
+ # The following tests, backup-4.*, test various error conditions:
363
+ #
364
+ # backup-4.1.*: Test invalid database names.
365
+ #
366
+ # backup-4.2.*: Test that the source database cannot be detached while
367
+ # a backup is in progress.
368
+ #
369
+ # backup-4.3.*: Test that the source database handle cannot be closed
370
+ # while a backup is in progress.
371
+ #
372
+ # backup-4.4.*: Test an attempt to specify the same handle for the
373
+ # source and destination databases.
374
+ #
375
+ # backup-4.5.*: Test that an in-memory destination with a different
376
+ # page-size to the source database is an error.
377
+ #
378
+ sqlite3 db test.db
379
+ sqlite3 db2 test2.db
380
+
381
+ do_test backup-4.1.1 {
382
+ catch { sqlite3_backup B db aux db2 main }
383
+ } {1}
384
+ do_test backup-4.1.2 {
385
+ sqlite3_errmsg db
386
+ } {unknown database aux}
387
+ do_test backup-4.1.3 {
388
+ catch { sqlite3_backup B db main db2 aux }
389
+ } {1}
390
+ do_test backup-4.1.4 {
391
+ sqlite3_errmsg db
392
+ } {unknown database aux}
393
+
394
+ do_test backup-4.2.1 {
395
+ catch { forcedelete test3.db }
396
+ catch { forcedelete test4.db }
397
+ execsql {
398
+ ATTACH 'test3.db' AS aux1;
399
+ CREATE TABLE aux1.t1(a, b);
400
+ }
401
+ execsql {
402
+ ATTACH 'test4.db' AS aux2;
403
+ CREATE TABLE aux2.t2(a, b);
404
+ } db2
405
+ sqlite3_backup B db aux1 db2 aux2
406
+ } {B}
407
+ do_test backup-4.2.2 {
408
+ catchsql { DETACH aux2 } db2
409
+ } {1 {database aux2 is locked}}
410
+ do_test backup-4.2.3 {
411
+ B step 50
412
+ } {SQLITE_DONE}
413
+ do_test backup-4.2.4 {
414
+ B finish
415
+ } {SQLITE_OK}
416
+
417
+ do_test backup-4.3.1 {
418
+ sqlite3_backup B db aux1 db2 aux2
419
+ } {B}
420
+ do_test backup-4.3.2 {
421
+ db2 cache flush
422
+ sqlite3_close db2
423
+ } {SQLITE_BUSY}
424
+ do_test backup-4.3.3 {
425
+ sqlite3_errmsg db2
426
+ } {unable to close due to unfinalized statements or unfinished backups}
427
+ do_test backup-4.3.4 {
428
+ B step 50
429
+ } {SQLITE_DONE}
430
+ do_test backup-4.3.5 {
431
+ B finish
432
+ } {SQLITE_OK}
433
+
434
+ do_test backup-4.4.1 {
435
+ set rc [catch {sqlite3_backup B db main db aux1}]
436
+ list $rc [sqlite3_errcode db] [sqlite3_errmsg db]
437
+ } {1 SQLITE_ERROR {source and destination must be distinct}}
438
+ db close
439
+ db2 close
440
+
441
+ do_test backup-4.5.1 {
442
+ catch { forcedelete test.db }
443
+ sqlite3 db test.db
444
+ sqlite3 db2 :memory:
445
+ execsql {
446
+ CREATE TABLE t1(a, b);
447
+ INSERT INTO t1 VALUES(1, 2);
448
+ }
449
+ execsql {
450
+ PRAGMA page_size = 4096;
451
+ CREATE TABLE t2(a, b);
452
+ INSERT INTO t2 VALUES(3, 4);
453
+ } db2
454
+ sqlite3_backup B db2 main db main
455
+ } {B}
456
+ do_test backup-4.5.2 {
457
+ B step 5000
458
+ } {SQLITE_READONLY}
459
+ do_test backup-4.5.3 {
460
+ B finish
461
+ } {SQLITE_READONLY}
462
+
463
+ db close
464
+ db2 close
465
+ #
466
+ # End of backup-4.* tests.
467
+ #---------------------------------------------------------------------
468
+
469
+ #---------------------------------------------------------------------
470
+ # The following tests, backup-5.*, test that the backup works properly
471
+ # when the source database is modified during the backup. Test cases
472
+ # are organized as follows:
473
+ #
474
+ # backup-5.x.1.*: Nothing special. Modify the database mid-backup.
475
+ #
476
+ # backup-5.x.2.*: Modify the database mid-backup so that one or more
477
+ # pages are written out due to cache stress. Then
478
+ # rollback the transaction.
479
+ #
480
+ # backup-5.x.3.*: Database is vacuumed.
481
+ #
482
+ # backup-5.x.4.*: Database is vacuumed and the page-size modified.
483
+ #
484
+ # backup-5.x.5.*: Database is shrunk via incr-vacuum.
485
+ #
486
+ # Each test is run three times, in the following configurations:
487
+ #
488
+ # 1) Backing up file-to-file. The writer writes via an external pager.
489
+ # 2) Backing up file-to-file. The writer writes via the same pager as
490
+ # is used by the backup operation.
491
+ # 3) Backing up memory-to-file.
492
+ #
493
+ set iTest 0
494
+ forcedelete bak.db-wal
495
+ foreach {writer file} {db test.db db3 test.db db :memory:} {
496
+ incr iTest
497
+ catch { delete_file bak.db }
498
+ sqlite3 db2 bak.db
499
+ catch { delete_file $file }
500
+ sqlite3 db $file
501
+ sqlite3 db3 $file
502
+
503
+ do_test backup-5.$iTest.1.1 {
504
+ execsql {
505
+ BEGIN;
506
+ CREATE TABLE t1(a, b);
507
+ CREATE INDEX i1 ON t1(a, b);
508
+ INSERT INTO t1 VALUES(1, randstr(1000,1000));
509
+ INSERT INTO t1 VALUES(2, randstr(1000,1000));
510
+ INSERT INTO t1 VALUES(3, randstr(1000,1000));
511
+ INSERT INTO t1 VALUES(4, randstr(1000,1000));
512
+ INSERT INTO t1 VALUES(5, randstr(1000,1000));
513
+ COMMIT;
514
+ }
515
+ expr {[execsql {PRAGMA page_count}] > 10}
516
+ } {1}
517
+ do_test backup-5.$iTest.1.2 {
518
+ sqlite3_backup B db2 main db main
519
+ B step 5
520
+ } {SQLITE_OK}
521
+ do_test backup-5.$iTest.1.3 {
522
+ execsql { UPDATE t1 SET a = a + 1 } $writer
523
+ B step 50
524
+ } {SQLITE_DONE}
525
+ do_test backup-5.$iTest.1.4 {
526
+ B finish
527
+ } {SQLITE_OK}
528
+ integrity_check backup-5.$iTest.1.5 db2
529
+ test_contents backup-5.$iTest.1.6 db main db2 main
530
+
531
+ do_test backup-5.$iTest.2.1 {
532
+ execsql {
533
+ PRAGMA cache_size = 10;
534
+ BEGIN;
535
+ INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
536
+ INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
537
+ INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
538
+ INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
539
+ COMMIT;
540
+ }
541
+ } {}
542
+ do_test backup-5.$iTest.2.2 {
543
+ sqlite3_backup B db2 main db main
544
+ B step 50
545
+ } {SQLITE_OK}
546
+ do_test backup-5.$iTest.2.3 {
547
+ execsql {
548
+ BEGIN;
549
+ UPDATE t1 SET a = a + 1;
550
+ ROLLBACK;
551
+ } $writer
552
+ B step 5000
553
+ } {SQLITE_DONE}
554
+ do_test backup-5.$iTest.2.4 {
555
+ B finish
556
+ } {SQLITE_OK}
557
+ integrity_check backup-5.$iTest.2.5 db2
558
+ test_contents backup-5.$iTest.2.6 db main db2 main
559
+
560
+ do_test backup-5.$iTest.3.1 {
561
+ execsql { UPDATE t1 SET b = randstr(1000,1000) }
562
+ } {}
563
+ do_test backup-5.$iTest.3.2 {
564
+ sqlite3_backup B db2 main db main
565
+ B step 50
566
+ } {SQLITE_OK}
567
+ do_test backup-5.$iTest.3.3 {
568
+ execsql { VACUUM } $writer
569
+ B step 5000
570
+ } {SQLITE_DONE}
571
+ do_test backup-5.$iTest.3.4 {
572
+ B finish
573
+ } {SQLITE_OK}
574
+ integrity_check backup-5.$iTest.3.5 db2
575
+ test_contents backup-5.$iTest.3.6 db main db2 main
576
+
577
+ do_test backup-5.$iTest.4.1 {
578
+ execsql { UPDATE t1 SET b = randstr(1000,1000) }
579
+ } {}
580
+ do_test backup-5.$iTest.4.2 {
581
+ sqlite3_backup B db2 main db main
582
+ B step 50
583
+ } {SQLITE_OK}
584
+ do_test backup-5.$iTest.4.3 {
585
+ execsql {
586
+ PRAGMA page_size = 2048;
587
+ VACUUM;
588
+ } $writer
589
+ B step 5000
590
+ } {SQLITE_DONE}
591
+ do_test backup-5.$iTest.4.4 {
592
+ B finish
593
+ } {SQLITE_OK}
594
+ integrity_check backup-5.$iTest.4.5 db2
595
+ test_contents backup-5.$iTest.4.6 db main db2 main
596
+
597
+ catch {db close}
598
+ catch {db2 close}
599
+ catch {db3 close}
600
+ catch { delete_file bak.db }
601
+ sqlite3 db2 bak.db
602
+ catch { delete_file $file }
603
+ sqlite3 db $file
604
+ sqlite3 db3 $file
605
+ do_test backup-5.$iTest.5.1 {
606
+ execsql {
607
+ PRAGMA auto_vacuum = incremental;
608
+ BEGIN;
609
+ CREATE TABLE t1(a, b);
610
+ CREATE INDEX i1 ON t1(a, b);
611
+ INSERT INTO t1 VALUES(1, randstr(1000,1000));
612
+ INSERT INTO t1 VALUES(2, randstr(1000,1000));
613
+ INSERT INTO t1 VALUES(3, randstr(1000,1000));
614
+ INSERT INTO t1 VALUES(4, randstr(1000,1000));
615
+ INSERT INTO t1 VALUES(5, randstr(1000,1000));
616
+ COMMIT;
617
+ }
618
+ } {}
619
+ do_test backup-5.$iTest.5.2 {
620
+ sqlite3_backup B db2 main db main
621
+ B step 8
622
+ } {SQLITE_OK}
623
+ do_test backup-5.$iTest.5.3 {
624
+ execsql {
625
+ DELETE FROM t1;
626
+ PRAGMA incremental_vacuum;
627
+ } $writer
628
+ B step 50
629
+ } {SQLITE_DONE}
630
+ do_test backup-5.$iTest.5.4 {
631
+ B finish
632
+ } {SQLITE_OK}
633
+ integrity_check backup-5.$iTest.5.5 db2
634
+ test_contents backup-5.$iTest.5.6 db main db2 main
635
+ catch {db close}
636
+ catch {db2 close}
637
+ catch {db3 close}
638
+ }
639
+ #
640
+ # End of backup-5.* tests.
641
+ #---------------------------------------------------------------------
642
+
643
+ #---------------------------------------------------------------------
644
+ # Test the sqlite3_backup_remaining() and backup_pagecount() APIs.
645
+ #
646
+ do_test backup-6.1 {
647
+ catch { forcedelete test.db }
648
+ catch { forcedelete test2.db }
649
+ sqlite3 db test.db
650
+ sqlite3 db2 test2.db
651
+ execsql {
652
+ BEGIN;
653
+ CREATE TABLE t1(a, b);
654
+ CREATE INDEX i1 ON t1(a, b);
655
+ INSERT INTO t1 VALUES(1, randstr(1000,1000));
656
+ INSERT INTO t1 VALUES(2, randstr(1000,1000));
657
+ INSERT INTO t1 VALUES(3, randstr(1000,1000));
658
+ INSERT INTO t1 VALUES(4, randstr(1000,1000));
659
+ INSERT INTO t1 VALUES(5, randstr(1000,1000));
660
+ COMMIT;
661
+ }
662
+ } {}
663
+ do_test backup-6.2 {
664
+ set nTotal [expr {[file size test.db]/1024}]
665
+ sqlite3_backup B db2 main db main
666
+ B step 1
667
+ } {SQLITE_OK}
668
+ do_test backup-6.3 {
669
+ B pagecount
670
+ } $nTotal
671
+ do_test backup-6.4 {
672
+ B remaining
673
+ } [expr $nTotal-1]
674
+ do_test backup-6.5 {
675
+ B step 5
676
+ list [B remaining] [B pagecount]
677
+ } [list [expr $nTotal-6] $nTotal]
678
+ do_test backup-6.6 {
679
+ execsql { CREATE TABLE t2(a PRIMARY KEY, b) }
680
+ B step 1
681
+ list [B remaining] [B pagecount]
682
+ } [list [expr $nTotal-5] [expr $nTotal+2]]
683
+
684
+ do_test backup-6.X {
685
+ B finish
686
+ } {SQLITE_OK}
687
+
688
+ catch {db close}
689
+ catch {db2 close}
690
+
691
+ #---------------------------------------------------------------------
692
+ # Test cases backup-7.* test that SQLITE_BUSY and SQLITE_LOCKED errors
693
+ # are returned correctly:
694
+ #
695
+ # backup-7.1.*: Source database is externally locked (return SQLITE_BUSY).
696
+ #
697
+ # backup-7.2.*: Attempt to step the backup process while a
698
+ # write-transaction is underway on the source pager (return
699
+ # SQLITE_LOCKED).
700
+ #
701
+ # backup-7.3.*: Destination database is externally locked (return SQLITE_BUSY).
702
+ #
703
+ do_test backup-7.0 {
704
+ catch { forcedelete test.db }
705
+ catch { forcedelete test2.db }
706
+ sqlite3 db2 test2.db
707
+ sqlite3 db test.db
708
+ execsql {
709
+ CREATE TABLE t1(a, b);
710
+ CREATE INDEX i1 ON t1(a, b);
711
+ INSERT INTO t1 VALUES(1, randstr(1000,1000));
712
+ INSERT INTO t1 SELECT a+ 1, randstr(1000,1000) FROM t1;
713
+ INSERT INTO t1 SELECT a+ 2, randstr(1000,1000) FROM t1;
714
+ INSERT INTO t1 SELECT a+ 4, randstr(1000,1000) FROM t1;
715
+ INSERT INTO t1 SELECT a+ 8, randstr(1000,1000) FROM t1;
716
+ INSERT INTO t1 SELECT a+16, randstr(1000,1000) FROM t1;
717
+ INSERT INTO t1 SELECT a+32, randstr(1000,1000) FROM t1;
718
+ INSERT INTO t1 SELECT a+64, randstr(1000,1000) FROM t1;
719
+ }
720
+ } {}
721
+
722
+ do_test backup-7.1.1 {
723
+ sqlite3_backup B db2 main db main
724
+ B step 5
725
+ } {SQLITE_OK}
726
+ do_test backup-7.1.2 {
727
+ sqlite3 db3 test.db
728
+ execsql { BEGIN EXCLUSIVE } db3
729
+ B step 5
730
+ } {SQLITE_BUSY}
731
+ do_test backup-7.1.3 {
732
+ execsql { ROLLBACK } db3
733
+ B step 5
734
+ } {SQLITE_OK}
735
+ do_test backup-7.2.1 {
736
+ execsql {
737
+ BEGIN;
738
+ INSERT INTO t1 VALUES(1, 4);
739
+ }
740
+ } {}
741
+ do_test backup-7.2.2 {
742
+ B step 5000
743
+ } {SQLITE_BUSY}
744
+ do_test backup-7.2.3 {
745
+ execsql { ROLLBACK }
746
+ B step 5000
747
+ } {SQLITE_DONE}
748
+ do_test backup-7.2.4 {
749
+ B finish
750
+ } {SQLITE_OK}
751
+ test_contents backup-7.2.5 db main db2 main
752
+ integrity_check backup-7.3.6 db2
753
+
754
+ do_test backup-7.3.1 {
755
+ db2 close
756
+ db3 close
757
+ forcedelete test2.db
758
+ sqlite3 db2 test2.db
759
+ sqlite3 db3 test2.db
760
+
761
+ sqlite3_backup B db2 main db main
762
+ execsql { BEGIN ; CREATE TABLE t2(a, b); } db3
763
+
764
+ B step 5
765
+ } {SQLITE_BUSY}
766
+ do_test backup-7.3.2 {
767
+ execsql { COMMIT } db3
768
+ B step 5000
769
+ } {SQLITE_DONE}
770
+ do_test backup-7.3.3 {
771
+ B finish
772
+ } {SQLITE_OK}
773
+ test_contents backup-7.3.4 db main db2 main
774
+ integrity_check backup-7.3.5 db2
775
+ catch { db2 close }
776
+ catch { db3 close }
777
+
778
+ #-----------------------------------------------------------------------
779
+ # The following tests, backup-8.*, test attaching multiple backup
780
+ # processes to the same source database. Also, reading from the source
781
+ # database while a read transaction is active.
782
+ #
783
+ # These tests reuse the database "test.db" left over from backup-7.*.
784
+ #
785
+ do_test backup-8.1 {
786
+ catch { forcedelete test2.db }
787
+ catch { forcedelete test3.db }
788
+ sqlite3 db2 test2.db
789
+ sqlite3 db3 test3.db
790
+
791
+ sqlite3_backup B2 db2 main db main
792
+ sqlite3_backup B3 db3 main db main
793
+ list [B2 finish] [B3 finish]
794
+ } {SQLITE_OK SQLITE_OK}
795
+ do_test backup-8.2 {
796
+ sqlite3_backup B3 db3 main db main
797
+ sqlite3_backup B2 db2 main db main
798
+ list [B2 finish] [B3 finish]
799
+ } {SQLITE_OK SQLITE_OK}
800
+ do_test backup-8.3 {
801
+ sqlite3_backup B2 db2 main db main
802
+ sqlite3_backup B3 db3 main db main
803
+ B2 step 5
804
+ } {SQLITE_OK}
805
+ do_test backup-8.4 {
806
+ execsql {
807
+ BEGIN;
808
+ SELECT * FROM sqlite_master;
809
+ }
810
+ B3 step 5
811
+ } {SQLITE_OK}
812
+ do_test backup-8.5 {
813
+ list [B3 step 5000] [B3 finish]
814
+ } {SQLITE_DONE SQLITE_OK}
815
+ do_test backup-8.6 {
816
+ list [B2 step 5000] [B2 finish]
817
+ } {SQLITE_DONE SQLITE_OK}
818
+ test_contents backup-8.7 db main db2 main
819
+ test_contents backup-8.8 db main db3 main
820
+ do_test backup-8.9 {
821
+ execsql { PRAGMA lock_status }
822
+ } {main shared temp closed}
823
+ do_test backup-8.10 {
824
+ execsql COMMIT
825
+ } {}
826
+ catch { db2 close }
827
+ catch { db3 close }
828
+
829
+ #-----------------------------------------------------------------------
830
+ # The following tests, backup-9.*, test that:
831
+ #
832
+ # * Passing 0 as an argument to sqlite3_backup_step() means no pages
833
+ # are backed up (backup-9.1.*), and
834
+ # * Passing a negative value as an argument to sqlite3_backup_step() means
835
+ # all pages are backed up (backup-9.2.*).
836
+ #
837
+ # These tests reuse the database "test.db" left over from backup-7.*.
838
+ #
839
+ do_test backup-9.1.1 {
840
+ sqlite3 db2 test2.db
841
+ sqlite3_backup B db2 main db main
842
+ B step 1
843
+ } {SQLITE_OK}
844
+ do_test backup-9.1.2 {
845
+ set nRemaining [B remaining]
846
+ expr {$nRemaining>100}
847
+ } {1}
848
+ do_test backup-9.1.3 {
849
+ B step 0
850
+ } {SQLITE_OK}
851
+ do_test backup-9.1.4 {
852
+ B remaining
853
+ } $nRemaining
854
+
855
+ do_test backup-9.2.1 {
856
+ B step -1
857
+ } {SQLITE_DONE}
858
+ do_test backup-9.2.2 {
859
+ B remaining
860
+ } {0}
861
+ do_test backup-9.2.3 {
862
+ B finish
863
+ } {SQLITE_OK}
864
+ catch {db2 close}
865
+
866
+ ifcapable memorymanage {
867
+ db close
868
+ forcedelete test.db
869
+ forcedelete bak.db
870
+
871
+ sqlite3 db test.db
872
+ sqlite3 db2 test.db
873
+ sqlite3 db3 bak.db
874
+
875
+ do_test backup-10.1.1 {
876
+ execsql {
877
+ BEGIN;
878
+ CREATE TABLE t1(a, b);
879
+ INSERT INTO t1 VALUES(1, randstr(1000,1000));
880
+ INSERT INTO t1 VALUES(2, randstr(1000,1000));
881
+ INSERT INTO t1 VALUES(3, randstr(1000,1000));
882
+ INSERT INTO t1 VALUES(4, randstr(1000,1000));
883
+ INSERT INTO t1 VALUES(5, randstr(1000,1000));
884
+ CREATE INDEX i1 ON t1(a, b);
885
+ COMMIT;
886
+ }
887
+ } {}
888
+ do_test backup-10.1.2 {
889
+ sqlite3_backup B db3 main db2 main
890
+ B step 5
891
+ } {SQLITE_OK}
892
+ do_test backup-10.1.3 {
893
+ execsql {
894
+ UPDATE t1 SET b = randstr(500,500);
895
+ }
896
+ } {}
897
+ sqlite3_release_memory [expr 1024*1024]
898
+ do_test backup-10.1.3 {
899
+ B step 50
900
+ } {SQLITE_DONE}
901
+ do_test backup-10.1.4 {
902
+ B finish
903
+ } {SQLITE_OK}
904
+ do_test backup-10.1.5 {
905
+ execsql { PRAGMA integrity_check } db3
906
+ } {ok}
907
+
908
+ db2 close
909
+ db3 close
910
+ }
911
+
912
+
913
+ #-----------------------------------------------------------------------
914
+ # Test that if the database is written to via the same database handle being
915
+ # used as the source by a backup operation:
916
+ #
917
+ # 10.1.*: If the db is in-memory, the backup is restarted.
918
+ # 10.2.*: If the db is a file, the backup is not restarted.
919
+ #
920
+ db close
921
+ forcedelete test.db test.db-journal
922
+ foreach {tn file rc} {
923
+ 1 test.db SQLITE_DONE
924
+ 2 :memory: SQLITE_OK
925
+ } {
926
+ do_test backup-10.$tn.1 {
927
+ sqlite3 db $file
928
+ execsql {
929
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);
930
+ BEGIN;
931
+ INSERT INTO t1 VALUES(NULL, randomblob(200));
932
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
933
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
934
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
935
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
936
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
937
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
938
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
939
+ INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
940
+ COMMIT;
941
+ SELECT count(*) FROM t1;
942
+ }
943
+ } {256}
944
+
945
+ do_test backup-10.$tn.2 {
946
+ set pgs [execsql {pragma page_count}]
947
+ expr {$pgs > 50 && $pgs < 75}
948
+ } {1}
949
+
950
+ do_test backup-10.$tn.3 {
951
+ forcedelete bak.db bak.db-journal
952
+ sqlite3 db2 bak.db
953
+ sqlite3_backup B db2 main db main
954
+ B step 50
955
+ } {SQLITE_OK}
956
+
957
+ do_test backup-10.$tn.4 {
958
+ execsql { UPDATE t1 SET b = randomblob(200) WHERE a IN (1, 250) }
959
+ } {}
960
+
961
+ do_test backup-10.$tn.5 {
962
+ B step 50
963
+ } $rc
964
+
965
+ do_test backup-10.$tn.6 {
966
+ B finish
967
+ } {SQLITE_OK}
968
+
969
+ db2 close
970
+ }
971
+
972
+ # 2021-01-31 https://sqlite.org/forum/forumpost/8b39fbf3e7
973
+ #
974
+ do_test backup-11.1 {
975
+ sqlite3 db1 :memory:
976
+ sqlite3 db2 :memory:
977
+ sqlite3_backup B db1 main db2 temp
978
+ B finish
979
+ } {SQLITE_OK}
980
+ db1 close
981
+ db2 close
982
+
983
+ #-------------------------------------------------------------------------
984
+ do_test backup-12.1 {
985
+ sqlite3 db1 :memory:
986
+ sqlite3 db2 :memory:
987
+ db1 eval {
988
+ PRAGMA page_size = 8192;
989
+ CREATE TABLE t1(x);
990
+ }
991
+ db2 eval {
992
+ PRAGMA page_size = 1024;
993
+ CREATE TABLE t2(x);
994
+ }
995
+
996
+ sqlite3_backup B db1 main db2 temp
997
+ B step 100
998
+ B finish
999
+ } {SQLITE_READONLY}
1000
+
1001
+
1002
+
1003
+
1004
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/badutf2.test ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2011 March 15
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library.
12
+ #
13
+ # This file checks to make sure SQLite is able to gracEFully
14
+ # handle malformed UTF-8.
15
+ #
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ proc utf8_to_ustr2 {s} {
21
+ set r ""
22
+ foreach i [split $s ""] {
23
+ scan $i %c c
24
+ append r [format \\u%04.4X $c]
25
+ }
26
+ set r
27
+ }
28
+
29
+ proc utf8_to_hstr {in} {
30
+ regsub -all -- {(..)} $in {%[format "%s" \1]} out
31
+ subst $out
32
+ }
33
+
34
+ proc utf8_to_xstr {in} {
35
+ regsub -all -- {(..)} $in {\\\\x[format "%s" \1]} out
36
+ subst $out
37
+ }
38
+
39
+ proc utf8_to_ustr {in} {
40
+ regsub -all -- {(..)} $in {\\\\u[format "%04.4X" 0x\1]} out
41
+ subst $out
42
+ }
43
+
44
+ do_test badutf2-1.0 {
45
+ db close
46
+ forcedelete test.db
47
+ sqlite3 db test.db
48
+ db eval "PRAGMA encoding = 'UTF-8'"
49
+ } {}
50
+
51
+ do_test badutf2-4.0 {
52
+ set S [sqlite3_prepare_v2 db "SELECT ?" -1 dummy]
53
+ sqlite3_expired $S
54
+ } {0}
55
+
56
+ foreach { i len uval xstr ustr u2u } {
57
+ 1 1 00 \x00 {} {}
58
+ 2 1 01 \x01 "\\u0001" 01
59
+ 3 1 3F \x3F "\\u003F" 3F
60
+ 4 1 7F \x7F "\\u007F" 7F
61
+ 5 1 80 \x80 "\\u0080" C280
62
+ 6 1 C3BF \xFF "\\u00FF" C3BF
63
+ 7 3 EFBFBD \xEF\xBF\xBD "\\uFFFD" {}
64
+ } {
65
+
66
+ set hstr [ utf8_to_hstr $uval ]
67
+
68
+ ifcapable bloblit {
69
+ if {$hstr != "%00"} {
70
+ do_test badutf2-2.1.$i {
71
+ set sql "SELECT '$hstr'=CAST(x'$uval' AS text) AS x;"
72
+ set res [ sqlite3_exec db $sql ]
73
+ lindex [ lindex $res 1] 1
74
+ } {1}
75
+ do_test badutf2-2.2.$i {
76
+ set sql "SELECT CAST('$hstr' AS blob)=x'$uval' AS x;"
77
+ set res [ sqlite3_exec db $sql ]
78
+ lindex [ lindex $res 1] 1
79
+ } {1}
80
+ }
81
+ do_test badutf2-2.3.$i {
82
+ set sql "SELECT hex(CAST(x'$uval' AS text)) AS x;"
83
+ set res [ sqlite3_exec db $sql ]
84
+ lindex [ lindex $res 1] 1
85
+ } $uval
86
+ do_test badutf2-2.4.$i {
87
+ set sql "SELECT hex(CAST(x'$uval' AS text)) AS x;"
88
+ set res [ sqlite3_exec db $sql ]
89
+ lindex [ lindex $res 1] 1
90
+ } $uval
91
+ }
92
+
93
+ if {$hstr != "%00"} {
94
+ do_test badutf2-3.1.$i {
95
+ set sql "SELECT hex('$hstr') AS x;"
96
+ set res [ sqlite3_exec db $sql ]
97
+ lindex [ lindex $res 1] 1
98
+ } $uval
99
+ }
100
+
101
+ # Tcl 8.7 and later do automatic bad-utf8 correction for
102
+ # characters 0x80 thru 0x9f so test case 5 does not work here.
103
+ if {$i==5 && $tcl_version>=8.7} {
104
+ # no-op
105
+ } else {
106
+ do_test badutf2-4.1.$i {
107
+ sqlite3_reset $S
108
+ sqlite3_bind_text $S 1 $xstr $len
109
+ sqlite3_step $S
110
+ utf8_to_ustr2 [ sqlite3_column_text $S 0 ]
111
+ } $ustr
112
+ }
113
+
114
+ ifcapable debug {
115
+ do_test badutf2-5.1.$i {
116
+ utf8_to_utf8 $uval
117
+ } $u2u
118
+ }
119
+
120
+ }
121
+
122
+ do_test badutf2-4.2 {
123
+ sqlite3_finalize $S
124
+ } {SQLITE_OK}
125
+
126
+
127
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/basexx1.test ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2022 November 22
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+ source $testdir/tester.tcl
15
+ set testprefix basexx
16
+
17
+ if {[catch {load_static_extension db basexx} error]} {
18
+ puts "Skipping basexx tests, hit load error: $error"
19
+ finish_test; return
20
+ }
21
+
22
+ # Empty blobs encode to empty strings.
23
+ do_execsql_test 100 {
24
+ SELECT base64(x'')||base85(x'');
25
+ } {{}}
26
+
27
+ # Empty strings decode to empty blobs.
28
+ do_execsql_test 101 {
29
+ SELECT hex(x'01'||base64('')||base85('')||x'02');
30
+ } {0102}
31
+
32
+ # Basic base64 encoding
33
+ do_execsql_test 102 {
34
+ SELECT base64(x'000102030405');
35
+ SELECT base64(x'0001020304');
36
+ SELECT base64(x'00010203');
37
+ } {{AAECAwQF
38
+ } {AAECAwQ=
39
+ } {AAECAw==
40
+ }}
41
+
42
+ # Basic base64 decoding with pad chars
43
+ do_execsql_test 103 {
44
+ SELECT hex(base64('AAECAwQF'));
45
+ SELECT hex(base64('AAECAwQ='));
46
+ SELECT hex(base64('AAECAw=='));
47
+ } {000102030405 0001020304 00010203}
48
+
49
+ # Basic base64 decoding without pad chars and with whitespace
50
+ do_execsql_test 104 {
51
+ SELECT hex(base64(' AAECAwQF '));
52
+ SELECT hex(base64(' AAECAwQ'));
53
+ SELECT hex(base64('AAECAw '));
54
+ } {000102030405 0001020304 00010203}
55
+
56
+ # Basic base85 encoding
57
+ do_execsql_test 105 {
58
+ SELECT base85(x'000102030405');
59
+ SELECT base85(x'0001020304');
60
+ SELECT base85(x'00010203');
61
+ } {{##/2,#2/
62
+ } {##/2,#*
63
+ } {##/2,
64
+ }}
65
+
66
+ # Basic base85 decoding with and without whitespace
67
+ do_execsql_test 106 {
68
+ SELECT hex(base85('##/2,#2/'));
69
+ SELECT hex(base85('##/2,#*'));
70
+ SELECT hex(base85('##/2,'));
71
+ SELECT hex(base85(' ##/2,#2/ '));
72
+ SELECT hex(base85(' ##/2,#*'));
73
+ SELECT hex(base85('##/2, '));
74
+ } {000102030405 0001020304 00010203 000102030405 0001020304 00010203}
75
+
76
+ # Round-trip some random blobs.
77
+ do_execsql_test 107 {
78
+ CREATE TEMP TABLE rb( len int, b blob ) STRICT;
79
+ INSERT INTO rb(len) VALUES (1),(2),(3),(4),(5),(150),(151),(152),(153),(1054);
80
+ UPDATE rb SET b = randomblob(len);
81
+ SELECT len, base64(base64(b))=b, base85(base85(b))=b
82
+ FROM rb ORDER BY len;
83
+ } {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 150 1 1 151 1 1 152 1 1 153 1 1 1054 1 1}
84
+
85
+ # Same round-trip but with space or junk prepended and/or appended or not.
86
+ do_execsql_test 108 {
87
+ CREATE TEMP TABLE junk(j text, rank int);
88
+ INSERT INTO junk VALUES ('',0),(' ',1),('~',2);
89
+ SELECT len, base64(j.j||base64(b)||j.j)=b, base85(j.j||base85(b)||j.j)=b
90
+ FROM rb r, junk j WHERE j.rank=(r.len+r.len/25)%3 ORDER BY len;
91
+ } {1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 150 1 1 151 1 1 152 1 1 153 1 1 1054 1 1}
92
+
93
+ # Exercise the fail-on-too-large result feature.
94
+
95
+ set inLimit [sqlite3_limit db SQLITE_LIMIT_LENGTH -1]
96
+ sqlite3_limit db SQLITE_LIMIT_LENGTH 1300
97
+
98
+ do_catchsql_test 109 {
99
+ SELECT len, base64(b) FROM rb WHERE len>200;
100
+ } {1 {blob expanded to base64 too big}}
101
+
102
+ do_catchsql_test 110 {
103
+ SELECT len, base85(b) FROM rb WHERE len>200;
104
+ } {1 {blob expanded to base85 too big}}
105
+
106
+ do_catchsql_test 111 {
107
+ SELECT length(base85(b))=1335 FROM rb WHERE len=1054;
108
+ } {1 {blob expanded to base85 too big}}
109
+
110
+ sqlite3_limit db SQLITE_LIMIT_LENGTH $inLimit
111
+
112
+ # Exercise is_base85(t)
113
+
114
+ do_execsql_test 112 {
115
+ SELECT is_base85(' '||base85(x'123456')||char(10)),
116
+ is_base85('#$%&*+,-./0123456789:;<=>?@'
117
+ ||'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
118
+ ||'[\]^_`'
119
+ ||'abcdefghijklmnopqrstuvwxyz'),
120
+ is_base85('!'), is_base85('"'), is_base85(''''), is_base85('('),
121
+ is_base85(')'), is_base85(char(123)), is_base85('|'), is_base85(char(125)),
122
+ is_base85('~'), is_base85(char(127));
123
+ } {1 1 0 0 0 0 0 0 0 0 0 0}
124
+
125
+ do_execsql_test 113 {
126
+ SELECT is_base85(NULL) IS NULL;
127
+ } {1}
128
+
129
+ do_catchsql_test 114 {
130
+ SELECT is_base85(1);
131
+ } {1 {is_base85 accepts only text or NULL}}
132
+
133
+ do_catchsql_test 115 {
134
+ SELECT is_base85(1.1);
135
+ } {1 {is_base85 accepts only text or NULL}}
136
+
137
+ do_catchsql_test 116 {
138
+ SELECT is_base85(x'00');
139
+ } {1 {is_base85 accepts only text or NULL}}
140
+
141
+ # Round-trip many bigger random blobs.
142
+
143
+ do_execsql_test 117 {
144
+ CREATE TABLE bs(b blob, num);
145
+ INSERT INTO bs SELECT randomblob(4000 + n%3), n
146
+ FROM (
147
+ WITH RECURSIVE seq(n) AS (
148
+ VALUES(1) UNION ALL SELECT n+1
149
+ FROM seq WHERE n<100
150
+ ) SELECT n FROM seq);
151
+ SELECT num FROM bs WHERE base64(base64(b))!=b;
152
+ SELECT num FROM bs WHERE base85(base85(b))!=b;
153
+ } {}
154
+
155
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/bestindex3.test ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2016 May 29
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+
12
+ set testdir [file dirname $argv0]
13
+ source $testdir/tester.tcl
14
+ set testprefix bestindex3
15
+
16
+ ifcapable !vtab {
17
+ finish_test
18
+ return
19
+ }
20
+
21
+ #-------------------------------------------------------------------------
22
+ # Virtual table callback for a virtual table named $tbl.
23
+ #
24
+ # The table created is:
25
+ #
26
+ # "CREATE TABLE t1 (a, b, c)"
27
+ #
28
+ # This virtual table supports both LIKE and = operators on all columns.
29
+ #
30
+ proc vtab_cmd {bOmit method args} {
31
+ switch -- $method {
32
+ xConnect {
33
+ return "CREATE TABLE t1(a, b, c)"
34
+ }
35
+
36
+ xBestIndex {
37
+ set hdl [lindex $args 0]
38
+ set clist [$hdl constraints]
39
+ set orderby [$hdl orderby]
40
+ set mask [$hdl mask]
41
+
42
+ set ret [list]
43
+ set use use
44
+ if {$bOmit} {set use omit}
45
+
46
+ for {set i 0} {$i < [llength $clist]} {incr i} {
47
+ array unset C
48
+ array set C [lindex $clist $i]
49
+ if {$C(usable) && ($C(op)=="like" || $C(op)=="eq")} {
50
+ lappend ret $use $i
51
+ lappend ret idxstr
52
+ lappend ret "[lindex {a b c} $C(column)] [string toupper $C(op)] ?"
53
+ break
54
+ }
55
+ }
56
+
57
+ if {$ret==""} {
58
+ lappend ret cost 1000000 rows 1000000
59
+ } else {
60
+ lappend ret cost 100 rows 10
61
+ }
62
+ return $ret
63
+ }
64
+
65
+ xFilter {
66
+ foreach {idxnum idxstr param} $args {}
67
+ set where ""
68
+ if {$bOmit && $idxstr != ""} {
69
+ set where " WHERE [string map [list ? '$param' EQ =] $idxstr]"
70
+ }
71
+ return [list sql "SELECT rowid, * FROM ttt$where"]
72
+ }
73
+ }
74
+ return ""
75
+ }
76
+
77
+ register_tcl_module db
78
+
79
+ do_execsql_test 1.0 {
80
+ CREATE VIRTUAL TABLE t1 USING tcl("vtab_cmd 0");
81
+ }
82
+
83
+ do_eqp_test 1.1 {
84
+ SELECT * FROM t1 WHERE a LIKE 'abc';
85
+ } {SCAN t1 VIRTUAL TABLE INDEX 0:a LIKE ?}
86
+
87
+ do_eqp_test 1.2 {
88
+ SELECT * FROM t1 WHERE a = 'abc';
89
+ } {SCAN t1 VIRTUAL TABLE INDEX 0:a EQ ?}
90
+
91
+ do_eqp_test 1.3 {
92
+ SELECT * FROM t1 WHERE a = 'abc' OR b = 'def';
93
+ } {
94
+ QUERY PLAN
95
+ `--MULTI-INDEX OR
96
+ |--INDEX 1
97
+ | `--SCAN t1 VIRTUAL TABLE INDEX 0:a EQ ?
98
+ `--INDEX 2
99
+ `--SCAN t1 VIRTUAL TABLE INDEX 0:b EQ ?
100
+ }
101
+
102
+ do_eqp_test 1.4 {
103
+ SELECT * FROM t1 WHERE a LIKE 'abc%' OR b = 'def';
104
+ } {
105
+ QUERY PLAN
106
+ `--MULTI-INDEX OR
107
+ |--INDEX 1
108
+ | `--SCAN t1 VIRTUAL TABLE INDEX 0:a LIKE ?
109
+ `--INDEX 2
110
+ `--SCAN t1 VIRTUAL TABLE INDEX 0:b EQ ?
111
+ }
112
+
113
+ do_execsql_test 1.5 {
114
+ CREATE TABLE ttt(a, b, c);
115
+
116
+ INSERT INTO ttt VALUES(1, 'two', 'three');
117
+ INSERT INTO ttt VALUES(2, 'one', 'two');
118
+ INSERT INTO ttt VALUES(3, 'three', 'one');
119
+ INSERT INTO ttt VALUES(4, 'y', 'one');
120
+ INSERT INTO ttt VALUES(5, 'x', 'two');
121
+ INSERT INTO ttt VALUES(6, 'y', 'three');
122
+ }
123
+
124
+ foreach omit {0 1} {
125
+ do_execsql_test 1.6.$omit.0 "
126
+ DROP TABLE t1;
127
+ CREATE VIRTUAL TABLE t1 USING tcl('vtab_cmd $omit');
128
+ "
129
+ do_execsql_test 1.6.$omit.1 {
130
+ SELECT rowid FROM t1 WHERE c LIKE 'o%'
131
+ } {3 4}
132
+
133
+ do_execsql_test 1.6.$omit.2 {
134
+ SELECT rowid FROM t1 WHERE c LIKE 'o%' OR b='y'
135
+ } {3 4 6}
136
+
137
+ do_execsql_test 1.6.$omit.3 {
138
+ SELECT rowid FROM t1 WHERE c = 'three' OR c LIKE 'o%'
139
+ } {1 6 3 4}
140
+ }
141
+
142
+ #-------------------------------------------------------------------------
143
+ # Test the same pattern works with ordinary tables.
144
+ #
145
+ # This test does not work if the ICU extension is enabled. ICU overrides
146
+ # LIKE - and this optimization only works with the built-in LIKE function.
147
+ #
148
+ ifcapable !icu {
149
+ do_execsql_test 2.1 {
150
+ CREATE TABLE t2(x TEXT COLLATE nocase, y TEXT);
151
+ CREATE INDEX t2x ON t2(x COLLATE nocase);
152
+ CREATE INDEX t2y ON t2(y);
153
+ }
154
+
155
+ do_eqp_test 2.2 {
156
+ SELECT * FROM t2 WHERE x LIKE 'abc%' OR y = 'def'
157
+ } [string map {"\n " \n} {
158
+ QUERY PLAN
159
+ `--MULTI-INDEX OR
160
+ |--INDEX 1
161
+ | `--SEARCH t2 USING INDEX t2x (x>? AND x<?)
162
+ `--INDEX 2
163
+ `--SEARCH t2 USING INDEX t2y (y=?)
164
+ }]
165
+ }
166
+
167
+ #-------------------------------------------------------------------------
168
+ # Test that any PRIMARY KEY within a sqlite3_decl_vtab() CREATE TABLE
169
+ # statement is currently ignored.
170
+ #
171
+ proc vvv_command {method args} {
172
+ switch -- $method {
173
+ xConnect { return "CREATE TABLE t1(a PRIMARY KEY, b, c)" }
174
+ }
175
+ }
176
+ proc yyy_command {method args} {
177
+ switch -- $method {
178
+ xConnect { return "CREATE TABLE t1(a, b, c, PRIMARY KEY(a, b))" }
179
+ }
180
+ }
181
+
182
+ do_execsql_test 3.1 { CREATE VIRTUAL TABLE t3 USING tcl('vvv_command') }
183
+ do_execsql_test 3.2 { CREATE VIRTUAL TABLE t4 USING tcl('yyy_command') }
184
+
185
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/bestindex8.test ADDED
@@ -0,0 +1,463 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2020-01-29
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+ source $testdir/tester.tcl
15
+ set testprefix bestindex8
16
+
17
+ ifcapable !vtab {
18
+ finish_test
19
+ return
20
+ }
21
+
22
+ register_tcl_module db
23
+
24
+ proc vtab_command {src method args} {
25
+ switch -- $method {
26
+ xConnect {
27
+ return "CREATE TABLE xxx(a, b)"
28
+ }
29
+
30
+ xBestIndex {
31
+ set hdl [lindex $args 0]
32
+ set clist [$hdl constraints]
33
+ set orderby [$hdl orderby]
34
+ lappend ::lBestIndexDistinct [$hdl distinct]
35
+
36
+ #puts "ORDERBY: $orderby"
37
+ set iCons 0
38
+ set ret [list]
39
+ foreach cons $clist {
40
+ catch { array unset C }
41
+ array set C $cons
42
+ if {$C(usable)} {
43
+ lappend ret use $iCons
44
+ }
45
+ incr iCons
46
+ }
47
+ if {$orderby=="{column 0 desc 0} {column 1 desc 0}"
48
+ || $orderby=="{column 0 desc 0}"
49
+ } {
50
+ lappend ret orderby 1
51
+ lappend ret idxnum 1
52
+ set ::lOrderByConsumed 1
53
+ }
54
+ return $ret
55
+ }
56
+
57
+ xFilter {
58
+ set idxnum [lindex $args 0]
59
+ if {$idxnum} {
60
+ return [list sql "SELECT rowid, a, b FROM $src order by 2, 3"]
61
+ }
62
+ return [list sql "SELECT rowid, a, b FROM $src"]
63
+ }
64
+
65
+ }
66
+
67
+ return {}
68
+ }
69
+
70
+ do_execsql_test 1.0 {
71
+ CREATE TABLE t1(a, b);
72
+ CREATE INDEX i1 ON t1(a, b);
73
+ INSERT INTO t1 VALUES('a', 'b'), ('c', 'd');
74
+ INSERT INTO t1 VALUES('a', 'b'), ('c', 'd');
75
+ CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
76
+
77
+ CREATE TABLE t0(c0);
78
+ INSERT INTO t0(c0) VALUES (1), (0);
79
+ }
80
+
81
+ foreach {tn sql bDistinct idxinsert bConsumed res} {
82
+ 1 "SELECT a, b FROM vt1" 0 0 0 {a b c d a b c d}
83
+ 2 "SELECT DISTINCT a, b FROM vt1" 2 1 1 {a b c d}
84
+ 3 "SELECT DISTINCT a FROM vt1" 2 1 1 {a c}
85
+ 4 "SELECT DISTINCT b FROM vt1" 2 1 0 {b d}
86
+ 5 "SELECT DISTINCT b FROM vt1 ORDER BY a" 0 1 1 {b d}
87
+ 6 "SELECT DISTINCT t0.c0 FROM vt1, t0 ORDER BY vt1.a" 0 1 1 {1 0}
88
+ 7 "SELECT DISTINCT a, b FROM vt1 ORDER BY a, b" 3 0 1 {a b c d}
89
+ 8 "SELECT DISTINCT a, b FROM vt1 ORDER BY a" 0 1 1 {a b c d}
90
+ 9 "SELECT DISTINCT a FROM vt1 ORDER BY a, b" 0 1 1 {a c}
91
+
92
+ 10 "SELECT DISTINCT a, b FROM vt1 WHERE b='b'" 2 1 1 {a b}
93
+ 11 "SELECT DISTINCT a, b FROM vt1 WHERE +b='b'" 2 1 1 {a b}
94
+ } {
95
+ set ::lBestIndexDistinct ""
96
+ set ::lOrderByConsumed 0
97
+ do_execsql_test 1.$tn.1 $sql $res
98
+ do_test 1.$tn.2 {
99
+ set ::lBestIndexDistinct
100
+ } $bDistinct
101
+ do_test 1.$tn.3 {
102
+ expr {[lsearch [execsql "explain $sql"] IdxInsert]>=0}
103
+ } $idxinsert
104
+ do_test 1.$tn.4 {
105
+ set ::lOrderByConsumed
106
+ } $bConsumed
107
+ }
108
+
109
+ #-------------------------------------------------------------------------
110
+ reset_db
111
+ register_tcl_module db
112
+
113
+ proc vtab_command {src method args} {
114
+ switch -- $method {
115
+ xConnect {
116
+ return "CREATE TABLE xxx(a, b)"
117
+ }
118
+
119
+ xBestIndex {
120
+ set hdl [lindex $args 0]
121
+ set ret [list]
122
+
123
+ set iCons 0
124
+ foreach cons [$hdl constraints] {
125
+ array set C $cons
126
+ if {($C(op)=="limit" || $C(op)=="offset") && $C(usable)} {
127
+ lappend ret use $iCons
128
+ }
129
+ incr iCons
130
+ }
131
+
132
+ return $ret
133
+ }
134
+
135
+ xFilter {
136
+ lappend ::lFilterArgs [lindex $args 2]
137
+ return [list sql "SELECT rowid, a, b FROM $src"]
138
+ }
139
+
140
+ }
141
+
142
+ return {}
143
+ }
144
+
145
+ do_execsql_test 2.0 {
146
+ CREATE TABLE t1(a, b);
147
+ CREATE INDEX i1 ON t1(a, b);
148
+ CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
149
+ }
150
+
151
+ do_test 2.1 {
152
+ set ::lFilterArgs [list]
153
+ execsql { SELECT * FROM vt1 LIMIT 10 }
154
+ set ::lFilterArgs
155
+ } {10}
156
+
157
+ do_test 2.2 {
158
+ set ::lFilterArgs [list]
159
+ execsql { SELECT * FROM vt1 LIMIT 5 OFFSET 50 }
160
+ set ::lFilterArgs
161
+ } {{50 5}}
162
+
163
+ do_test 2.3 {
164
+ set ::lFilterArgs [list]
165
+ execsql { SELECT * FROM vt1 ORDER BY a, b LIMIT 1 OFFSET 1 }
166
+ set ::lFilterArgs
167
+ } {{1 1}}
168
+
169
+ do_test 2.4 {
170
+ set ::lFilterArgs [list]
171
+ execsql { SELECT * FROM vt1 ORDER BY a, +b LIMIT 1 OFFSET 1 }
172
+ set ::lFilterArgs
173
+ } {{}}
174
+
175
+ #-------------------------------------------------------------------------
176
+ reset_db
177
+ register_tcl_module db
178
+
179
+ proc vtab_command {src method args} {
180
+ switch -- $method {
181
+ xConnect {
182
+ return "CREATE TABLE xxx(a, b)"
183
+ }
184
+
185
+ xBestIndex {
186
+ set hdl [lindex $args 0]
187
+ set lCons [$hdl constraints]
188
+
189
+ set ret [list]
190
+ for {set i 0} {$i < [llength $lCons]} {incr i} {
191
+ array set C [lindex $lCons $i]
192
+ if {$C(usable)} {
193
+ lappend ret use $i
194
+ $hdl in $i 1
195
+ }
196
+ }
197
+ return $ret
198
+ }
199
+
200
+ xFilter {
201
+ set lArg [lindex $args 2]
202
+ lappend ::lFilterArg {*}$lArg
203
+ return [list sql "SELECT rowid, a, b FROM $src"]
204
+ }
205
+
206
+ }
207
+
208
+ return {}
209
+ }
210
+
211
+ do_execsql_test 3.0 {
212
+ CREATE TABLE t1(a, b);
213
+ CREATE INDEX i1 ON t1(a, b);
214
+ CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
215
+ }
216
+
217
+ foreach {tn sql lfa} {
218
+ 1 "SELECT * FROM vt1 WHERE b IN (10, 20, 30)" {{10 20 30}}
219
+ 2 "SELECT * FROM vt1 WHERE b IN ('abc', 'def')" {{abc def}}
220
+ 3 "SELECT * FROM vt1 WHERE a IS NULL AND b IN ('abc', 'def')" {{} {abc def}}
221
+ 4 "SELECT * FROM vt1 WHERE a IN (1,2,3) AND b IN ('abc', 'def')"
222
+ {{1 2 3} {abc def}}
223
+
224
+ 5 "SELECT * FROM vt1
225
+ WHERE a IN (SELECT 1 UNION SELECT 2) AND b IN ('abc', 'def')"
226
+ {{1 2} {abc def}}
227
+
228
+ 6 "SELECT * FROM vt1
229
+ WHERE b IN ('abc', 'def') AND a IN (SELECT 1 UNION SELECT 2)"
230
+ {{abc def} {1 2}}
231
+ } {
232
+ do_test 3.$tn {
233
+ set ::lFilterArg [list]
234
+ execsql $sql
235
+ set ::lFilterArg
236
+ } $lfa
237
+ }
238
+
239
+ #explain_i { SELECT * FROM vt1 WHERE b IN (10, 20, 30) }
240
+
241
+ #-------------------------------------------------------------------------
242
+ reset_db
243
+ register_tcl_module db
244
+
245
+ proc vtab_command {src method args} {
246
+ switch -- $method {
247
+ xConnect {
248
+ return "CREATE TABLE xxx(a, b, c)"
249
+ }
250
+
251
+ xBestIndex {
252
+ set hdl [lindex $args 0]
253
+ set lCons [$hdl constraints]
254
+
255
+ set ret [list]
256
+ for {set i 0} {$i < [llength $lCons]} {incr i} {
257
+ lappend ::lBestIndexRhs [$hdl rhs_value $i -]
258
+ }
259
+ return $ret
260
+ }
261
+
262
+ xFilter {
263
+ return [list sql "SELECT rowid, a, b, c FROM $src"]
264
+ }
265
+
266
+ }
267
+
268
+ return {}
269
+ }
270
+
271
+ do_execsql_test 4.0 {
272
+ CREATE TABLE t1(a, b, c);
273
+ CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
274
+ }
275
+
276
+ foreach {tn sql lbir} {
277
+ 1 "SELECT * FROM vt1 WHERE b = 10" {10}
278
+ 2 "SELECT * FROM vt1 WHERE a = 'abc' AND b < 30" {abc 30}
279
+ 3 "SELECT * FROM vt1 WHERE a = 'abc' AND b < 30+2" {abc -}
280
+ 4 "SELECT * FROM vt1 WHERE a IN (1,2,3) AND b < 30+2" {- -}
281
+ 5 "SELECT * FROM vt1 WHERE a IS 111 AND b < 30+2" {111 -}
282
+ } {
283
+ do_test 4.$tn {
284
+ set ::lBestIndexRhs [list]
285
+ execsql $sql
286
+ set ::lBestIndexRhs
287
+ } $lbir
288
+ }
289
+
290
+ #-------------------------------------------------------------------------
291
+ reset_db
292
+ db cache size 0
293
+ register_tcl_module db
294
+
295
+ set ::vtab_handle_in 1
296
+ proc vtab_command {src method args} {
297
+ switch -- $method {
298
+ xConnect {
299
+ return "CREATE TABLE xxx(a, b, c)"
300
+ }
301
+
302
+ xBestIndex {
303
+ set lCols [list a b c]
304
+
305
+ set hdl [lindex $args 0]
306
+ set lCons [$hdl constraints]
307
+ set lOrder [$hdl order]
308
+
309
+ set L ""
310
+ set O ""
311
+ set W [list]
312
+ set a 0
313
+ for {set i 0} {$i < [llength $lCons]} {incr i} {
314
+ array set C [lindex $lCons $i]
315
+ if {$C(usable)} {
316
+ if { $C(op)=="eq" } {
317
+ set bIn 0
318
+ if {$::vtab_handle_in} { set bIn [$hdl in $i 1] }
319
+ if {$bIn} {
320
+ lappend W "[lindex $lCols $C(column)] IN (%I$a%)"
321
+ } else {
322
+ lappend W "[lindex $lCols $C(column)] = %$a%"
323
+ }
324
+ lappend ret omit $i
325
+ }
326
+ if { $C(op)=="limit" } { set L " LIMIT %$a%" ; lappend ret use $i }
327
+ if { $C(op)=="offset" } { set O " OFFSET %$a%" ; lappend ret use $i }
328
+ incr a
329
+ }
330
+ }
331
+
332
+ set order ""
333
+ set selectlist "rowid, a, b, c"
334
+ if {[llength $lOrder]} {
335
+ array set sl [list]
336
+ set lO [list]
337
+ foreach s $lOrder {
338
+ array set C $s
339
+ set ad ""
340
+ if {$C(desc)} { set ad " DESC" }
341
+ lappend lO "[lindex $lCols $C(column)]$ad"
342
+ set sl($C(column)) 1
343
+ }
344
+ if {[$hdl distinct]==2} {
345
+ set selectlist "DISTINCT 0"
346
+ foreach i {0 1 2} {
347
+ if {[info exists sl($i)]} {
348
+ append selectlist ", [lindex $lCols $i]"
349
+ } else {
350
+ append selectlist ", 0"
351
+ }
352
+ }
353
+ } else {
354
+ set order " ORDER BY [join $lO ,]"
355
+ }
356
+ }
357
+
358
+ set where ""
359
+ if {[llength $W]} { set where " WHERE [join $W { AND }]" }
360
+ set sql "SELECT $selectlist FROM $src$where$order$L$O"
361
+
362
+ lappend ret idxStr $sql
363
+ return $ret
364
+ }
365
+
366
+ xFilter {
367
+ foreach {idxnum idxstr lArg} $args {}
368
+ set ii 0
369
+ set sql $idxstr
370
+ foreach a $lArg {
371
+ set sql [string map [list %$ii% $a] $sql]
372
+ set sql [string map [list %I$ii% [join $a ,]] $sql]
373
+ incr ii
374
+ }
375
+ lappend ::lFilterSql $sql
376
+
377
+ if {[regexp {OFFSET (.*)$} $sql -> off]} {
378
+ set real_sql "
379
+ WITH c(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<$off )
380
+ SELECT 0,0,0,0 FROM c
381
+ UNION ALL SELECT * FROM (
382
+ $sql
383
+ )
384
+ "
385
+ } else {
386
+ set real_sql $sql
387
+ }
388
+
389
+ return [list sql $real_sql]
390
+ }
391
+
392
+ }
393
+
394
+ return {}
395
+ }
396
+
397
+ do_execsql_test 5.0 {
398
+ CREATE TABLE t1(a, b, c);
399
+ CREATE VIRTUAL TABLE vt1 USING tcl(vtab_command t1);
400
+ INSERT INTO t1 VALUES(1, 2, 3);
401
+ INSERT INTO t1 VALUES(2, 3, 4);
402
+ INSERT INTO t1 VALUES(3, 4, 5);
403
+ INSERT INTO t1 VALUES(1, 5, 6);
404
+ INSERT INTO t1 VALUES(2, 6, 7);
405
+ INSERT INTO t1 VALUES(3, 7, 8);
406
+ INSERT INTO t1 VALUES(1, 8, 9);
407
+ INSERT INTO t1 VALUES(2, 9, 0);
408
+ }
409
+
410
+ proc do_vtab_test {tn sql vtsql {res {}}} {
411
+ set ::lFilterSql [list]
412
+ uplevel [list do_execsql_test $tn.1 $sql $res]
413
+ uplevel [list do_test $tn.2 {set ::lFilterSql} [list {*}$vtsql]]
414
+ }
415
+
416
+ do_vtab_test 5.1.1 {
417
+ SELECT DISTINCT a FROM vt1
418
+ } {
419
+ {SELECT DISTINCT 0, a, 0, 0 FROM t1}
420
+ } {1 2 3}
421
+
422
+ do_vtab_test 5.1.2 {
423
+ SELECT DISTINCT a FROM vt1 ORDER BY a
424
+ } {
425
+ {SELECT rowid, a, b, c FROM t1 ORDER BY a}
426
+ } {1 2 3}
427
+
428
+ do_vtab_test 5.1.3 {
429
+ SELECT DISTINCT a FROM vt1 WHERE c IN (4,5,6,7,8)
430
+ } {
431
+ {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c IN (4,5,6,7,8)}
432
+ } {2 3 1}
433
+
434
+ set ::vtab_handle_in 0
435
+ do_vtab_test 5.1.4 {
436
+ SELECT DISTINCT a FROM vt1 WHERE c IN (4,5,6,7,8)
437
+ } {
438
+ {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 4}
439
+ {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 5}
440
+ {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 6}
441
+ {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 7}
442
+ {SELECT DISTINCT 0, a, 0, 0 FROM t1 WHERE c = 8}
443
+ } {2 3 1}
444
+
445
+ set ::vtab_handle_in 1
446
+ do_vtab_test 5.1.5a {
447
+ SELECT a, b, c FROM vt1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2
448
+ } {
449
+ {SELECT rowid, a, b, c FROM t1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2}
450
+ } {1 5 6 2 6 7}
451
+
452
+ set ::vtab_handle_in 0
453
+ do_vtab_test 5.1.5b {
454
+ SELECT a, b, c FROM vt1 WHERE c IN (4,5,6,7,8) LIMIT 2 OFFSET 2
455
+ } {
456
+ {SELECT rowid, a, b, c FROM t1 WHERE c = 4}
457
+ {SELECT rowid, a, b, c FROM t1 WHERE c = 5}
458
+ {SELECT rowid, a, b, c FROM t1 WHERE c = 6}
459
+ {SELECT rowid, a, b, c FROM t1 WHERE c = 7}
460
+ } {1 5 6 2 6 7}
461
+ set ::vtab_handle_in 1
462
+
463
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/bestindex9.test ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2020-01-29
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+ source $testdir/tester.tcl
15
+ set testprefix bestindex9
16
+
17
+ ifcapable !vtab {
18
+ finish_test
19
+ return
20
+ }
21
+
22
+
23
+ proc vtab_command {method args} {
24
+ switch -- $method {
25
+ xConnect {
26
+ return $::create_table
27
+ }
28
+
29
+ xBestIndex {
30
+ set hdl [lindex $args 0]
31
+ set ::vtab_orderby [$hdl orderby]
32
+ set ::vtab_distinct [$hdl distinct]
33
+
34
+ return [list orderby 1]
35
+ }
36
+
37
+ xFilter {
38
+ return ""
39
+ }
40
+ }
41
+
42
+ return {}
43
+ }
44
+
45
+ proc do_bestindex9_test {tn create select orderby distinct} {
46
+ forcedelete test.db2
47
+ sqlite3 dbtest test.db2
48
+ register_tcl_module dbtest
49
+
50
+ set ::create_table $create
51
+ dbtest eval { CREATE VIRTUAL TABLE t1 USING tcl(vtab_command); }
52
+ dbtest eval $select
53
+
54
+ do_test $tn.orderby [list set {} $::vtab_orderby] $orderby
55
+ do_test $tn.distinct [list set {} $::vtab_distinct] $distinct
56
+
57
+ dbtest close
58
+ }
59
+
60
+ #-------------------------------------------------------------------------
61
+ #
62
+ do_bestindex9_test 1.0 {
63
+ CREATE TABLE x(k1, k2, v1, PRIMARY KEY(k1, k2))
64
+ } {
65
+ SELECT DISTINCT k1, k2 FROM t1
66
+ } {{column 0 desc 0} {column 1 desc 0}} 2
67
+
68
+ do_bestindex9_test 1.1 {
69
+ CREATE TABLE x(k1, k2, v1, PRIMARY KEY(k1, k2)) WITHOUT ROWID
70
+ } {
71
+ SELECT DISTINCT k1, k2 FROM t1
72
+ } {} 0
73
+
74
+ do_bestindex9_test 1.2 {
75
+ CREATE TABLE x(k1 NOT NULL, k2 NOT NULL, v1, PRIMARY KEY(k1, k2))
76
+ } {
77
+ SELECT DISTINCT k1, k2 FROM t1
78
+ } {} 0
79
+
80
+ #-------------------------------------------------------------------------
81
+ #
82
+ do_bestindex9_test 2 {
83
+ CREATE TABLE x(c1, c2, c3);
84
+ } {
85
+ SELECT DISTINCT c1 FROM t1 ORDER BY c1
86
+ } {{column 0 desc 0}} 3
87
+
88
+ #-------------------------------------------------------------------------
89
+ #
90
+ do_bestindex9_test 3 {
91
+ CREATE TABLE x(c1, c2, c3);
92
+ } {
93
+ SELECT DISTINCT c1 FROM t1 GROUP BY c1
94
+ } {{column 0 desc 0}} 1
95
+
96
+ do_bestindex9_test 4 {
97
+ CREATE TABLE x(c1, c2, c3);
98
+ } {
99
+ CREATE TABLE t2(balls);
100
+ SELECT DISTINCT c1 FROM t1, t2
101
+ } {{column 0 desc 0}} 2
102
+
103
+
104
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/between.test ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2005 July 28
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is testing the use of indices in WHERE clauses
13
+ # when the WHERE clause contains the BETWEEN operator.
14
+ #
15
+ # $Id: between.test,v 1.2 2006/01/17 09:35:02 danielk1977 Exp $
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ # Build some test data
21
+ #
22
+ do_test between-1.0 {
23
+ execsql {
24
+ BEGIN;
25
+ CREATE TABLE t1(w int, x int, y int, z int);
26
+ }
27
+ for {set i 1} {$i<=100} {incr i} {
28
+ set w $i
29
+ set x [expr {int(log($i)/log(2))}]
30
+ set y [expr {$i*$i + 2*$i + 1}]
31
+ set z [expr {$x+$y}]
32
+ ifcapable tclvar {
33
+ # Random unplanned test of the $varname variable syntax.
34
+ execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)}
35
+ } else {
36
+ # If the $varname syntax is not available, use the regular variable
37
+ # declaration syntax.
38
+ execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)}
39
+ }
40
+ }
41
+ execsql {
42
+ CREATE UNIQUE INDEX i1w ON t1(w);
43
+ CREATE INDEX i1xy ON t1(x,y);
44
+ CREATE INDEX i1zyx ON t1(z,y,x);
45
+ COMMIT;
46
+ }
47
+ } {}
48
+
49
+ # This procedure executes the SQL. Then it appends to the result the
50
+ # "sort" or "nosort" keyword depending on whether or not any sorting
51
+ # is done. Then it appends the names of the table and index used.
52
+ #
53
+ proc queryplan {sql} {
54
+ set ::sqlite_sort_count 0
55
+ set data [execsql $sql]
56
+ if {$::sqlite_sort_count} {set x sort} {set x nosort}
57
+ lappend data $x
58
+ set eqp [execsql "EXPLAIN QUERY PLAN $sql"]
59
+ # puts eqp=$eqp
60
+ foreach {a b c x} $eqp {
61
+ if {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \
62
+ $x all ss as tab idx]} {
63
+ lappend data $tab $idx
64
+ } elseif {[regexp {(SCAN|SEARCH) (\w+ AS )?(\w+)\y} $x all ss as tab]} {
65
+ lappend data $tab *
66
+ }
67
+ }
68
+ return $data
69
+ }
70
+
71
+ do_test between-1.1.1 {
72
+ queryplan {
73
+ SELECT * FROM t1 WHERE w BETWEEN 5 AND 6 ORDER BY +w
74
+ }
75
+ } {5 2 36 38 6 2 49 51 sort t1 i1w}
76
+ do_test between-1.1.2 {
77
+ queryplan {
78
+ SELECT * FROM t1 WHERE +w BETWEEN 5 AND 6 ORDER BY +w
79
+ }
80
+ } {5 2 36 38 6 2 49 51 sort t1 *}
81
+ do_test between-1.2.1 {
82
+ queryplan {
83
+ SELECT * FROM t1 WHERE w BETWEEN 5 AND 65-y ORDER BY +w
84
+ }
85
+ } {5 2 36 38 6 2 49 51 sort t1 i1w}
86
+ do_test between-1.2.2 {
87
+ queryplan {
88
+ SELECT * FROM t1 WHERE +w BETWEEN 5 AND 65-y ORDER BY +w
89
+ }
90
+ } {5 2 36 38 6 2 49 51 sort t1 *}
91
+ do_test between-1.3.1 {
92
+ queryplan {
93
+ SELECT * FROM t1 WHERE w BETWEEN 41-y AND 6 ORDER BY +w
94
+ }
95
+ } {5 2 36 38 6 2 49 51 sort t1 i1w}
96
+ do_test between-1.3.2 {
97
+ queryplan {
98
+ SELECT * FROM t1 WHERE +w BETWEEN 41-y AND 6 ORDER BY +w
99
+ }
100
+ } {5 2 36 38 6 2 49 51 sort t1 *}
101
+ do_test between-1.4 {
102
+ queryplan {
103
+ SELECT * FROM t1 WHERE w BETWEEN 41-y AND 65-y ORDER BY +w
104
+ }
105
+ } {5 2 36 38 6 2 49 51 sort t1 *}
106
+ do_test between-1.5.1 {
107
+ queryplan {
108
+ SELECT * FROM t1 WHERE 26 BETWEEN y AND z ORDER BY +w
109
+ }
110
+ } {4 2 25 27 sort t1 i1zyx}
111
+ do_test between-1.5.2 {
112
+ queryplan {
113
+ SELECT * FROM t1 WHERE 26 BETWEEN +y AND z ORDER BY +w
114
+ }
115
+ } {4 2 25 27 sort t1 i1zyx}
116
+ do_test between-1.5.3 {
117
+ queryplan {
118
+ SELECT * FROM t1 WHERE 26 BETWEEN y AND +z ORDER BY +w
119
+ }
120
+ } {4 2 25 27 sort t1 *}
121
+
122
+ #-------------------------------------------------------------------------
123
+ reset_db
124
+ do_execsql_test between-2.0 {
125
+ CREATE TABLE t1(x TEXT, y TEXT COLLATE nocase);
126
+ INSERT INTO t1 VALUES('0', 'abc');
127
+ }
128
+
129
+ foreach {tn expr res} {
130
+ 1 "x BETWEEN 1 AND '5'" 0
131
+ 2 "x COLLATE binary BETWEEN 1 AND '5'" 0
132
+ 3 "x COLLATE nocase BETWEEN 1 AND '5'" 0
133
+
134
+ 4 "y BETWEEN 'A' AND 'B'" 1
135
+ 5 "y COLLATE nocase BETWEEN 'A' AND 'B'" 1
136
+ 6 "y COLLATE binary BETWEEN 'A' AND 'B'" 0
137
+ 7 "(y COLLATE binary) BETWEEN 'A' AND 'B'" 0
138
+ } {
139
+ set sql "SELECT $expr FROM t1"
140
+ do_execsql_test between-2.1.$tn $sql $res
141
+ }
142
+
143
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/bigmmap.test ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2017 August 07
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script testing the ability of SQLite to use mmap
13
+ # to access files larger than 4GiB.
14
+ #
15
+
16
+ if {[file exists skip-big-file]} return
17
+ if {$tcl_platform(os)=="Darwin"} return
18
+
19
+ set testdir [file dirname $argv0]
20
+ source $testdir/tester.tcl
21
+ set testprefix bigmmap
22
+
23
+ ifcapable !mmap||!vtab {
24
+ finish_test
25
+ return
26
+ }
27
+
28
+ set mmap_limit 0
29
+ db eval {
30
+ SELECT compile_options AS x FROM pragma_compile_options
31
+ WHERE x LIKE 'max_mmap_size=%'
32
+ } {
33
+ regexp {MAX_MMAP_SIZE=([0-9]*)} $x -> mmap_limit
34
+ }
35
+ if {$mmap_limit < [expr 8 * 1<<30]} {
36
+ puts "Skipping bigmmap.test - requires SQLITE_MAX_MMAP_SIZE >= 8G"
37
+ finish_test
38
+ return
39
+ }
40
+
41
+
42
+ #-------------------------------------------------------------------------
43
+ # Create the database file roughly 8GiB in size. Most pages are unused,
44
+ # except that there is a table and index clustered around each 1GiB
45
+ # boundary.
46
+ #
47
+ do_execsql_test 1.0 {
48
+ PRAGMA page_size = 4096;
49
+ CREATE TABLE t0(a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c));
50
+ WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 100 )
51
+ INSERT INTO t0 SELECT i, 't0', randomblob(800) FROM s;
52
+ }
53
+
54
+ for {set i 1} {$i < 8} {incr i} {
55
+ if {[catch {fake_big_file [expr $i*1024] [get_pwd]/test.db}]} {
56
+ puts "Cannot create ${i}MB sparse file"
57
+ finish_test
58
+ return
59
+ }
60
+ hexio_write test.db 28 [format %.8x [expr ($i*1024*1024*1024/4096) - 5]]
61
+
62
+ do_execsql_test 1.$i "
63
+ CREATE TABLE t$i (a INTEGER PRIMARY KEY, b, c, UNIQUE(b, c));
64
+ WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 100 )
65
+ INSERT INTO t$i SELECT i, 't$i', randomblob(800) FROM s;
66
+ "
67
+ }
68
+
69
+ #-------------------------------------------------------------------------
70
+ # Check that data can be retrieved from the db with a variety of
71
+ # configured mmap size limits.
72
+ #
73
+ for {set i 0} {$i < 9} {incr i} {
74
+
75
+ # Configure a memory mapping $i GB in size.
76
+ #
77
+ set val [expr $i*1024*1024*1024]
78
+ execsql "PRAGMA main.mmap_size = $val"
79
+ do_execsql_test 2.$i.0 {
80
+ PRAGMA main.mmap_size
81
+ } $val
82
+
83
+ for {set t 0} {$t < 8} {incr t} {
84
+ do_execsql_test 2.$i.$t.1 "
85
+ SELECT count(*) FROM t$t;
86
+ SELECT count(b || c) FROM t$t GROUP BY b;
87
+ " {100 100}
88
+
89
+ do_execsql_test 2.$i.$t.2 "
90
+ SELECT * FROM t$t AS o WHERE
91
+ NOT EXISTS( SELECT * FROM t$t AS i WHERE a=o.a AND +b=o.b AND +c=o.c )
92
+ ORDER BY b, c;
93
+ " {}
94
+
95
+ do_eqp_test 2.$i.$t.3 "
96
+ SELECT * FROM t$t AS o WHERE
97
+ NOT EXISTS( SELECT * FROM t$t AS i WHERE a=o.a AND +b=o.b AND +c=o.c )
98
+ ORDER BY b, c;
99
+ " [string map {"\n " "\n"} "
100
+ QUERY PLAN
101
+ |--SCAN o USING COVERING INDEX sqlite_autoindex_t${t}_1
102
+ `--CORRELATED SCALAR SUBQUERY xxxxxx
103
+ `--SEARCH i USING INTEGER PRIMARY KEY (rowid=?)
104
+ "]
105
+ }
106
+ }
107
+
108
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/bigrow.test ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2001 September 23
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this file is stressing the library by putting large amounts
13
+ # of data in a single row of a table.
14
+ #
15
+ # $Id: bigrow.test,v 1.5 2004/08/07 23:54:48 drh Exp $
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ # Make a big string that we can use for test data
21
+ #
22
+ do_test bigrow-1.0 {
23
+ set ::bigstr {}
24
+ for {set i 1} {$i<=9999} {incr i} {
25
+ set sep [string index "abcdefghijklmnopqrstuvwxyz" [expr {$i%26}]]
26
+ append ::bigstr "$sep [format %04d $i] "
27
+ }
28
+ string length $::bigstr
29
+ } {69993}
30
+
31
+ # Make a table into which we can insert some but records.
32
+ #
33
+ do_test bigrow-1.1 {
34
+ execsql {
35
+ CREATE TABLE t1(a text, b text, c text);
36
+ SELECT name FROM sqlite_master
37
+ WHERE type='table' OR type='index'
38
+ ORDER BY name
39
+ }
40
+ } {t1}
41
+
42
+ do_test bigrow-1.2 {
43
+ set ::big1 [string range $::bigstr 0 65519]
44
+ set sql "INSERT INTO t1 VALUES('abc',"
45
+ append sql "'$::big1', 'xyz');"
46
+ execsql $sql
47
+ execsql {SELECT a, c FROM t1}
48
+ } {abc xyz}
49
+ do_test bigrow-1.3 {
50
+ execsql {SELECT b FROM t1}
51
+ } [list $::big1]
52
+ do_test bigrow-1.4 {
53
+ set ::big2 [string range $::bigstr 0 65520]
54
+ set sql "INSERT INTO t1 VALUES('abc2',"
55
+ append sql "'$::big2', 'xyz2');"
56
+ set r [catch {execsql $sql} msg]
57
+ lappend r $msg
58
+ } {0 {}}
59
+ do_test bigrow-1.4.1 {
60
+ execsql {SELECT b FROM t1 ORDER BY c}
61
+ } [list $::big1 $::big2]
62
+ do_test bigrow-1.4.2 {
63
+ execsql {SELECT c FROM t1 ORDER BY c}
64
+ } {xyz xyz2}
65
+ do_test bigrow-1.4.3 {
66
+ execsql {DELETE FROM t1 WHERE a='abc2'}
67
+ execsql {SELECT c FROM t1}
68
+ } {xyz}
69
+
70
+ do_test bigrow-1.5 {
71
+ execsql {
72
+ UPDATE t1 SET a=b, b=a;
73
+ SELECT b,c FROM t1
74
+ }
75
+ } {abc xyz}
76
+ do_test bigrow-1.6 {
77
+ execsql {
78
+ SELECT * FROM t1
79
+ }
80
+ } [list $::big1 abc xyz]
81
+ do_test bigrow-1.7 {
82
+ execsql {
83
+ INSERT INTO t1 VALUES('1','2','3');
84
+ INSERT INTO t1 VALUES('A','B','C');
85
+ SELECT b FROM t1 WHERE a=='1';
86
+ }
87
+ } {2}
88
+ do_test bigrow-1.8 {
89
+ execsql "SELECT b FROM t1 WHERE a=='$::big1'"
90
+ } {abc}
91
+ do_test bigrow-1.9 {
92
+ execsql "SELECT b FROM t1 WHERE a!='$::big1' ORDER BY a"
93
+ } {2 B}
94
+
95
+ # Try doing some indexing on big columns
96
+ #
97
+ do_test bigrow-2.1 {
98
+ execsql {
99
+ CREATE INDEX i1 ON t1(a)
100
+ }
101
+ execsql "SELECT b FROM t1 WHERE a=='$::big1'"
102
+ } {abc}
103
+ do_test bigrow-2.2 {
104
+ execsql {
105
+ UPDATE t1 SET a=b, b=a
106
+ }
107
+ execsql "SELECT b FROM t1 WHERE a=='abc'"
108
+ } [list $::big1]
109
+ do_test bigrow-2.3 {
110
+ execsql {
111
+ UPDATE t1 SET a=b, b=a
112
+ }
113
+ execsql "SELECT b FROM t1 WHERE a=='$::big1'"
114
+ } {abc}
115
+ catch {unset ::bigstr}
116
+ catch {unset ::big1}
117
+ catch {unset ::big2}
118
+
119
+ # Mosts of the tests above were created back when rows were limited in
120
+ # size to 64K. Now rows can be much bigger. Test that logic. Also
121
+ # make sure things work correctly at the transition boundries between
122
+ # row sizes of 256 to 257 bytes and from 65536 to 65537 bytes.
123
+ #
124
+ # We begin by testing the 256..257 transition.
125
+ #
126
+ do_test bigrow-3.1 {
127
+ execsql {
128
+ DELETE FROM t1;
129
+ INSERT INTO t1(a,b,c) VALUES('one','abcdefghijklmnopqrstuvwxyz0123','hi');
130
+ }
131
+ execsql {SELECT a,length(b),c FROM t1}
132
+ } {one 30 hi}
133
+ do_test bigrow-3.2 {
134
+ execsql {
135
+ UPDATE t1 SET b=b||b;
136
+ UPDATE t1 SET b=b||b;
137
+ UPDATE t1 SET b=b||b;
138
+ }
139
+ execsql {SELECT a,length(b),c FROM t1}
140
+ } {one 240 hi}
141
+ for {set i 1} {$i<10} {incr i} {
142
+ do_test bigrow-3.3.$i {
143
+ execsql "UPDATE t1 SET b=b||'$i'"
144
+ execsql {SELECT a,length(b),c FROM t1}
145
+ } "one [expr {240+$i}] hi"
146
+ }
147
+
148
+ # Now test the 65536..65537 row-size transition.
149
+ #
150
+ do_test bigrow-4.1 {
151
+ execsql {
152
+ DELETE FROM t1;
153
+ INSERT INTO t1(a,b,c) VALUES('one','abcdefghijklmnopqrstuvwxyz0123','hi');
154
+ }
155
+ execsql {SELECT a,length(b),c FROM t1}
156
+ } {one 30 hi}
157
+ do_test bigrow-4.2 {
158
+ execsql {
159
+ UPDATE t1 SET b=b||b;
160
+ UPDATE t1 SET b=b||b;
161
+ UPDATE t1 SET b=b||b;
162
+ UPDATE t1 SET b=b||b;
163
+ UPDATE t1 SET b=b||b;
164
+ UPDATE t1 SET b=b||b;
165
+ UPDATE t1 SET b=b||b;
166
+ UPDATE t1 SET b=b||b;
167
+ UPDATE t1 SET b=b||b;
168
+ UPDATE t1 SET b=b||b;
169
+ UPDATE t1 SET b=b||b;
170
+ UPDATE t1 SET b=b||b;
171
+ }
172
+ execsql {SELECT a,length(b),c FROM t1}
173
+ } {one 122880 hi}
174
+ do_test bigrow-4.3 {
175
+ execsql {
176
+ UPDATE t1 SET b=substr(b,1,65515)
177
+ }
178
+ execsql {SELECT a,length(b),c FROM t1}
179
+ } {one 65515 hi}
180
+ for {set i 1} {$i<10} {incr i} {
181
+ do_test bigrow-4.4.$i {
182
+ execsql "UPDATE t1 SET b=b||'$i'"
183
+ execsql {SELECT a,length(b),c FROM t1}
184
+ } "one [expr {65515+$i}] hi"
185
+ }
186
+
187
+ # Check to make sure the library recovers safely if a row contains
188
+ # too much data.
189
+ #
190
+ do_test bigrow-5.1 {
191
+ execsql {
192
+ DELETE FROM t1;
193
+ INSERT INTO t1(a,b,c) VALUES('one','abcdefghijklmnopqrstuvwxyz0123','hi');
194
+ }
195
+ execsql {SELECT a,length(b),c FROM t1}
196
+ } {one 30 hi}
197
+ set i 1
198
+ for {set sz 60} {$sz<1048560} {incr sz $sz} {
199
+ do_test bigrow-5.2.$i {
200
+ execsql {
201
+ UPDATE t1 SET b=b||b;
202
+ SELECT a,length(b),c FROM t1;
203
+ }
204
+ } "one $sz hi"
205
+ incr i
206
+ }
207
+ do_test bigrow-5.3 {
208
+ catchsql {UPDATE t1 SET b=b||b}
209
+ } {0 {}}
210
+ do_test bigrow-5.4 {
211
+ execsql {SELECT length(b) FROM t1}
212
+ } 1966080
213
+ do_test bigrow-5.5 {
214
+ catchsql {UPDATE t1 SET b=b||b}
215
+ } {0 {}}
216
+ do_test bigrow-5.6 {
217
+ execsql {SELECT length(b) FROM t1}
218
+ } 3932160
219
+ do_test bigrow-5.99 {
220
+ execsql {DROP TABLE t1}
221
+ } {}
222
+
223
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/bind2.test ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2022 Feb 10
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ #
12
+
13
+ set testdir [file dirname $argv0]
14
+ source $testdir/tester.tcl
15
+ set testprefix bind2
16
+
17
+
18
+ # Test that using bind_value() on a REAL sqlite3_value that was stored
19
+ # as an INTEGER works properly.
20
+ #
21
+ # 1.1: An IntReal value read from a table,
22
+ # 1.2: IntReal values obtained via the sqlite3_preupdate_old|new()
23
+ # interfaces.
24
+ #
25
+ do_execsql_test 1.0 {
26
+ CREATE TABLE t1(a REAL);
27
+ INSERT INTO t1 VALUES(42.0);
28
+ SELECT * FROM t1;
29
+ } {42.0}
30
+
31
+ do_test 1.1 {
32
+ set stmt [sqlite3_prepare db "SELECT ?" -1 tail]
33
+ sqlite3_bind_value_from_select $stmt 1 "SELECT a FROM t1"
34
+ sqlite3_step $stmt
35
+ sqlite3_column_text $stmt 0
36
+ } {42.0}
37
+ sqlite3_finalize $stmt
38
+
39
+ ifcapable !preupdate {
40
+ finish_test
41
+ return
42
+ }
43
+
44
+ proc preup {args} {
45
+ set stmt [sqlite3_prepare db "SELECT ?" -1 tail]
46
+ sqlite3_bind_value_from_preupdate $stmt 1 old 0
47
+ sqlite3_step $stmt
48
+ lappend ::reslist [sqlite3_column_text $stmt 0]
49
+ sqlite3_reset $stmt
50
+ sqlite3_bind_value_from_preupdate $stmt 1 new 0
51
+ sqlite3_step $stmt
52
+ lappend ::reslist [sqlite3_column_text $stmt 0]
53
+ sqlite3_finalize $stmt
54
+ }
55
+ db preupdate hook preup
56
+
57
+ do_test 1.2 {
58
+ set ::reslist [list]
59
+ execsql { UPDATE t1 SET a=43; }
60
+ set ::reslist
61
+ } {42.0 43.0}
62
+
63
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/bindxfer.test ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 2005 April 21
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library. The
12
+ # focus of this script testing the sqlite_transfer_bindings() API.
13
+ #
14
+ # $Id: bindxfer.test,v 1.9 2009/04/17 11:56:28 drh Exp $
15
+ #
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ proc sqlite_step {stmt VALS COLS} {
21
+ upvar #0 $VALS vals
22
+ upvar #0 $COLS cols
23
+ set vals [list]
24
+ set cols [list]
25
+
26
+ set rc [sqlite3_step $stmt]
27
+ for {set i 0} {$i < [sqlite3_column_count $stmt]} {incr i} {
28
+ lappend cols [sqlite3_column_name $stmt $i]
29
+ }
30
+ for {set i 0} {$i < [sqlite3_data_count $stmt]} {incr i} {
31
+ lappend vals [sqlite3_column_text $stmt $i]
32
+ }
33
+
34
+ return $rc
35
+ }
36
+
37
+ do_test bindxfer-1.1 {
38
+ set DB [sqlite3_connection_pointer db]
39
+ execsql {CREATE TABLE t1(a,b,c);}
40
+ set VM1 [sqlite3_prepare $DB {SELECT ?, ?, ?} -1 TAIL]
41
+ set TAIL
42
+ } {}
43
+ do_test bindxfer-1.2 {
44
+ sqlite3_bind_parameter_count $VM1
45
+ } 3
46
+ do_test bindxfer-1.3 {
47
+ set VM2 [sqlite3_prepare $DB {SELECT ?, ?, ?} -1 TAIL]
48
+ set TAIL
49
+ } {}
50
+ do_test bindxfer-1.4 {
51
+ sqlite3_bind_parameter_count $VM2
52
+ } 3
53
+ ifcapable deprecated {
54
+ do_test bindxfer-1.5 {
55
+ sqlite_bind $VM1 1 one normal
56
+ set sqlite_static_bind_value two
57
+ sqlite_bind $VM1 2 {} static
58
+ sqlite_bind $VM1 3 {} null
59
+ sqlite3_transfer_bindings $VM1 $VM2
60
+ sqlite_step $VM1 VALUES COLNAMES
61
+ } SQLITE_ROW
62
+ do_test bindxfer-1.6 {
63
+ set VALUES
64
+ } {{} {} {}}
65
+ do_test bindxfer-1.7 {
66
+ sqlite_step $VM2 VALUES COLNAMES
67
+ } SQLITE_ROW
68
+ do_test bindxfer-1.8 {
69
+ set VALUES
70
+ } {one two {}}
71
+ }
72
+ catch {sqlite3_finalize $VM1}
73
+ catch {sqlite3_finalize $VM2}
74
+
75
+
76
+ finish_test
local-test-sqlite3-delta-03/afc-sqlite3/test/boundary3.tcl ADDED
@@ -0,0 +1,289 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ puts {# 2008 December 11
2
+ #
3
+ # The author disclaims copyright to this source code. In place of
4
+ # a legal notice, here is a blessing:
5
+ #
6
+ # May you do good and not evil.
7
+ # May you find forgiveness for yourself and forgive others.
8
+ # May you share freely, never taking more than you give.
9
+ #
10
+ #***********************************************************************
11
+ # This file implements regression tests for SQLite library.
12
+ #
13
+ # This file is automatically generated from a separate TCL script.
14
+ # This file seeks to exercise integer boundary values.
15
+ #
16
+
17
+ set testdir [file dirname $argv0]
18
+ source $testdir/tester.tcl
19
+
20
+ # Many of the boundary tests depend on a working 64-bit implementation.
21
+ if {![working_64bit_int]} { finish_test; return }
22
+ }
23
+
24
+ expr srand(0)
25
+
26
+ # Generate interesting boundary numbers
27
+ #
28
+ foreach x {
29
+ 0
30
+ 1
31
+ 0x7f
32
+ 0x7fff
33
+ 0x7fffff
34
+ 0x7fffffff
35
+ 0x7fffffffff
36
+ 0x7fffffffffff
37
+ 0x7fffffffffffff
38
+ 0x7fffffffffffffff
39
+ } {
40
+ set x [expr {wide($x)}]
41
+ set boundarynum($x) 1
42
+ set boundarynum([expr {wide($x+1)}]) 1
43
+ set boundarynum([expr {wide(-($x+1))}]) 1
44
+ set boundarynum([expr {wide(-($x+2))}]) 1
45
+ set boundarynum([expr {wide($x+$x+1)}]) 1
46
+ set boundarynum([expr {wide($x+$x+2)}]) 1
47
+ }
48
+ set x [expr {wide(127)}]
49
+ for {set i 1} {$i<=9} {incr i} {
50
+ set boundarynum($x) 1
51
+ set boundarynum([expr {wide($x+1)}]) 1
52
+ set x [expr {wide($x*128 + 127)}]
53
+ }
54
+
55
+ # Scramble the $inlist into a random order.
56
+ #
57
+ proc scramble {inlist} {
58
+ set y {}
59
+ foreach x $inlist {
60
+ lappend y [list [expr {rand()}] $x]
61
+ }
62
+ set y [lsort $y]
63
+ set outlist {}
64
+ foreach x $y {
65
+ lappend outlist [lindex $x 1]
66
+ }
67
+ return $outlist
68
+ }
69
+
70
+ # A simple selection sort. Not trying to be efficient.
71
+ #
72
+ proc sort {inlist} {
73
+ set outlist {}
74
+ set mn [lindex $inlist 0]
75
+ foreach x $inlist {
76
+ if {$x<$mn} {set mn $x}
77
+ }
78
+ set outlist $mn
79
+ set mx $mn
80
+ while {1} {
81
+ set valid 0
82
+ foreach x $inlist {
83
+ if {$x>$mx && (!$valid || $mn>$x)} {
84
+ set mn $x
85
+ set valid 1
86
+ }
87
+ }
88
+ if {!$valid} break
89
+ lappend outlist $mn
90
+ set mx $mn
91
+ }
92
+ return $outlist
93
+ }
94
+
95
+ # Reverse the order of a list
96
+ #
97
+ proc reverse {inlist} {
98
+ set i [llength $inlist]
99
+ set outlist {}
100
+ for {incr i -1} {$i>=0} {incr i -1} {
101
+ lappend outlist [lindex $inlist $i]
102
+ }
103
+ return $outlist
104
+ }
105
+
106
+ set nums1 [scramble [array names boundarynum]]
107
+ set nums2 [scramble [array names boundarynum]]
108
+
109
+ set tname boundary3
110
+ puts "do_test $tname-1.1 \173"
111
+ puts " db eval \173"
112
+ puts " CREATE TABLE t1(a,x);"
113
+ set a 0
114
+ foreach r $nums1 {
115
+ incr a
116
+ set t1ra($r) $a
117
+ set t1ar($a) $r
118
+ set x [format %016x [expr {wide($r)}]]
119
+ set t1rx($r) $x
120
+ set t1xr($x) $r
121
+ puts " INSERT INTO t1(oid,a,x) VALUES($r,$a,'$x');"
122
+ }
123
+ puts " CREATE INDEX t1i1 ON t1(a);"
124
+ puts " CREATE INDEX t1i2 ON t1(x);"
125
+ puts " \175"
126
+ puts "\175 {}"
127
+
128
+ puts "do_test $tname-1.2 \173"
129
+ puts " db eval \173"
130
+ puts " SELECT count(*) FROM t1"
131
+ puts " \175"
132
+ puts "\175 {64}"
133
+
134
+ puts "do_test $tname-1.3 \173"
135
+ puts " db eval \173"
136
+ puts " CREATE TABLE t2(r,a);"
137
+ puts " INSERT INTO t2 SELECT rowid, a FROM t1;"
138
+ puts " CREATE INDEX t2i1 ON t2(r);"
139
+ puts " CREATE INDEX t2i2 ON t2(a);"
140
+ puts " INSERT INTO t2 VALUES(9.22337303685477580800e+18,65);"
141
+ set t1ra(9.22337303685477580800e+18) 65
142
+ set t1ar(65) 9.22337303685477580800e+18)
143
+ puts " INSERT INTO t2 VALUES(-9.22337303685477580800e+18,66);"
144
+ set t1ra(-9.22337303685477580800e+18) 66
145
+ set t1ar(66) -9.22337303685477580800e+18)
146
+ puts " SELECT count(*) FROM t2;"
147
+ puts " \175"
148
+ puts "\175 {66}"
149
+
150
+ set nums3 $nums2
151
+ lappend nums3 9.22337303685477580800e+18
152
+ lappend nums3 -9.22337303685477580800e+18
153
+
154
+ set i 0
155
+ foreach r $nums3 {
156
+ incr i
157
+
158
+ set r5 $r.5
159
+ set r0 $r.0
160
+ if {abs($r)<0x7FFFFFFFFFFFFFFF || $r==-9223372036854775808} {
161
+ set x $t1rx($r)
162
+ set a $t1ra($r)
163
+ puts "do_test $tname-2.$i.1 \173"
164
+ puts " db eval \173"
165
+ puts " SELECT t1.* FROM t1, t2 WHERE t1.rowid=$r AND t2.a=t1.a"
166
+ puts " \175"
167
+ puts "\175 {$a $x}"
168
+ puts "do_test $tname-2.$i.2 \173"
169
+ puts " db eval \173"
170
+ puts " SELECT t2.* FROM t1 JOIN t2 USING(a) WHERE x='$x'"
171
+ puts " \175"
172
+ puts "\175 {$r $a}"
173
+ puts "do_test $tname-2.$i.3 \173"
174
+ puts " db eval \173"
175
+ puts " SELECT t1.rowid, x FROM t1 JOIN t2 ON t2.r=t1.rowid WHERE t2.a=$a"
176
+ puts " \175"
177
+ puts "\175 {$r $x}"
178
+ }
179
+
180
+ foreach op {> >= < <=} subno {gt ge lt le} {
181
+
182
+ ################################################################ 2.x.y.1
183
+ set rset {}
184
+ set aset {}
185
+ foreach rx $nums2 {
186
+ if "\$rx $op \$r" {
187
+ lappend rset $rx
188
+ lappend aset $t1ra($rx)
189
+ }
190
+ }
191
+ puts "do_test $tname-2.$i.$subno.1 \173"
192
+ puts " db eval \173"
193
+ puts " SELECT t2.a FROM t1 JOIN t2 USING(a)"
194
+ puts " WHERE t1.rowid $op $r ORDER BY t2.a"
195
+ puts " \175"
196
+ puts "\175 {[sort $aset]}"
197
+
198
+ ################################################################ 2.x.y.2
199
+ puts "do_test $tname-2.$i.$subno.2 \173"
200
+ puts " db eval \173"
201
+ puts " SELECT t2.a FROM t2 NATURAL JOIN t1"
202
+ puts " WHERE t1.rowid $op $r ORDER BY t1.a DESC"
203
+ puts " \175"
204
+ puts "\175 {[reverse [sort $aset]]}"
205
+
206
+
207
+ ################################################################ 2.x.y.3
208
+ set ax $t1ra($r)
209
+ set aset {}
210
+ foreach rx [sort $rset] {
211
+ lappend aset $t1ra($rx)
212
+ }
213
+ puts "do_test $tname-2.$i.$subno.3 \173"
214
+ puts " db eval \173"
215
+ puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op t2.r"
216
+ puts " WHERE t2.a=$ax"
217
+ puts " ORDER BY t1.rowid"
218
+ puts " \175"
219
+ puts "\175 {$aset}"
220
+
221
+ ################################################################ 2.x.y.4
222
+ set aset {}
223
+ foreach rx [reverse [sort $rset]] {
224
+ lappend aset $t1ra($rx)
225
+ }
226
+ puts "do_test $tname-2.$i.$subno.4 \173"
227
+ puts " db eval \173"
228
+ puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op t2.r"
229
+ puts " WHERE t2.a=$ax"
230
+ puts " ORDER BY t1.rowid DESC"
231
+ puts " \175"
232
+ puts "\175 {$aset}"
233
+
234
+ ################################################################ 2.x.y.5
235
+ set aset {}
236
+ set xset {}
237
+ foreach rx $rset {
238
+ lappend xset $t1rx($rx)
239
+ }
240
+ foreach x [sort $xset] {
241
+ set rx $t1xr($x)
242
+ lappend aset $t1ra($rx)
243
+ }
244
+ puts "do_test $tname-2.$i.$subno.5 \173"
245
+ puts " db eval \173"
246
+ puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op t2.r"
247
+ puts " WHERE t2.a=$ax"
248
+ puts " ORDER BY x"
249
+ puts " \175"
250
+ puts "\175 {$aset}"
251
+
252
+ ################################################################ 2.x.y.10
253
+ if {[string length $r5]>15} continue
254
+ set rset {}
255
+ set aset {}
256
+ foreach rx $nums2 {
257
+ if "\$rx $op \$r0" {
258
+ lappend rset $rx
259
+ }
260
+ }
261
+ foreach rx [sort $rset] {
262
+ lappend aset $t1ra($rx)
263
+ }
264
+ puts "do_test $tname-2.$i.$subno.10 \173"
265
+ puts " db eval \173"
266
+ puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op CAST(t2.r AS real)"
267
+ puts " WHERE t2.a=$ax"
268
+ puts " ORDER BY t1.rowid"
269
+ puts " \175"
270
+ puts "\175 {$aset}"
271
+
272
+ ################################################################ 2.x.y.11
273
+ set aset {}
274
+ foreach rx [reverse [sort $rset]] {
275
+ lappend aset $t1ra($rx)
276
+ }
277
+ puts "do_test $tname-2.$i.$subno.11 \173"
278
+ puts " db eval \173"
279
+ puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op CAST(t2.r AS real)"
280
+ puts " WHERE t2.a=$ax"
281
+ puts " ORDER BY t1.rowid DESC"
282
+ puts " \175"
283
+ puts "\175 {$aset}"
284
+ }
285
+
286
+ }
287
+
288
+
289
+ puts {finish_test}
local-test-sqlite3-delta-03/afc-sqlite3/test/boundary3.test ADDED
The diff for this file is too large to render. See raw diff