Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- local-test-sqlite3-delta-02/afc-sqlite3/test/8_3_names.test +197 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/affinity2.test +136 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/affinity3.test +123 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/aggfault.test +43 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/aggnested.test +487 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/aggorderby.test +162 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alias.test +140 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alter.test +986 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alter2.test +470 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alterauth.test +72 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alterauth2.test +119 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/altercol.test +949 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/altercorrupt.test +181 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alterdropcol.test +339 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alterdropcol2.test +222 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/alterfault.test +41 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/altermalloc.test +71 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/altertab.test +1002 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/altertab3.test +789 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/altertrig.test +162 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/amatch1.test +117 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyze.test +399 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyze3.test +766 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyze6.test +126 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyze8.test +115 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyze9.test +1235 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyzeC.test +181 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyzeD.test +107 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/analyzer1.test +55 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/atof1.test +86 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/atrc.c +150 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/attach3.test +353 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/attachmalloc.test +78 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/auth.test +2678 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/auth2.test +160 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/auth3.test +134 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/autoanalyze1.test +123 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/autoinc.test +883 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/autoindex2.test +232 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/autovacuum.test +715 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/autovacuum2.test +87 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/autovacuum_ioerr2.test +132 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/avfs.test +396 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/avtrans.test +927 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/backup.test +1004 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/backup_ioerr.test +286 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/badutf2.test +127 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/basexx1.test +155 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/bc_common.tcl +75 -0
- local-test-sqlite3-delta-02/afc-sqlite3/test/bestindex1.test +344 -0
local-test-sqlite3-delta-02/afc-sqlite3/test/8_3_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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |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|
|
|
|
|
|
| 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-02/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-02/afc-sqlite3/test/aggfault.test
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2023 March 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 |
+
#
|
| 12 |
+
|
| 13 |
+
set testdir [file dirname $argv0]
|
| 14 |
+
source $testdir/tester.tcl
|
| 15 |
+
set testprefix aggfault
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
do_execsql_test 1 {
|
| 19 |
+
CREATE TABLE t1(x);
|
| 20 |
+
CREATE INDEX t1x ON t1(x, x=0);
|
| 21 |
+
}
|
| 22 |
+
faultsim_save_and_close
|
| 23 |
+
|
| 24 |
+
do_faultsim_test 2 -faults oom* -prep {
|
| 25 |
+
faultsim_restore_and_reopen
|
| 26 |
+
execsql { SELECT * FROM sqlite_schema }
|
| 27 |
+
} -body {
|
| 28 |
+
execsql {
|
| 29 |
+
SELECT * FROM t1 AS a1 WHERE (
|
| 30 |
+
SELECT count(x AND 0=a1.x) FROM t1 GROUP BY abs(1)
|
| 31 |
+
) AND x=(
|
| 32 |
+
SELECT * FROM t1 AS a1
|
| 33 |
+
WHERE (SELECT count(x IS 1 AND a1.x=0)
|
| 34 |
+
FROM t1
|
| 35 |
+
GROUP BY abs(1)) AND x=0
|
| 36 |
+
);
|
| 37 |
+
}
|
| 38 |
+
} -test {
|
| 39 |
+
faultsim_test_result {0 {}}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/aggnested.test
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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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|
|
| 1 |
+
# 2023-10-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 tests for ORDER BY on aggregate functions.
|
| 12 |
+
#
|
| 13 |
+
|
| 14 |
+
set testdir [file dirname $argv0]
|
| 15 |
+
source $testdir/tester.tcl
|
| 16 |
+
|
| 17 |
+
do_execsql_test aggorderby-1.1 {
|
| 18 |
+
CREATE TABLE t1(a TEXT,b INT,c INT,d INT);
|
| 19 |
+
WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<9)
|
| 20 |
+
INSERT INTO t1(a,b,c,d) SELECT printf('%d',(x*7)%10),1,x,10-x FROM c;
|
| 21 |
+
INSERT INTO t1(a,b,c,d) SELECT a, 2, c, 10-d FROM t1;
|
| 22 |
+
CREATE INDEX t1b ON t1(b);
|
| 23 |
+
}
|
| 24 |
+
do_catchsql_test aggorderby-1.2 {
|
| 25 |
+
SELECT b, group_concat(a ORDER BY max(d)) FROM t1 GROUP BY b;
|
| 26 |
+
} {1 {misuse of aggregate function max()}}
|
| 27 |
+
do_catchsql_test aggorderby-1.3 {
|
| 28 |
+
SELECT abs(a ORDER BY max(d)) FROM t1;
|
| 29 |
+
} {1 {ORDER BY may not be used with non-aggregate abs()}}
|
| 30 |
+
|
| 31 |
+
do_execsql_test aggorderby-2.0 {
|
| 32 |
+
SELECT group_concat(a ORDER BY a) FROM t1 WHERE b=1;
|
| 33 |
+
} {0,1,2,3,4,5,6,7,8,9}
|
| 34 |
+
do_execsql_test aggorderby-2.1 {
|
| 35 |
+
SELECT group_concat(a ORDER BY c) FROM t1 WHERE b=1;
|
| 36 |
+
} {0,7,4,1,8,5,2,9,6,3}
|
| 37 |
+
do_execsql_test aggorderby-2.2 {
|
| 38 |
+
SELECT group_concat(a ORDER BY b, d) FROM t1;
|
| 39 |
+
} {3,6,9,2,5,8,1,4,7,0,0,7,4,1,8,5,2,9,6,3}
|
| 40 |
+
do_execsql_test aggorderby-2.3 {
|
| 41 |
+
SELECT string_agg(a, ',' ORDER BY b DESC, d) FROM t1;
|
| 42 |
+
} {0,7,4,1,8,5,2,9,6,3,3,6,9,2,5,8,1,4,7,0}
|
| 43 |
+
do_execsql_test aggorderby-2.4 {
|
| 44 |
+
SELECT b, group_concat(a ORDER BY d) FROM t1 GROUP BY b ORDER BY b;
|
| 45 |
+
} {1 3,6,9,2,5,8,1,4,7,0 2 0,7,4,1,8,5,2,9,6,3}
|
| 46 |
+
|
| 47 |
+
do_execsql_test aggorderby-3.0 {
|
| 48 |
+
SELECT group_concat(DISTINCT a ORDER BY a) FROM t1;
|
| 49 |
+
} {0,1,2,3,4,5,6,7,8,9}
|
| 50 |
+
do_execsql_test aggorderby-3.1 {
|
| 51 |
+
SELECT group_concat(DISTINCT a ORDER BY c) FROM t1;
|
| 52 |
+
} {0,7,4,1,8,5,2,9,6,3}
|
| 53 |
+
|
| 54 |
+
do_execsql_test aggorderby-4.0 {
|
| 55 |
+
SELECT count(ORDER BY a) FROM t1;
|
| 56 |
+
} 20
|
| 57 |
+
do_execsql_test aggorderby-4.1 {
|
| 58 |
+
SELECT c, max(a ORDER BY a) FROM t1;
|
| 59 |
+
} {7 9}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
do_execsql_test aggorderby-5.0 {
|
| 63 |
+
DROP TABLE IF EXISTS t1;
|
| 64 |
+
DROP TABLE IF EXISTS t3;
|
| 65 |
+
CREATE TABLE t1(a TEXT); INSERT INTO t1 VALUES('aaa'),('bbb');
|
| 66 |
+
CREATE TABLE t3(d TEXT); INSERT INTO t3 VALUES('/'),('-');
|
| 67 |
+
SELECT (SELECT string_agg(a,d) FROM t3) FROM t1;
|
| 68 |
+
} {aaa-aaa bbb-bbb}
|
| 69 |
+
do_execsql_test aggorderby-5.1 {
|
| 70 |
+
SELECT (SELECT group_concat(a,d ORDER BY d) FROM t3) FROM t1;
|
| 71 |
+
} {aaa/aaa bbb/bbb}
|
| 72 |
+
do_execsql_test aggorderby-5.2 {
|
| 73 |
+
SELECT (SELECT string_agg(a,d ORDER BY d DESC) FROM t3) FROM t1;
|
| 74 |
+
} {aaa-aaa bbb-bbb}
|
| 75 |
+
do_execsql_test aggorderby-5.3 {
|
| 76 |
+
SELECT (SELECT string_agg(a,'#' ORDER BY d) FROM t3) FROM t1;
|
| 77 |
+
} {aaa#aaa bbb#bbb}
|
| 78 |
+
|
| 79 |
+
# COLLATE works on the ORDER BY.
|
| 80 |
+
#
|
| 81 |
+
do_execsql_test aggorderby-6.0 {
|
| 82 |
+
WITH c(x) AS (VALUES('abc'),('DEF'),('xyz'),('ABC'),('XYZ'))
|
| 83 |
+
SELECT string_agg(x,',' ORDER BY x COLLATE nocase),
|
| 84 |
+
string_agg(x,',' ORDER BY x) FROM c;
|
| 85 |
+
} {abc,ABC,DEF,xyz,XYZ ABC,DEF,XYZ,abc,xyz}
|
| 86 |
+
do_execsql_test aggorderby-6.1 {
|
| 87 |
+
WITH c(x,y) AS (VALUES(1,'a'),(2,'B'),(3,'c'),(4,'D'))
|
| 88 |
+
SELECT group_concat(x ORDER BY y COLLATE nocase),
|
| 89 |
+
group_concat(x ORDER BY y COLLATE binary) FROM c;
|
| 90 |
+
} {1,2,3,4 2,4,1,3}
|
| 91 |
+
|
| 92 |
+
# NULLS FIRST and NULLS LAST work on the ORDER BY
|
| 93 |
+
#
|
| 94 |
+
do_execsql_test aggorderby-7.0 {
|
| 95 |
+
WITH c(x) AS (VALUES(1),(NULL),(2.5),(NULL),('three'))
|
| 96 |
+
SELECT json_group_array(x ORDER BY x NULLS FIRST),
|
| 97 |
+
json_group_array(x ORDER BY x NULLS LAST) FROM c;
|
| 98 |
+
} {[null,null,1,2.5,"three"] [1,2.5,"three",null,null]}
|
| 99 |
+
do_execsql_test aggorderby-7.1 {
|
| 100 |
+
WITH c(x,y) AS (VALUES(1,9),(2,null),(3,5),(4,null),(5,1))
|
| 101 |
+
SELECT json_group_array(x ORDER BY y NULLS FIRST, x),
|
| 102 |
+
json_group_array(x ORDER BY y NULLS LAST, x) FROM c;
|
| 103 |
+
} {[2,4,5,3,1] [5,3,1,2,4]}
|
| 104 |
+
|
| 105 |
+
# The DISTINCT only applies to the function arguments, not to the
|
| 106 |
+
# ORDER BY arguments.
|
| 107 |
+
#
|
| 108 |
+
do_execsql_test aggorderby-8.0 {
|
| 109 |
+
WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('c',2,7),('c',1,8))
|
| 110 |
+
SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c;
|
| 111 |
+
} {c,b,a}
|
| 112 |
+
do_execsql_test aggorderby-8.1 {
|
| 113 |
+
WITH c(x,y,z) AS (VALUES('a',4,5),('b',3,6),('b',2,7),('c',1,8))
|
| 114 |
+
SELECT group_concat(DISTINCT x ORDER BY y, z) FROM c;
|
| 115 |
+
} {c,b,a}
|
| 116 |
+
do_execsql_test aggorderby-8.2 {
|
| 117 |
+
WITH c(x,y) AS (VALUES(1,1),(2,2),(3,3),(3,4),(3,5),(3,6))
|
| 118 |
+
SELECT sum(DISTINCT x ORDER BY y) FROM c;
|
| 119 |
+
} 6
|
| 120 |
+
|
| 121 |
+
# Subtype information is transfered through the sorter for aggregates
|
| 122 |
+
# that make use of subtype info.
|
| 123 |
+
#
|
| 124 |
+
do_execsql_test aggorderby-9.0 {
|
| 125 |
+
WITH c(x,y) AS (VALUES
|
| 126 |
+
('{a:3}', 3),
|
| 127 |
+
('[1,1]', 1),
|
| 128 |
+
('[4,4]', 4),
|
| 129 |
+
('{x:2}', 2))
|
| 130 |
+
SELECT json_group_array(json(x) ORDER BY y) FROM c;
|
| 131 |
+
} {{[[1,1],{"x":2},{"a":3},[4,4]]}}
|
| 132 |
+
do_execsql_test aggorderby-9.1 {
|
| 133 |
+
WITH c(x,y) AS (VALUES
|
| 134 |
+
('[4,4]', 4),
|
| 135 |
+
('{a:3}', 3),
|
| 136 |
+
('[4,4]', 4),
|
| 137 |
+
('[1,1]', 1),
|
| 138 |
+
('[4,4]', 4),
|
| 139 |
+
('{x:2}', 2))
|
| 140 |
+
SELECT json_group_array(DISTINCT json(x) ORDER BY y) FROM c;
|
| 141 |
+
} {{[[1,1],{"x":2},{"a":3},[4,4]]}}
|
| 142 |
+
do_execsql_test aggorderby-9.2 {
|
| 143 |
+
WITH c(x,y) AS (VALUES
|
| 144 |
+
('{a:3}', 3),
|
| 145 |
+
('[1,1]', 1),
|
| 146 |
+
('[4,4]', 4),
|
| 147 |
+
('{x:2}', 2))
|
| 148 |
+
SELECT json_group_array(json(x) ORDER BY json(x)) FROM c;
|
| 149 |
+
} {{[[1,1],[4,4],{"a":3},{"x":2}]}}
|
| 150 |
+
do_execsql_test aggorderby-9.3 {
|
| 151 |
+
WITH c(x,y) AS (VALUES
|
| 152 |
+
('[4,4]', 4),
|
| 153 |
+
('{a:3}', 3),
|
| 154 |
+
('[4,4]', 4),
|
| 155 |
+
('[1,1]', 1),
|
| 156 |
+
('[4,4]', 4),
|
| 157 |
+
('{x:2}', 2))
|
| 158 |
+
SELECT json_group_array(DISTINCT json(x) ORDER BY json(x)) FROM c;
|
| 159 |
+
} {{[[1,1],[4,4],{"a":3},{"x":2}]}}
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/alias.test
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2008 August 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 |
+
#
|
| 12 |
+
# This file implements regression tests for SQLite library. The
|
| 13 |
+
# focus of this script is correct code generation of aliased result-set
|
| 14 |
+
# values. See ticket #3343.
|
| 15 |
+
#
|
| 16 |
+
# $Id: alias.test,v 1.3 2009/04/23 13:22:44 drh Exp $
|
| 17 |
+
#
|
| 18 |
+
set testdir [file dirname $argv0]
|
| 19 |
+
source $testdir/tester.tcl
|
| 20 |
+
|
| 21 |
+
# Aliases are currently evaluated twice. We might try to change this
|
| 22 |
+
# in the future. But not now.
|
| 23 |
+
return
|
| 24 |
+
|
| 25 |
+
# A procedure to return a sequence of increasing integers.
|
| 26 |
+
#
|
| 27 |
+
namespace eval ::seq {
|
| 28 |
+
variable counter 0
|
| 29 |
+
proc value {args} {
|
| 30 |
+
variable counter
|
| 31 |
+
incr counter
|
| 32 |
+
return $counter
|
| 33 |
+
}
|
| 34 |
+
proc reset {} {
|
| 35 |
+
variable counter
|
| 36 |
+
set counter 0
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
do_test alias-1.1 {
|
| 42 |
+
db function sequence ::seq::value
|
| 43 |
+
db eval {
|
| 44 |
+
CREATE TABLE t1(x);
|
| 45 |
+
INSERT INTO t1 VALUES(9);
|
| 46 |
+
INSERT INTO t1 VALUES(8);
|
| 47 |
+
INSERT INTO t1 VALUES(7);
|
| 48 |
+
SELECT x, sequence() FROM t1;
|
| 49 |
+
}
|
| 50 |
+
} {9 1 8 2 7 3}
|
| 51 |
+
do_test alias-1.2 {
|
| 52 |
+
::seq::reset
|
| 53 |
+
db eval {
|
| 54 |
+
SELECT x, sequence() AS y FROM t1 WHERE y>0
|
| 55 |
+
}
|
| 56 |
+
} {9 1 8 2 7 3}
|
| 57 |
+
do_test alias-1.3 {
|
| 58 |
+
::seq::reset
|
| 59 |
+
db eval {
|
| 60 |
+
SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99
|
| 61 |
+
}
|
| 62 |
+
} {9 1 8 2 7 3}
|
| 63 |
+
do_test alias-1.4 {
|
| 64 |
+
::seq::reset
|
| 65 |
+
db eval {
|
| 66 |
+
SELECT x, sequence() AS y FROM t1 WHERE y>0 AND y<99 AND y!=55
|
| 67 |
+
}
|
| 68 |
+
} {9 1 8 2 7 3}
|
| 69 |
+
do_test alias-1.5 {
|
| 70 |
+
::seq::reset
|
| 71 |
+
db eval {
|
| 72 |
+
SELECT x, sequence() AS y FROM t1
|
| 73 |
+
WHERE y>0 AND y<99 AND y!=55 AND y NOT IN (56,57,58)
|
| 74 |
+
AND y NOT LIKE 'abc%' AND y%10==2
|
| 75 |
+
}
|
| 76 |
+
} {8 2}
|
| 77 |
+
do_test alias-1.6 {
|
| 78 |
+
::seq::reset
|
| 79 |
+
db eval {
|
| 80 |
+
SELECT x, sequence() AS y FROM t1 WHERE y BETWEEN 0 AND 99
|
| 81 |
+
}
|
| 82 |
+
} {9 1 8 2 7 3}
|
| 83 |
+
#do_test alias-1.7 {
|
| 84 |
+
# ::seq::reset
|
| 85 |
+
# db eval {
|
| 86 |
+
# SELECT x, sequence() AS y FROM t1 WHERE y IN (55,66,3)
|
| 87 |
+
# }
|
| 88 |
+
#} {7 3}
|
| 89 |
+
do_test alias-1.8 {
|
| 90 |
+
::seq::reset
|
| 91 |
+
db eval {
|
| 92 |
+
SELECT x, 1-sequence() AS y FROM t1 ORDER BY y
|
| 93 |
+
}
|
| 94 |
+
} {7 -2 8 -1 9 0}
|
| 95 |
+
do_test alias-1.9 {
|
| 96 |
+
::seq::reset
|
| 97 |
+
db eval {
|
| 98 |
+
SELECT x, sequence() AS y FROM t1 ORDER BY -y
|
| 99 |
+
}
|
| 100 |
+
} {7 3 8 2 9 1}
|
| 101 |
+
do_test alias-1.10 {
|
| 102 |
+
::seq::reset
|
| 103 |
+
db eval {
|
| 104 |
+
SELECT x, sequence() AS y FROM t1 ORDER BY x%2, y
|
| 105 |
+
}
|
| 106 |
+
} {8 2 9 1 7 3}
|
| 107 |
+
|
| 108 |
+
unset -nocomplain random_int_list
|
| 109 |
+
set random_int_list [db eval {
|
| 110 |
+
SELECT random()&2147483647 AS r FROM t1, t1, t1, t1 ORDER BY r
|
| 111 |
+
}]
|
| 112 |
+
do_test alias-1.11 {
|
| 113 |
+
lsort -integer $::random_int_list
|
| 114 |
+
} $random_int_list
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
do_test alias-2.1 {
|
| 118 |
+
db eval {
|
| 119 |
+
SELECT 4 UNION SELECT 1 ORDER BY 1
|
| 120 |
+
}
|
| 121 |
+
} {1 4}
|
| 122 |
+
do_test alias-2.2 {
|
| 123 |
+
db eval {
|
| 124 |
+
SELECT 4 UNION SELECT 1 UNION SELECT 9 ORDER BY 1
|
| 125 |
+
}
|
| 126 |
+
} {1 4 9}
|
| 127 |
+
|
| 128 |
+
if 0 {
|
| 129 |
+
# Aliases in the GROUP BY clause cause the expression to be evaluated
|
| 130 |
+
# twice in the current implementation. This might change in the future.
|
| 131 |
+
#
|
| 132 |
+
do_test alias-3.1 {
|
| 133 |
+
::seq::reset
|
| 134 |
+
db eval {
|
| 135 |
+
SELECT sequence(*) AS y, count(*) AS z FROM t1 GROUP BY y ORDER BY z, y
|
| 136 |
+
}
|
| 137 |
+
} {1 1 2 1 3 1}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/alter.test
ADDED
|
@@ -0,0 +1,986 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2004 November 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 |
+
# This file implements regression tests for SQLite library. The
|
| 12 |
+
# focus of this script is testing the ALTER TABLE statement.
|
| 13 |
+
#
|
| 14 |
+
# $Id: alter.test,v 1.32 2009/03/24 15:08:10 drh Exp $
|
| 15 |
+
#
|
| 16 |
+
|
| 17 |
+
set testdir [file dirname $argv0]
|
| 18 |
+
source $testdir/tester.tcl
|
| 19 |
+
|
| 20 |
+
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
|
| 21 |
+
ifcapable !altertable {
|
| 22 |
+
finish_test
|
| 23 |
+
return
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
#----------------------------------------------------------------------
|
| 27 |
+
# Test organization:
|
| 28 |
+
#
|
| 29 |
+
# alter-1.1.* - alter-1.7.*: Basic tests of ALTER TABLE, including tables
|
| 30 |
+
# with implicit and explicit indices. These tests came from an earlier
|
| 31 |
+
# fork of SQLite that also supported ALTER TABLE.
|
| 32 |
+
# alter-1.8.*: Tests for ALTER TABLE when the table resides in an
|
| 33 |
+
# attached database.
|
| 34 |
+
# alter-1.9.*: Tests for ALTER TABLE when their is whitespace between the
|
| 35 |
+
# table name and left parenthesis token. i.e:
|
| 36 |
+
# "CREATE TABLE abc (a, b, c);"
|
| 37 |
+
# alter-2.*: Test error conditions and messages.
|
| 38 |
+
# alter-3.*: Test ALTER TABLE on tables that have TRIGGERs attached to them.
|
| 39 |
+
# alter-4.*: Test ALTER TABLE on tables that have AUTOINCREMENT fields.
|
| 40 |
+
# ...
|
| 41 |
+
# alter-12.*: Test ALTER TABLE on views.
|
| 42 |
+
#
|
| 43 |
+
|
| 44 |
+
# Create some tables to rename. Be sure to include some TEMP tables
|
| 45 |
+
# and some tables with odd names.
|
| 46 |
+
#
|
| 47 |
+
do_test alter-1.1 {
|
| 48 |
+
ifcapable tempdb {
|
| 49 |
+
set ::temp TEMP
|
| 50 |
+
} else {
|
| 51 |
+
set ::temp {}
|
| 52 |
+
}
|
| 53 |
+
execsql [subst -nocommands {
|
| 54 |
+
CREATE TABLE t1(a,b);
|
| 55 |
+
INSERT INTO t1 VALUES(1,2);
|
| 56 |
+
CREATE TABLE [t1'x1](c UNIQUE, b PRIMARY KEY);
|
| 57 |
+
INSERT INTO [t1'x1] VALUES(3,4);
|
| 58 |
+
CREATE INDEX t1i1 ON T1(B);
|
| 59 |
+
CREATE INDEX t1i2 ON t1(a,b);
|
| 60 |
+
CREATE INDEX i3 ON [t1'x1](b,c);
|
| 61 |
+
CREATE $::temp TABLE "temp table"(e,f,g UNIQUE);
|
| 62 |
+
CREATE INDEX i2 ON [temp table](f);
|
| 63 |
+
INSERT INTO [temp table] VALUES(5,6,7);
|
| 64 |
+
}]
|
| 65 |
+
execsql {
|
| 66 |
+
SELECT 't1', * FROM t1;
|
| 67 |
+
SELECT 't1''x1', * FROM "t1'x1";
|
| 68 |
+
SELECT * FROM [temp table];
|
| 69 |
+
}
|
| 70 |
+
} {t1 1 2 t1'x1 3 4 5 6 7}
|
| 71 |
+
do_test alter-1.2 {
|
| 72 |
+
execsql [subst {
|
| 73 |
+
CREATE $::temp TABLE objlist(type, name, tbl_name);
|
| 74 |
+
INSERT INTO objlist SELECT type, name, tbl_name
|
| 75 |
+
FROM sqlite_master WHERE NAME!='objlist';
|
| 76 |
+
}]
|
| 77 |
+
ifcapable tempdb {
|
| 78 |
+
execsql {
|
| 79 |
+
INSERT INTO objlist SELECT type, name, tbl_name
|
| 80 |
+
FROM temp.sqlite_master WHERE NAME!='objlist';
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
execsql {
|
| 85 |
+
SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name;
|
| 86 |
+
}
|
| 87 |
+
} [list \
|
| 88 |
+
table t1 t1 \
|
| 89 |
+
index t1i1 t1 \
|
| 90 |
+
index t1i2 t1 \
|
| 91 |
+
table t1'x1 t1'x1 \
|
| 92 |
+
index i3 t1'x1 \
|
| 93 |
+
index {sqlite_autoindex_t1'x1_1} t1'x1 \
|
| 94 |
+
index {sqlite_autoindex_t1'x1_2} t1'x1 \
|
| 95 |
+
table {temp table} {temp table} \
|
| 96 |
+
index i2 {temp table} \
|
| 97 |
+
index {sqlite_autoindex_temp table_1} {temp table} \
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
# Make some changes
|
| 101 |
+
#
|
| 102 |
+
integrity_check alter-1.3.0
|
| 103 |
+
do_test alter-1.3 {
|
| 104 |
+
execsql {
|
| 105 |
+
ALTER TABLE [T1] RENAME to [-t1-];
|
| 106 |
+
ALTER TABLE "t1'x1" RENAME TO T2;
|
| 107 |
+
ALTER TABLE [temp table] RENAME to TempTab;
|
| 108 |
+
}
|
| 109 |
+
} {}
|
| 110 |
+
integrity_check alter-1.3.1
|
| 111 |
+
do_test alter-1.4 {
|
| 112 |
+
execsql {
|
| 113 |
+
SELECT 't1', * FROM [-t1-];
|
| 114 |
+
SELECT 't2', * FROM t2;
|
| 115 |
+
SELECT * FROM temptab;
|
| 116 |
+
}
|
| 117 |
+
} {t1 1 2 t2 3 4 5 6 7}
|
| 118 |
+
do_test alter-1.5 {
|
| 119 |
+
execsql {
|
| 120 |
+
DELETE FROM objlist;
|
| 121 |
+
INSERT INTO objlist SELECT type, name, tbl_name
|
| 122 |
+
FROM sqlite_master WHERE NAME!='objlist';
|
| 123 |
+
}
|
| 124 |
+
catchsql {
|
| 125 |
+
INSERT INTO objlist SELECT type, name, tbl_name
|
| 126 |
+
FROM sqlite_temp_master WHERE NAME!='objlist';
|
| 127 |
+
}
|
| 128 |
+
execsql {
|
| 129 |
+
SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name;
|
| 130 |
+
}
|
| 131 |
+
} [list \
|
| 132 |
+
table -t1- -t1- \
|
| 133 |
+
index t1i1 -t1- \
|
| 134 |
+
index t1i2 -t1- \
|
| 135 |
+
table T2 T2 \
|
| 136 |
+
index i3 T2 \
|
| 137 |
+
index {sqlite_autoindex_T2_1} T2 \
|
| 138 |
+
index {sqlite_autoindex_T2_2} T2 \
|
| 139 |
+
table {TempTab} {TempTab} \
|
| 140 |
+
index i2 {TempTab} \
|
| 141 |
+
index {sqlite_autoindex_TempTab_1} {TempTab} \
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
# Make sure the changes persist after restarting the database.
|
| 145 |
+
# (The TEMP table will not persist, of course.)
|
| 146 |
+
#
|
| 147 |
+
ifcapable tempdb {
|
| 148 |
+
do_test alter-1.6 {
|
| 149 |
+
db close
|
| 150 |
+
sqlite3 db test.db
|
| 151 |
+
set DB [sqlite3_connection_pointer db]
|
| 152 |
+
execsql {
|
| 153 |
+
CREATE TEMP TABLE objlist(type, name, tbl_name);
|
| 154 |
+
INSERT INTO objlist SELECT type, name, tbl_name FROM sqlite_master;
|
| 155 |
+
INSERT INTO objlist
|
| 156 |
+
SELECT type, name, tbl_name FROM temp.sqlite_master
|
| 157 |
+
WHERE NAME!='objlist';
|
| 158 |
+
SELECT type, name, tbl_name FROM objlist
|
| 159 |
+
ORDER BY tbl_name, type desc, name;
|
| 160 |
+
}
|
| 161 |
+
} [list \
|
| 162 |
+
table -t1- -t1- \
|
| 163 |
+
index t1i1 -t1- \
|
| 164 |
+
index t1i2 -t1- \
|
| 165 |
+
table T2 T2 \
|
| 166 |
+
index i3 T2 \
|
| 167 |
+
index {sqlite_autoindex_T2_1} T2 \
|
| 168 |
+
index {sqlite_autoindex_T2_2} T2 \
|
| 169 |
+
]
|
| 170 |
+
} else {
|
| 171 |
+
execsql {
|
| 172 |
+
DROP TABLE TempTab;
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
# Create bogus application-defined functions for functions used
|
| 177 |
+
# internally by ALTER TABLE, to ensure that ALTER TABLE falls back
|
| 178 |
+
# to the built-in functions.
|
| 179 |
+
#
|
| 180 |
+
proc failing_app_func {args} {error "bad function"}
|
| 181 |
+
do_test alter-1.7-prep {
|
| 182 |
+
db func substr failing_app_func
|
| 183 |
+
db func like failing_app_func
|
| 184 |
+
db func sqlite_rename_table failing_app_func
|
| 185 |
+
db func sqlite_rename_trigger failing_app_func
|
| 186 |
+
db func sqlite_rename_parent failing_app_func
|
| 187 |
+
catchsql {SELECT substr(name,1,3) FROM sqlite_master}
|
| 188 |
+
} {1 {bad function}}
|
| 189 |
+
|
| 190 |
+
# Make sure the ALTER TABLE statements work with the
|
| 191 |
+
# non-callback API
|
| 192 |
+
#
|
| 193 |
+
do_test alter-1.7 {
|
| 194 |
+
stepsql $DB {
|
| 195 |
+
ALTER TABLE [-t1-] RENAME to [*t1*];
|
| 196 |
+
ALTER TABLE T2 RENAME TO [<t2>];
|
| 197 |
+
}
|
| 198 |
+
execsql {
|
| 199 |
+
DELETE FROM objlist;
|
| 200 |
+
INSERT INTO objlist SELECT type, name, tbl_name
|
| 201 |
+
FROM sqlite_master WHERE NAME!='objlist';
|
| 202 |
+
}
|
| 203 |
+
catchsql {
|
| 204 |
+
INSERT INTO objlist SELECT type, name, tbl_name
|
| 205 |
+
FROM sqlite_temp_master WHERE NAME!='objlist';
|
| 206 |
+
}
|
| 207 |
+
execsql {
|
| 208 |
+
SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name;
|
| 209 |
+
}
|
| 210 |
+
} [list \
|
| 211 |
+
table *t1* *t1* \
|
| 212 |
+
index t1i1 *t1* \
|
| 213 |
+
index t1i2 *t1* \
|
| 214 |
+
table <t2> <t2> \
|
| 215 |
+
index i3 <t2> \
|
| 216 |
+
index {sqlite_autoindex_<t2>_1} <t2> \
|
| 217 |
+
index {sqlite_autoindex_<t2>_2} <t2> \
|
| 218 |
+
]
|
| 219 |
+
|
| 220 |
+
# Check that ALTER TABLE works on attached databases.
|
| 221 |
+
#
|
| 222 |
+
ifcapable attach {
|
| 223 |
+
do_test alter-1.8.1 {
|
| 224 |
+
forcedelete test2.db
|
| 225 |
+
forcedelete test2.db-journal
|
| 226 |
+
execsql {
|
| 227 |
+
ATTACH 'test2.db' AS aux;
|
| 228 |
+
}
|
| 229 |
+
} {}
|
| 230 |
+
do_test alter-1.8.2 {
|
| 231 |
+
execsql {
|
| 232 |
+
CREATE TABLE t4(a PRIMARY KEY, b, c);
|
| 233 |
+
CREATE TABLE aux.t4(a PRIMARY KEY, b, c);
|
| 234 |
+
CREATE INDEX i4 ON t4(b);
|
| 235 |
+
CREATE INDEX aux.i4 ON t4(b);
|
| 236 |
+
}
|
| 237 |
+
} {}
|
| 238 |
+
do_test alter-1.8.3 {
|
| 239 |
+
execsql {
|
| 240 |
+
INSERT INTO t4 VALUES('main', 'main', 'main');
|
| 241 |
+
INSERT INTO aux.t4 VALUES('aux', 'aux', 'aux');
|
| 242 |
+
SELECT * FROM t4 WHERE a = 'main';
|
| 243 |
+
}
|
| 244 |
+
} {main main main}
|
| 245 |
+
do_test alter-1.8.4 {
|
| 246 |
+
execsql {
|
| 247 |
+
ALTER TABLE t4 RENAME TO t5;
|
| 248 |
+
SELECT * FROM t4 WHERE a = 'aux';
|
| 249 |
+
}
|
| 250 |
+
} {aux aux aux}
|
| 251 |
+
do_test alter-1.8.5 {
|
| 252 |
+
execsql {
|
| 253 |
+
SELECT * FROM t5;
|
| 254 |
+
}
|
| 255 |
+
} {main main main}
|
| 256 |
+
do_test alter-1.8.6 {
|
| 257 |
+
execsql {
|
| 258 |
+
SELECT * FROM t5 WHERE b = 'main';
|
| 259 |
+
}
|
| 260 |
+
} {main main main}
|
| 261 |
+
do_test alter-1.8.7 {
|
| 262 |
+
execsql {
|
| 263 |
+
ALTER TABLE aux.t4 RENAME TO t5;
|
| 264 |
+
SELECT * FROM aux.t5 WHERE b = 'aux';
|
| 265 |
+
}
|
| 266 |
+
} {aux aux aux}
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
do_test alter-1.9.1 {
|
| 270 |
+
execsql {
|
| 271 |
+
CREATE TABLE tbl1 (a, b, c);
|
| 272 |
+
INSERT INTO tbl1 VALUES(1, 2, 3);
|
| 273 |
+
}
|
| 274 |
+
} {}
|
| 275 |
+
do_test alter-1.9.2 {
|
| 276 |
+
execsql {
|
| 277 |
+
SELECT * FROM tbl1;
|
| 278 |
+
}
|
| 279 |
+
} {1 2 3}
|
| 280 |
+
do_test alter-1.9.3 {
|
| 281 |
+
execsql {
|
| 282 |
+
ALTER TABLE tbl1 RENAME TO tbl2;
|
| 283 |
+
SELECT * FROM tbl2;
|
| 284 |
+
}
|
| 285 |
+
} {1 2 3}
|
| 286 |
+
do_test alter-1.9.4 {
|
| 287 |
+
execsql {
|
| 288 |
+
DROP TABLE tbl2;
|
| 289 |
+
}
|
| 290 |
+
} {}
|
| 291 |
+
|
| 292 |
+
# Test error messages
|
| 293 |
+
#
|
| 294 |
+
do_test alter-2.1 {
|
| 295 |
+
catchsql {
|
| 296 |
+
ALTER TABLE none RENAME TO hi;
|
| 297 |
+
}
|
| 298 |
+
} {1 {no such table: none}}
|
| 299 |
+
do_test alter-2.2 {
|
| 300 |
+
execsql {
|
| 301 |
+
CREATE TABLE t3(p,q,r);
|
| 302 |
+
}
|
| 303 |
+
catchsql {
|
| 304 |
+
ALTER TABLE [<t2>] RENAME TO t3;
|
| 305 |
+
}
|
| 306 |
+
} {1 {there is already another table or index with this name: t3}}
|
| 307 |
+
do_test alter-2.3 {
|
| 308 |
+
catchsql {
|
| 309 |
+
ALTER TABLE [<t2>] RENAME TO i3;
|
| 310 |
+
}
|
| 311 |
+
} {1 {there is already another table or index with this name: i3}}
|
| 312 |
+
do_test alter-2.4 {
|
| 313 |
+
catchsql {
|
| 314 |
+
ALTER TABLE SqLiTe_master RENAME TO master;
|
| 315 |
+
}
|
| 316 |
+
} {1 {table sqlite_master may not be altered}}
|
| 317 |
+
do_test alter-2.5 {
|
| 318 |
+
catchsql {
|
| 319 |
+
ALTER TABLE t3 RENAME TO sqlite_t3;
|
| 320 |
+
}
|
| 321 |
+
} {1 {object name reserved for internal use: sqlite_t3}}
|
| 322 |
+
do_test alter-2.6 {
|
| 323 |
+
catchsql {
|
| 324 |
+
ALTER TABLE t3 ADD COLUMN (ALTER TABLE t3 ADD COLUMN);
|
| 325 |
+
}
|
| 326 |
+
} {1 {near "(": syntax error}}
|
| 327 |
+
|
| 328 |
+
# If this compilation does not include triggers, omit the alter-3.* tests.
|
| 329 |
+
ifcapable trigger {
|
| 330 |
+
|
| 331 |
+
#-----------------------------------------------------------------------
|
| 332 |
+
# Tests alter-3.* test ALTER TABLE on tables that have triggers.
|
| 333 |
+
#
|
| 334 |
+
# alter-3.1.*: ALTER TABLE with triggers.
|
| 335 |
+
# alter-3.2.*: Test that the ON keyword cannot be used as a database,
|
| 336 |
+
# table or column name unquoted. This is done because part of the
|
| 337 |
+
# ALTER TABLE code (specifically the implementation of SQL function
|
| 338 |
+
# "sqlite_alter_trigger") will break in this case.
|
| 339 |
+
# alter-3.3.*: ALTER TABLE with TEMP triggers (todo).
|
| 340 |
+
#
|
| 341 |
+
|
| 342 |
+
# An SQL user-function for triggers to fire, so that we know they
|
| 343 |
+
# are working.
|
| 344 |
+
proc trigfunc {args} {
|
| 345 |
+
set ::TRIGGER $args
|
| 346 |
+
}
|
| 347 |
+
db func trigfunc trigfunc
|
| 348 |
+
|
| 349 |
+
do_test alter-3.1.0 {
|
| 350 |
+
execsql {
|
| 351 |
+
CREATE TABLE t6(a, b, c);
|
| 352 |
+
-- Different case for the table name in the trigger.
|
| 353 |
+
CREATE TRIGGER trig1 AFTER INSERT ON T6 BEGIN
|
| 354 |
+
SELECT trigfunc('trig1', new.a, new.b, new.c);
|
| 355 |
+
END;
|
| 356 |
+
}
|
| 357 |
+
} {}
|
| 358 |
+
do_test alter-3.1.1 {
|
| 359 |
+
execsql {
|
| 360 |
+
INSERT INTO t6 VALUES(1, 2, 3);
|
| 361 |
+
}
|
| 362 |
+
set ::TRIGGER
|
| 363 |
+
} {trig1 1 2 3}
|
| 364 |
+
do_test alter-3.1.2 {
|
| 365 |
+
execsql {
|
| 366 |
+
ALTER TABLE t6 RENAME TO t7;
|
| 367 |
+
INSERT INTO t7 VALUES(4, 5, 6);
|
| 368 |
+
}
|
| 369 |
+
set ::TRIGGER
|
| 370 |
+
} {trig1 4 5 6}
|
| 371 |
+
do_test alter-3.1.3 {
|
| 372 |
+
execsql {
|
| 373 |
+
DROP TRIGGER trig1;
|
| 374 |
+
}
|
| 375 |
+
} {}
|
| 376 |
+
do_test alter-3.1.4 {
|
| 377 |
+
execsql {
|
| 378 |
+
CREATE TRIGGER trig2 AFTER INSERT ON main.t7 BEGIN
|
| 379 |
+
SELECT trigfunc('trig2', new.a, new.b, new.c);
|
| 380 |
+
END;
|
| 381 |
+
INSERT INTO t7 VALUES(1, 2, 3);
|
| 382 |
+
}
|
| 383 |
+
set ::TRIGGER
|
| 384 |
+
} {trig2 1 2 3}
|
| 385 |
+
do_test alter-3.1.5 {
|
| 386 |
+
execsql {
|
| 387 |
+
ALTER TABLE t7 RENAME TO t8;
|
| 388 |
+
INSERT INTO t8 VALUES(4, 5, 6);
|
| 389 |
+
}
|
| 390 |
+
set ::TRIGGER
|
| 391 |
+
} {trig2 4 5 6}
|
| 392 |
+
do_test alter-3.1.6 {
|
| 393 |
+
execsql {
|
| 394 |
+
DROP TRIGGER trig2;
|
| 395 |
+
}
|
| 396 |
+
} {}
|
| 397 |
+
do_test alter-3.1.7 {
|
| 398 |
+
execsql {
|
| 399 |
+
CREATE TRIGGER trig3 AFTER INSERT ON main.'t8'BEGIN
|
| 400 |
+
SELECT trigfunc('trig3', new.a, new.b, new.c);
|
| 401 |
+
END;
|
| 402 |
+
INSERT INTO t8 VALUES(1, 2, 3);
|
| 403 |
+
}
|
| 404 |
+
set ::TRIGGER
|
| 405 |
+
} {trig3 1 2 3}
|
| 406 |
+
do_test alter-3.1.8 {
|
| 407 |
+
execsql {
|
| 408 |
+
ALTER TABLE t8 RENAME TO t9;
|
| 409 |
+
INSERT INTO t9 VALUES(4, 5, 6);
|
| 410 |
+
}
|
| 411 |
+
set ::TRIGGER
|
| 412 |
+
} {trig3 4 5 6}
|
| 413 |
+
|
| 414 |
+
# Make sure "ON" cannot be used as a database, table or column name without
|
| 415 |
+
# quoting. Otherwise the sqlite_alter_trigger() function might not work.
|
| 416 |
+
forcedelete test3.db
|
| 417 |
+
forcedelete test3.db-journal
|
| 418 |
+
ifcapable attach {
|
| 419 |
+
do_test alter-3.2.1 {
|
| 420 |
+
catchsql {
|
| 421 |
+
ATTACH 'test3.db' AS ON;
|
| 422 |
+
}
|
| 423 |
+
} {1 {near "ON": syntax error}}
|
| 424 |
+
do_test alter-3.2.2 {
|
| 425 |
+
catchsql {
|
| 426 |
+
ATTACH 'test3.db' AS 'ON';
|
| 427 |
+
}
|
| 428 |
+
} {0 {}}
|
| 429 |
+
do_test alter-3.2.3 {
|
| 430 |
+
catchsql {
|
| 431 |
+
CREATE TABLE ON.t1(a, b, c);
|
| 432 |
+
}
|
| 433 |
+
} {1 {near "ON": syntax error}}
|
| 434 |
+
do_test alter-3.2.4 {
|
| 435 |
+
catchsql {
|
| 436 |
+
CREATE TABLE 'ON'.t1(a, b, c);
|
| 437 |
+
}
|
| 438 |
+
} {0 {}}
|
| 439 |
+
do_test alter-3.2.4 {
|
| 440 |
+
catchsql {
|
| 441 |
+
CREATE TABLE 'ON'.ON(a, b, c);
|
| 442 |
+
}
|
| 443 |
+
} {1 {near "ON": syntax error}}
|
| 444 |
+
do_test alter-3.2.5 {
|
| 445 |
+
catchsql {
|
| 446 |
+
CREATE TABLE 'ON'.'ON'(a, b, c);
|
| 447 |
+
}
|
| 448 |
+
} {0 {}}
|
| 449 |
+
}
|
| 450 |
+
do_test alter-3.2.6 {
|
| 451 |
+
catchsql {
|
| 452 |
+
CREATE TABLE t10(a, ON, c);
|
| 453 |
+
}
|
| 454 |
+
} {1 {near "ON": syntax error}}
|
| 455 |
+
do_test alter-3.2.7 {
|
| 456 |
+
catchsql {
|
| 457 |
+
CREATE TABLE t10(a, 'ON', c);
|
| 458 |
+
}
|
| 459 |
+
} {0 {}}
|
| 460 |
+
do_test alter-3.2.8 {
|
| 461 |
+
catchsql {
|
| 462 |
+
CREATE TRIGGER trig4 AFTER INSERT ON ON BEGIN SELECT 1; END;
|
| 463 |
+
}
|
| 464 |
+
} {1 {near "ON": syntax error}}
|
| 465 |
+
ifcapable attach {
|
| 466 |
+
do_test alter-3.2.9 {
|
| 467 |
+
catchsql {
|
| 468 |
+
CREATE TRIGGER 'on'.trig4 AFTER INSERT ON 'ON' BEGIN SELECT 1; END;
|
| 469 |
+
}
|
| 470 |
+
} {0 {}}
|
| 471 |
+
}
|
| 472 |
+
do_test alter-3.2.10 {
|
| 473 |
+
execsql {
|
| 474 |
+
DROP TABLE t10;
|
| 475 |
+
}
|
| 476 |
+
} {}
|
| 477 |
+
|
| 478 |
+
do_test alter-3.3.1 {
|
| 479 |
+
execsql [subst {
|
| 480 |
+
CREATE TABLE tbl1(a, b, c);
|
| 481 |
+
CREATE $::temp TRIGGER trig1 AFTER INSERT ON tbl1 BEGIN
|
| 482 |
+
SELECT trigfunc('trig1', new.a, new.b, new.c);
|
| 483 |
+
END;
|
| 484 |
+
}]
|
| 485 |
+
} {}
|
| 486 |
+
do_test alter-3.3.2 {
|
| 487 |
+
execsql {
|
| 488 |
+
INSERT INTO tbl1 VALUES('a', 'b', 'c');
|
| 489 |
+
}
|
| 490 |
+
set ::TRIGGER
|
| 491 |
+
} {trig1 a b c}
|
| 492 |
+
do_test alter-3.3.3 {
|
| 493 |
+
execsql {
|
| 494 |
+
ALTER TABLE tbl1 RENAME TO tbl2;
|
| 495 |
+
INSERT INTO tbl2 VALUES('d', 'e', 'f');
|
| 496 |
+
}
|
| 497 |
+
set ::TRIGGER
|
| 498 |
+
} {trig1 d e f}
|
| 499 |
+
do_test alter-3.3.4 {
|
| 500 |
+
execsql [subst {
|
| 501 |
+
CREATE $::temp TRIGGER trig2 AFTER UPDATE ON tbl2 BEGIN
|
| 502 |
+
SELECT trigfunc('trig2', new.a, new.b, new.c);
|
| 503 |
+
END;
|
| 504 |
+
}]
|
| 505 |
+
} {}
|
| 506 |
+
do_test alter-3.3.5 {
|
| 507 |
+
execsql {
|
| 508 |
+
ALTER TABLE tbl2 RENAME TO tbl3;
|
| 509 |
+
INSERT INTO tbl3 VALUES('g', 'h', 'i');
|
| 510 |
+
}
|
| 511 |
+
set ::TRIGGER
|
| 512 |
+
} {trig1 g h i}
|
| 513 |
+
do_test alter-3.3.6 {
|
| 514 |
+
execsql {
|
| 515 |
+
UPDATE tbl3 SET a = 'G' where a = 'g';
|
| 516 |
+
}
|
| 517 |
+
set ::TRIGGER
|
| 518 |
+
} {trig2 G h i}
|
| 519 |
+
do_test alter-3.3.7 {
|
| 520 |
+
execsql {
|
| 521 |
+
DROP TABLE tbl3;
|
| 522 |
+
}
|
| 523 |
+
} {}
|
| 524 |
+
ifcapable tempdb {
|
| 525 |
+
do_test alter-3.3.8 {
|
| 526 |
+
execsql {
|
| 527 |
+
SELECT * FROM temp.sqlite_master WHERE type = 'trigger';
|
| 528 |
+
}
|
| 529 |
+
} {}
|
| 530 |
+
}
|
| 531 |
+
|
| 532 |
+
} ;# ifcapable trigger
|
| 533 |
+
|
| 534 |
+
# If the build does not include AUTOINCREMENT fields, omit alter-4.*.
|
| 535 |
+
ifcapable autoinc {
|
| 536 |
+
|
| 537 |
+
do_test alter-4.1 {
|
| 538 |
+
execsql {
|
| 539 |
+
CREATE TABLE tbl1(a INTEGER PRIMARY KEY AUTOINCREMENT);
|
| 540 |
+
INSERT INTO tbl1 VALUES(10);
|
| 541 |
+
}
|
| 542 |
+
} {}
|
| 543 |
+
do_test alter-4.2 {
|
| 544 |
+
execsql {
|
| 545 |
+
INSERT INTO tbl1 VALUES(NULL);
|
| 546 |
+
SELECT a FROM tbl1;
|
| 547 |
+
}
|
| 548 |
+
} {10 11}
|
| 549 |
+
do_test alter-4.3 {
|
| 550 |
+
execsql {
|
| 551 |
+
ALTER TABLE tbl1 RENAME TO tbl2;
|
| 552 |
+
DELETE FROM tbl2;
|
| 553 |
+
INSERT INTO tbl2 VALUES(NULL);
|
| 554 |
+
SELECT a FROM tbl2;
|
| 555 |
+
}
|
| 556 |
+
} {12}
|
| 557 |
+
do_test alter-4.4 {
|
| 558 |
+
execsql {
|
| 559 |
+
DROP TABLE tbl2;
|
| 560 |
+
}
|
| 561 |
+
} {}
|
| 562 |
+
|
| 563 |
+
} ;# ifcapable autoinc
|
| 564 |
+
|
| 565 |
+
# Test that it is Ok to execute an ALTER TABLE immediately after
|
| 566 |
+
# opening a database.
|
| 567 |
+
do_test alter-5.1 {
|
| 568 |
+
execsql {
|
| 569 |
+
CREATE TABLE tbl1(a, b, c);
|
| 570 |
+
INSERT INTO tbl1 VALUES('x', 'y', 'z');
|
| 571 |
+
}
|
| 572 |
+
} {}
|
| 573 |
+
do_test alter-5.2 {
|
| 574 |
+
sqlite3 db2 test.db
|
| 575 |
+
execsql {
|
| 576 |
+
ALTER TABLE tbl1 RENAME TO tbl2;
|
| 577 |
+
SELECT * FROM tbl2;
|
| 578 |
+
} db2
|
| 579 |
+
} {x y z}
|
| 580 |
+
do_test alter-5.3 {
|
| 581 |
+
db2 close
|
| 582 |
+
} {}
|
| 583 |
+
|
| 584 |
+
foreach tblname [execsql {
|
| 585 |
+
SELECT name FROM sqlite_master
|
| 586 |
+
WHERE type='table' AND name NOT GLOB 'sqlite*'
|
| 587 |
+
}] {
|
| 588 |
+
execsql "DROP TABLE \"$tblname\""
|
| 589 |
+
}
|
| 590 |
+
|
| 591 |
+
set ::tbl_name "abc\uABCDdef"
|
| 592 |
+
do_test alter-6.1 {
|
| 593 |
+
string length $::tbl_name
|
| 594 |
+
} {7}
|
| 595 |
+
do_test alter-6.2 {
|
| 596 |
+
execsql "
|
| 597 |
+
CREATE TABLE ${tbl_name}(a, b, c);
|
| 598 |
+
"
|
| 599 |
+
set ::oid [execsql {SELECT max(oid) FROM sqlite_master}]
|
| 600 |
+
execsql "
|
| 601 |
+
SELECT sql FROM sqlite_master WHERE oid = $::oid;
|
| 602 |
+
"
|
| 603 |
+
} "{CREATE TABLE ${::tbl_name}(a, b, c)}"
|
| 604 |
+
execsql "
|
| 605 |
+
SELECT * FROM ${::tbl_name}
|
| 606 |
+
"
|
| 607 |
+
set ::tbl_name2 "abcXdef"
|
| 608 |
+
do_test alter-6.3 {
|
| 609 |
+
execsql "
|
| 610 |
+
ALTER TABLE $::tbl_name RENAME TO $::tbl_name2
|
| 611 |
+
"
|
| 612 |
+
execsql "
|
| 613 |
+
SELECT sql FROM sqlite_master WHERE oid = $::oid
|
| 614 |
+
"
|
| 615 |
+
} "{CREATE TABLE \"${::tbl_name2}\"(a, b, c)}"
|
| 616 |
+
do_test alter-6.4 {
|
| 617 |
+
execsql "
|
| 618 |
+
ALTER TABLE $::tbl_name2 RENAME TO $::tbl_name
|
| 619 |
+
"
|
| 620 |
+
execsql "
|
| 621 |
+
SELECT sql FROM sqlite_master WHERE oid = $::oid
|
| 622 |
+
"
|
| 623 |
+
} "{CREATE TABLE \"${::tbl_name}\"(a, b, c)}"
|
| 624 |
+
set ::col_name ghi\1234\jkl
|
| 625 |
+
do_test alter-6.5 {
|
| 626 |
+
execsql "
|
| 627 |
+
ALTER TABLE $::tbl_name ADD COLUMN $::col_name VARCHAR
|
| 628 |
+
"
|
| 629 |
+
execsql "
|
| 630 |
+
SELECT sql FROM sqlite_master WHERE oid = $::oid
|
| 631 |
+
"
|
| 632 |
+
} "{CREATE TABLE \"${::tbl_name}\"(a, b, c, $::col_name VARCHAR)}"
|
| 633 |
+
set ::col_name2 B\3421\A
|
| 634 |
+
do_test alter-6.6 {
|
| 635 |
+
db close
|
| 636 |
+
sqlite3 db test.db
|
| 637 |
+
execsql "
|
| 638 |
+
ALTER TABLE $::tbl_name ADD COLUMN $::col_name2
|
| 639 |
+
"
|
| 640 |
+
execsql "
|
| 641 |
+
SELECT sql FROM sqlite_master WHERE oid = $::oid
|
| 642 |
+
"
|
| 643 |
+
} "{CREATE TABLE \"${::tbl_name}\"(a, b, c, $::col_name VARCHAR, $::col_name2)}"
|
| 644 |
+
do_test alter-6.7 {
|
| 645 |
+
execsql "
|
| 646 |
+
INSERT INTO ${::tbl_name} VALUES(1, 2, 3, 4, 5);
|
| 647 |
+
SELECT $::col_name, $::col_name2 FROM $::tbl_name;
|
| 648 |
+
"
|
| 649 |
+
} {4 5}
|
| 650 |
+
|
| 651 |
+
# Ticket #1665: Make sure ALTER TABLE ADD COLUMN works on a table
|
| 652 |
+
# that includes a COLLATE clause.
|
| 653 |
+
#
|
| 654 |
+
do_realnum_test alter-7.1 {
|
| 655 |
+
execsql {
|
| 656 |
+
CREATE TABLE t1(a TEXT COLLATE BINARY);
|
| 657 |
+
ALTER TABLE t1 ADD COLUMN b INTEGER COLLATE NOCASE;
|
| 658 |
+
INSERT INTO t1 VALUES(1,'-2');
|
| 659 |
+
INSERT INTO t1 VALUES(5.4e-08,'5.4e-08');
|
| 660 |
+
SELECT typeof(a), a, typeof(b), b FROM t1;
|
| 661 |
+
}
|
| 662 |
+
} {text 1 integer -2 text 5.4e-08 real 5.4e-08}
|
| 663 |
+
|
| 664 |
+
# Make sure that when a column is added by ALTER TABLE ADD COLUMN and has
|
| 665 |
+
# a default value that the default value is used by aggregate functions.
|
| 666 |
+
#
|
| 667 |
+
do_test alter-8.1 {
|
| 668 |
+
execsql {
|
| 669 |
+
CREATE TABLE t2(a INTEGER);
|
| 670 |
+
INSERT INTO t2 VALUES(1);
|
| 671 |
+
INSERT INTO t2 VALUES(1);
|
| 672 |
+
INSERT INTO t2 VALUES(2);
|
| 673 |
+
ALTER TABLE t2 ADD COLUMN b INTEGER DEFAULT 9;
|
| 674 |
+
SELECT sum(b) FROM t2;
|
| 675 |
+
}
|
| 676 |
+
} {27}
|
| 677 |
+
do_test alter-8.2 {
|
| 678 |
+
execsql {
|
| 679 |
+
SELECT a, sum(b) FROM t2 GROUP BY a;
|
| 680 |
+
}
|
| 681 |
+
} {1 18 2 9}
|
| 682 |
+
|
| 683 |
+
#--------------------------------------------------------------------------
|
| 684 |
+
# alter-9.X - Special test: Make sure the sqlite_rename_column() and
|
| 685 |
+
# rename_table() functions do not crash when handed bad input.
|
| 686 |
+
#
|
| 687 |
+
sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
|
| 688 |
+
do_test alter-9.1 {
|
| 689 |
+
execsql {SELECT SQLITE_RENAME_COLUMN(0,0,0,0,0,0,0,0,0)}
|
| 690 |
+
} {{}}
|
| 691 |
+
foreach {tn sql} {
|
| 692 |
+
1 { SELECT SQLITE_RENAME_TABLE(0,0,0,0,0,0,0) }
|
| 693 |
+
2 { SELECT SQLITE_RENAME_TABLE(10,20,30,40,50,60,70) }
|
| 694 |
+
3 { SELECT SQLITE_RENAME_TABLE('foo','foo','foo','foo','foo','foo','foo') }
|
| 695 |
+
} {
|
| 696 |
+
do_test alter-9.2.$tn {
|
| 697 |
+
catch { execsql $sql }
|
| 698 |
+
} 1
|
| 699 |
+
}
|
| 700 |
+
sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
|
| 701 |
+
|
| 702 |
+
# If the INTERNAL_FUNCTIONS test-control is disabled (which is the default),
|
| 703 |
+
# then the sqlite_rename_table() SQL function is not accessible to ordinary SQL.
|
| 704 |
+
#
|
| 705 |
+
do_catchsql_test alter-9.3 {
|
| 706 |
+
SELECT sqlite_rename_table(0,0,0,0,0,0,0);
|
| 707 |
+
} {1 {no such function: sqlite_rename_table}}
|
| 708 |
+
|
| 709 |
+
#------------------------------------------------------------------------
|
| 710 |
+
# alter-10.X - Make sure ALTER TABLE works with multi-byte UTF-8 characters
|
| 711 |
+
# in the names.
|
| 712 |
+
#
|
| 713 |
+
do_test alter-10.1 {
|
| 714 |
+
execsql "CREATE TABLE xyz(x UNIQUE)"
|
| 715 |
+
execsql "ALTER TABLE xyz RENAME TO xyz\u1234abc"
|
| 716 |
+
execsql {SELECT name FROM sqlite_master WHERE name GLOB 'xyz*'}
|
| 717 |
+
} [list xyz\u1234abc]
|
| 718 |
+
do_test alter-10.2 {
|
| 719 |
+
execsql {SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_autoindex*'}
|
| 720 |
+
} [list sqlite_autoindex_xyz\u1234abc_1]
|
| 721 |
+
do_test alter-10.3 {
|
| 722 |
+
execsql "ALTER TABLE xyz\u1234abc RENAME TO xyzabc"
|
| 723 |
+
execsql {SELECT name FROM sqlite_master WHERE name GLOB 'xyz*'}
|
| 724 |
+
} [list xyzabc]
|
| 725 |
+
do_test alter-10.4 {
|
| 726 |
+
execsql {SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_autoindex*'}
|
| 727 |
+
} [list sqlite_autoindex_xyzabc_1]
|
| 728 |
+
|
| 729 |
+
do_test alter-11.1 {
|
| 730 |
+
sqlite3_exec db {CREATE TABLE t11(%c6%c6)}
|
| 731 |
+
execsql {
|
| 732 |
+
ALTER TABLE t11 ADD COLUMN abc;
|
| 733 |
+
}
|
| 734 |
+
catchsql {
|
| 735 |
+
ALTER TABLE t11 ADD COLUMN abc;
|
| 736 |
+
}
|
| 737 |
+
} {1 {duplicate column name: abc}}
|
| 738 |
+
set isutf16 [regexp 16 [db one {PRAGMA encoding}]]
|
| 739 |
+
if {!$isutf16} {
|
| 740 |
+
do_test alter-11.2 {
|
| 741 |
+
execsql {INSERT INTO t11 VALUES(1,2)}
|
| 742 |
+
sqlite3_exec db {SELECT %c6%c6 AS xyz, abc FROM t11}
|
| 743 |
+
} {0 {xyz abc 1 2}}
|
| 744 |
+
}
|
| 745 |
+
do_test alter-11.3 {
|
| 746 |
+
sqlite3_exec db {CREATE TABLE t11b("%81%82%83" text)}
|
| 747 |
+
execsql {
|
| 748 |
+
ALTER TABLE t11b ADD COLUMN abc;
|
| 749 |
+
}
|
| 750 |
+
catchsql {
|
| 751 |
+
ALTER TABLE t11b ADD COLUMN abc;
|
| 752 |
+
}
|
| 753 |
+
} {1 {duplicate column name: abc}}
|
| 754 |
+
if {!$isutf16} {
|
| 755 |
+
do_test alter-11.4 {
|
| 756 |
+
execsql {INSERT INTO t11b VALUES(3,4)}
|
| 757 |
+
sqlite3_exec db {SELECT %81%82%83 AS xyz, abc FROM t11b}
|
| 758 |
+
} {0 {xyz abc 3 4}}
|
| 759 |
+
do_test alter-11.5 {
|
| 760 |
+
sqlite3_exec db {SELECT [%81%82%83] AS xyz, abc FROM t11b}
|
| 761 |
+
} {0 {xyz abc 3 4}}
|
| 762 |
+
do_test alter-11.6 {
|
| 763 |
+
sqlite3_exec db {SELECT "%81%82%83" AS xyz, abc FROM t11b}
|
| 764 |
+
} {0 {xyz abc 3 4}}
|
| 765 |
+
}
|
| 766 |
+
do_test alter-11.7 {
|
| 767 |
+
sqlite3_exec db {CREATE TABLE t11c(%81%82%83 text)}
|
| 768 |
+
execsql {
|
| 769 |
+
ALTER TABLE t11c ADD COLUMN abc;
|
| 770 |
+
}
|
| 771 |
+
catchsql {
|
| 772 |
+
ALTER TABLE t11c ADD COLUMN abc;
|
| 773 |
+
}
|
| 774 |
+
} {1 {duplicate column name: abc}}
|
| 775 |
+
if {!$isutf16} {
|
| 776 |
+
do_test alter-11.8 {
|
| 777 |
+
execsql {INSERT INTO t11c VALUES(5,6)}
|
| 778 |
+
sqlite3_exec db {SELECT %81%82%83 AS xyz, abc FROM t11c}
|
| 779 |
+
} {0 {xyz abc 5 6}}
|
| 780 |
+
do_test alter-11.9 {
|
| 781 |
+
sqlite3_exec db {SELECT [%81%82%83] AS xyz, abc FROM t11c}
|
| 782 |
+
} {0 {xyz abc 5 6}}
|
| 783 |
+
do_test alter-11.10 {
|
| 784 |
+
sqlite3_exec db {SELECT "%81%82%83" AS xyz, abc FROM t11c}
|
| 785 |
+
} {0 {xyz abc 5 6}}
|
| 786 |
+
}
|
| 787 |
+
|
| 788 |
+
do_test alter-12.1 {
|
| 789 |
+
execsql {
|
| 790 |
+
CREATE TABLE t12(a, b, c);
|
| 791 |
+
CREATE VIEW v1 AS SELECT * FROM t12;
|
| 792 |
+
}
|
| 793 |
+
} {}
|
| 794 |
+
do_test alter-12.2 {
|
| 795 |
+
catchsql {
|
| 796 |
+
ALTER TABLE v1 RENAME TO v2;
|
| 797 |
+
}
|
| 798 |
+
} {1 {view v1 may not be altered}}
|
| 799 |
+
do_test alter-12.3 {
|
| 800 |
+
execsql { SELECT * FROM v1; }
|
| 801 |
+
} {}
|
| 802 |
+
do_test alter-12.4 {
|
| 803 |
+
db close
|
| 804 |
+
sqlite3 db test.db
|
| 805 |
+
execsql { SELECT * FROM v1; }
|
| 806 |
+
} {}
|
| 807 |
+
do_test alter-12.5 {
|
| 808 |
+
catchsql {
|
| 809 |
+
ALTER TABLE v1 ADD COLUMN new_column;
|
| 810 |
+
}
|
| 811 |
+
} {1 {Cannot add a column to a view}}
|
| 812 |
+
|
| 813 |
+
# Ticket #3102:
|
| 814 |
+
# Verify that comments do not interfere with the table rename
|
| 815 |
+
# algorithm.
|
| 816 |
+
#
|
| 817 |
+
do_test alter-13.1 {
|
| 818 |
+
execsql {
|
| 819 |
+
CREATE TABLE /* hi */ t3102a(x);
|
| 820 |
+
CREATE TABLE t3102b -- comment
|
| 821 |
+
(y);
|
| 822 |
+
CREATE INDEX t3102c ON t3102a(x);
|
| 823 |
+
SELECT name FROM sqlite_master WHERE name GLOB 't3102*' ORDER BY 1;
|
| 824 |
+
}
|
| 825 |
+
} {t3102a t3102b t3102c}
|
| 826 |
+
do_test alter-13.2 {
|
| 827 |
+
execsql {
|
| 828 |
+
ALTER TABLE t3102a RENAME TO t3102a_rename;
|
| 829 |
+
SELECT name FROM sqlite_master WHERE name GLOB 't3102*' ORDER BY 1;
|
| 830 |
+
}
|
| 831 |
+
} {t3102a_rename t3102b t3102c}
|
| 832 |
+
do_test alter-13.3 {
|
| 833 |
+
execsql {
|
| 834 |
+
ALTER TABLE t3102b RENAME TO t3102b_rename;
|
| 835 |
+
SELECT name FROM sqlite_master WHERE name GLOB 't3102*' ORDER BY 1;
|
| 836 |
+
}
|
| 837 |
+
} {t3102a_rename t3102b_rename t3102c}
|
| 838 |
+
|
| 839 |
+
# Ticket #3651
|
| 840 |
+
do_test alter-14.1 {
|
| 841 |
+
catchsql {
|
| 842 |
+
CREATE TABLE t3651(a UNIQUE);
|
| 843 |
+
INSERT INTO t3651 VALUES(5);
|
| 844 |
+
ALTER TABLE t3651 ADD COLUMN b UNIQUE;
|
| 845 |
+
}
|
| 846 |
+
} {1 {Cannot add a UNIQUE column}}
|
| 847 |
+
do_test alter-14.2 {
|
| 848 |
+
catchsql {
|
| 849 |
+
ALTER TABLE t3651 ADD COLUMN b PRIMARY KEY;
|
| 850 |
+
}
|
| 851 |
+
} {1 {Cannot add a PRIMARY KEY column}}
|
| 852 |
+
|
| 853 |
+
|
| 854 |
+
#-------------------------------------------------------------------------
|
| 855 |
+
# Test that it is not possible to use ALTER TABLE on any system table.
|
| 856 |
+
#
|
| 857 |
+
set system_table_list {1 sqlite_master}
|
| 858 |
+
catchsql ANALYZE
|
| 859 |
+
ifcapable analyze { lappend system_table_list 2 sqlite_stat1 }
|
| 860 |
+
ifcapable stat4 { lappend system_table_list 4 sqlite_stat4 }
|
| 861 |
+
|
| 862 |
+
foreach {tn tbl} $system_table_list {
|
| 863 |
+
do_test alter-15.$tn.1 {
|
| 864 |
+
catchsql "ALTER TABLE $tbl RENAME TO xyz"
|
| 865 |
+
} [list 1 "table $tbl may not be altered"]
|
| 866 |
+
|
| 867 |
+
do_test alter-15.$tn.2 {
|
| 868 |
+
catchsql "ALTER TABLE $tbl ADD COLUMN xyz"
|
| 869 |
+
} [list 1 "table $tbl may not be altered"]
|
| 870 |
+
}
|
| 871 |
+
|
| 872 |
+
#------------------------------------------------------------------------
|
| 873 |
+
# Verify that ALTER TABLE works on tables with the WITHOUT rowid option.
|
| 874 |
+
#
|
| 875 |
+
do_execsql_test alter-16.1 {
|
| 876 |
+
CREATE TABLE t16a(a TEXT, b REAL, c INT, PRIMARY KEY(a,b)) WITHOUT rowid;
|
| 877 |
+
INSERT INTO t16a VALUES('abc',1.25,99);
|
| 878 |
+
ALTER TABLE t16a ADD COLUMN d TEXT DEFAULT 'xyzzy';
|
| 879 |
+
INSERT INTO t16a VALUES('cba',5.5,98,'fizzle');
|
| 880 |
+
SELECT * FROM t16a ORDER BY a;
|
| 881 |
+
} {abc 1.25 99 xyzzy cba 5.5 98 fizzle}
|
| 882 |
+
do_execsql_test alter-16.2 {
|
| 883 |
+
ALTER TABLE t16a RENAME TO t16a_rn;
|
| 884 |
+
SELECT * FROM t16a_rn ORDER BY a;
|
| 885 |
+
} {abc 1.25 99 xyzzy cba 5.5 98 fizzle}
|
| 886 |
+
|
| 887 |
+
# 2018-09-16 ticket b41031ea2b5372378cb3d2d43cf9fe2a4a5c2510
|
| 888 |
+
#
|
| 889 |
+
ifcapable rtree {
|
| 890 |
+
db close
|
| 891 |
+
sqlite3 db :memory:
|
| 892 |
+
do_execsql_test alter-17.100 {
|
| 893 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
|
| 894 |
+
CREATE VIRTUAL TABLE t2 USING rtree(id,x0,x1);
|
| 895 |
+
INSERT INTO t1 VALUES(1,'apple'),(2,'fig'),(3,'pear');
|
| 896 |
+
INSERT INTO t2 VALUES(1,1.0,2.0),(2,2.0,3.0),(3,1.5,3.5);
|
| 897 |
+
CREATE TRIGGER r1 AFTER UPDATE ON t1 BEGIN
|
| 898 |
+
DELETE FROM t2 WHERE id = OLD.a;
|
| 899 |
+
END;
|
| 900 |
+
ALTER TABLE t1 RENAME TO t3;
|
| 901 |
+
UPDATE t3 SET b='peach' WHERE a=2;
|
| 902 |
+
SELECT * FROM t2 ORDER BY 1;
|
| 903 |
+
} {1 1.0 2.0 3 1.5 3.5}
|
| 904 |
+
}
|
| 905 |
+
|
| 906 |
+
# 2021-03-08 dbsqlfuzz 3f0a7245b69cd08617d7d7781ebaedb0fe765a93
|
| 907 |
+
reset_db
|
| 908 |
+
do_catchsql_test alter-18.1 {
|
| 909 |
+
CREATE TABLE t1(a,b,c);
|
| 910 |
+
CREATE TABLE log(a INTEGER PRIMARY KEY,b,c);
|
| 911 |
+
CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
|
| 912 |
+
INSERT INTO logx(a,b,c) VALUES(new.a,new.b,new.c)
|
| 913 |
+
ON CONFLICT(a) DO UPDATE SET c=excluded.c, b=new.b;
|
| 914 |
+
END;
|
| 915 |
+
ALTER TABLE log RENAME COLUMN a TO x;
|
| 916 |
+
} {1 {error in trigger tr1: no such table: main.logx}}
|
| 917 |
+
|
| 918 |
+
# 2021-10-13 dbsqlfuzz e89174cbfad2d904f06b5e24df0a22510b6a1c1e
|
| 919 |
+
reset_db
|
| 920 |
+
do_execsql_test alter-19.1 {
|
| 921 |
+
CREATE TABLE t1(x);
|
| 922 |
+
CREATE TABLE t2(c);
|
| 923 |
+
CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN
|
| 924 |
+
UPDATE t2 SET (c)=(
|
| 925 |
+
EXISTS(SELECT 1 WHERE (WITH cte1(a) AS (SELECT 1 FROM t1 WHERE (SELECT 1 WHERE (WITH cte2(b) AS (VALUES(1))SELECT b FROM cte2)))SELECT a FROM cte1))
|
| 926 |
+
);
|
| 927 |
+
END;
|
| 928 |
+
ALTER TABLE t2 RENAME TO t3;
|
| 929 |
+
} {}
|
| 930 |
+
do_execsql_test alter-19.2 {
|
| 931 |
+
SELECT name FROM sqlite_schema WHERE sql LIKE '%t2%';
|
| 932 |
+
} {}
|
| 933 |
+
do_execsql_test alter-19.3 {
|
| 934 |
+
SELECT name FROM sqlite_schema WHERE sql LIKE '%t3%' ORDER BY name;
|
| 935 |
+
} {r1 t3}
|
| 936 |
+
|
| 937 |
+
# 2023-10-14
|
| 938 |
+
# On an ALTER TABLE ADD COLUMN with a DEFAULT clause on a STRICT table
|
| 939 |
+
# make sure that the DEFAULT has a compatible type.
|
| 940 |
+
#
|
| 941 |
+
reset_db
|
| 942 |
+
do_execsql_test alter-20.1 {
|
| 943 |
+
CREATE TABLE t1(a INT) STRICT;
|
| 944 |
+
INSERT INTO t1(a) VALUES(45);
|
| 945 |
+
} {}
|
| 946 |
+
do_catchsql_test alter-20.2 {
|
| 947 |
+
ALTER TABLE t1 ADD COLUMN b TEXT DEFAULT x'313233';
|
| 948 |
+
} {1 {type mismatch on DEFAULT}}
|
| 949 |
+
do_execsql_test alter-20.2 {
|
| 950 |
+
DELETE FROM t1;
|
| 951 |
+
ALTER TABLE t1 ADD COLUMN b TEXT DEFAULT x'313233';
|
| 952 |
+
} {}
|
| 953 |
+
do_catchsql_test alter-20.3 {
|
| 954 |
+
INSERT INTO t1(a) VALUES(45);
|
| 955 |
+
} {1 {cannot store BLOB value in TEXT column t1.b}}
|
| 956 |
+
|
| 957 |
+
# 2023-11-17 dbsqlfuzz e0900262dadd5c78c2226ad6a435c7f0255be2cd
|
| 958 |
+
# Assertion fault associated with ALTER TABLE and an
|
| 959 |
+
# aggregate ORDER BY within an unknown aggregate function.
|
| 960 |
+
#
|
| 961 |
+
reset_db
|
| 962 |
+
do_execsql_test alter-21.1 {
|
| 963 |
+
CREATE TABLE t1(a,b,c,d);
|
| 964 |
+
CREATE TABLE t2(a,b,c,d,x);
|
| 965 |
+
CREATE TRIGGER r1 AFTER INSERT ON t2 BEGIN
|
| 966 |
+
SELECT unknown_function(a ORDER BY (SELECT group_concat(DISTINCT a ORDER BY a) FROM t1)) FROM t1;
|
| 967 |
+
END;
|
| 968 |
+
ALTER TABLE t2 RENAME TO e;
|
| 969 |
+
} {}
|
| 970 |
+
do_execsql_test alter-21.2 {
|
| 971 |
+
SELECT name, type FROM sqlite_schema ORDER BY name;
|
| 972 |
+
} {e table r1 trigger t1 table}
|
| 973 |
+
do_execsql_test alter-21.3 {
|
| 974 |
+
DROP TRIGGER r1;
|
| 975 |
+
CREATE TRIGGER r2 AFTER INSERT ON e BEGIN
|
| 976 |
+
SELECT unknown_function(a ORDER BY (SELECT group_concat(a ORDER BY a) FROM (SELECT b FROM t1))) FROM t1;
|
| 977 |
+
END;
|
| 978 |
+
ALTER TABLE e RENAME TO t99;
|
| 979 |
+
}
|
| 980 |
+
do_execsql_test alter-21.4 {
|
| 981 |
+
SELECT name, type FROM sqlite_schema ORDER BY name;
|
| 982 |
+
} {r2 trigger t1 table t99 table}
|
| 983 |
+
|
| 984 |
+
|
| 985 |
+
|
| 986 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/alter2.test
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/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-02/afc-sqlite3/test/alterauth2.test
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2018 October 6
|
| 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 alterauth2
|
| 23 |
+
|
| 24 |
+
set ::auth [list]
|
| 25 |
+
proc xAuth {type args} {
|
| 26 |
+
lappend ::auth [concat $type [lrange $args 0 3]]
|
| 27 |
+
if {$type=="SQLITE_READ" && [lindex $args 0] == "t2"} breakpoint
|
| 28 |
+
return SQLITE_OK
|
| 29 |
+
}
|
| 30 |
+
db auth xAuth
|
| 31 |
+
|
| 32 |
+
proc do_auth_test {tn sql authcode} {
|
| 33 |
+
set script "
|
| 34 |
+
set ::auth \[list\]
|
| 35 |
+
execsql {$sql}
|
| 36 |
+
lsort -unique \[set ::auth\]
|
| 37 |
+
"
|
| 38 |
+
|
| 39 |
+
set normal [list {*}$authcode]
|
| 40 |
+
uplevel [list do_test $tn $script $normal]
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
do_execsql_test 1.0 {
|
| 44 |
+
CREATE TABLE t1(a, b, c);
|
| 45 |
+
CREATE VIEW v1 AS SELECT * FROM t1;
|
| 46 |
+
CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
|
| 47 |
+
DELETE FROM t1 WHERE a<new.a;
|
| 48 |
+
END;
|
| 49 |
+
|
| 50 |
+
CREATE TEMP TRIGGER tr2 AFTER UPDATE OF a, b ON t1 BEGIN
|
| 51 |
+
UPDATE t1 SET a=a+1 WHERE new.b<b;
|
| 52 |
+
END;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
do_auth_test 1.1 {
|
| 56 |
+
ALTER TABLE t1 RENAME TO t2;
|
| 57 |
+
} {
|
| 58 |
+
{SQLITE_ALTER_TABLE main t1 {} {}}
|
| 59 |
+
{SQLITE_FUNCTION {} like {} {}}
|
| 60 |
+
{SQLITE_FUNCTION {} sqlite_rename_table {} {}}
|
| 61 |
+
{SQLITE_FUNCTION {} sqlite_rename_test {} {}}
|
| 62 |
+
{SQLITE_FUNCTION {} substr {} {}}
|
| 63 |
+
{SQLITE_READ sqlite_master name main {}}
|
| 64 |
+
{SQLITE_READ sqlite_master sql main {}}
|
| 65 |
+
{SQLITE_READ sqlite_master tbl_name main {}}
|
| 66 |
+
{SQLITE_READ sqlite_master type main {}}
|
| 67 |
+
{SQLITE_READ sqlite_temp_master name temp {}}
|
| 68 |
+
{SQLITE_READ sqlite_temp_master sql temp {}}
|
| 69 |
+
{SQLITE_READ sqlite_temp_master tbl_name temp {}}
|
| 70 |
+
{SQLITE_READ sqlite_temp_master type temp {}}
|
| 71 |
+
{SQLITE_SELECT {} {} {} {}}
|
| 72 |
+
{SQLITE_UPDATE sqlite_master name main {}}
|
| 73 |
+
{SQLITE_UPDATE sqlite_master sql main {}}
|
| 74 |
+
{SQLITE_UPDATE sqlite_master tbl_name main {}}
|
| 75 |
+
{SQLITE_UPDATE sqlite_temp_master sql temp {}}
|
| 76 |
+
{SQLITE_UPDATE sqlite_temp_master tbl_name temp {}}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
do_auth_test 1.2 {
|
| 80 |
+
ALTER TABLE t2 RENAME a TO aaa;
|
| 81 |
+
} {
|
| 82 |
+
{SQLITE_ALTER_TABLE main t2 {} {}}
|
| 83 |
+
{SQLITE_FUNCTION {} like {} {}}
|
| 84 |
+
{SQLITE_FUNCTION {} sqlite_rename_column {} {}}
|
| 85 |
+
{SQLITE_FUNCTION {} sqlite_rename_quotefix {} {}}
|
| 86 |
+
{SQLITE_FUNCTION {} sqlite_rename_test {} {}}
|
| 87 |
+
{SQLITE_READ sqlite_master name main {}}
|
| 88 |
+
{SQLITE_READ sqlite_master sql main {}}
|
| 89 |
+
{SQLITE_READ sqlite_master tbl_name main {}}
|
| 90 |
+
{SQLITE_READ sqlite_master type main {}}
|
| 91 |
+
{SQLITE_READ sqlite_temp_master name temp {}}
|
| 92 |
+
{SQLITE_READ sqlite_temp_master sql temp {}}
|
| 93 |
+
{SQLITE_READ sqlite_temp_master type temp {}}
|
| 94 |
+
{SQLITE_SELECT {} {} {} {}}
|
| 95 |
+
{SQLITE_UPDATE sqlite_master sql main {}}
|
| 96 |
+
{SQLITE_UPDATE sqlite_temp_master sql temp {}}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
do_auth_test 1.3 {
|
| 100 |
+
ALTER TABLE t2 DROP COLUMN c;
|
| 101 |
+
} {
|
| 102 |
+
{SQLITE_ALTER_TABLE main t2 c {}}
|
| 103 |
+
{SQLITE_FUNCTION {} like {} {}}
|
| 104 |
+
{SQLITE_FUNCTION {} sqlite_drop_column {} {}}
|
| 105 |
+
{SQLITE_FUNCTION {} sqlite_rename_quotefix {} {}}
|
| 106 |
+
{SQLITE_FUNCTION {} sqlite_rename_test {} {}}
|
| 107 |
+
{SQLITE_READ sqlite_master name main {}}
|
| 108 |
+
{SQLITE_READ sqlite_master sql main {}}
|
| 109 |
+
{SQLITE_READ sqlite_master tbl_name main {}}
|
| 110 |
+
{SQLITE_READ sqlite_master type main {}}
|
| 111 |
+
{SQLITE_READ sqlite_temp_master name temp {}}
|
| 112 |
+
{SQLITE_READ sqlite_temp_master sql temp {}}
|
| 113 |
+
{SQLITE_READ sqlite_temp_master type temp {}}
|
| 114 |
+
{SQLITE_SELECT {} {} {} {}}
|
| 115 |
+
{SQLITE_UPDATE sqlite_master sql main {}}
|
| 116 |
+
{SQLITE_UPDATE sqlite_temp_master sql temp {}}
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/altercol.test
ADDED
|
@@ -0,0 +1,949 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/afc-sqlite3/test/altercorrupt.test
ADDED
|
@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/afc-sqlite3/test/alterdropcol.test
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 |
+
# 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-02/afc-sqlite3/test/alterdropcol2.test
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2021 February 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 |
+
|
| 13 |
+
set testdir [file dirname $argv0]
|
| 14 |
+
source $testdir/tester.tcl
|
| 15 |
+
set testprefix alterdropcol2
|
| 16 |
+
|
| 17 |
+
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
|
| 18 |
+
ifcapable !altertable {
|
| 19 |
+
finish_test
|
| 20 |
+
return
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# EVIDENCE-OF: R-58318-35349 The DROP COLUMN syntax is used to remove an
|
| 24 |
+
# existing column from a table.
|
| 25 |
+
do_execsql_test 1.0 {
|
| 26 |
+
CREATE TABLE t1(c, b, a, PRIMARY KEY(b, a)) WITHOUT ROWID;
|
| 27 |
+
INSERT INTO t1 VALUES(1, 2, 3), (4, 5, 6);
|
| 28 |
+
}
|
| 29 |
+
do_execsql_test 1.1 {
|
| 30 |
+
ALTER TABLE t1 DROP c;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# EVIDENCE-OF: The DROP COLUMN command removes the named column from the table,
|
| 34 |
+
# and also rewrites the entire table to purge the data associated with that
|
| 35 |
+
# column.
|
| 36 |
+
do_execsql_test 1.2.1 {
|
| 37 |
+
SELECT * FROM t1;
|
| 38 |
+
} {2 3 5 6}
|
| 39 |
+
|
| 40 |
+
do_execsql_test 1.2.2 {
|
| 41 |
+
SELECT sql FROM sqlite_schema;
|
| 42 |
+
} {
|
| 43 |
+
{CREATE TABLE t1(b, a, PRIMARY KEY(b, a)) WITHOUT ROWID}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
proc do_atdc_error_test {tn schema atdc error} {
|
| 47 |
+
reset_db
|
| 48 |
+
execsql $schema
|
| 49 |
+
uplevel [list do_catchsql_test $tn $atdc [list 1 [string trim $error]]]
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
#-------------------------------------------------------------------------
|
| 53 |
+
# Test cases 2.* attempt to verify the following:
|
| 54 |
+
#
|
| 55 |
+
# EVIDENCE-OF: R-24098-10282 The DROP COLUMN command only works if the column
|
| 56 |
+
# is not referenced by any other parts of the schema and is not a PRIMARY KEY
|
| 57 |
+
# and does not have a UNIQUE constraint.
|
| 58 |
+
#
|
| 59 |
+
|
| 60 |
+
# EVIDENCE-OF: R-52436-31752 The column is a PRIMARY KEY or part of one.
|
| 61 |
+
#
|
| 62 |
+
do_atdc_error_test 2.1.1 {
|
| 63 |
+
CREATE TABLE x1(a PRIMARY KEY, b, c);
|
| 64 |
+
} {
|
| 65 |
+
ALTER TABLE x1 DROP COLUMN a
|
| 66 |
+
} {
|
| 67 |
+
cannot drop PRIMARY KEY column: "a"
|
| 68 |
+
}
|
| 69 |
+
do_atdc_error_test 2.1.2 {
|
| 70 |
+
CREATE TABLE x1(a,b,c,d,e, PRIMARY KEY(b,c,d));
|
| 71 |
+
} {
|
| 72 |
+
ALTER TABLE x1 DROP COLUMN c
|
| 73 |
+
} {
|
| 74 |
+
cannot drop PRIMARY KEY column: "c"
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
# EVIDENCE-OF: R-43412-16016 The column has a UNIQUE constraint.
|
| 78 |
+
#
|
| 79 |
+
do_atdc_error_test 2.2.1 {
|
| 80 |
+
CREATE TABLE x1(a PRIMARY KEY, b, c UNIQUE);
|
| 81 |
+
} {
|
| 82 |
+
ALTER TABLE x1 DROP COLUMN c
|
| 83 |
+
} {
|
| 84 |
+
cannot drop UNIQUE column: "c"
|
| 85 |
+
}
|
| 86 |
+
do_atdc_error_test 2.2.2 {
|
| 87 |
+
CREATE TABLE x1(a PRIMARY KEY, b, c, UNIQUE(b, c));
|
| 88 |
+
} {
|
| 89 |
+
ALTER TABLE x1 DROP COLUMN c
|
| 90 |
+
} {
|
| 91 |
+
error in table x1 after drop column: no such column: c
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
# EVIDENCE-OF: R-46731-08965 The column is indexed.
|
| 95 |
+
#
|
| 96 |
+
do_atdc_error_test 2.3.1 {
|
| 97 |
+
CREATE TABLE 'one two'('x y', 'z 1', 'a b');
|
| 98 |
+
CREATE INDEX idx ON 'one two'('z 1');
|
| 99 |
+
} {
|
| 100 |
+
ALTER TABLE 'one two' DROP COLUMN 'z 1'
|
| 101 |
+
} {
|
| 102 |
+
error in index idx after drop column: no such column: z 1
|
| 103 |
+
}
|
| 104 |
+
do_atdc_error_test 2.3.2 {
|
| 105 |
+
CREATE TABLE x1(a, b, c);
|
| 106 |
+
CREATE INDEX idx ON x1(a);
|
| 107 |
+
} {
|
| 108 |
+
ALTER TABLE x1 DROP COLUMN a;
|
| 109 |
+
} {
|
| 110 |
+
error in index idx after drop column: no such column: a
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
# EVIDENCE-OF: R-46731-08965 The column is indexed.
|
| 114 |
+
#
|
| 115 |
+
do_atdc_error_test 2.4.1 {
|
| 116 |
+
CREATE TABLE x1234(a, b, c PRIMARY KEY) WITHOUT ROWID;
|
| 117 |
+
CREATE INDEX i1 ON x1234(b) WHERE ((a+5) % 10)==0;
|
| 118 |
+
} {
|
| 119 |
+
ALTER TABLE x1234 DROP a
|
| 120 |
+
} {
|
| 121 |
+
error in index i1 after drop column: no such column: a
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
# EVIDENCE-OF: R-47838-03249 The column is named in a table or column
|
| 125 |
+
# CHECK constraint not associated with the column being dropped.
|
| 126 |
+
#
|
| 127 |
+
do_atdc_error_test 2.5.1 {
|
| 128 |
+
CREATE TABLE x1234(a, b, c PRIMARY KEY, CHECK(((a+5)%10)!=0)) WITHOUT ROWID;
|
| 129 |
+
} {
|
| 130 |
+
ALTER TABLE x1234 DROP a
|
| 131 |
+
} {
|
| 132 |
+
error in table x1234 after drop column: no such column: a
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
# EVIDENCE-OF: R-55640-01652 The column is used in a foreign key constraint.
|
| 136 |
+
#
|
| 137 |
+
do_atdc_error_test 2.6.1 {
|
| 138 |
+
CREATE TABLE p1(x, y UNIQUE);
|
| 139 |
+
CREATE TABLE c1(u, v, FOREIGN KEY (v) REFERENCES p1(y))
|
| 140 |
+
} {
|
| 141 |
+
ALTER TABLE c1 DROP v
|
| 142 |
+
} {
|
| 143 |
+
error in table c1 after drop column: unknown column "v" in foreign key definition
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
# EVIDENCE-OF: R-20795-39479 The column is used in the expression of a
|
| 147 |
+
# generated column.
|
| 148 |
+
do_atdc_error_test 2.7.1 {
|
| 149 |
+
CREATE TABLE c1(u, v, w AS (u+v));
|
| 150 |
+
} {
|
| 151 |
+
ALTER TABLE c1 DROP v
|
| 152 |
+
} {
|
| 153 |
+
error in table c1 after drop column: no such column: v
|
| 154 |
+
}
|
| 155 |
+
do_atdc_error_test 2.7.2 {
|
| 156 |
+
CREATE TABLE c1(u, v, w AS (u+v) STORED);
|
| 157 |
+
} {
|
| 158 |
+
ALTER TABLE c1 DROP u
|
| 159 |
+
} {
|
| 160 |
+
error in table c1 after drop column: no such column: u
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
# EVIDENCE-OF: R-01515-49025 The column appears in a trigger or view.
|
| 164 |
+
#
|
| 165 |
+
do_atdc_error_test 2.8.1 {
|
| 166 |
+
CREATE TABLE log(l);
|
| 167 |
+
CREATE TABLE c1(u, v, w);
|
| 168 |
+
CREATE TRIGGER tr1 AFTER INSERT ON c1 BEGIN
|
| 169 |
+
INSERT INTO log VALUES(new.w);
|
| 170 |
+
END;
|
| 171 |
+
} {
|
| 172 |
+
ALTER TABLE c1 DROP w
|
| 173 |
+
} {
|
| 174 |
+
error in trigger tr1 after drop column: no such column: new.w
|
| 175 |
+
}
|
| 176 |
+
do_atdc_error_test 2.8.2 {
|
| 177 |
+
CREATE TABLE c1(u, v, w);
|
| 178 |
+
CREATE VIEW v1 AS SELECT u, v, w FROM c1;
|
| 179 |
+
} {
|
| 180 |
+
ALTER TABLE c1 DROP w
|
| 181 |
+
} {
|
| 182 |
+
error in view v1 after drop column: no such column: w
|
| 183 |
+
}
|
| 184 |
+
do_atdc_error_test 2.8.3 {
|
| 185 |
+
CREATE TABLE c1(u, v, w);
|
| 186 |
+
CREATE VIEW v1 AS SELECT * FROM c1 WHERE w IS NOT NULL;
|
| 187 |
+
} {
|
| 188 |
+
ALTER TABLE c1 DROP w
|
| 189 |
+
} {
|
| 190 |
+
error in view v1 after drop column: no such column: w
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
#-------------------------------------------------------------------------
|
| 194 |
+
# Verify that a column that is part of a CHECK constraint may be dropped
|
| 195 |
+
# if the CHECK constraint was specified as part of the column definition.
|
| 196 |
+
#
|
| 197 |
+
|
| 198 |
+
# STALE-EVIDENCE: R-60924-11170 However, the column being deleted can be used in a
|
| 199 |
+
# column CHECK constraint because the column CHECK constraint is dropped
|
| 200 |
+
# together with the column itself.
|
| 201 |
+
do_execsql_test 3.0 {
|
| 202 |
+
CREATE TABLE yyy(q, w, e CHECK (e > 0), r);
|
| 203 |
+
INSERT INTO yyy VALUES(1,1,1,1), (2,2,2,2);
|
| 204 |
+
|
| 205 |
+
CREATE TABLE zzz(q, w, e, r, CHECK (e > 0));
|
| 206 |
+
INSERT INTO zzz VALUES(1,1,1,1), (2,2,2,2);
|
| 207 |
+
}
|
| 208 |
+
do_catchsql_test 3.1.1 {
|
| 209 |
+
INSERT INTO yyy VALUES(0,0,0,0);
|
| 210 |
+
} {1 {CHECK constraint failed: e > 0}}
|
| 211 |
+
do_catchsql_test 3.1.2 {
|
| 212 |
+
INSERT INTO yyy VALUES(0,0,0,0);
|
| 213 |
+
} {1 {CHECK constraint failed: e > 0}}
|
| 214 |
+
|
| 215 |
+
do_execsql_test 3.2.1 {
|
| 216 |
+
ALTER TABLE yyy DROP e;
|
| 217 |
+
}
|
| 218 |
+
do_catchsql_test 3.2.2 {
|
| 219 |
+
ALTER TABLE zzz DROP e;
|
| 220 |
+
} {1 {error in table zzz after drop column: no such column: e}}
|
| 221 |
+
|
| 222 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/alterfault.test
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2021 November 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.
|
| 12 |
+
#
|
| 13 |
+
|
| 14 |
+
set testdir [file dirname $argv0]
|
| 15 |
+
source $testdir/tester.tcl
|
| 16 |
+
set testprefix alterfault
|
| 17 |
+
|
| 18 |
+
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
|
| 19 |
+
ifcapable !altertable {
|
| 20 |
+
finish_test
|
| 21 |
+
return
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
do_execsql_test 1.0 {
|
| 25 |
+
CREATE TABLE t1(a);
|
| 26 |
+
}
|
| 27 |
+
faultsim_save_and_close
|
| 28 |
+
|
| 29 |
+
do_faultsim_test 1.1 -faults oom* -prep {
|
| 30 |
+
faultsim_restore_and_reopen
|
| 31 |
+
} -body {
|
| 32 |
+
execsql {
|
| 33 |
+
ALTER TABLE t1 ADD COLUMN b CHECK (a!=1)
|
| 34 |
+
}
|
| 35 |
+
} -test {
|
| 36 |
+
faultsim_test_result {0 {}}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/altermalloc.test
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ALTER TABLE statement and
|
| 13 |
+
# specifically out-of-memory conditions within that command.
|
| 14 |
+
#
|
| 15 |
+
# $Id: altermalloc.test,v 1.10 2008/10/30 17:21:13 danielk1977 Exp $
|
| 16 |
+
#
|
| 17 |
+
|
| 18 |
+
set testdir [file dirname $argv0]
|
| 19 |
+
source $testdir/tester.tcl
|
| 20 |
+
|
| 21 |
+
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
|
| 22 |
+
ifcapable !altertable {
|
| 23 |
+
finish_test
|
| 24 |
+
return
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
source $testdir/malloc_common.tcl
|
| 28 |
+
|
| 29 |
+
do_malloc_test altermalloc-1 -tclprep {
|
| 30 |
+
db close
|
| 31 |
+
} -tclbody {
|
| 32 |
+
if {[catch {sqlite3 db test.db}]} {
|
| 33 |
+
error "out of memory"
|
| 34 |
+
}
|
| 35 |
+
sqlite3_db_config_lookaside db 0 0 0
|
| 36 |
+
sqlite3_extended_result_codes db 1
|
| 37 |
+
} -sqlbody {
|
| 38 |
+
CREATE TABLE t1(a int);
|
| 39 |
+
ALTER TABLE t1 ADD COLUMN b INTEGER DEFAULT NULL;
|
| 40 |
+
ALTER TABLE t1 ADD COLUMN c TEXT DEFAULT 'default-text';
|
| 41 |
+
ALTER TABLE t1 RENAME TO t2;
|
| 42 |
+
ALTER TABLE t2 ADD COLUMN d BLOB DEFAULT X'ABCD';
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Test malloc() failure on an ALTER TABLE on a virtual table.
|
| 46 |
+
#
|
| 47 |
+
ifcapable vtab {
|
| 48 |
+
do_malloc_test altermalloc-vtab -tclprep {
|
| 49 |
+
sqlite3 db2 test.db
|
| 50 |
+
sqlite3_db_config_lookaside db2 0 0 0
|
| 51 |
+
sqlite3_extended_result_codes db2 1
|
| 52 |
+
register_echo_module [sqlite3_connection_pointer db2]
|
| 53 |
+
db2 eval {
|
| 54 |
+
CREATE TABLE t1(a, b VARCHAR, c INTEGER);
|
| 55 |
+
CREATE VIRTUAL TABLE t1echo USING echo(t1);
|
| 56 |
+
}
|
| 57 |
+
db2 close
|
| 58 |
+
|
| 59 |
+
register_echo_module [sqlite3_connection_pointer db]
|
| 60 |
+
} -tclbody {
|
| 61 |
+
set rc [catch {db eval { ALTER TABLE t1echo RENAME TO t1_echo }} msg]
|
| 62 |
+
if {$msg eq "vtable constructor failed: t1echo"} {
|
| 63 |
+
set msg "out of memory"
|
| 64 |
+
}
|
| 65 |
+
if {$rc} {
|
| 66 |
+
error $msg
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/altertab.test
ADDED
|
@@ -0,0 +1,1002 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2018 August 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 |
+
|
| 13 |
+
set testdir [file dirname $argv0]
|
| 14 |
+
source $testdir/tester.tcl
|
| 15 |
+
set testprefix altertab
|
| 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, CHECK(t1.a != t1.b));
|
| 25 |
+
|
| 26 |
+
CREATE TABLE t2(a, b);
|
| 27 |
+
CREATE INDEX t2expr ON t2(a) WHERE t2.b>0;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
do_execsql_test 1.1 {
|
| 31 |
+
SELECT sql FROM sqlite_master
|
| 32 |
+
} {
|
| 33 |
+
{CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))}
|
| 34 |
+
{CREATE TABLE t2(a, b)}
|
| 35 |
+
{CREATE INDEX t2expr ON t2(a) WHERE t2.b>0}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
do_execsql_test 1.2 {
|
| 39 |
+
ALTER TABLE t1 RENAME TO t1new;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
do_execsql_test 1.3 {
|
| 43 |
+
CREATE TABLE t3(c, d);
|
| 44 |
+
ALTER TABLE t3 RENAME TO t3new;
|
| 45 |
+
DROP TABLE t3new;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
do_execsql_test 1.4 {
|
| 49 |
+
SELECT sql FROM sqlite_master
|
| 50 |
+
} {
|
| 51 |
+
{CREATE TABLE "t1new"(a, b, CHECK("t1new".a != "t1new".b))}
|
| 52 |
+
{CREATE TABLE t2(a, b)}
|
| 53 |
+
{CREATE INDEX t2expr ON t2(a) WHERE t2.b>0}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
do_execsql_test 1.3 {
|
| 58 |
+
ALTER TABLE t2 RENAME TO t2new;
|
| 59 |
+
}
|
| 60 |
+
do_execsql_test 1.4 {
|
| 61 |
+
SELECT sql FROM sqlite_master
|
| 62 |
+
} {
|
| 63 |
+
{CREATE TABLE "t1new"(a, b, CHECK("t1new".a != "t1new".b))}
|
| 64 |
+
{CREATE TABLE "t2new"(a, b)}
|
| 65 |
+
{CREATE INDEX t2expr ON "t2new"(a) WHERE "t2new".b>0}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
#-------------------------------------------------------------------------
|
| 70 |
+
reset_db
|
| 71 |
+
ifcapable vtab {
|
| 72 |
+
register_echo_module db
|
| 73 |
+
|
| 74 |
+
do_execsql_test 2.0 {
|
| 75 |
+
CREATE TABLE abc(a, b, c);
|
| 76 |
+
INSERT INTO abc VALUES(1, 2, 3);
|
| 77 |
+
CREATE VIRTUAL TABLE eee USING echo('abc');
|
| 78 |
+
SELECT * FROM eee;
|
| 79 |
+
} {1 2 3}
|
| 80 |
+
|
| 81 |
+
do_execsql_test 2.1 {
|
| 82 |
+
ALTER TABLE eee RENAME TO fff;
|
| 83 |
+
SELECT * FROM fff;
|
| 84 |
+
} {1 2 3}
|
| 85 |
+
|
| 86 |
+
db close
|
| 87 |
+
sqlite3 db test.db
|
| 88 |
+
|
| 89 |
+
do_catchsql_test 2.2 {
|
| 90 |
+
ALTER TABLE fff RENAME TO ggg;
|
| 91 |
+
} {1 {no such module: echo}}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
#-------------------------------------------------------------------------
|
| 95 |
+
reset_db
|
| 96 |
+
|
| 97 |
+
do_execsql_test 3.0 {
|
| 98 |
+
CREATE TABLE txx(a, b, c);
|
| 99 |
+
INSERT INTO txx VALUES(1, 2, 3);
|
| 100 |
+
CREATE VIEW vvv AS SELECT main.txx.a, txx.b, c FROM txx;
|
| 101 |
+
CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM txx AS one;
|
| 102 |
+
CREATE VIEW temp.ttt AS SELECT main.txx.a, txx.b, one.b, main.one.a FROM txx AS one, txx;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
do_execsql_test 3.1.1 {
|
| 106 |
+
SELECT * FROM vvv;
|
| 107 |
+
} {1 2 3}
|
| 108 |
+
do_execsql_test 3.1.2 {
|
| 109 |
+
ALTER TABLE txx RENAME TO "t xx";
|
| 110 |
+
SELECT * FROM vvv;
|
| 111 |
+
} {1 2 3}
|
| 112 |
+
do_execsql_test 3.1.3 {
|
| 113 |
+
SELECT sql FROM sqlite_master WHERE name='vvv';
|
| 114 |
+
} {{CREATE VIEW vvv AS SELECT main."t xx".a, "t xx".b, c FROM "t xx"}}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
do_execsql_test 3.2.1 {
|
| 118 |
+
SELECT * FROM uuu;
|
| 119 |
+
} {1 2 3}
|
| 120 |
+
do_execsql_test 3.2.2 {
|
| 121 |
+
SELECT sql FROM sqlite_master WHERE name='uuu';;
|
| 122 |
+
} {{CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM "t xx" AS one}}
|
| 123 |
+
|
| 124 |
+
do_execsql_test 3.3.1 {
|
| 125 |
+
SELECT * FROM ttt;
|
| 126 |
+
} {1 2 2 1}
|
| 127 |
+
do_execsql_test 3.3.2 {
|
| 128 |
+
SELECT sql FROM sqlite_temp_master WHERE name='ttt';
|
| 129 |
+
} {{CREATE VIEW ttt AS SELECT main."t xx".a, "t xx".b, one.b, main.one.a FROM "t xx" AS one, "t xx"}}
|
| 130 |
+
|
| 131 |
+
#-------------------------------------------------------------------------
|
| 132 |
+
reset_db
|
| 133 |
+
do_execsql_test 4.0 {
|
| 134 |
+
CREATE table t1(x, y);
|
| 135 |
+
CREATE table t2(a, b);
|
| 136 |
+
|
| 137 |
+
CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
|
| 138 |
+
SELECT t1.x, * FROM t1, t2;
|
| 139 |
+
INSERT INTO t2 VALUES(new.x, new.y);
|
| 140 |
+
END;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
do_execsql_test 4.1 {
|
| 144 |
+
INSERT INTO t1 VALUES(1, 1);
|
| 145 |
+
ALTER TABLE t1 RENAME TO t11;
|
| 146 |
+
INSERT INTO t11 VALUES(2, 2);
|
| 147 |
+
ALTER TABLE t2 RENAME TO t22;
|
| 148 |
+
INSERT INTO t11 VALUES(3, 3);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
proc squish {a} {
|
| 152 |
+
string trim [regsub -all {[[:space:]][[:space:]]*} $a { }]
|
| 153 |
+
}
|
| 154 |
+
db func squish squish
|
| 155 |
+
do_test 4.2 {
|
| 156 |
+
execsql { SELECT squish(sql) FROM sqlite_master WHERE name = 'tr1' }
|
| 157 |
+
} [list [squish {
|
| 158 |
+
CREATE TRIGGER tr1 AFTER INSERT ON "t11" BEGIN
|
| 159 |
+
SELECT "t11".x, * FROM "t11", "t22";
|
| 160 |
+
INSERT INTO "t22" VALUES(new.x, new.y);
|
| 161 |
+
END
|
| 162 |
+
}]]
|
| 163 |
+
|
| 164 |
+
#-------------------------------------------------------------------------
|
| 165 |
+
reset_db
|
| 166 |
+
do_execsql_test 5.0 {
|
| 167 |
+
CREATE TABLE t9(a, b, c);
|
| 168 |
+
CREATE TABLE t10(a, b, c);
|
| 169 |
+
CREATE TEMP TABLE t9(a, b, c);
|
| 170 |
+
|
| 171 |
+
CREATE TRIGGER temp.t9t AFTER INSERT ON temp.t9 BEGIN
|
| 172 |
+
INSERT INTO t10 VALUES(new.a, new.b, new.c);
|
| 173 |
+
END;
|
| 174 |
+
|
| 175 |
+
INSERT INTO temp.t9 VALUES(1, 2, 3);
|
| 176 |
+
SELECT * FROM t10;
|
| 177 |
+
} {1 2 3}
|
| 178 |
+
|
| 179 |
+
do_execsql_test 5.1 {
|
| 180 |
+
ALTER TABLE temp.t9 RENAME TO 't1234567890'
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
do_execsql_test 5.2 {
|
| 184 |
+
CREATE TABLE t1(a, b);
|
| 185 |
+
CREATE TABLE t2(a, b);
|
| 186 |
+
INSERT INTO t1 VALUES(1, 2);
|
| 187 |
+
INSERT INTO t2 VALUES(3, 4);
|
| 188 |
+
CREATE VIEW v AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2;
|
| 189 |
+
SELECT * FROM v;
|
| 190 |
+
} {1 2 3 4}
|
| 191 |
+
|
| 192 |
+
do_catchsql_test 5.3 {
|
| 193 |
+
ALTER TABLE t2 RENAME TO one;
|
| 194 |
+
} {1 {error in view v after rename: ambiguous column name: one.a}}
|
| 195 |
+
|
| 196 |
+
do_execsql_test 5.4 {
|
| 197 |
+
SELECT * FROM v
|
| 198 |
+
} {1 2 3 4}
|
| 199 |
+
|
| 200 |
+
do_execsql_test 5.5 {
|
| 201 |
+
DROP VIEW v;
|
| 202 |
+
CREATE VIEW temp.vv AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2;
|
| 203 |
+
SELECT * FROM vv;
|
| 204 |
+
} {1 2 3 4}
|
| 205 |
+
|
| 206 |
+
do_catchsql_test 5.6 {
|
| 207 |
+
ALTER TABLE t2 RENAME TO one;
|
| 208 |
+
} {1 {error in view vv after rename: ambiguous column name: one.a}}
|
| 209 |
+
|
| 210 |
+
#-------------------------------------------------------------------------
|
| 211 |
+
|
| 212 |
+
ifcapable vtab {
|
| 213 |
+
register_tcl_module db
|
| 214 |
+
proc tcl_command {method args} {
|
| 215 |
+
switch -- $method {
|
| 216 |
+
xConnect {
|
| 217 |
+
return "CREATE TABLE t1(a, b, c)"
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
return {}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
do_execsql_test 6.0 {
|
| 224 |
+
CREATE VIRTUAL TABLE x1 USING tcl(tcl_command);
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
do_execsql_test 6.1 {
|
| 228 |
+
ALTER TABLE x1 RENAME TO x2;
|
| 229 |
+
SELECT sql FROM sqlite_master WHERE name = 'x2'
|
| 230 |
+
} {{CREATE VIRTUAL TABLE "x2" USING tcl(tcl_command)}}
|
| 231 |
+
|
| 232 |
+
do_execsql_test 7.1 {
|
| 233 |
+
CREATE TABLE ddd(db, sql, zOld, zNew, bTemp);
|
| 234 |
+
INSERT INTO ddd VALUES(
|
| 235 |
+
'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', 'ddd', NULL, 0
|
| 236 |
+
), (
|
| 237 |
+
'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', NULL, 'eee', 0
|
| 238 |
+
), (
|
| 239 |
+
'main', NULL, 'ddd', 'eee', 0
|
| 240 |
+
);
|
| 241 |
+
} {}
|
| 242 |
+
|
| 243 |
+
sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
|
| 244 |
+
do_execsql_test 7.2 {
|
| 245 |
+
SELECT
|
| 246 |
+
sqlite_rename_table(db, 0, 0, sql, zOld, zNew, bTemp)
|
| 247 |
+
FROM ddd;
|
| 248 |
+
} {{} {} {}}
|
| 249 |
+
sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
#-------------------------------------------------------------------------
|
| 253 |
+
#
|
| 254 |
+
reset_db
|
| 255 |
+
forcedelete test.db2
|
| 256 |
+
do_execsql_test 8.1 {
|
| 257 |
+
ATTACH 'test.db2' AS aux;
|
| 258 |
+
PRAGMA foreign_keys = on;
|
| 259 |
+
CREATE TABLE aux.p1(a INTEGER PRIMARY KEY, b);
|
| 260 |
+
CREATE TABLE aux.c1(x INTEGER PRIMARY KEY, y REFERENCES p1(a));
|
| 261 |
+
INSERT INTO aux.p1 VALUES(1, 1);
|
| 262 |
+
INSERT INTO aux.p1 VALUES(2, 2);
|
| 263 |
+
INSERT INTO aux.c1 VALUES(NULL, 2);
|
| 264 |
+
CREATE TABLE aux.c2(x INTEGER PRIMARY KEY, y REFERENCES c1(a));
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
do_execsql_test 8.2 {
|
| 268 |
+
ALTER TABLE aux.p1 RENAME TO ppp;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
do_execsql_test 8.2 {
|
| 272 |
+
INSERT INTO aux.c1 VALUES(NULL, 1);
|
| 273 |
+
SELECT sql FROM aux.sqlite_master WHERE name = 'c1';
|
| 274 |
+
} {{CREATE TABLE c1(x INTEGER PRIMARY KEY, y REFERENCES "ppp"(a))}}
|
| 275 |
+
|
| 276 |
+
reset_db
|
| 277 |
+
do_execsql_test 9.0 {
|
| 278 |
+
CREATE TABLE t1(a, b, c);
|
| 279 |
+
CREATE VIEW v1 AS SELECT * FROM t2;
|
| 280 |
+
}
|
| 281 |
+
do_catchsql_test 9.1 {
|
| 282 |
+
ALTER TABLE t1 RENAME TO t3;
|
| 283 |
+
} {1 {error in view v1: no such table: main.t2}}
|
| 284 |
+
do_execsql_test 9.2 {
|
| 285 |
+
DROP VIEW v1;
|
| 286 |
+
CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN
|
| 287 |
+
INSERT INTO t2 VALUES(new.a);
|
| 288 |
+
END;
|
| 289 |
+
}
|
| 290 |
+
do_catchsql_test 9.3 {
|
| 291 |
+
ALTER TABLE t1 RENAME TO t3;
|
| 292 |
+
} {1 {error in trigger tr: no such table: main.t2}}
|
| 293 |
+
|
| 294 |
+
forcedelete test.db2
|
| 295 |
+
do_execsql_test 9.4 {
|
| 296 |
+
DROP TRIGGER tr;
|
| 297 |
+
|
| 298 |
+
ATTACH 'test.db2' AS aux;
|
| 299 |
+
CREATE TRIGGER tr AFTER INSERT ON t1 WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END;
|
| 300 |
+
|
| 301 |
+
CREATE TABLE aux.t1(x);
|
| 302 |
+
CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END;
|
| 303 |
+
}
|
| 304 |
+
do_execsql_test 9.5 {
|
| 305 |
+
ALTER TABLE main.t1 RENAME TO t3;
|
| 306 |
+
}
|
| 307 |
+
do_execsql_test 9.6 {
|
| 308 |
+
SELECT sql FROM sqlite_temp_master;
|
| 309 |
+
SELECT sql FROM sqlite_master WHERE type='trigger';
|
| 310 |
+
} {
|
| 311 |
+
{CREATE TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END}
|
| 312 |
+
{CREATE TRIGGER tr AFTER INSERT ON "t3" WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END}
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
#-------------------------------------------------------------------------
|
| 316 |
+
reset_db
|
| 317 |
+
ifcapable fts5 {
|
| 318 |
+
do_execsql_test 10.0 {
|
| 319 |
+
CREATE VIRTUAL TABLE fff USING fts5(x, y, z);
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
do_execsql_test 10.1 {
|
| 323 |
+
BEGIN;
|
| 324 |
+
INSERT INTO fff VALUES('a', 'b', 'c');
|
| 325 |
+
ALTER TABLE fff RENAME TO ggg;
|
| 326 |
+
COMMIT;
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
do_execsql_test 10.2 {
|
| 330 |
+
SELECT * FROM ggg;
|
| 331 |
+
} {a b c}
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
#-------------------------------------------------------------------------
|
| 335 |
+
reset_db
|
| 336 |
+
forcedelete test.db2
|
| 337 |
+
db func trigger trigger
|
| 338 |
+
set ::trigger [list]
|
| 339 |
+
proc trigger {args} {
|
| 340 |
+
lappend ::trigger $args
|
| 341 |
+
}
|
| 342 |
+
do_execsql_test 11.0 {
|
| 343 |
+
ATTACH 'test.db2' AS aux;
|
| 344 |
+
CREATE TABLE aux.t1(a, b, c);
|
| 345 |
+
CREATE TABLE main.t1(a, b, c);
|
| 346 |
+
CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN
|
| 347 |
+
SELECT trigger(new.a, new.b, new.c);
|
| 348 |
+
END;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
do_execsql_test 11.1 {
|
| 352 |
+
INSERT INTO main.t1 VALUES(1, 2, 3);
|
| 353 |
+
INSERT INTO aux.t1 VALUES(4, 5, 6);
|
| 354 |
+
}
|
| 355 |
+
do_test 11.2 { set ::trigger } {{4 5 6}}
|
| 356 |
+
|
| 357 |
+
do_execsql_test 11.3 {
|
| 358 |
+
SELECT name, tbl_name FROM sqlite_temp_master;
|
| 359 |
+
} {tr t1}
|
| 360 |
+
|
| 361 |
+
do_execsql_test 11.4 {
|
| 362 |
+
ALTER TABLE main.t1 RENAME TO t2;
|
| 363 |
+
SELECT name, tbl_name FROM sqlite_temp_master;
|
| 364 |
+
} {tr t1}
|
| 365 |
+
|
| 366 |
+
do_execsql_test 11.5 {
|
| 367 |
+
ALTER TABLE aux.t1 RENAME TO t2;
|
| 368 |
+
SELECT name, tbl_name FROM sqlite_temp_master;
|
| 369 |
+
} {tr t2}
|
| 370 |
+
|
| 371 |
+
do_execsql_test 11.6 {
|
| 372 |
+
INSERT INTO aux.t2 VALUES(7, 8, 9);
|
| 373 |
+
}
|
| 374 |
+
do_test 11.7 { set ::trigger } {{4 5 6} {7 8 9}}
|
| 375 |
+
|
| 376 |
+
#-------------------------------------------------------------------------
|
| 377 |
+
reset_db
|
| 378 |
+
do_execsql_test 12.0 {
|
| 379 |
+
CREATE TABLE t1(a);
|
| 380 |
+
CREATE TABLE t2(w);
|
| 381 |
+
CREATE TRIGGER temp.r1 AFTER INSERT ON main.t2 BEGIN
|
| 382 |
+
INSERT INTO t1(a) VALUES(new.w);
|
| 383 |
+
END;
|
| 384 |
+
CREATE TEMP TABLE t2(x);
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
do_execsql_test 12.1 {
|
| 388 |
+
ALTER TABLE main.t2 RENAME TO t3;
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
do_execsql_test 12.2 {
|
| 392 |
+
INSERT INTO t3 VALUES('WWW');
|
| 393 |
+
SELECT * FROM t1;
|
| 394 |
+
} {WWW}
|
| 395 |
+
|
| 396 |
+
|
| 397 |
+
#-------------------------------------------------------------------------
|
| 398 |
+
reset_db
|
| 399 |
+
do_execsql_test 13.0 {
|
| 400 |
+
CREATE TABLE t1(x, y);
|
| 401 |
+
CREATE TABLE t2(a, b);
|
| 402 |
+
CREATE TABLE log(c);
|
| 403 |
+
CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
|
| 404 |
+
INSERT INTO log SELECT y FROM t1, t2;
|
| 405 |
+
END;
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
do_execsql_test 13.1 {
|
| 409 |
+
INSERT INTO t1 VALUES(1, 2);
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
do_catchsql_test 13.2 {
|
| 413 |
+
ALTER TABLE t2 RENAME b TO y;
|
| 414 |
+
} {1 {error in trigger tr1 after rename: ambiguous column name: y}}
|
| 415 |
+
|
| 416 |
+
#-------------------------------------------------------------------------
|
| 417 |
+
reset_db
|
| 418 |
+
|
| 419 |
+
ifcapable rtree {
|
| 420 |
+
do_execsql_test 14.0 {
|
| 421 |
+
CREATE VIRTUAL TABLE rt USING rtree(id, minx, maxx, miny, maxy);
|
| 422 |
+
|
| 423 |
+
CREATE TABLE "mytable" ( "fid" INTEGER PRIMARY KEY, "geom" BLOB);
|
| 424 |
+
|
| 425 |
+
CREATE TRIGGER tr1 AFTER UPDATE OF "geom" ON "mytable"
|
| 426 |
+
WHEN OLD."fid" = NEW."fid" AND NEW."geom" IS NULL BEGIN
|
| 427 |
+
DELETE FROM rt WHERE id = OLD."fid";
|
| 428 |
+
END;
|
| 429 |
+
|
| 430 |
+
INSERT INTO mytable VALUES(1, X'abcd');
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
do_execsql_test 14.1 {
|
| 434 |
+
UPDATE mytable SET geom = X'1234'
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
do_execsql_test 14.2 {
|
| 438 |
+
ALTER TABLE mytable RENAME TO mytable_renamed;
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
do_execsql_test 14.3 {
|
| 442 |
+
CREATE TRIGGER tr2 AFTER INSERT ON mytable_renamed BEGIN
|
| 443 |
+
DELETE FROM rt WHERE id=(SELECT min(id) FROM rt);
|
| 444 |
+
END;
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
do_execsql_test 14.4 {
|
| 448 |
+
ALTER TABLE mytable_renamed RENAME TO mytable2;
|
| 449 |
+
}
|
| 450 |
+
}
|
| 451 |
+
|
| 452 |
+
reset_db
|
| 453 |
+
do_execsql_test 14.5 {
|
| 454 |
+
CREATE TABLE t1(a, b, c);
|
| 455 |
+
CREATE VIEW v1 AS SELECT * FROM t1;
|
| 456 |
+
CREATE TRIGGER xyz AFTER INSERT ON t1 BEGIN
|
| 457 |
+
SELECT a, b FROM v1;
|
| 458 |
+
END;
|
| 459 |
+
}
|
| 460 |
+
do_execsql_test 14.6 {
|
| 461 |
+
ALTER TABLE t1 RENAME TO tt1;
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
#-------------------------------------------------------------------------
|
| 465 |
+
reset_db
|
| 466 |
+
do_execsql_test 15.0 {
|
| 467 |
+
CREATE TABLE t1(a integer NOT NULL PRIMARY KEY);
|
| 468 |
+
CREATE VIEW v1 AS SELECT a FROM t1;
|
| 469 |
+
CREATE TRIGGER tr1 INSTEAD OF INSERT ON v1 BEGIN
|
| 470 |
+
UPDATE t1 SET a = NEW.a;
|
| 471 |
+
END;
|
| 472 |
+
CREATE TRIGGER tr2 INSTEAD OF INSERT ON v1 BEGIN
|
| 473 |
+
SELECT new.a;
|
| 474 |
+
END;
|
| 475 |
+
CREATE TABLE t2 (b);
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
do_execsql_test 15.1 {
|
| 479 |
+
INSERT INTO v1 VALUES(1);
|
| 480 |
+
ALTER TABLE t2 RENAME TO t3;
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
do_execsql_test 15.2 {
|
| 484 |
+
CREATE TABLE x(f1 integer NOT NULL);
|
| 485 |
+
CREATE VIEW y AS SELECT f1 AS f1 FROM x;
|
| 486 |
+
CREATE TRIGGER t INSTEAD OF UPDATE OF f1 ON y BEGIN
|
| 487 |
+
UPDATE x SET f1 = NEW.f1;
|
| 488 |
+
END;
|
| 489 |
+
CREATE TABLE z (f1 integer NOT NULL PRIMARY KEY);
|
| 490 |
+
ALTER TABLE z RENAME TO z2;
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
do_execsql_test 15.3 {
|
| 494 |
+
INSERT INTO x VALUES(1), (2), (3);
|
| 495 |
+
ALTER TABLE x RENAME f1 TO f2;
|
| 496 |
+
SELECT * FROM x;
|
| 497 |
+
} {1 2 3}
|
| 498 |
+
|
| 499 |
+
do_execsql_test 15.4 {
|
| 500 |
+
UPDATE y SET f1 = 'x' WHERE f1 = 1;
|
| 501 |
+
SELECT * FROM x;
|
| 502 |
+
} {x x x}
|
| 503 |
+
|
| 504 |
+
do_execsql_test 15.5 {
|
| 505 |
+
SELECT sql FROM sqlite_master WHERE name = 'y';
|
| 506 |
+
} {{CREATE VIEW y AS SELECT f2 AS f1 FROM x}}
|
| 507 |
+
|
| 508 |
+
#-------------------------------------------------------------------------
|
| 509 |
+
# Test that it is not possible to rename a shadow table in DEFENSIVE mode.
|
| 510 |
+
#
|
| 511 |
+
ifcapable fts3 {
|
| 512 |
+
proc vtab_command {method args} {
|
| 513 |
+
switch -- $method {
|
| 514 |
+
xConnect {
|
| 515 |
+
if {[info exists ::vtab_connect_sql]} {
|
| 516 |
+
execsql $::vtab_connect_sql
|
| 517 |
+
}
|
| 518 |
+
return "CREATE TABLE t1(a, b, c)"
|
| 519 |
+
}
|
| 520 |
+
|
| 521 |
+
xBestIndex {
|
| 522 |
+
set clist [lindex $args 0]
|
| 523 |
+
if {[llength $clist]!=1} { error "unexpected constraint list" }
|
| 524 |
+
catch { array unset C }
|
| 525 |
+
array set C [lindex $clist 0]
|
| 526 |
+
if {$C(usable)} {
|
| 527 |
+
return "omit 0 cost 0 rows 1 idxnum 555 idxstr eq!"
|
| 528 |
+
} else {
|
| 529 |
+
return "cost 1000000 rows 0 idxnum 0 idxstr scan..."
|
| 530 |
+
}
|
| 531 |
+
}
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
return {}
|
| 535 |
+
}
|
| 536 |
+
|
| 537 |
+
register_tcl_module db
|
| 538 |
+
|
| 539 |
+
sqlite3_db_config db DEFENSIVE 1
|
| 540 |
+
|
| 541 |
+
do_execsql_test 16.0 {
|
| 542 |
+
CREATE VIRTUAL TABLE y1 USING fts3;
|
| 543 |
+
VACUUM;
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
do_catchsql_test 16.10 {
|
| 547 |
+
INSERT INTO y1_segments VALUES(1, X'1234567890');
|
| 548 |
+
} {1 {table y1_segments may not be modified}}
|
| 549 |
+
|
| 550 |
+
do_catchsql_test 16.20 {
|
| 551 |
+
DROP TABLE y1_segments;
|
| 552 |
+
} {1 {table y1_segments may not be dropped}}
|
| 553 |
+
|
| 554 |
+
do_catchsql_test 16.20 {
|
| 555 |
+
ALTER TABLE y1_segments RENAME TO abc;
|
| 556 |
+
} {1 {table y1_segments may not be altered}}
|
| 557 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 558 |
+
do_catchsql_test 16.22 {
|
| 559 |
+
ALTER TABLE y1_segments RENAME TO abc;
|
| 560 |
+
} {0 {}}
|
| 561 |
+
sqlite3_db_config db DEFENSIVE 1
|
| 562 |
+
do_catchsql_test 16.23 {
|
| 563 |
+
CREATE TABLE y1_segments AS SELECT * FROM abc;
|
| 564 |
+
} {1 {object name reserved for internal use: y1_segments}}
|
| 565 |
+
do_catchsql_test 16.24 {
|
| 566 |
+
CREATE VIEW y1_segments AS SELECT * FROM abc;
|
| 567 |
+
} {1 {object name reserved for internal use: y1_segments}}
|
| 568 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 569 |
+
do_catchsql_test 16.25 {
|
| 570 |
+
ALTER TABLE abc RENAME TO y1_segments;
|
| 571 |
+
} {0 {}}
|
| 572 |
+
sqlite3_db_config db DEFENSIVE 1
|
| 573 |
+
|
| 574 |
+
do_execsql_test 16.30 {
|
| 575 |
+
ALTER TABLE y1 RENAME TO z1;
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
do_execsql_test 16.40 {
|
| 579 |
+
SELECT * FROM z1_segments;
|
| 580 |
+
}
|
| 581 |
+
}
|
| 582 |
+
|
| 583 |
+
#-------------------------------------------------------------------------
|
| 584 |
+
reset_db
|
| 585 |
+
do_execsql_test 17.0 {
|
| 586 |
+
CREATE TABLE sqlite1234 (id integer);
|
| 587 |
+
ALTER TABLE sqlite1234 RENAME TO User;
|
| 588 |
+
SELECT name, sql FROM sqlite_master WHERE sql IS NOT NULL;
|
| 589 |
+
} {
|
| 590 |
+
User {CREATE TABLE "User" (id integer)}
|
| 591 |
+
}
|
| 592 |
+
|
| 593 |
+
#-------------------------------------------------------------------------
|
| 594 |
+
reset_db
|
| 595 |
+
do_execsql_test 18.1.0 {
|
| 596 |
+
CREATE TABLE t0 (c0 INTEGER, PRIMARY KEY(c0)) WITHOUT ROWID;
|
| 597 |
+
}
|
| 598 |
+
do_execsql_test 18.1.1 {
|
| 599 |
+
ALTER TABLE t0 RENAME COLUMN c0 TO c1;
|
| 600 |
+
}
|
| 601 |
+
do_execsql_test 18.1.2 {
|
| 602 |
+
SELECT sql FROM sqlite_master;
|
| 603 |
+
} {{CREATE TABLE t0 (c1 INTEGER, PRIMARY KEY(c1)) WITHOUT ROWID}}
|
| 604 |
+
|
| 605 |
+
reset_db
|
| 606 |
+
do_execsql_test 18.2.0 {
|
| 607 |
+
CREATE TABLE t0 (c0 INTEGER, PRIMARY KEY(c0));
|
| 608 |
+
}
|
| 609 |
+
do_execsql_test 18.2.1 {
|
| 610 |
+
ALTER TABLE t0 RENAME COLUMN c0 TO c1;
|
| 611 |
+
}
|
| 612 |
+
do_execsql_test 18.2.2 {
|
| 613 |
+
SELECT sql FROM sqlite_master;
|
| 614 |
+
} {{CREATE TABLE t0 (c1 INTEGER, PRIMARY KEY(c1))}}
|
| 615 |
+
|
| 616 |
+
# 2020-02-23 ticket f50af3e8a565776b
|
| 617 |
+
reset_db
|
| 618 |
+
do_execsql_test 19.100 {
|
| 619 |
+
CREATE TABLE t1(x);
|
| 620 |
+
CREATE VIEW t2 AS SELECT 1 FROM t1, (t1 AS a0, t1);
|
| 621 |
+
ALTER TABLE t1 RENAME TO t3;
|
| 622 |
+
SELECT sql FROM sqlite_master;
|
| 623 |
+
} {{CREATE TABLE "t3"(x)} {CREATE VIEW t2 AS SELECT 1 FROM "t3", ("t3" AS a0, "t3")}}
|
| 624 |
+
do_execsql_test 19.110 {
|
| 625 |
+
INSERT INTO t3(x) VALUES(123);
|
| 626 |
+
SELECT * FROM t2;
|
| 627 |
+
} {1}
|
| 628 |
+
do_execsql_test 19.120 {
|
| 629 |
+
INSERT INTO t3(x) VALUES('xyz');
|
| 630 |
+
SELECT * FROM t2;
|
| 631 |
+
} {1 1 1 1 1 1 1 1}
|
| 632 |
+
|
| 633 |
+
# Ticket 4722bdab08cb14
|
| 634 |
+
reset_db
|
| 635 |
+
do_execsql_test 20.0 {
|
| 636 |
+
CREATE TABLE a(a);
|
| 637 |
+
CREATE VIEW b AS SELECT(SELECT *FROM c JOIN a USING(d, a, a, a) JOIN a) IN();
|
| 638 |
+
}
|
| 639 |
+
do_execsql_test 20.1 {
|
| 640 |
+
ALTER TABLE a RENAME a TO e;
|
| 641 |
+
} {}
|
| 642 |
+
|
| 643 |
+
reset_db
|
| 644 |
+
do_execsql_test 21.0 {
|
| 645 |
+
CREATE TABLE a(b);
|
| 646 |
+
CREATE VIEW c AS
|
| 647 |
+
SELECT NULL INTERSECT
|
| 648 |
+
SELECT NULL ORDER BY
|
| 649 |
+
likelihood(NULL, (d, (SELECT c)));
|
| 650 |
+
} {}
|
| 651 |
+
do_catchsql_test 21.1 {
|
| 652 |
+
SELECT likelihood(NULL, (d, (SELECT c)));
|
| 653 |
+
} {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
|
| 654 |
+
do_catchsql_test 21.2 {
|
| 655 |
+
SELECT * FROM c;
|
| 656 |
+
} {1 {1st ORDER BY term does not match any column in the result set}}
|
| 657 |
+
|
| 658 |
+
do_catchsql_test 21.3 {
|
| 659 |
+
ALTER TABLE a RENAME TO e;
|
| 660 |
+
} {1 {error in view c: 1st ORDER BY term does not match any column in the result set}}
|
| 661 |
+
|
| 662 |
+
# After forum thread https://sqlite.org/forum/forumpost/ddbe1c7efa
|
| 663 |
+
# Ensure that PRAGMA schema_version=N causes a full schema reload.
|
| 664 |
+
#
|
| 665 |
+
reset_db
|
| 666 |
+
do_execsql_test 22.0 {
|
| 667 |
+
CREATE TABLE t1(a INT, b TEXT NOT NULL);
|
| 668 |
+
INSERT INTO t1 VALUES(1,2),('a','b');
|
| 669 |
+
BEGIN;
|
| 670 |
+
PRAGMA writable_schema=ON;
|
| 671 |
+
UPDATE sqlite_schema SET sql='CREATE TABLE t1(a INT, b TEXT)' WHERE name LIKE 't1';
|
| 672 |
+
PRAGMA schema_version=1234;
|
| 673 |
+
COMMIT;
|
| 674 |
+
PRAGMA integrity_check;
|
| 675 |
+
} {ok}
|
| 676 |
+
do_execsql_test 22.1 {
|
| 677 |
+
ALTER TABLE t1 ADD COLUMN c INT DEFAULT 78;
|
| 678 |
+
SELECT * FROM t1;
|
| 679 |
+
} {1 2 78 a b 78}
|
| 680 |
+
|
| 681 |
+
#-------------------------------------------------------------------------
|
| 682 |
+
reset_db
|
| 683 |
+
db collate compare64 compare64
|
| 684 |
+
|
| 685 |
+
do_execsql_test 23.1 {
|
| 686 |
+
CREATE TABLE gigo(a text);
|
| 687 |
+
CREATE TABLE idx(x text COLLATE compare64);
|
| 688 |
+
CREATE VIEW v1 AS SELECT * FROM idx WHERE x='abc';
|
| 689 |
+
}
|
| 690 |
+
db close
|
| 691 |
+
sqlite3 db test.db
|
| 692 |
+
|
| 693 |
+
do_execsql_test 23.2 {
|
| 694 |
+
alter table gigo rename to ggiiggoo;
|
| 695 |
+
alter table idx rename to idx2;
|
| 696 |
+
}
|
| 697 |
+
|
| 698 |
+
do_execsql_test 23.3 {
|
| 699 |
+
SELECT sql FROM sqlite_master;
|
| 700 |
+
} {
|
| 701 |
+
{CREATE TABLE "ggiiggoo"(a text)}
|
| 702 |
+
{CREATE TABLE "idx2"(x text COLLATE compare64)}
|
| 703 |
+
{CREATE VIEW v1 AS SELECT * FROM "idx2" WHERE x='abc'}
|
| 704 |
+
}
|
| 705 |
+
|
| 706 |
+
do_execsql_test 23.4 {
|
| 707 |
+
ALTER TABLE idx2 RENAME x TO y;
|
| 708 |
+
SELECT sql FROM sqlite_master;
|
| 709 |
+
} {
|
| 710 |
+
{CREATE TABLE "ggiiggoo"(a text)}
|
| 711 |
+
{CREATE TABLE "idx2"(y text COLLATE compare64)}
|
| 712 |
+
{CREATE VIEW v1 AS SELECT * FROM "idx2" WHERE y='abc'}
|
| 713 |
+
}
|
| 714 |
+
|
| 715 |
+
#-------------------------------------------------------------------------
|
| 716 |
+
#
|
| 717 |
+
reset_db
|
| 718 |
+
do_execsql_test 24.1.0 {
|
| 719 |
+
CREATE TABLE t1(a, b);
|
| 720 |
+
CREATE TRIGGER AFTER INSERT ON t1 BEGIN
|
| 721 |
+
INSERT INTO nosuchtable VALUES(new.a) ON CONFLICT(a) DO NOTHING;
|
| 722 |
+
END;
|
| 723 |
+
}
|
| 724 |
+
do_catchsql_test 24.1.1 {
|
| 725 |
+
ALTER TABLE t1 RENAME TO t2;
|
| 726 |
+
} {1 {error in trigger AFTER: no such table: main.nosuchtable}}
|
| 727 |
+
|
| 728 |
+
reset_db
|
| 729 |
+
do_execsql_test 24.2.0 {
|
| 730 |
+
CREATE TABLE t1(a, b);
|
| 731 |
+
CREATE TRIGGER AFTER INSERT ON t1 BEGIN
|
| 732 |
+
INSERT INTO v1 VALUES(new.a) ON CONFLICT(a) DO NOTHING;
|
| 733 |
+
END;
|
| 734 |
+
CREATE VIEW v1 AS SELECT * FROM nosuchtable;
|
| 735 |
+
}
|
| 736 |
+
do_catchsql_test 24.2.1 {
|
| 737 |
+
ALTER TABLE t1 RENAME TO t2;
|
| 738 |
+
} {1 {error in trigger AFTER: no such table: main.nosuchtable}}
|
| 739 |
+
|
| 740 |
+
#--------------------------------------------------------------------------
|
| 741 |
+
#
|
| 742 |
+
reset_db
|
| 743 |
+
do_execsql_test 25.1 {
|
| 744 |
+
CREATE TABLE xx(x);
|
| 745 |
+
CREATE VIEW v3(b) AS WITH b AS (SELECT b FROM (SELECT * FROM t2)) VALUES(1);
|
| 746 |
+
}
|
| 747 |
+
|
| 748 |
+
ifcapable json1&&vtab {
|
| 749 |
+
do_catchsql_test 25.2 {
|
| 750 |
+
ALTER TABLE json_each RENAME TO t4;
|
| 751 |
+
} {1 {table json_each may not be altered}}
|
| 752 |
+
}
|
| 753 |
+
|
| 754 |
+
# 2021-05-01 dbsqlfuzz bc17a306a09329bba0ecc61547077f6178bcf321
|
| 755 |
+
# Remove a NEVER() inserted on 2019-12-09 that is reachable after all.
|
| 756 |
+
#
|
| 757 |
+
reset_db
|
| 758 |
+
do_execsql_test 26.1 {
|
| 759 |
+
CREATE TABLE t1(k,v);
|
| 760 |
+
CREATE TABLE t2_a(k,v);
|
| 761 |
+
CREATE VIEW t2 AS SELECT * FROM t2_a;
|
| 762 |
+
CREATE TRIGGER r2 AFTER INSERT ON t1 BEGIN
|
| 763 |
+
UPDATE t1
|
| 764 |
+
SET (k,v)=((WITH cte1(a) AS (SELECT 1 FROM t2) SELECT t2.k FROM t2, cte1),1);
|
| 765 |
+
END;
|
| 766 |
+
ALTER TABLE t1 RENAME TO t1x;
|
| 767 |
+
INSERT INTO t2_a VALUES(2,3);
|
| 768 |
+
INSERT INTO t1x VALUES(98,99);
|
| 769 |
+
SELECT * FROM t1x;
|
| 770 |
+
} {2 1}
|
| 771 |
+
|
| 772 |
+
#-------------------------------------------------------------------------
|
| 773 |
+
reset_db
|
| 774 |
+
|
| 775 |
+
do_execsql_test 27.1 {
|
| 776 |
+
|
| 777 |
+
create table t_sa (
|
| 778 |
+
c_muyat INTEGER NOT NULL,
|
| 779 |
+
c_d4u TEXT
|
| 780 |
+
);
|
| 781 |
+
|
| 782 |
+
create table t2 ( abc );
|
| 783 |
+
|
| 784 |
+
CREATE TRIGGER trig AFTER DELETE ON t_sa
|
| 785 |
+
BEGIN
|
| 786 |
+
DELETE FROM t_sa WHERE (
|
| 787 |
+
SELECT 123 FROM t2
|
| 788 |
+
WINDOW oamat7fzf AS ( PARTITION BY t_sa.c_d4u )
|
| 789 |
+
);
|
| 790 |
+
END;
|
| 791 |
+
}
|
| 792 |
+
|
| 793 |
+
do_execsql_test 27.2 {
|
| 794 |
+
alter table t_sa rename column c_muyat to c_dg;
|
| 795 |
+
}
|
| 796 |
+
|
| 797 |
+
#-------------------------------------------------------------------------
|
| 798 |
+
reset_db
|
| 799 |
+
do_execsql_test 29.1 {
|
| 800 |
+
CREATE TABLE t1(a, b, c);
|
| 801 |
+
INSERT INTO t1 VALUES('a', 'b', 'c');
|
| 802 |
+
|
| 803 |
+
CREATE VIEW v0 AS
|
| 804 |
+
WITH p AS ( SELECT 1 FROM t1 ),
|
| 805 |
+
g AS ( SELECT 1 FROM p, t1 )
|
| 806 |
+
SELECT 1 FROM g;
|
| 807 |
+
}
|
| 808 |
+
|
| 809 |
+
do_execsql_test 29.2 {
|
| 810 |
+
SELECT * FROM v0
|
| 811 |
+
} 1
|
| 812 |
+
|
| 813 |
+
do_execsql_test 29.2 {
|
| 814 |
+
ALTER TABLE t1 RENAME TO t2
|
| 815 |
+
}
|
| 816 |
+
|
| 817 |
+
do_execsql_test 29.3 {
|
| 818 |
+
SELECT sql FROM sqlite_schema WHERE name='v0'
|
| 819 |
+
} {{CREATE VIEW v0 AS
|
| 820 |
+
WITH p AS ( SELECT 1 FROM "t2" ),
|
| 821 |
+
g AS ( SELECT 1 FROM p, "t2" )
|
| 822 |
+
SELECT 1 FROM g}}
|
| 823 |
+
|
| 824 |
+
do_execsql_test 29.4 {
|
| 825 |
+
CREATE VIEW v2 AS
|
| 826 |
+
WITH p AS ( SELECT 1 FROM t2 ),
|
| 827 |
+
g AS ( SELECT 1 FROM (
|
| 828 |
+
WITH i AS (SELECT 1 FROM p, t2)
|
| 829 |
+
SELECT * FROM i
|
| 830 |
+
)
|
| 831 |
+
)
|
| 832 |
+
SELECT 1 FROM g;
|
| 833 |
+
}
|
| 834 |
+
|
| 835 |
+
do_execsql_test 29.4 {
|
| 836 |
+
SELECT * FROM v2;
|
| 837 |
+
} 1
|
| 838 |
+
|
| 839 |
+
do_execsql_test 29.5 {
|
| 840 |
+
ALTER TABLE t2 RENAME TO t3;
|
| 841 |
+
}
|
| 842 |
+
|
| 843 |
+
do_execsql_test 29.5 {
|
| 844 |
+
SELECT sql FROM sqlite_schema WHERE name='v2'
|
| 845 |
+
} {{CREATE VIEW v2 AS
|
| 846 |
+
WITH p AS ( SELECT 1 FROM "t3" ),
|
| 847 |
+
g AS ( SELECT 1 FROM (
|
| 848 |
+
WITH i AS (SELECT 1 FROM p, "t3")
|
| 849 |
+
SELECT * FROM i
|
| 850 |
+
)
|
| 851 |
+
)
|
| 852 |
+
SELECT 1 FROM g}}
|
| 853 |
+
|
| 854 |
+
|
| 855 |
+
#-------------------------------------------------------------------------
|
| 856 |
+
reset_db
|
| 857 |
+
do_execsql_test 28.1 {
|
| 858 |
+
CREATE TABLE t1(a);
|
| 859 |
+
CREATE TABLE t2(b,c);
|
| 860 |
+
CREATE TABLE t4(b,c);
|
| 861 |
+
INSERT INTO t2 VALUES(1,2),(1,3),(2,5);
|
| 862 |
+
INSERT INTO t4 VALUES(1,2),(1,3),(2,5);
|
| 863 |
+
|
| 864 |
+
CREATE VIEW v3 AS
|
| 865 |
+
WITH RECURSIVE t3(x,y,z) AS (
|
| 866 |
+
SELECT b,c,NULL FROM t4
|
| 867 |
+
UNION
|
| 868 |
+
SELECT x,y,NULL FROM t3, t2
|
| 869 |
+
)
|
| 870 |
+
SELECT * FROM t3 AS xyz;
|
| 871 |
+
}
|
| 872 |
+
|
| 873 |
+
do_execsql_test 28.2 {
|
| 874 |
+
SELECT * FROM v3
|
| 875 |
+
} {
|
| 876 |
+
1 2 {} 1 3 {} 2 5 {}
|
| 877 |
+
}
|
| 878 |
+
|
| 879 |
+
do_execsql_test 28.3 {
|
| 880 |
+
ALTER TABLE t1 RENAME a TO a2; -- fails in v3
|
| 881 |
+
}
|
| 882 |
+
|
| 883 |
+
do_execsql_test 28.4 {
|
| 884 |
+
ALTER TABLE t2 RENAME TO t5;
|
| 885 |
+
}
|
| 886 |
+
|
| 887 |
+
do_execsql_test 28.5 {
|
| 888 |
+
SELECT sql FROM sqlite_schema WHERE name='v3'
|
| 889 |
+
} {{CREATE VIEW v3 AS
|
| 890 |
+
WITH RECURSIVE t3(x,y,z) AS (
|
| 891 |
+
SELECT b,c,NULL FROM t4
|
| 892 |
+
UNION
|
| 893 |
+
SELECT x,y,NULL FROM t3, "t5"
|
| 894 |
+
)
|
| 895 |
+
SELECT * FROM t3 AS xyz}}
|
| 896 |
+
|
| 897 |
+
#-------------------------------------------------------------------------
|
| 898 |
+
reset_db
|
| 899 |
+
do_execsql_test 30.0 {
|
| 900 |
+
CREATE TABLE t1(a,b,c,d,e,f);
|
| 901 |
+
CREATE TABLE t2(a,b,c);
|
| 902 |
+
CREATE INDEX t1abc ON t1(a,b,c+d+e);
|
| 903 |
+
CREATE VIEW v1(x,y) AS
|
| 904 |
+
SELECT t1.b,t2.b FROM t1,t2 WHERE t1.a=t2.a
|
| 905 |
+
GROUP BY 1 HAVING t2.c NOT NULL LIMIT 10;
|
| 906 |
+
CREATE TRIGGER r1 AFTER INSERT ON t1 WHEN 'no' NOT NULL BEGIN
|
| 907 |
+
INSERT INTO t2(a,a,b,c) VALUES(new.b,new.a,new.c-7);
|
| 908 |
+
WITH c1(x) AS (
|
| 909 |
+
VALUES(0)
|
| 910 |
+
UNION ALL
|
| 911 |
+
SELECT current_time+x FROM c1 WHERE x
|
| 912 |
+
UNION ALL
|
| 913 |
+
SELECT 1+x FROM c1 WHERE x<1
|
| 914 |
+
), c2(x) AS (VALUES(0),(1))
|
| 915 |
+
SELECT * FROM c1 AS x1, c2 AS x2, (
|
| 916 |
+
SELECT x+1 FROM c1 WHERE x IS NOT TRUE
|
| 917 |
+
UNION ALL
|
| 918 |
+
SELECT 1+x FROM c1 WHERE 1<x
|
| 919 |
+
) AS x3, c2 x5;
|
| 920 |
+
END;
|
| 921 |
+
}
|
| 922 |
+
|
| 923 |
+
do_execsql_test 30.1 {
|
| 924 |
+
ALTER TABLE t1 RENAME TO t1x;
|
| 925 |
+
}
|
| 926 |
+
|
| 927 |
+
do_execsql_test 30.2 {
|
| 928 |
+
SELECT sql FROM sqlite_schema ORDER BY rowid
|
| 929 |
+
} {
|
| 930 |
+
{CREATE TABLE "t1x"(a,b,c,d,e,f)}
|
| 931 |
+
{CREATE TABLE t2(a,b,c)}
|
| 932 |
+
{CREATE INDEX t1abc ON "t1x"(a,b,c+d+e)}
|
| 933 |
+
{CREATE VIEW v1(x,y) AS
|
| 934 |
+
SELECT "t1x".b,t2.b FROM "t1x",t2 WHERE "t1x".a=t2.a
|
| 935 |
+
GROUP BY 1 HAVING t2.c NOT NULL LIMIT 10}
|
| 936 |
+
{CREATE TRIGGER r1 AFTER INSERT ON "t1x" WHEN 'no' NOT NULL BEGIN
|
| 937 |
+
INSERT INTO t2(a,a,b,c) VALUES(new.b,new.a,new.c-7);
|
| 938 |
+
WITH c1(x) AS (
|
| 939 |
+
VALUES(0)
|
| 940 |
+
UNION ALL
|
| 941 |
+
SELECT current_time+x FROM c1 WHERE x
|
| 942 |
+
UNION ALL
|
| 943 |
+
SELECT 1+x FROM c1 WHERE x<1
|
| 944 |
+
), c2(x) AS (VALUES(0),(1))
|
| 945 |
+
SELECT * FROM c1 AS x1, c2 AS x2, (
|
| 946 |
+
SELECT x+1 FROM c1 WHERE x IS NOT TRUE
|
| 947 |
+
UNION ALL
|
| 948 |
+
SELECT 1+x FROM c1 WHERE 1<x
|
| 949 |
+
) AS x3, c2 x5;
|
| 950 |
+
END}
|
| 951 |
+
}
|
| 952 |
+
|
| 953 |
+
#-------------------------------------------------------------------------
|
| 954 |
+
reset_db
|
| 955 |
+
do_execsql_test 31.0 {
|
| 956 |
+
CREATE TABLE t1(q);
|
| 957 |
+
CREATE VIEW vvv AS WITH x AS (WITH y AS (SELECT * FROM x) SELECT 1) SELECT 1;
|
| 958 |
+
}
|
| 959 |
+
|
| 960 |
+
do_execsql_test 31.1 {
|
| 961 |
+
SELECT * FROM vvv;
|
| 962 |
+
} {1}
|
| 963 |
+
|
| 964 |
+
do_execsql_test 31.2 {
|
| 965 |
+
ALTER TABLE t1 RENAME TO t1x;
|
| 966 |
+
}
|
| 967 |
+
|
| 968 |
+
do_execsql_test 31.3 {
|
| 969 |
+
ALTER TABLE t1x RENAME q TO x;
|
| 970 |
+
}
|
| 971 |
+
|
| 972 |
+
# 2021-07-02 OSSFuzz https://oss-fuzz.com/testcase-detail/5517690440646656
|
| 973 |
+
# Bad assert() statement
|
| 974 |
+
#
|
| 975 |
+
reset_db
|
| 976 |
+
do_catchsql_test 32.0 {
|
| 977 |
+
CREATE TABLE t1(x);
|
| 978 |
+
CREATE TRIGGER r1 BEFORE INSERT ON t1 BEGIN
|
| 979 |
+
UPDATE t1 SET x=x FROM (SELECT*);
|
| 980 |
+
END;
|
| 981 |
+
ALTER TABLE t1 RENAME TO x;
|
| 982 |
+
} {1 {error in trigger r1: no tables specified}}
|
| 983 |
+
|
| 984 |
+
# 2023-04-13 https://sqlite.org/forum/forumpost/ff3840145a
|
| 985 |
+
#
|
| 986 |
+
reset_db
|
| 987 |
+
do_execsql_test 33.0 {
|
| 988 |
+
CREATE TABLE t1(a TEXT);
|
| 989 |
+
INSERT INTO t1(a) VALUES('abc'),('def'),(NULL);
|
| 990 |
+
CREATE TABLE t2(b TEXT);
|
| 991 |
+
CREATE TRIGGER r3 AFTER INSERT ON t1 BEGIN
|
| 992 |
+
UPDATE t2 SET (b,a)=(SELECT 1) FROM t1 JOIN t2 ON (SELECT * FROM (SELECT a));
|
| 993 |
+
END;
|
| 994 |
+
}
|
| 995 |
+
do_catchsql_test 33.1 {
|
| 996 |
+
ALTER TABLE t1 RENAME COLUMN a TO b;
|
| 997 |
+
} {1 {error in trigger r3 after rename: no such column: a}}
|
| 998 |
+
do_execsql_test 33.2 {
|
| 999 |
+
SELECT quote(a) FROM t1 ORDER BY +a;
|
| 1000 |
+
} {NULL 'abc' 'def'}
|
| 1001 |
+
|
| 1002 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-s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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/afc-sqlite3/test/altertrig.test
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2022 May 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 |
+
|
| 13 |
+
set testdir [file dirname $argv0]
|
| 14 |
+
source $testdir/tester.tcl
|
| 15 |
+
set testprefix altertrig
|
| 16 |
+
|
| 17 |
+
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
|
| 18 |
+
ifcapable !altertable {
|
| 19 |
+
finish_test
|
| 20 |
+
return
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
proc collapse_whitespace {in} {
|
| 24 |
+
regsub -all {[ \t\n]+} [string trim $in] { }
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
proc do_whitespace_sql_test {tn sql res} {
|
| 28 |
+
set got [execsql $sql]
|
| 29 |
+
set wgot [list]
|
| 30 |
+
set wres [list]
|
| 31 |
+
foreach g $got { lappend wgot [collapse_whitespace $g] }
|
| 32 |
+
foreach r $res { lappend wres [collapse_whitespace $r] }
|
| 33 |
+
|
| 34 |
+
uplevel [list do_test $tn [list set {} $wgot] $wres]
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
do_execsql_test 1.0 {
|
| 38 |
+
CREATE TABLE t1(x);
|
| 39 |
+
CREATE TABLE t2(y);
|
| 40 |
+
CREATE TABLE t3(z);
|
| 41 |
+
CREATE TABLE t4(a);
|
| 42 |
+
|
| 43 |
+
CREATE TRIGGER r1 INSERT ON t1 BEGIN
|
| 44 |
+
UPDATE t1 SET d='xyz' FROM t2, t3;
|
| 45 |
+
END;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
do_whitespace_sql_test 1.1 {
|
| 49 |
+
ALTER TABLE t3 RENAME TO t5;
|
| 50 |
+
SELECT sql FROM sqlite_schema WHERE type='trigger';
|
| 51 |
+
} {{
|
| 52 |
+
CREATE TRIGGER r1 INSERT ON t1 BEGIN
|
| 53 |
+
UPDATE t1 SET d='xyz' FROM t2, "t5";
|
| 54 |
+
END
|
| 55 |
+
}}
|
| 56 |
+
|
| 57 |
+
do_execsql_test 1.2 {
|
| 58 |
+
DROP TRIGGER r1;
|
| 59 |
+
CREATE TRIGGER r1 INSERT ON t1 BEGIN
|
| 60 |
+
UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM t5);
|
| 61 |
+
END;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
do_whitespace_sql_test 1.3 {
|
| 65 |
+
ALTER TABLE t5 RENAME TO t3;
|
| 66 |
+
SELECT sql FROM sqlite_schema WHERE type='trigger';
|
| 67 |
+
} {{
|
| 68 |
+
CREATE TRIGGER r1 INSERT ON t1 BEGIN
|
| 69 |
+
UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM "t3");
|
| 70 |
+
END
|
| 71 |
+
}}
|
| 72 |
+
|
| 73 |
+
foreach {tn alter update final} {
|
| 74 |
+
1 {
|
| 75 |
+
ALTER TABLE t3 RENAME TO t10
|
| 76 |
+
} {
|
| 77 |
+
UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM t3)
|
| 78 |
+
} {
|
| 79 |
+
UPDATE t1 SET d='xyz' FROM t2, (SELECT * FROM "t10")
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
2 {
|
| 83 |
+
ALTER TABLE t3 RENAME TO t10
|
| 84 |
+
} {
|
| 85 |
+
UPDATE t1 SET a='xyz' FROM t3, (SELECT * FROM (SELECT e FROM t3))
|
| 86 |
+
} {
|
| 87 |
+
UPDATE t1 SET a='xyz' FROM "t10", (SELECT * FROM (SELECT e FROM "t10"))
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
3 {
|
| 91 |
+
ALTER TABLE t3 RENAME e TO abc
|
| 92 |
+
} {
|
| 93 |
+
UPDATE t1 SET a='xyz' FROM t3, (SELECT * FROM (SELECT e FROM t3))
|
| 94 |
+
} {
|
| 95 |
+
UPDATE t1 SET a='xyz' FROM t3, (SELECT * FROM (SELECT abc FROM t3))
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
4 {
|
| 99 |
+
ALTER TABLE t2 RENAME c TO abc
|
| 100 |
+
} {
|
| 101 |
+
UPDATE t1 SET a='xyz' FROM t3, (SELECT 1 FROM t2 WHERE c)
|
| 102 |
+
} {
|
| 103 |
+
UPDATE t1 SET a='xyz' FROM t3, (SELECT 1 FROM t2 WHERE abc)
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
5 {
|
| 107 |
+
ALTER TABLE t2 RENAME c TO abc
|
| 108 |
+
} {
|
| 109 |
+
UPDATE t1 SET a=t2.c FROM t2
|
| 110 |
+
} {
|
| 111 |
+
UPDATE t1 SET a=t2.abc FROM t2
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
6 {
|
| 115 |
+
ALTER TABLE t2 RENAME c TO abc
|
| 116 |
+
} {
|
| 117 |
+
UPDATE t1 SET a=t2.c FROM t2, t3
|
| 118 |
+
} {
|
| 119 |
+
UPDATE t1 SET a=t2.abc FROM t2, t3
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
7 {
|
| 123 |
+
ALTER TABLE t4 RENAME e TO abc
|
| 124 |
+
} {
|
| 125 |
+
UPDATE t1 SET a=1 FROM t3 NATURAL JOIN t4 WHERE t4.e=a
|
| 126 |
+
} {
|
| 127 |
+
UPDATE t1 SET a=1 FROM t3 NATURAL JOIN t4 WHERE t4.abc=a
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
8 {
|
| 131 |
+
ALTER TABLE t4 RENAME TO abc
|
| 132 |
+
} {
|
| 133 |
+
UPDATE t1 SET a=1 FROM t3 NATURAL JOIN t4 WHERE t4.e=a
|
| 134 |
+
} {
|
| 135 |
+
UPDATE t1 SET a=1 FROM t3 NATURAL JOIN "abc" WHERE "abc".e=a
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
} {
|
| 139 |
+
reset_db
|
| 140 |
+
do_execsql_test 2.$tn.1 {
|
| 141 |
+
CREATE TABLE t1(a,b);
|
| 142 |
+
CREATE TABLE t2(c,d);
|
| 143 |
+
CREATE TABLE t3(e,f);
|
| 144 |
+
CREATE TABLE t4(e,f);
|
| 145 |
+
}
|
| 146 |
+
do_execsql_test 2.$tn.2 "
|
| 147 |
+
CREATE TRIGGER r1 INSERT ON t1 BEGIN
|
| 148 |
+
$update;
|
| 149 |
+
END
|
| 150 |
+
"
|
| 151 |
+
do_execsql_test 2.$tn.3 $alter
|
| 152 |
+
|
| 153 |
+
do_whitespace_sql_test 2.$tn.4 {
|
| 154 |
+
SELECT sqL FROM sqlite_schema WHERE type='trigger'
|
| 155 |
+
} "{
|
| 156 |
+
CREATE TRIGGER r1 INSERT ON t1 BEGIN
|
| 157 |
+
$final;
|
| 158 |
+
END
|
| 159 |
+
}"
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
finish_test
|
local-test-sqlite3-delta-02/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-02/afc-sqlite3/test/analyze.test
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2005 July 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 |
+
# This file implements regression tests for SQLite library.
|
| 12 |
+
# This file implements tests for the ANALYZE command.
|
| 13 |
+
#
|
| 14 |
+
# $Id: analyze.test,v 1.9 2008/08/11 18:44:58 drh Exp $
|
| 15 |
+
|
| 16 |
+
set testdir [file dirname $argv0]
|
| 17 |
+
source $testdir/tester.tcl
|
| 18 |
+
|
| 19 |
+
# There is nothing to test if ANALYZE is disable for this build.
|
| 20 |
+
#
|
| 21 |
+
ifcapable {!analyze} {
|
| 22 |
+
finish_test
|
| 23 |
+
return
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Basic sanity checks.
|
| 27 |
+
#
|
| 28 |
+
do_test analyze-1.1 {
|
| 29 |
+
catchsql {
|
| 30 |
+
ANALYZE no_such_table
|
| 31 |
+
}
|
| 32 |
+
} {1 {no such table: no_such_table}}
|
| 33 |
+
do_test analyze-1.2 {
|
| 34 |
+
execsql {
|
| 35 |
+
SELECT count(*) FROM sqlite_master WHERE name='sqlite_stat1'
|
| 36 |
+
}
|
| 37 |
+
} {0}
|
| 38 |
+
do_test analyze-1.3 {
|
| 39 |
+
catchsql {
|
| 40 |
+
ANALYZE no_such_db.no_such_table
|
| 41 |
+
}
|
| 42 |
+
} {1 {unknown database no_such_db}}
|
| 43 |
+
do_test analyze-1.4 {
|
| 44 |
+
execsql {
|
| 45 |
+
SELECT count(*) FROM sqlite_master WHERE name='sqlite_stat1'
|
| 46 |
+
}
|
| 47 |
+
} {0}
|
| 48 |
+
do_test analyze-1.5.1 {
|
| 49 |
+
catchsql {
|
| 50 |
+
ANALYZE
|
| 51 |
+
}
|
| 52 |
+
} {0 {}}
|
| 53 |
+
do_test analyze-1.5.2 {
|
| 54 |
+
catchsql {
|
| 55 |
+
PRAGMA empty_result_callbacks=1;
|
| 56 |
+
ANALYZE
|
| 57 |
+
}
|
| 58 |
+
} {0 {}}
|
| 59 |
+
do_test analyze-1.6 {
|
| 60 |
+
execsql {
|
| 61 |
+
SELECT count(*) FROM sqlite_master WHERE name='sqlite_stat1'
|
| 62 |
+
}
|
| 63 |
+
} {1}
|
| 64 |
+
do_test analyze-1.6.2 {
|
| 65 |
+
catchsql {
|
| 66 |
+
CREATE INDEX stat1idx ON sqlite_stat1(idx);
|
| 67 |
+
}
|
| 68 |
+
} {1 {table sqlite_stat1 may not be indexed}}
|
| 69 |
+
do_test analyze-1.6.3 {
|
| 70 |
+
catchsql {
|
| 71 |
+
CREATE INDEX main.stat1idx ON SQLite_stat1(idx);
|
| 72 |
+
}
|
| 73 |
+
} {1 {table sqlite_stat1 may not be indexed}}
|
| 74 |
+
do_test analyze-1.7 {
|
| 75 |
+
execsql {
|
| 76 |
+
SELECT * FROM sqlite_stat1 WHERE idx NOT NULL
|
| 77 |
+
}
|
| 78 |
+
} {}
|
| 79 |
+
do_test analyze-1.8 {
|
| 80 |
+
catchsql {
|
| 81 |
+
ANALYZE main
|
| 82 |
+
}
|
| 83 |
+
} {0 {}}
|
| 84 |
+
do_test analyze-1.9 {
|
| 85 |
+
execsql {
|
| 86 |
+
SELECT * FROM sqlite_stat1 WHERE idx NOT NULL
|
| 87 |
+
}
|
| 88 |
+
} {}
|
| 89 |
+
do_test analyze-1.10 {
|
| 90 |
+
catchsql {
|
| 91 |
+
CREATE TABLE t1(a,b);
|
| 92 |
+
ANALYZE main.t1;
|
| 93 |
+
}
|
| 94 |
+
} {0 {}}
|
| 95 |
+
do_test analyze-1.11 {
|
| 96 |
+
execsql {
|
| 97 |
+
SELECT * FROM sqlite_stat1
|
| 98 |
+
}
|
| 99 |
+
} {}
|
| 100 |
+
do_test analyze-1.12 {
|
| 101 |
+
catchsql {
|
| 102 |
+
ANALYZE t1;
|
| 103 |
+
}
|
| 104 |
+
} {0 {}}
|
| 105 |
+
do_test analyze-1.13 {
|
| 106 |
+
execsql {
|
| 107 |
+
SELECT * FROM sqlite_stat1
|
| 108 |
+
}
|
| 109 |
+
} {}
|
| 110 |
+
|
| 111 |
+
# Create some indices that can be analyzed. But do not yet add
|
| 112 |
+
# data. Without data in the tables, no analysis is done.
|
| 113 |
+
#
|
| 114 |
+
do_test analyze-2.1 {
|
| 115 |
+
execsql {
|
| 116 |
+
CREATE INDEX t1i1 ON t1(a);
|
| 117 |
+
ANALYZE main.t1;
|
| 118 |
+
SELECT * FROM sqlite_stat1 ORDER BY idx;
|
| 119 |
+
}
|
| 120 |
+
} {}
|
| 121 |
+
do_test analyze-2.2 {
|
| 122 |
+
execsql {
|
| 123 |
+
CREATE INDEX t1i2 ON t1(b);
|
| 124 |
+
ANALYZE t1;
|
| 125 |
+
SELECT * FROM sqlite_stat1 ORDER BY idx;
|
| 126 |
+
}
|
| 127 |
+
} {}
|
| 128 |
+
do_test analyze-2.3 {
|
| 129 |
+
execsql {
|
| 130 |
+
CREATE INDEX t1i3 ON t1(a,b);
|
| 131 |
+
ANALYZE main;
|
| 132 |
+
SELECT * FROM sqlite_stat1 ORDER BY idx;
|
| 133 |
+
}
|
| 134 |
+
} {}
|
| 135 |
+
|
| 136 |
+
# Start adding data to the table. Verify that the analysis
|
| 137 |
+
# is done correctly.
|
| 138 |
+
#
|
| 139 |
+
do_test analyze-3.1 {
|
| 140 |
+
execsql {
|
| 141 |
+
INSERT INTO t1 VALUES(1,2);
|
| 142 |
+
INSERT INTO t1 VALUES(1,3);
|
| 143 |
+
ANALYZE main.t1;
|
| 144 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 145 |
+
}
|
| 146 |
+
} {t1i1 {2 2} t1i2 {2 1} t1i3 {2 2 1}}
|
| 147 |
+
do_test analyze-3.2 {
|
| 148 |
+
execsql {
|
| 149 |
+
INSERT INTO t1 VALUES(1,4);
|
| 150 |
+
INSERT INTO t1 VALUES(1,5);
|
| 151 |
+
ANALYZE t1;
|
| 152 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 153 |
+
}
|
| 154 |
+
} {t1i1 {4 4} t1i2 {4 1} t1i3 {4 4 1}}
|
| 155 |
+
do_test analyze-3.3 {
|
| 156 |
+
execsql {
|
| 157 |
+
INSERT INTO t1 VALUES(2,5);
|
| 158 |
+
ANALYZE main;
|
| 159 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 160 |
+
}
|
| 161 |
+
} {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1}}
|
| 162 |
+
do_test analyze-3.4 {
|
| 163 |
+
execsql {
|
| 164 |
+
CREATE TABLE t2 AS SELECT * FROM t1;
|
| 165 |
+
CREATE INDEX t2i1 ON t2(a);
|
| 166 |
+
CREATE INDEX t2i2 ON t2(b);
|
| 167 |
+
CREATE INDEX t2i3 ON t2(a,b);
|
| 168 |
+
ANALYZE;
|
| 169 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 170 |
+
}
|
| 171 |
+
} {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3} t2i2 {5 2} t2i3 {5 3 1}}
|
| 172 |
+
do_test analyze-3.5 {
|
| 173 |
+
execsql {
|
| 174 |
+
DROP INDEX t2i3;
|
| 175 |
+
ANALYZE t1;
|
| 176 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 177 |
+
}
|
| 178 |
+
} {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3} t2i2 {5 2}}
|
| 179 |
+
do_test analyze-3.6 {
|
| 180 |
+
execsql {
|
| 181 |
+
ANALYZE t2;
|
| 182 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 183 |
+
}
|
| 184 |
+
} {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3} t2i2 {5 2}}
|
| 185 |
+
do_test analyze-3.7 {
|
| 186 |
+
execsql {
|
| 187 |
+
DROP INDEX t2i2;
|
| 188 |
+
ANALYZE t2;
|
| 189 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 190 |
+
}
|
| 191 |
+
} {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3}}
|
| 192 |
+
do_test analyze-3.8 {
|
| 193 |
+
execsql {
|
| 194 |
+
CREATE TABLE t3 AS SELECT a, b, rowid AS c, 'hi' AS d FROM t1;
|
| 195 |
+
CREATE INDEX t3i1 ON t3(a);
|
| 196 |
+
CREATE INDEX t3i2 ON t3(a,b,c,d);
|
| 197 |
+
CREATE INDEX t3i3 ON t3(d,b,c,a);
|
| 198 |
+
DROP TABLE t1;
|
| 199 |
+
DROP TABLE t2;
|
| 200 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 201 |
+
}
|
| 202 |
+
} {}
|
| 203 |
+
do_test analyze-3.9 {
|
| 204 |
+
execsql {
|
| 205 |
+
ANALYZE;
|
| 206 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 207 |
+
}
|
| 208 |
+
} {t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
|
| 209 |
+
|
| 210 |
+
do_test analyze-3.10 {
|
| 211 |
+
execsql {
|
| 212 |
+
CREATE TABLE [silly " name](a, b, c);
|
| 213 |
+
CREATE INDEX 'foolish '' name' ON [silly " name](a, b);
|
| 214 |
+
CREATE INDEX 'another foolish '' name' ON [silly " name](c);
|
| 215 |
+
INSERT INTO [silly " name] VALUES(1, 2, 3);
|
| 216 |
+
INSERT INTO [silly " name] VALUES(4, 5, 6);
|
| 217 |
+
ANALYZE;
|
| 218 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 219 |
+
}
|
| 220 |
+
} {{another foolish ' name} {2 1} {foolish ' name} {2 1 1} t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
|
| 221 |
+
do_test analyze-3.11 {
|
| 222 |
+
execsql {
|
| 223 |
+
DROP INDEX "foolish ' name";
|
| 224 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 225 |
+
}
|
| 226 |
+
} {{another foolish ' name} {2 1} t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
|
| 227 |
+
do_test analyze-3.11 {
|
| 228 |
+
execsql {
|
| 229 |
+
DROP TABLE "silly "" name";
|
| 230 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 231 |
+
}
|
| 232 |
+
} {t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
|
| 233 |
+
|
| 234 |
+
# Try corrupting the sqlite_stat1 table and make sure the
|
| 235 |
+
# database is still able to function.
|
| 236 |
+
#
|
| 237 |
+
do_test analyze-4.0 {
|
| 238 |
+
sqlite3 db2 test.db
|
| 239 |
+
db2 eval {
|
| 240 |
+
CREATE TABLE t4(x,y,z);
|
| 241 |
+
CREATE INDEX t4i1 ON t4(x);
|
| 242 |
+
CREATE INDEX t4i2 ON t4(y);
|
| 243 |
+
INSERT INTO t4 SELECT a,b,c FROM t3;
|
| 244 |
+
}
|
| 245 |
+
db2 close
|
| 246 |
+
db close
|
| 247 |
+
sqlite3 db test.db
|
| 248 |
+
execsql {
|
| 249 |
+
ANALYZE;
|
| 250 |
+
SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
|
| 251 |
+
}
|
| 252 |
+
} {t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1} t4i1 {5 3} t4i2 {5 2}}
|
| 253 |
+
do_test analyze-4.1 {
|
| 254 |
+
execsql {
|
| 255 |
+
PRAGMA writable_schema=on;
|
| 256 |
+
INSERT INTO sqlite_stat1 VALUES(null,null,null);
|
| 257 |
+
PRAGMA writable_schema=off;
|
| 258 |
+
}
|
| 259 |
+
db close
|
| 260 |
+
sqlite3 db test.db
|
| 261 |
+
execsql {
|
| 262 |
+
SELECT * FROM t4 WHERE x=1234;
|
| 263 |
+
}
|
| 264 |
+
} {}
|
| 265 |
+
do_test analyze-4.2 {
|
| 266 |
+
execsql {
|
| 267 |
+
PRAGMA writable_schema=on;
|
| 268 |
+
DELETE FROM sqlite_stat1;
|
| 269 |
+
INSERT INTO sqlite_stat1 VALUES('t4','t4i1','nonsense');
|
| 270 |
+
INSERT INTO sqlite_stat1 VALUES('t4','t4i2','120897349817238741092873198273409187234918720394817209384710928374109827172901827349871928741910');
|
| 271 |
+
PRAGMA writable_schema=off;
|
| 272 |
+
}
|
| 273 |
+
db close
|
| 274 |
+
sqlite3 db test.db
|
| 275 |
+
execsql {
|
| 276 |
+
SELECT * FROM t4 WHERE x=1234;
|
| 277 |
+
}
|
| 278 |
+
} {}
|
| 279 |
+
do_test analyze-4.3 {
|
| 280 |
+
execsql {
|
| 281 |
+
INSERT INTO sqlite_stat1 VALUES('t4','xyzzy','0 1 2 3');
|
| 282 |
+
}
|
| 283 |
+
db close
|
| 284 |
+
sqlite3 db test.db
|
| 285 |
+
execsql {
|
| 286 |
+
SELECT * FROM t4 WHERE x=1234;
|
| 287 |
+
}
|
| 288 |
+
} {}
|
| 289 |
+
|
| 290 |
+
# Verify that DROP TABLE and DROP INDEX remove entries from the
|
| 291 |
+
# sqlite_stat1 and sqlite_stat4 tables.
|
| 292 |
+
#
|
| 293 |
+
do_test analyze-5.0 {
|
| 294 |
+
execsql {
|
| 295 |
+
DELETE FROM t3;
|
| 296 |
+
DELETE FROM t4;
|
| 297 |
+
INSERT INTO t3 VALUES(1,2,3,4);
|
| 298 |
+
INSERT INTO t3 VALUES(5,6,7,8);
|
| 299 |
+
INSERT INTO t3 SELECT a+8, b+8, c+8, d+8 FROM t3;
|
| 300 |
+
INSERT INTO t3 SELECT a+16, b+16, c+16, d+16 FROM t3;
|
| 301 |
+
INSERT INTO t3 SELECT a+32, b+32, c+32, d+32 FROM t3;
|
| 302 |
+
INSERT INTO t3 SELECT a+64, b+64, c+64, d+64 FROM t3;
|
| 303 |
+
INSERT INTO t4 SELECT a, b, c FROM t3;
|
| 304 |
+
ANALYZE;
|
| 305 |
+
SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
|
| 306 |
+
SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
|
| 307 |
+
}
|
| 308 |
+
} {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
|
| 309 |
+
ifcapable stat4 {
|
| 310 |
+
do_test analyze-5.1 {
|
| 311 |
+
execsql {
|
| 312 |
+
SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1;
|
| 313 |
+
SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1;
|
| 314 |
+
}
|
| 315 |
+
} {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
|
| 316 |
+
}
|
| 317 |
+
do_test analyze-5.2 {
|
| 318 |
+
execsql {
|
| 319 |
+
DROP INDEX t3i2;
|
| 320 |
+
SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
|
| 321 |
+
SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
|
| 322 |
+
}
|
| 323 |
+
} {t3i1 t3i3 t4i1 t4i2 t3 t4}
|
| 324 |
+
ifcapable stat4 {
|
| 325 |
+
do_test analyze-5.3 {
|
| 326 |
+
execsql {
|
| 327 |
+
SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1;
|
| 328 |
+
SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1;
|
| 329 |
+
}
|
| 330 |
+
} {t3i1 t3i3 t4i1 t4i2 t3 t4}
|
| 331 |
+
}
|
| 332 |
+
do_test analyze-5.4 {
|
| 333 |
+
execsql {
|
| 334 |
+
DROP TABLE t3;
|
| 335 |
+
SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
|
| 336 |
+
SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
|
| 337 |
+
}
|
| 338 |
+
} {t4i1 t4i2 t4}
|
| 339 |
+
ifcapable stat4 {
|
| 340 |
+
do_test analyze-5.5 {
|
| 341 |
+
execsql {
|
| 342 |
+
SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1;
|
| 343 |
+
SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1;
|
| 344 |
+
}
|
| 345 |
+
} {t4i1 t4i2 t4}
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
# This test corrupts the database file so it must be the last test
|
| 349 |
+
# in the series.
|
| 350 |
+
#
|
| 351 |
+
do_test analyze-5.99 {
|
| 352 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 353 |
+
execsql {
|
| 354 |
+
PRAGMA writable_schema=on;
|
| 355 |
+
UPDATE sqlite_master SET sql='nonsense' WHERE name='sqlite_stat1';
|
| 356 |
+
}
|
| 357 |
+
db close
|
| 358 |
+
catch { sqlite3 db test.db }
|
| 359 |
+
catchsql {
|
| 360 |
+
ANALYZE
|
| 361 |
+
}
|
| 362 |
+
} {1 {malformed database schema (sqlite_stat1)}}
|
| 363 |
+
|
| 364 |
+
# Verify that tables whose names begin with "sqlite" but not
|
| 365 |
+
# "sqlite_" are analyzed.
|
| 366 |
+
#
|
| 367 |
+
db close
|
| 368 |
+
sqlite3 db :memory:
|
| 369 |
+
do_execsql_test analyze-6.1 {
|
| 370 |
+
CREATE TABLE sqliteDemo(a);
|
| 371 |
+
INSERT INTO sqliteDemo(a) VALUES(1),(2),(3),(4),(5);
|
| 372 |
+
CREATE TABLE SQLiteDemo2(a INTEGER PRIMARY KEY AUTOINCREMENT);
|
| 373 |
+
INSERT INTO SQLiteDemo2 SELECT * FROM sqliteDemo;
|
| 374 |
+
CREATE TABLE t1(b);
|
| 375 |
+
INSERT INTO t1(b) SELECT a FROM sqliteDemo;
|
| 376 |
+
ANALYZE;
|
| 377 |
+
SELECT tbl FROM sqlite_stat1 WHERE idx IS NULL ORDER BY tbl;
|
| 378 |
+
} {SQLiteDemo2 sqliteDemo t1}
|
| 379 |
+
|
| 380 |
+
# The following caused a small buffer overread in STAT4 processing prior
|
| 381 |
+
# to check-in [b99135288b157044].
|
| 382 |
+
#
|
| 383 |
+
ifcapable stat4 {
|
| 384 |
+
reset_db
|
| 385 |
+
database_may_be_corrupt
|
| 386 |
+
do_execsql_test analyze-7.1 {
|
| 387 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
|
| 388 |
+
INSERT INTO t1 VALUES(1, 7223372036854775);
|
| 389 |
+
INSERT INTO t1 VALUES(2, 7223372036854776);
|
| 390 |
+
INSERT INTO t1 VALUES(3, 7223372036854777);
|
| 391 |
+
CREATE INDEX i1 ON t1(b);
|
| 392 |
+
ANALYZE;
|
| 393 |
+
UPDATE sqlite_stat4 SET sample = substr(sample, 0, 4);
|
| 394 |
+
ANALYZE sqlite_schema;
|
| 395 |
+
SELECT * FROM t1 WHERE b>7223372036854775
|
| 396 |
+
} {2 7223372036854776 3 7223372036854777}
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/analyze3.test
ADDED
|
@@ -0,0 +1,766 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2009 August 06
|
| 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. This file
|
| 13 |
+
# implements tests for range and LIKE constraints that use bound variables
|
| 14 |
+
# instead of literal constant arguments.
|
| 15 |
+
#
|
| 16 |
+
|
| 17 |
+
set testdir [file dirname $argv0]
|
| 18 |
+
source $testdir/tester.tcl
|
| 19 |
+
set testprefix analyze3
|
| 20 |
+
|
| 21 |
+
ifcapable !stat4 {
|
| 22 |
+
finish_test
|
| 23 |
+
return
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# This test cannot be run with the sqlite3_prepare() permutation, as it
|
| 27 |
+
# tests that stat4 data can be used to influence the plans of queries
|
| 28 |
+
# based on bound variable values. And this is not possible when using
|
| 29 |
+
# sqlite3_prepare() - as queries cannot be internally re-prepared after
|
| 30 |
+
# binding values are available.
|
| 31 |
+
if {[permutation]=="prepare"} {
|
| 32 |
+
finish_test
|
| 33 |
+
return
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
#----------------------------------------------------------------------
|
| 37 |
+
# Test Organization:
|
| 38 |
+
#
|
| 39 |
+
# analyze3-1.*: Test that the values of bound parameters are considered
|
| 40 |
+
# in the same way as constants when planning queries that
|
| 41 |
+
# use range constraints.
|
| 42 |
+
#
|
| 43 |
+
# analyze3-2.*: Test that the values of bound parameters are considered
|
| 44 |
+
# in the same way as constants when planning queries that
|
| 45 |
+
# use LIKE expressions in the WHERE clause.
|
| 46 |
+
#
|
| 47 |
+
# analyze3-3.*: Test that binding to a variable does not invalidate the
|
| 48 |
+
# query plan when there is no way in which replanning the
|
| 49 |
+
# query may produce a superior outcome.
|
| 50 |
+
#
|
| 51 |
+
# analyze3-4.*: Test that SQL or authorization callback errors occuring
|
| 52 |
+
# within sqlite3Reprepare() are handled correctly.
|
| 53 |
+
#
|
| 54 |
+
# analyze3-5.*: Check that the query plans of applicable statements are
|
| 55 |
+
# invalidated if the values of SQL parameter are modified
|
| 56 |
+
# using the clear_bindings() or transfer_bindings() APIs.
|
| 57 |
+
#
|
| 58 |
+
# analyze3-6.*: Test that the problem fixed by commit [127a5b776d] is fixed.
|
| 59 |
+
#
|
| 60 |
+
# analyze3-7.*: Test that some memory leaks discovered by fuzz testing
|
| 61 |
+
# have been fixed.
|
| 62 |
+
#
|
| 63 |
+
|
| 64 |
+
proc getvar {varname} { uplevel #0 set $varname }
|
| 65 |
+
db function var getvar
|
| 66 |
+
|
| 67 |
+
proc eqp {sql {db db}} {
|
| 68 |
+
uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
proc sf_execsql {sql {db db}} {
|
| 72 |
+
set ::sqlite_search_count 0
|
| 73 |
+
set r [uplevel [list execsql $sql $db]]
|
| 74 |
+
|
| 75 |
+
concat $::sqlite_search_count [$db status step] $r
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
#-------------------------------------------------------------------------
|
| 79 |
+
#
|
| 80 |
+
# analyze3-1.1.1:
|
| 81 |
+
# Create a table with two columns. Populate the first column (affinity
|
| 82 |
+
# INTEGER) with integer values from 100 to 1100. Create an index on this
|
| 83 |
+
# column. ANALYZE the table.
|
| 84 |
+
#
|
| 85 |
+
# analyze3-1.1.2 - 3.1.3
|
| 86 |
+
# Show that there are two possible plans for querying the table with
|
| 87 |
+
# a range constraint on the indexed column - "full table scan" or "use
|
| 88 |
+
# the index". When the range is specified using literal values, SQLite
|
| 89 |
+
# is able to pick the best plan based on the samples in sqlite_stat3.
|
| 90 |
+
#
|
| 91 |
+
# analyze3-1.1.4 - 3.1.9
|
| 92 |
+
# Show that using SQL variables produces the same results as using
|
| 93 |
+
# literal values to constrain the range scan.
|
| 94 |
+
#
|
| 95 |
+
# These tests also check that the compiler code considers column
|
| 96 |
+
# affinities when estimating the number of rows scanned by the "use
|
| 97 |
+
# index strategy".
|
| 98 |
+
#
|
| 99 |
+
do_test analyze3-1.1.1 {
|
| 100 |
+
execsql {
|
| 101 |
+
BEGIN;
|
| 102 |
+
CREATE TABLE t1(x INTEGER, y);
|
| 103 |
+
CREATE INDEX i1 ON t1(x);
|
| 104 |
+
}
|
| 105 |
+
for {set i 0} {$i < 1000} {incr i} {
|
| 106 |
+
execsql { INSERT INTO t1 VALUES($i+100, $i) }
|
| 107 |
+
}
|
| 108 |
+
execsql {
|
| 109 |
+
COMMIT;
|
| 110 |
+
ANALYZE;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
execsql { SELECT count(*)>0 FROM sqlite_stat4; }
|
| 114 |
+
} {1}
|
| 115 |
+
|
| 116 |
+
do_execsql_test analyze3-1.1.x {
|
| 117 |
+
SELECT count(*) FROM t1 WHERE x>200 AND x<300;
|
| 118 |
+
SELECT count(*) FROM t1 WHERE x>0 AND x<1100;
|
| 119 |
+
} {99 1000}
|
| 120 |
+
|
| 121 |
+
# The first of the following two SELECT statements visits 99 rows. So
|
| 122 |
+
# it is better to use the index. But the second visits every row in
|
| 123 |
+
# the table (1000 in total) so it is better to do a full-table scan.
|
| 124 |
+
#
|
| 125 |
+
do_eqp_test analyze3-1.1.2 {
|
| 126 |
+
SELECT sum(y) FROM t1 WHERE x>200 AND x<300
|
| 127 |
+
} {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
|
| 128 |
+
do_eqp_test analyze3-1.1.3 {
|
| 129 |
+
SELECT sum(y) FROM t1 WHERE x>0 AND x<1100
|
| 130 |
+
} {SCAN t1}
|
| 131 |
+
|
| 132 |
+
# 2017-06-26: Verify that the SQLITE_DBCONFIG_ENABLE_QPSG setting disables
|
| 133 |
+
# the use of bound parameters by STAT4
|
| 134 |
+
#
|
| 135 |
+
db cache flush
|
| 136 |
+
unset -nocomplain l
|
| 137 |
+
unset -nocomplain u
|
| 138 |
+
do_eqp_test analyze3-1.1.3.100 {
|
| 139 |
+
SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
|
| 140 |
+
} {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
|
| 141 |
+
set l 200
|
| 142 |
+
set u 300
|
| 143 |
+
do_eqp_test analyze3-1.1.3.101 {
|
| 144 |
+
SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
|
| 145 |
+
} {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
|
| 146 |
+
set l 0
|
| 147 |
+
set u 1100
|
| 148 |
+
do_eqp_test analyze3-1.1.3.102 {
|
| 149 |
+
SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
|
| 150 |
+
} {SCAN t1}
|
| 151 |
+
db cache flush
|
| 152 |
+
sqlite3_db_config db ENABLE_QPSG 1
|
| 153 |
+
do_eqp_test analyze3-1.1.3.103 {
|
| 154 |
+
SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
|
| 155 |
+
} {SEARCH t1 USING INDEX i1 (x>? AND x<?)}
|
| 156 |
+
db cache flush
|
| 157 |
+
sqlite3_db_config db ENABLE_QPSG 0
|
| 158 |
+
do_eqp_test analyze3-1.1.3.104 {
|
| 159 |
+
SELECT sum(y) FROM t1 WHERE x>$l AND x<$u
|
| 160 |
+
} {SCAN t1}
|
| 161 |
+
|
| 162 |
+
do_test analyze3-1.1.4 {
|
| 163 |
+
sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
|
| 164 |
+
} {199 0 14850}
|
| 165 |
+
do_test analyze3-1.1.5 {
|
| 166 |
+
set l [string range "200" 0 end]
|
| 167 |
+
set u [string range "300" 0 end]
|
| 168 |
+
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
|
| 169 |
+
} {199 0 14850}
|
| 170 |
+
do_test analyze3-1.1.6 {
|
| 171 |
+
set l [expr int(200)]
|
| 172 |
+
set u [expr int(300)]
|
| 173 |
+
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
|
| 174 |
+
} {199 0 14850}
|
| 175 |
+
do_test analyze3-1.1.7 {
|
| 176 |
+
sf_execsql { SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 }
|
| 177 |
+
} {999 999 499500}
|
| 178 |
+
do_test analyze3-1.1.8 {
|
| 179 |
+
set l [string range "0" 0 end]
|
| 180 |
+
set u [string range "1100" 0 end]
|
| 181 |
+
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
|
| 182 |
+
} {999 999 499500}
|
| 183 |
+
do_test analyze3-1.1.9 {
|
| 184 |
+
set l [expr int(0)]
|
| 185 |
+
set u [expr int(1100)]
|
| 186 |
+
sf_execsql { SELECT sum(y) FROM t1 WHERE x>$l AND x<$u }
|
| 187 |
+
} {999 999 499500}
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
# The following tests are similar to the block above. The difference is
|
| 191 |
+
# that the indexed column has TEXT affinity in this case. In the tests
|
| 192 |
+
# above the affinity is INTEGER.
|
| 193 |
+
#
|
| 194 |
+
do_test analyze3-1.2.1 {
|
| 195 |
+
execsql {
|
| 196 |
+
BEGIN;
|
| 197 |
+
CREATE TABLE t2(x TEXT, y);
|
| 198 |
+
INSERT INTO t2 SELECT * FROM t1;
|
| 199 |
+
CREATE INDEX i2 ON t2(x);
|
| 200 |
+
COMMIT;
|
| 201 |
+
ANALYZE;
|
| 202 |
+
}
|
| 203 |
+
} {}
|
| 204 |
+
do_execsql_test analyze3-2.1.x {
|
| 205 |
+
SELECT count(*) FROM t2 WHERE x>1 AND x<2;
|
| 206 |
+
SELECT count(*) FROM t2 WHERE x>0 AND x<99;
|
| 207 |
+
} {200 990}
|
| 208 |
+
do_eqp_test analyze3-1.2.2 {
|
| 209 |
+
SELECT sum(y) FROM t2 WHERE x>1 AND x<2
|
| 210 |
+
} {SEARCH t2 USING INDEX i2 (x>? AND x<?)}
|
| 211 |
+
do_eqp_test analyze3-1.2.3 {
|
| 212 |
+
SELECT sum(y) FROM t2 WHERE x>0 AND x<99
|
| 213 |
+
} {SCAN t2}
|
| 214 |
+
|
| 215 |
+
do_test analyze3-1.2.4 {
|
| 216 |
+
sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
|
| 217 |
+
} {161 0 4760}
|
| 218 |
+
do_test analyze3-1.2.5 {
|
| 219 |
+
set l [string range "12" 0 end]
|
| 220 |
+
set u [string range "20" 0 end]
|
| 221 |
+
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
|
| 222 |
+
} {161 0 text text 4760}
|
| 223 |
+
do_test analyze3-1.2.6 {
|
| 224 |
+
set l [expr int(12)]
|
| 225 |
+
set u [expr int(20)]
|
| 226 |
+
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
|
| 227 |
+
} {161 0 integer integer 4760}
|
| 228 |
+
do_test analyze3-1.2.7 {
|
| 229 |
+
sf_execsql { SELECT sum(y) FROM t2 WHERE x>0 AND x<99 }
|
| 230 |
+
} {999 999 490555}
|
| 231 |
+
do_test analyze3-1.2.8 {
|
| 232 |
+
set l [string range "0" 0 end]
|
| 233 |
+
set u [string range "99" 0 end]
|
| 234 |
+
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
|
| 235 |
+
} {999 999 text text 490555}
|
| 236 |
+
do_test analyze3-1.2.9 {
|
| 237 |
+
set l [expr int(0)]
|
| 238 |
+
set u [expr int(99)]
|
| 239 |
+
sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
|
| 240 |
+
} {999 999 integer integer 490555}
|
| 241 |
+
|
| 242 |
+
# Same tests a third time. This time, column x has INTEGER affinity and
|
| 243 |
+
# is not the leftmost column of the table. This triggered a bug causing
|
| 244 |
+
# SQLite to use sub-optimal query plans in 3.6.18 and earlier.
|
| 245 |
+
#
|
| 246 |
+
do_test analyze3-1.3.1 {
|
| 247 |
+
execsql {
|
| 248 |
+
BEGIN;
|
| 249 |
+
CREATE TABLE t3(y TEXT, x INTEGER);
|
| 250 |
+
INSERT INTO t3 SELECT y, x FROM t1;
|
| 251 |
+
CREATE INDEX i3 ON t3(x);
|
| 252 |
+
COMMIT;
|
| 253 |
+
ANALYZE;
|
| 254 |
+
}
|
| 255 |
+
} {}
|
| 256 |
+
do_execsql_test analyze3-1.3.x {
|
| 257 |
+
SELECT count(*) FROM t3 WHERE x>200 AND x<300;
|
| 258 |
+
SELECT count(*) FROM t3 WHERE x>0 AND x<1100
|
| 259 |
+
} {99 1000}
|
| 260 |
+
do_eqp_test analyze3-1.3.2 {
|
| 261 |
+
SELECT sum(y) FROM t3 WHERE x>200 AND x<300
|
| 262 |
+
} {SEARCH t3 USING INDEX i3 (x>? AND x<?)}
|
| 263 |
+
do_eqp_test analyze3-1.3.3 {
|
| 264 |
+
SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
|
| 265 |
+
} {SCAN t3}
|
| 266 |
+
|
| 267 |
+
do_test analyze3-1.3.4 {
|
| 268 |
+
sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
|
| 269 |
+
} {199 0 14850}
|
| 270 |
+
do_test analyze3-1.3.5 {
|
| 271 |
+
set l [string range "200" 0 end]
|
| 272 |
+
set u [string range "300" 0 end]
|
| 273 |
+
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
|
| 274 |
+
} {199 0 14850}
|
| 275 |
+
do_test analyze3-1.3.6 {
|
| 276 |
+
set l [expr int(200)]
|
| 277 |
+
set u [expr int(300)]
|
| 278 |
+
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
|
| 279 |
+
} {199 0 14850}
|
| 280 |
+
do_test analyze3-1.3.7 {
|
| 281 |
+
sf_execsql { SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 }
|
| 282 |
+
} {999 999 499500}
|
| 283 |
+
do_test analyze3-1.3.8 {
|
| 284 |
+
set l [string range "0" 0 end]
|
| 285 |
+
set u [string range "1100" 0 end]
|
| 286 |
+
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
|
| 287 |
+
} {999 999 499500}
|
| 288 |
+
do_test analyze3-1.3.9 {
|
| 289 |
+
set l [expr int(0)]
|
| 290 |
+
set u [expr int(1100)]
|
| 291 |
+
sf_execsql { SELECT sum(y) FROM t3 WHERE x>$l AND x<$u }
|
| 292 |
+
} {999 999 499500}
|
| 293 |
+
|
| 294 |
+
#-------------------------------------------------------------------------
|
| 295 |
+
# Test that the values of bound SQL variables may be used for the LIKE
|
| 296 |
+
# optimization.
|
| 297 |
+
#
|
| 298 |
+
drop_all_tables
|
| 299 |
+
do_test analyze3-2.1 {
|
| 300 |
+
execsql {
|
| 301 |
+
PRAGMA case_sensitive_like=off;
|
| 302 |
+
BEGIN;
|
| 303 |
+
CREATE TABLE t1(a, b TEXT COLLATE nocase);
|
| 304 |
+
CREATE INDEX i1 ON t1(b);
|
| 305 |
+
}
|
| 306 |
+
for {set i 0} {$i < 1000} {incr i} {
|
| 307 |
+
set t ""
|
| 308 |
+
append t [lindex {a b c d e f g h i j} [expr $i/100]]
|
| 309 |
+
append t [lindex {a b c d e f g h i j} [expr ($i/10)%10]]
|
| 310 |
+
append t [lindex {a b c d e f g h i j} [expr ($i%10)]]
|
| 311 |
+
execsql { INSERT INTO t1 VALUES($i, $t) }
|
| 312 |
+
}
|
| 313 |
+
execsql COMMIT
|
| 314 |
+
} {}
|
| 315 |
+
do_eqp_test analyze3-2.2 {
|
| 316 |
+
SELECT count(a) FROM t1 WHERE b LIKE 'a%'
|
| 317 |
+
} {SEARCH t1 USING INDEX i1 (b>? AND b<?)}
|
| 318 |
+
do_eqp_test analyze3-2.3 {
|
| 319 |
+
SELECT count(a) FROM t1 WHERE b LIKE '%a'
|
| 320 |
+
} {SCAN t1}
|
| 321 |
+
|
| 322 |
+
# Return the first argument if like_match_blobs is true (the default)
|
| 323 |
+
# or the second argument if not
|
| 324 |
+
#
|
| 325 |
+
proc ilmb {a b} {
|
| 326 |
+
ifcapable like_match_blobs {return $a}
|
| 327 |
+
return $b
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
do_test analyze3-2.4 {
|
| 331 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
|
| 332 |
+
} [list [ilmb 102 101] 0 100]
|
| 333 |
+
do_test analyze3-2.5 {
|
| 334 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
|
| 335 |
+
} {999 999 100}
|
| 336 |
+
|
| 337 |
+
do_test analyze3-2.6 {
|
| 338 |
+
set like "a%"
|
| 339 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
|
| 340 |
+
} [list [ilmb 102 101] 0 100]
|
| 341 |
+
do_test analyze3-2.7 {
|
| 342 |
+
set like "%a"
|
| 343 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
|
| 344 |
+
} {999 999 100}
|
| 345 |
+
do_test analyze3-2.8 {
|
| 346 |
+
set like "a"
|
| 347 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
|
| 348 |
+
} [list [ilmb 102 101] 0 0]
|
| 349 |
+
do_test analyze3-2.9 {
|
| 350 |
+
set like "ab"
|
| 351 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
|
| 352 |
+
} [list [ilmb 12 11] 0 0]
|
| 353 |
+
do_test analyze3-2.10 {
|
| 354 |
+
set like "abc"
|
| 355 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
|
| 356 |
+
} [list [ilmb 3 2] 0 1]
|
| 357 |
+
do_test analyze3-2.11 {
|
| 358 |
+
set like "a_c"
|
| 359 |
+
sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE $like }
|
| 360 |
+
} [list [ilmb 102 101] 0 10]
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
#-------------------------------------------------------------------------
|
| 364 |
+
# This block of tests checks that statements are correctly marked as
|
| 365 |
+
# expired when the values bound to any parameters that may affect the
|
| 366 |
+
# query plan are modified.
|
| 367 |
+
#
|
| 368 |
+
drop_all_tables
|
| 369 |
+
db auth auth
|
| 370 |
+
proc auth {args} {
|
| 371 |
+
set ::auth 1
|
| 372 |
+
return SQLITE_OK
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
do_test analyze3-3.1 {
|
| 376 |
+
execsql {
|
| 377 |
+
BEGIN;
|
| 378 |
+
CREATE TABLE t1(a, b, c);
|
| 379 |
+
CREATE INDEX i1 ON t1(b);
|
| 380 |
+
}
|
| 381 |
+
for {set i 0} {$i < 100} {incr i} {
|
| 382 |
+
execsql { INSERT INTO t1 VALUES($i, $i, $i) }
|
| 383 |
+
}
|
| 384 |
+
execsql COMMIT
|
| 385 |
+
execsql ANALYZE
|
| 386 |
+
} {}
|
| 387 |
+
do_test analyze3-3.2.1 {
|
| 388 |
+
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b>?" -1 dummy]
|
| 389 |
+
sqlite3_expired $S
|
| 390 |
+
} {0}
|
| 391 |
+
do_test analyze3-3.2.2 {
|
| 392 |
+
sqlite3_bind_text $S 1 "abc" 3
|
| 393 |
+
sqlite3_expired $S
|
| 394 |
+
} {1}
|
| 395 |
+
do_test analyze3-3.2.4 {
|
| 396 |
+
sqlite3_finalize $S
|
| 397 |
+
} {SQLITE_OK}
|
| 398 |
+
|
| 399 |
+
do_test analyze3-3.2.5 {
|
| 400 |
+
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b=?" -1 dummy]
|
| 401 |
+
sqlite3_expired $S
|
| 402 |
+
} {0}
|
| 403 |
+
do_test analyze3-3.2.6 {
|
| 404 |
+
sqlite3_bind_text $S 1 "abc" 3
|
| 405 |
+
sqlite3_expired $S
|
| 406 |
+
} {1}
|
| 407 |
+
do_test analyze3-3.2.7 {
|
| 408 |
+
sqlite3_finalize $S
|
| 409 |
+
} {SQLITE_OK}
|
| 410 |
+
|
| 411 |
+
do_test analyze3-3.4.1 {
|
| 412 |
+
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
|
| 413 |
+
sqlite3_expired $S
|
| 414 |
+
} {0}
|
| 415 |
+
do_test analyze3-3.4.2 {
|
| 416 |
+
sqlite3_bind_text $S 1 "abc" 3
|
| 417 |
+
sqlite3_expired $S
|
| 418 |
+
} {0}
|
| 419 |
+
do_test analyze3-3.4.3 {
|
| 420 |
+
sqlite3_bind_text $S 2 "def" 3
|
| 421 |
+
sqlite3_expired $S
|
| 422 |
+
} {1}
|
| 423 |
+
do_test analyze3-3.4.4 {
|
| 424 |
+
sqlite3_bind_text $S 2 "ghi" 3
|
| 425 |
+
sqlite3_expired $S
|
| 426 |
+
} {1}
|
| 427 |
+
do_test analyze3-3.4.5 {
|
| 428 |
+
sqlite3_expired $S
|
| 429 |
+
} {1}
|
| 430 |
+
do_test analyze3-3.4.6 {
|
| 431 |
+
sqlite3_finalize $S
|
| 432 |
+
} {SQLITE_OK}
|
| 433 |
+
|
| 434 |
+
do_test analyze3-3.5.1 {
|
| 435 |
+
set S [sqlite3_prepare_v2 db {
|
| 436 |
+
SELECT * FROM t1 WHERE a IN (
|
| 437 |
+
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
|
| 438 |
+
?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
|
| 439 |
+
?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31
|
| 440 |
+
) AND b>?32;
|
| 441 |
+
} -1 dummy]
|
| 442 |
+
sqlite3_expired $S
|
| 443 |
+
} {0}
|
| 444 |
+
do_test analyze3-3.5.2 {
|
| 445 |
+
sqlite3_bind_text $S 31 "abc" 3
|
| 446 |
+
sqlite3_expired $S
|
| 447 |
+
} {0}
|
| 448 |
+
do_test analyze3-3.5.3 {
|
| 449 |
+
sqlite3_bind_text $S 32 "def" 3
|
| 450 |
+
sqlite3_expired $S
|
| 451 |
+
} {1}
|
| 452 |
+
do_test analyze3-3.5.5 {
|
| 453 |
+
sqlite3_finalize $S
|
| 454 |
+
} {SQLITE_OK}
|
| 455 |
+
|
| 456 |
+
do_test analyze3-3.6.1 {
|
| 457 |
+
set S [sqlite3_prepare_v2 db {
|
| 458 |
+
SELECT * FROM t1 WHERE a IN (
|
| 459 |
+
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10,
|
| 460 |
+
?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
|
| 461 |
+
?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
|
| 462 |
+
) AND b>?33;
|
| 463 |
+
} -1 dummy]
|
| 464 |
+
sqlite3_expired $S
|
| 465 |
+
} {0}
|
| 466 |
+
do_test analyze3-3.6.2 {
|
| 467 |
+
sqlite3_bind_text $S 32 "abc" 3
|
| 468 |
+
sqlite3_expired $S
|
| 469 |
+
} {1}
|
| 470 |
+
do_test analyze3-3.6.3 {
|
| 471 |
+
sqlite3_bind_text $S 33 "def" 3
|
| 472 |
+
sqlite3_expired $S
|
| 473 |
+
} {1}
|
| 474 |
+
do_test analyze3-3.6.5 {
|
| 475 |
+
sqlite3_finalize $S
|
| 476 |
+
} {SQLITE_OK}
|
| 477 |
+
|
| 478 |
+
do_test analyze3-3.7.1 {
|
| 479 |
+
set S [sqlite3_prepare_v2 db {
|
| 480 |
+
SELECT * FROM t1 WHERE a IN (
|
| 481 |
+
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?33,
|
| 482 |
+
?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19, ?20,
|
| 483 |
+
?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28, ?29, ?30, ?31, ?32
|
| 484 |
+
) AND b>?10;
|
| 485 |
+
} -1 dummy]
|
| 486 |
+
sqlite3_expired $S
|
| 487 |
+
} {0}
|
| 488 |
+
do_test analyze3-3.7.2 {
|
| 489 |
+
sqlite3_bind_text $S 32 "abc" 3
|
| 490 |
+
sqlite3_expired $S
|
| 491 |
+
} {0}
|
| 492 |
+
do_test analyze3-3.7.3 {
|
| 493 |
+
sqlite3_bind_text $S 33 "def" 3
|
| 494 |
+
sqlite3_expired $S
|
| 495 |
+
} {0}
|
| 496 |
+
do_test analyze3-3.7.4 {
|
| 497 |
+
sqlite3_bind_text $S 10 "def" 3
|
| 498 |
+
sqlite3_expired $S
|
| 499 |
+
} {1}
|
| 500 |
+
do_test analyze3-3.7.6 {
|
| 501 |
+
sqlite3_finalize $S
|
| 502 |
+
} {SQLITE_OK}
|
| 503 |
+
|
| 504 |
+
do_test analyze3-3.8.1 {
|
| 505 |
+
execsql {
|
| 506 |
+
CREATE TABLE t4(x, y TEXT COLLATE NOCASE);
|
| 507 |
+
CREATE INDEX i4 ON t4(y);
|
| 508 |
+
}
|
| 509 |
+
} {}
|
| 510 |
+
do_test analyze3-3.8.2 {
|
| 511 |
+
set S [sqlite3_prepare_v2 db {
|
| 512 |
+
SELECT * FROM t4 WHERE x != ? AND y LIKE ?
|
| 513 |
+
} -1 dummy]
|
| 514 |
+
sqlite3_expired $S
|
| 515 |
+
} {0}
|
| 516 |
+
do_test analyze3-3.8.3 {
|
| 517 |
+
sqlite3_bind_text $S 1 "abc" 3
|
| 518 |
+
sqlite3_expired $S
|
| 519 |
+
} {0}
|
| 520 |
+
do_test analyze3-3.8.4 {
|
| 521 |
+
sqlite3_bind_text $S 2 "def" 3
|
| 522 |
+
sqlite3_expired $S
|
| 523 |
+
} {1}
|
| 524 |
+
do_test analyze3-3.8.7 {
|
| 525 |
+
sqlite3_bind_text $S 2 "ghi%" 4
|
| 526 |
+
sqlite3_expired $S
|
| 527 |
+
} {1}
|
| 528 |
+
do_test analyze3-3.8.8 {
|
| 529 |
+
sqlite3_expired $S
|
| 530 |
+
} {1}
|
| 531 |
+
do_test analyze3-3.8.9 {
|
| 532 |
+
sqlite3_bind_text $S 2 "ghi%def" 7
|
| 533 |
+
sqlite3_expired $S
|
| 534 |
+
} {1}
|
| 535 |
+
do_test analyze3-3.8.10 {
|
| 536 |
+
sqlite3_expired $S
|
| 537 |
+
} {1}
|
| 538 |
+
do_test analyze3-3.8.11 {
|
| 539 |
+
sqlite3_bind_text $S 2 "%ab" 3
|
| 540 |
+
sqlite3_expired $S
|
| 541 |
+
} {1}
|
| 542 |
+
do_test analyze3-3.8.12 {
|
| 543 |
+
sqlite3_expired $S
|
| 544 |
+
} {1}
|
| 545 |
+
do_test analyze3-3.8.12 {
|
| 546 |
+
sqlite3_bind_text $S 2 "%de" 3
|
| 547 |
+
sqlite3_expired $S
|
| 548 |
+
} {1}
|
| 549 |
+
do_test analyze3-3.8.13 {
|
| 550 |
+
sqlite3_expired $S
|
| 551 |
+
} {1}
|
| 552 |
+
do_test analyze3-3.8.14 {
|
| 553 |
+
sqlite3_finalize $S
|
| 554 |
+
} {SQLITE_OK}
|
| 555 |
+
|
| 556 |
+
#-------------------------------------------------------------------------
|
| 557 |
+
# These tests check that errors encountered while repreparing an SQL
|
| 558 |
+
# statement within sqlite3Reprepare() are handled correctly.
|
| 559 |
+
#
|
| 560 |
+
|
| 561 |
+
# Check a schema error.
|
| 562 |
+
#
|
| 563 |
+
do_test analyze3-4.1.1 {
|
| 564 |
+
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
|
| 565 |
+
sqlite3_step $S
|
| 566 |
+
} {SQLITE_DONE}
|
| 567 |
+
do_test analyze3-4.1.2 {
|
| 568 |
+
sqlite3_reset $S
|
| 569 |
+
sqlite3_bind_text $S 2 "abc" 3
|
| 570 |
+
execsql { DROP TABLE t1 }
|
| 571 |
+
sqlite3_step $S
|
| 572 |
+
} {SQLITE_ERROR}
|
| 573 |
+
do_test analyze3-4.1.3 {
|
| 574 |
+
sqlite3_finalize $S
|
| 575 |
+
} {SQLITE_ERROR}
|
| 576 |
+
|
| 577 |
+
# Check an authorization error.
|
| 578 |
+
#
|
| 579 |
+
do_test analyze3-4.2.1 {
|
| 580 |
+
execsql {
|
| 581 |
+
BEGIN;
|
| 582 |
+
CREATE TABLE t1(a, b, c);
|
| 583 |
+
CREATE INDEX i1 ON t1(b);
|
| 584 |
+
}
|
| 585 |
+
for {set i 0} {$i < 100} {incr i} {
|
| 586 |
+
execsql { INSERT INTO t1 VALUES($i, $i, $i) }
|
| 587 |
+
}
|
| 588 |
+
execsql COMMIT
|
| 589 |
+
execsql ANALYZE
|
| 590 |
+
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
|
| 591 |
+
sqlite3_step $S
|
| 592 |
+
} {SQLITE_DONE}
|
| 593 |
+
db auth auth
|
| 594 |
+
proc auth {args} {
|
| 595 |
+
if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
|
| 596 |
+
return SQLITE_OK
|
| 597 |
+
}
|
| 598 |
+
do_test analyze3-4.2.2 {
|
| 599 |
+
sqlite3_reset $S
|
| 600 |
+
sqlite3_bind_text $S 2 "abc" 3
|
| 601 |
+
sqlite3_step $S
|
| 602 |
+
} {SQLITE_AUTH}
|
| 603 |
+
do_test analyze3-4.2.4 {
|
| 604 |
+
sqlite3_finalize $S
|
| 605 |
+
} {SQLITE_AUTH}
|
| 606 |
+
|
| 607 |
+
# Check the effect of an authorization error that occurs in a re-prepare
|
| 608 |
+
# performed by sqlite3_step() is the same as one that occurs within
|
| 609 |
+
# sqlite3Reprepare().
|
| 610 |
+
#
|
| 611 |
+
do_test analyze3-4.3.1 {
|
| 612 |
+
db auth {}
|
| 613 |
+
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
|
| 614 |
+
execsql { CREATE TABLE t2(d, e, f) }
|
| 615 |
+
db auth auth
|
| 616 |
+
sqlite3_step $S
|
| 617 |
+
} {SQLITE_AUTH}
|
| 618 |
+
do_test analyze3-4.3.2 {
|
| 619 |
+
sqlite3_finalize $S
|
| 620 |
+
} {SQLITE_AUTH}
|
| 621 |
+
db auth {}
|
| 622 |
+
|
| 623 |
+
#-------------------------------------------------------------------------
|
| 624 |
+
# Test that modifying bound variables using the clear_bindings() or
|
| 625 |
+
# transfer_bindings() APIs works.
|
| 626 |
+
#
|
| 627 |
+
# analyze3-5.1.*: sqlite3_clear_bindings()
|
| 628 |
+
# analyze3-5.2.*: sqlite3_transfer_bindings()
|
| 629 |
+
#
|
| 630 |
+
do_test analyze3-5.1.1 {
|
| 631 |
+
drop_all_tables
|
| 632 |
+
execsql {
|
| 633 |
+
CREATE TABLE t1(x TEXT COLLATE NOCASE);
|
| 634 |
+
CREATE INDEX i1 ON t1(x);
|
| 635 |
+
INSERT INTO t1 VALUES('aaa');
|
| 636 |
+
INSERT INTO t1 VALUES('abb');
|
| 637 |
+
INSERT INTO t1 VALUES('acc');
|
| 638 |
+
INSERT INTO t1 VALUES('baa');
|
| 639 |
+
INSERT INTO t1 VALUES('bbb');
|
| 640 |
+
INSERT INTO t1 VALUES('bcc');
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
|
| 644 |
+
sqlite3_bind_text $S 1 "a%" 2
|
| 645 |
+
set R [list]
|
| 646 |
+
while { "SQLITE_ROW" == [sqlite3_step $S] } {
|
| 647 |
+
lappend R [sqlite3_column_text $S 0]
|
| 648 |
+
}
|
| 649 |
+
concat [sqlite3_reset $S] $R
|
| 650 |
+
} {SQLITE_OK aaa abb acc}
|
| 651 |
+
do_test analyze3-5.1.2 {
|
| 652 |
+
sqlite3_clear_bindings $S
|
| 653 |
+
set R [list]
|
| 654 |
+
while { "SQLITE_ROW" == [sqlite3_step $S] } {
|
| 655 |
+
lappend R [sqlite3_column_text $S 0]
|
| 656 |
+
}
|
| 657 |
+
concat [sqlite3_reset $S] $R
|
| 658 |
+
} {SQLITE_OK}
|
| 659 |
+
do_test analyze3-5.1.3 {
|
| 660 |
+
sqlite3_finalize $S
|
| 661 |
+
} {SQLITE_OK}
|
| 662 |
+
|
| 663 |
+
do_test analyze3-5.1.1 {
|
| 664 |
+
set S1 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x LIKE ?" -1 dummy]
|
| 665 |
+
sqlite3_bind_text $S1 1 "b%" 2
|
| 666 |
+
set R [list]
|
| 667 |
+
while { "SQLITE_ROW" == [sqlite3_step $S1] } {
|
| 668 |
+
lappend R [sqlite3_column_text $S1 0]
|
| 669 |
+
}
|
| 670 |
+
concat [sqlite3_reset $S1] $R
|
| 671 |
+
} {SQLITE_OK baa bbb bcc}
|
| 672 |
+
|
| 673 |
+
do_test analyze3-5.1.2 {
|
| 674 |
+
set S2 [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE x = ?" -1 dummy]
|
| 675 |
+
sqlite3_bind_text $S2 1 "a%" 2
|
| 676 |
+
sqlite3_transfer_bindings $S2 $S1
|
| 677 |
+
set R [list]
|
| 678 |
+
while { "SQLITE_ROW" == [sqlite3_step $S1] } {
|
| 679 |
+
lappend R [sqlite3_column_text $S1 0]
|
| 680 |
+
}
|
| 681 |
+
concat [sqlite3_reset $S1] $R
|
| 682 |
+
} {SQLITE_OK aaa abb acc}
|
| 683 |
+
do_test analyze3-5.1.3 {
|
| 684 |
+
sqlite3_finalize $S2
|
| 685 |
+
sqlite3_finalize $S1
|
| 686 |
+
} {SQLITE_OK}
|
| 687 |
+
|
| 688 |
+
#-------------------------------------------------------------------------
|
| 689 |
+
|
| 690 |
+
do_test analyze3-6.1 {
|
| 691 |
+
execsql { DROP TABLE IF EXISTS t1 }
|
| 692 |
+
execsql BEGIN
|
| 693 |
+
execsql { CREATE TABLE t1(a, b, c) }
|
| 694 |
+
for {set i 0} {$i < 1000} {incr i} {
|
| 695 |
+
execsql "INSERT INTO t1 VALUES([expr $i/100], 'x', [expr $i/10])"
|
| 696 |
+
}
|
| 697 |
+
execsql {
|
| 698 |
+
CREATE INDEX i1 ON t1(a, b);
|
| 699 |
+
CREATE INDEX i2 ON t1(c);
|
| 700 |
+
}
|
| 701 |
+
execsql COMMIT
|
| 702 |
+
execsql ANALYZE
|
| 703 |
+
} {}
|
| 704 |
+
|
| 705 |
+
do_eqp_test analyze3-6-3 {
|
| 706 |
+
SELECT * FROM t1 WHERE a = 5 AND c = 13;
|
| 707 |
+
} {SEARCH t1 USING INDEX i2 (c=?)}
|
| 708 |
+
|
| 709 |
+
do_eqp_test analyze3-6-2 {
|
| 710 |
+
SELECT * FROM t1 WHERE a = 5 AND b > 'w' AND c = 13;
|
| 711 |
+
} {SEARCH t1 USING INDEX i2 (c=?)}
|
| 712 |
+
|
| 713 |
+
#-----------------------------------------------------------------------------
|
| 714 |
+
# 2015-04-20.
|
| 715 |
+
# Memory leak in sqlite3Stat4ProbeFree(). (Discovered while fuzzing.)
|
| 716 |
+
#
|
| 717 |
+
do_execsql_test analyze-7.1 {
|
| 718 |
+
DROP TABLE IF EXISTS t1;
|
| 719 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
|
| 720 |
+
INSERT INTO t1 VALUES(1,1,'0000');
|
| 721 |
+
CREATE INDEX t0b ON t1(b);
|
| 722 |
+
ANALYZE;
|
| 723 |
+
SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND hex(1);
|
| 724 |
+
} {}
|
| 725 |
+
|
| 726 |
+
# At one point duplicate stat1 entries were causing a memory leak.
|
| 727 |
+
#
|
| 728 |
+
reset_db
|
| 729 |
+
do_execsql_test 7.2 {
|
| 730 |
+
CREATE TABLE t1(a,b,c);
|
| 731 |
+
CREATE INDEX t1a ON t1(a);
|
| 732 |
+
ANALYZE;
|
| 733 |
+
SELECT * FROM sqlite_stat1;
|
| 734 |
+
INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t1','t1a','12000');
|
| 735 |
+
INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t1','t1a','12000');
|
| 736 |
+
ANALYZE sqlite_master;
|
| 737 |
+
}
|
| 738 |
+
|
| 739 |
+
# 2023-04-22 https://sqlite.org/forum/info/6c118daad0f1f5ef
|
| 740 |
+
# Case differences in the sqlite_stat4.idx field should not matter.
|
| 741 |
+
#
|
| 742 |
+
reset_db
|
| 743 |
+
do_execsql_test 8.0 {
|
| 744 |
+
CREATE TABLE t1(a PRIMARY KEY, v) WITHOUT ROWID;
|
| 745 |
+
ANALYZE sqlite_schema;
|
| 746 |
+
INSERT INTO sqlite_stat1 VALUES('t1','t1','1 1');
|
| 747 |
+
INSERT INTO sqlite_stat4 VALUES('t1','t1','1','0','0',X'021b76657273696f6e');
|
| 748 |
+
INSERT INTO sqlite_stat4 VALUES('T1','T1','1','0','0',X'021b76657273696f6e');
|
| 749 |
+
ANALYZE sqlite_schema;
|
| 750 |
+
} {}
|
| 751 |
+
|
| 752 |
+
# 2023-05-03 https://sqlite.org/forum/forumpost/537d8ab118
|
| 753 |
+
# Same index appears by two different names in the sqlite_stat4 table.
|
| 754 |
+
#
|
| 755 |
+
reset_db
|
| 756 |
+
do_execsql_test 8.1 {
|
| 757 |
+
CREATE TABLE t1(a INT PRIMARY KEY, b INT) WITHOUT ROWID;
|
| 758 |
+
ANALYZE sqlite_schema;
|
| 759 |
+
INSERT INTO sqlite_stat4 VALUES
|
| 760 |
+
('t1','t1','1','2','2',X'03000103'),
|
| 761 |
+
('t1','sqlite_autoindex_t1_1','1','2','2',X'03000103');
|
| 762 |
+
ANALYZE sqlite_schema;
|
| 763 |
+
PRAGMA integrity_check;
|
| 764 |
+
} {ok}
|
| 765 |
+
|
| 766 |
+
finish_test
|
local-test-sqlite3-delta-02/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-02/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<?)*/}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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/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-02/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-02/afc-sqlite3/test/atof1.test
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2012 June 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 |
+
#
|
| 12 |
+
# Tests of the sqlite3AtoF() function.
|
| 13 |
+
#
|
| 14 |
+
|
| 15 |
+
set testdir [file dirname $argv0]
|
| 16 |
+
source $testdir/tester.tcl
|
| 17 |
+
|
| 18 |
+
set mxpow 35
|
| 19 |
+
expr srand(1)
|
| 20 |
+
for {set i 1} {$i<20000} {incr i} {
|
| 21 |
+
set pow [expr {int((rand()-0.5)*$mxpow)}]
|
| 22 |
+
set x [expr {pow((rand()-0.5)*2*rand(),$pow)}]
|
| 23 |
+
set xf [format %.32e $x]
|
| 24 |
+
|
| 25 |
+
# Verify that text->real conversions get exactly same ieee754 floating-
|
| 26 |
+
# point value in SQLite as they do in TCL.
|
| 27 |
+
#
|
| 28 |
+
do_test atof1-1.$i.1 {
|
| 29 |
+
set y [db eval "SELECT $xf=\$x"]
|
| 30 |
+
if {!$y} {
|
| 31 |
+
puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175
|
| 32 |
+
db eval "SELECT $xf+0.0 AS a, \$x AS b" {
|
| 33 |
+
puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b]
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
set y
|
| 37 |
+
} {1}
|
| 38 |
+
|
| 39 |
+
# Verify that round-trip real->text->real conversions using the quote()
|
| 40 |
+
# function preserve the bits of the numeric value exactly.
|
| 41 |
+
#
|
| 42 |
+
do_test atof1-1.$i.2 {
|
| 43 |
+
set y [db eval {SELECT $x=CAST(quote($x) AS real)}]
|
| 44 |
+
if {!$y} {
|
| 45 |
+
db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {}
|
| 46 |
+
puts ""
|
| 47 |
+
if {$x<0} {
|
| 48 |
+
puts "[format {!SCALE: %17s 1 23456789 123456789 123456789} {}]"
|
| 49 |
+
} else {
|
| 50 |
+
puts "[format {!SCALE: %16s 1 23456789 123456789 123456789} {}]"
|
| 51 |
+
}
|
| 52 |
+
puts "!IN: $a $xf"
|
| 53 |
+
puts [format {!QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]]
|
| 54 |
+
db eval {SELECT CAST(quote($x) AS real) c} {}
|
| 55 |
+
puts "!OUT: $b [format %.32e $c]"
|
| 56 |
+
}
|
| 57 |
+
set y
|
| 58 |
+
} {1}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# 2020-01-08 ticket 9eda2697f5cc1aba
|
| 62 |
+
# When running sqlite3AtoF() on a blob with an odd number of bytes using
|
| 63 |
+
# UTF16, ignore the last byte so that the string has an integer number of
|
| 64 |
+
# UTF16 code points.
|
| 65 |
+
#
|
| 66 |
+
reset_db
|
| 67 |
+
do_execsql_test atof1-2.10 {
|
| 68 |
+
PRAGMA encoding = 'UTF16be';
|
| 69 |
+
CREATE TABLE t1(a, b);
|
| 70 |
+
INSERT INTO t1(rowid,a) VALUES (1,x'00'),(2,3);
|
| 71 |
+
SELECT substr(a,',') is true FROM t1 ORDER BY rowid;
|
| 72 |
+
} {0 1}
|
| 73 |
+
do_execsql_test atof1-2.20 {
|
| 74 |
+
SELECT substr(a,',') is true FROM t1 ORDER BY rowid DESC;
|
| 75 |
+
} {1 0}
|
| 76 |
+
do_execsql_test atof1-2.30 {
|
| 77 |
+
CREATE INDEX i1 ON t1(a);
|
| 78 |
+
SELECT count(*) FROM t1 WHERE substr(a,',');
|
| 79 |
+
} {1}
|
| 80 |
+
# 2020-08-27 OSSFuzz find related to the above.
|
| 81 |
+
do_execsql_test atof1-2.40 {
|
| 82 |
+
SELECT randomblob(0) - 1;
|
| 83 |
+
} {-1}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
finish_test
|
local-test-sqlite3-delta-02/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-02/afc-sqlite3/test/attach3.test
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/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-02/afc-sqlite3/test/auth.test
ADDED
|
@@ -0,0 +1,2678 @@
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/afc-sqlite3/test/auth2.test
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2006 Aug 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 |
+
# 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: auth2.test,v 1.3 2008/07/02 13:13:53 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 |
+
do_test auth2-1.1 {
|
| 29 |
+
execsql {
|
| 30 |
+
CREATE TABLE t1(a,b,c);
|
| 31 |
+
INSERT INTO t1 VALUES(1,2,3);
|
| 32 |
+
}
|
| 33 |
+
set ::flist {}
|
| 34 |
+
proc auth {code arg1 arg2 arg3 arg4 args} {
|
| 35 |
+
if {$code=="SQLITE_FUNCTION"} {
|
| 36 |
+
lappend ::flist $arg2
|
| 37 |
+
if {$arg2=="max"} {
|
| 38 |
+
return SQLITE_DENY
|
| 39 |
+
} elseif {$arg2=="min"} {
|
| 40 |
+
return SQLITE_IGNORE
|
| 41 |
+
} else {
|
| 42 |
+
return SQLITE_OK
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
return SQLITE_OK
|
| 46 |
+
}
|
| 47 |
+
db authorizer ::auth
|
| 48 |
+
catchsql {SELECT max(a,b,c) FROM t1}
|
| 49 |
+
} {1 {not authorized to use function: max}}
|
| 50 |
+
do_test auth2-1.2 {
|
| 51 |
+
set ::flist
|
| 52 |
+
} max
|
| 53 |
+
do_test auth2-1.3 {
|
| 54 |
+
set ::flist {}
|
| 55 |
+
catchsql {SELECT min(a,b,c) FROM t1}
|
| 56 |
+
} {0 {{}}}
|
| 57 |
+
do_test auth2-1.4 {
|
| 58 |
+
set ::flist
|
| 59 |
+
} min
|
| 60 |
+
do_test auth2-1.5 {
|
| 61 |
+
set ::flist {}
|
| 62 |
+
catchsql {SELECT coalesce(min(a,b,c),999) FROM t1}
|
| 63 |
+
} {0 999}
|
| 64 |
+
do_test auth2-1.6 {
|
| 65 |
+
set ::flist
|
| 66 |
+
} {coalesce min}
|
| 67 |
+
do_test auth2-1.7 {
|
| 68 |
+
set ::flist {}
|
| 69 |
+
catchsql {SELECT coalesce(a,b,c) FROM t1}
|
| 70 |
+
} {0 1}
|
| 71 |
+
do_test auth2-1.8 {
|
| 72 |
+
set ::flist
|
| 73 |
+
} coalesce
|
| 74 |
+
|
| 75 |
+
# Make sure the authorizer is not called when parsing the schema
|
| 76 |
+
# and when computing the result set of a view.
|
| 77 |
+
#
|
| 78 |
+
db close
|
| 79 |
+
sqlite3 db test.db
|
| 80 |
+
sqlite3 db2 test.db
|
| 81 |
+
proc auth {args} {
|
| 82 |
+
global authargs
|
| 83 |
+
append authargs [lrange $args 0 4]\n
|
| 84 |
+
return SQLITE_OK
|
| 85 |
+
}
|
| 86 |
+
db auth auth
|
| 87 |
+
do_test auth2-2.1 {
|
| 88 |
+
set ::authargs {}
|
| 89 |
+
db eval {
|
| 90 |
+
CREATE TABLE t2(x,y,z);
|
| 91 |
+
}
|
| 92 |
+
set ::authargs
|
| 93 |
+
} {SQLITE_INSERT sqlite_master {} main {}
|
| 94 |
+
SQLITE_CREATE_TABLE t2 {} main {}
|
| 95 |
+
SQLITE_UPDATE sqlite_master type main {}
|
| 96 |
+
SQLITE_UPDATE sqlite_master name main {}
|
| 97 |
+
SQLITE_UPDATE sqlite_master tbl_name main {}
|
| 98 |
+
SQLITE_UPDATE sqlite_master rootpage main {}
|
| 99 |
+
SQLITE_UPDATE sqlite_master sql main {}
|
| 100 |
+
SQLITE_READ sqlite_master ROWID main {}
|
| 101 |
+
}
|
| 102 |
+
do_test auth2-2.2 {
|
| 103 |
+
set ::authargs {}
|
| 104 |
+
db eval {
|
| 105 |
+
CREATE VIEW v2 AS SELECT x+y AS a, y+z AS b from t2;
|
| 106 |
+
}
|
| 107 |
+
set ::authargs
|
| 108 |
+
} {SQLITE_INSERT sqlite_master {} main {}
|
| 109 |
+
SQLITE_CREATE_VIEW v2 {} main {}
|
| 110 |
+
SQLITE_UPDATE sqlite_master type main {}
|
| 111 |
+
SQLITE_UPDATE sqlite_master name main {}
|
| 112 |
+
SQLITE_UPDATE sqlite_master tbl_name main {}
|
| 113 |
+
SQLITE_UPDATE sqlite_master rootpage main {}
|
| 114 |
+
SQLITE_UPDATE sqlite_master sql main {}
|
| 115 |
+
SQLITE_READ sqlite_master ROWID main {}
|
| 116 |
+
}
|
| 117 |
+
do_test auth2-2.3 {
|
| 118 |
+
set ::authargs {}
|
| 119 |
+
db eval {
|
| 120 |
+
SELECT a, b FROM v2;
|
| 121 |
+
}
|
| 122 |
+
set ::authargs
|
| 123 |
+
} {SQLITE_SELECT {} {} {} {}
|
| 124 |
+
SQLITE_READ t2 x main v2
|
| 125 |
+
SQLITE_READ t2 y main v2
|
| 126 |
+
SQLITE_READ t2 y main v2
|
| 127 |
+
SQLITE_READ t2 z main v2
|
| 128 |
+
SQLITE_READ v2 a main {}
|
| 129 |
+
SQLITE_READ v2 b main {}
|
| 130 |
+
SQLITE_SELECT {} {} {} v2
|
| 131 |
+
}
|
| 132 |
+
do_test auth2-2.4 {
|
| 133 |
+
db2 eval {
|
| 134 |
+
CREATE TABLE t3(p,q,r);
|
| 135 |
+
}
|
| 136 |
+
set ::authargs {}
|
| 137 |
+
db eval {
|
| 138 |
+
SELECT b, a FROM v2;
|
| 139 |
+
}
|
| 140 |
+
set ::authargs
|
| 141 |
+
} {SQLITE_SELECT {} {} {} {}
|
| 142 |
+
SQLITE_READ t2 x main v2
|
| 143 |
+
SQLITE_READ t2 y main v2
|
| 144 |
+
SQLITE_READ t2 y main v2
|
| 145 |
+
SQLITE_READ t2 z main v2
|
| 146 |
+
SQLITE_READ v2 b main {}
|
| 147 |
+
SQLITE_READ v2 a main {}
|
| 148 |
+
SQLITE_SELECT {} {} {} v2
|
| 149 |
+
SQLITE_SELECT {} {} {} {}
|
| 150 |
+
SQLITE_READ t2 x main v2
|
| 151 |
+
SQLITE_READ t2 y main v2
|
| 152 |
+
SQLITE_READ t2 y main v2
|
| 153 |
+
SQLITE_READ t2 z main v2
|
| 154 |
+
SQLITE_READ v2 b main {}
|
| 155 |
+
SQLITE_READ v2 a main {}
|
| 156 |
+
SQLITE_SELECT {} {} {} v2
|
| 157 |
+
}
|
| 158 |
+
db2 close
|
| 159 |
+
|
| 160 |
+
finish_test
|
local-test-sqlite3-delta-02/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-02/afc-sqlite3/test/autoanalyze1.test
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2017-02-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 |
+
# This file implements tests for the logic used to estimate when
|
| 13 |
+
# running ANALYZE would be beneficial.
|
| 14 |
+
#
|
| 15 |
+
# Note that this test uses some hard-coded bitmask values from sqliteInt.h.
|
| 16 |
+
# If any of the following constants changes:
|
| 17 |
+
#
|
| 18 |
+
# define TF_HasStat1 0x0010
|
| 19 |
+
# define TF_StatsUsed 0x0100
|
| 20 |
+
#
|
| 21 |
+
# then some of the magic numbers in test results below might need to be
|
| 22 |
+
# adjusted.
|
| 23 |
+
#
|
| 24 |
+
|
| 25 |
+
set testdir [file dirname $argv0]
|
| 26 |
+
source $testdir/tester.tcl
|
| 27 |
+
|
| 28 |
+
# There is nothing to test if ANALYZE is disable for this build.
|
| 29 |
+
# These tests also use "PRAGMA stats" which are only enabled for
|
| 30 |
+
# debugging builds.
|
| 31 |
+
#
|
| 32 |
+
ifcapable {!debug || !analyze || !vtab} {
|
| 33 |
+
finish_test
|
| 34 |
+
return
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
do_execsql_test autoanalyze1-100 {
|
| 38 |
+
-- Build up a test table with some indexes
|
| 39 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);
|
| 40 |
+
CREATE UNIQUE INDEX t1bc ON t1(b,c);
|
| 41 |
+
CREATE INDEX t1d ON t1(d);
|
| 42 |
+
WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
|
| 43 |
+
INSERT INTO t1(a,b,c,d) SELECT x, x, x, x FROM c;
|
| 44 |
+
-- Verify that the hasStat1 flag is clear on on indexes
|
| 45 |
+
SELECT idx, flgs FROM pragma_stats
|
| 46 |
+
WHERE idx IS NOT NULL
|
| 47 |
+
ORDER BY idx;
|
| 48 |
+
-- Verify that the TF_HasStat1 flag is clear on the table
|
| 49 |
+
SELECT tbl, (flgs & 0x10)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 50 |
+
} {t1bc 0 t1d 0 t1 0}
|
| 51 |
+
|
| 52 |
+
# No use of stat1 recorded so far
|
| 53 |
+
do_execsql_test autoanalyze1-110 {
|
| 54 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 55 |
+
} {0}
|
| 56 |
+
|
| 57 |
+
# Access using a unique index does not set the TF_StatsUsed flag.
|
| 58 |
+
#
|
| 59 |
+
do_execsql_test autoanalyze1-200 {
|
| 60 |
+
SELECT * FROM t1 WHERE a=55;
|
| 61 |
+
} {55 55 55 55}
|
| 62 |
+
do_execsql_test autoanalyze1-201 {
|
| 63 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 64 |
+
} {0}
|
| 65 |
+
|
| 66 |
+
do_execsql_test autoanalyze1-210 {
|
| 67 |
+
SELECT * FROM t1 WHERE a IN (55,199,299);
|
| 68 |
+
} {55 55 55 55}
|
| 69 |
+
do_execsql_test autoanalyze1-211 {
|
| 70 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 71 |
+
} {0}
|
| 72 |
+
|
| 73 |
+
do_execsql_test autoanalyze1-220 {
|
| 74 |
+
SELECT * FROM t1 WHERE (b,c)=(45,45);
|
| 75 |
+
} {45 45 45 45}
|
| 76 |
+
do_execsql_test autoanalyze1-221 {
|
| 77 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 78 |
+
} {0}
|
| 79 |
+
|
| 80 |
+
# Any use of the non-unique t1d index triggers the use of stats.
|
| 81 |
+
#
|
| 82 |
+
sqlite3 db test.db
|
| 83 |
+
do_execsql_test autoanalyze1-300 {
|
| 84 |
+
SELECT * FROM t1 WHERE d=45;
|
| 85 |
+
} {45 45 45 45}
|
| 86 |
+
do_execsql_test autoanalyze1-301 {
|
| 87 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 88 |
+
} {1}
|
| 89 |
+
|
| 90 |
+
sqlite3 db test.db
|
| 91 |
+
do_execsql_test autoanalyze1-310 {
|
| 92 |
+
SELECT * FROM t1 WHERE d=45 AND a=45;
|
| 93 |
+
} {45 45 45 45}
|
| 94 |
+
do_execsql_test autoanalyze1-311 {
|
| 95 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 96 |
+
} {0} ;# The ROWID lookup short-circuits the d=45 constraint
|
| 97 |
+
|
| 98 |
+
sqlite3 db test.db
|
| 99 |
+
do_execsql_test autoanalyze1-320 {
|
| 100 |
+
SELECT * FROM t1 WHERE d=45 AND a IN (45,46);
|
| 101 |
+
} {45 45 45 45}
|
| 102 |
+
do_execsql_test autoanalyze1-321 {
|
| 103 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 104 |
+
} {1}
|
| 105 |
+
|
| 106 |
+
# Any use of prefix of a unique index triggers the use of stats
|
| 107 |
+
#
|
| 108 |
+
sqlite3 db test.db
|
| 109 |
+
do_execsql_test autoanalyze1-400 {
|
| 110 |
+
SELECT * FROM t1 WHERE b=45;
|
| 111 |
+
} {45 45 45 45}
|
| 112 |
+
do_execsql_test autoanalyze1-401 {
|
| 113 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 114 |
+
} {1}
|
| 115 |
+
|
| 116 |
+
# The TF_StatsUsed flag is reset when the database is reopened
|
| 117 |
+
#
|
| 118 |
+
sqlite3 db test.db
|
| 119 |
+
do_execsql_test autoanalyze1-500 {
|
| 120 |
+
SELECT (flgs & 0x0100)!=0 FROM pragma_stats WHERE tbl='t1' AND idx IS NULL;
|
| 121 |
+
} {0}
|
| 122 |
+
|
| 123 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/autoinc.test
ADDED
|
@@ -0,0 +1,883 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2004 November 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 script is testing the AUTOINCREMENT features.
|
| 13 |
+
#
|
| 14 |
+
# $Id: autoinc.test,v 1.14 2009/06/23 20:28:54 drh Exp $
|
| 15 |
+
#
|
| 16 |
+
|
| 17 |
+
set testdir [file dirname $argv0]
|
| 18 |
+
source $testdir/tester.tcl
|
| 19 |
+
set testprefix autoinc
|
| 20 |
+
|
| 21 |
+
# If the library is not compiled with autoincrement support then
|
| 22 |
+
# skip all tests in this file.
|
| 23 |
+
#
|
| 24 |
+
ifcapable {!autoinc} {
|
| 25 |
+
finish_test
|
| 26 |
+
return
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
if {[permutation]=="inmemory_journal"} {
|
| 30 |
+
finish_test
|
| 31 |
+
return
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
sqlite3_db_config_lookaside db 0 0 0
|
| 35 |
+
|
| 36 |
+
# The database is initially empty.
|
| 37 |
+
#
|
| 38 |
+
do_test autoinc-1.1 {
|
| 39 |
+
execsql {
|
| 40 |
+
SELECT name FROM sqlite_master WHERE type='table';
|
| 41 |
+
}
|
| 42 |
+
} {}
|
| 43 |
+
|
| 44 |
+
# Add a table with the AUTOINCREMENT feature. Verify that the
|
| 45 |
+
# SQLITE_SEQUENCE table gets created.
|
| 46 |
+
#
|
| 47 |
+
do_test autoinc-1.2 {
|
| 48 |
+
execsql {
|
| 49 |
+
CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
|
| 50 |
+
SELECT name FROM sqlite_master WHERE type='table';
|
| 51 |
+
}
|
| 52 |
+
} {t1 sqlite_sequence}
|
| 53 |
+
|
| 54 |
+
# The SQLITE_SEQUENCE table is initially empty
|
| 55 |
+
#
|
| 56 |
+
do_test autoinc-1.3 {
|
| 57 |
+
execsql {
|
| 58 |
+
SELECT * FROM sqlite_sequence;
|
| 59 |
+
}
|
| 60 |
+
} {}
|
| 61 |
+
do_test autoinc-1.3.1 {
|
| 62 |
+
catchsql {
|
| 63 |
+
CREATE INDEX seqidx ON sqlite_sequence(name)
|
| 64 |
+
}
|
| 65 |
+
} {1 {table sqlite_sequence may not be indexed}}
|
| 66 |
+
|
| 67 |
+
# Close and reopen the database. Verify that everything is still there.
|
| 68 |
+
#
|
| 69 |
+
do_test autoinc-1.4 {
|
| 70 |
+
db close
|
| 71 |
+
sqlite3 db test.db
|
| 72 |
+
execsql {
|
| 73 |
+
SELECT * FROM sqlite_sequence;
|
| 74 |
+
}
|
| 75 |
+
} {}
|
| 76 |
+
|
| 77 |
+
# We are not allowed to drop the sqlite_sequence table.
|
| 78 |
+
#
|
| 79 |
+
do_test autoinc-1.5 {
|
| 80 |
+
catchsql {DROP TABLE sqlite_sequence}
|
| 81 |
+
} {1 {table sqlite_sequence may not be dropped}}
|
| 82 |
+
do_test autoinc-1.6 {
|
| 83 |
+
execsql {SELECT name FROM sqlite_master WHERE type='table'}
|
| 84 |
+
} {t1 sqlite_sequence}
|
| 85 |
+
|
| 86 |
+
# Insert an entries into the t1 table and make sure the largest key
|
| 87 |
+
# is always recorded in the sqlite_sequence table.
|
| 88 |
+
#
|
| 89 |
+
do_test autoinc-2.1 {
|
| 90 |
+
execsql {
|
| 91 |
+
SELECT * FROM sqlite_sequence
|
| 92 |
+
}
|
| 93 |
+
} {}
|
| 94 |
+
do_test autoinc-2.2 {
|
| 95 |
+
execsql {
|
| 96 |
+
INSERT INTO t1 VALUES(12,34);
|
| 97 |
+
SELECT * FROM sqlite_sequence;
|
| 98 |
+
}
|
| 99 |
+
} {t1 12}
|
| 100 |
+
do_test autoinc-2.3 {
|
| 101 |
+
execsql {
|
| 102 |
+
INSERT INTO t1 VALUES(1,23);
|
| 103 |
+
SELECT * FROM sqlite_sequence;
|
| 104 |
+
}
|
| 105 |
+
} {t1 12}
|
| 106 |
+
do_test autoinc-2.4 {
|
| 107 |
+
execsql {
|
| 108 |
+
INSERT INTO t1 VALUES(123,456);
|
| 109 |
+
SELECT * FROM sqlite_sequence;
|
| 110 |
+
}
|
| 111 |
+
} {t1 123}
|
| 112 |
+
do_test autoinc-2.5 {
|
| 113 |
+
execsql {
|
| 114 |
+
INSERT INTO t1 VALUES(NULL,567);
|
| 115 |
+
SELECT * FROM sqlite_sequence;
|
| 116 |
+
}
|
| 117 |
+
} {t1 124}
|
| 118 |
+
do_test autoinc-2.6 {
|
| 119 |
+
execsql {
|
| 120 |
+
DELETE FROM t1 WHERE y=567;
|
| 121 |
+
SELECT * FROM sqlite_sequence;
|
| 122 |
+
}
|
| 123 |
+
} {t1 124}
|
| 124 |
+
do_test autoinc-2.7 {
|
| 125 |
+
execsql {
|
| 126 |
+
INSERT INTO t1 VALUES(NULL,567);
|
| 127 |
+
SELECT * FROM sqlite_sequence;
|
| 128 |
+
}
|
| 129 |
+
} {t1 125}
|
| 130 |
+
do_test autoinc-2.8 {
|
| 131 |
+
execsql {
|
| 132 |
+
DELETE FROM t1;
|
| 133 |
+
SELECT * FROM sqlite_sequence;
|
| 134 |
+
}
|
| 135 |
+
} {t1 125}
|
| 136 |
+
do_test autoinc-2.9 {
|
| 137 |
+
execsql {
|
| 138 |
+
INSERT INTO t1 VALUES(12,34);
|
| 139 |
+
SELECT * FROM sqlite_sequence;
|
| 140 |
+
}
|
| 141 |
+
} {t1 125}
|
| 142 |
+
do_test autoinc-2.10 {
|
| 143 |
+
execsql {
|
| 144 |
+
INSERT INTO t1 VALUES(125,456);
|
| 145 |
+
SELECT * FROM sqlite_sequence;
|
| 146 |
+
}
|
| 147 |
+
} {t1 125}
|
| 148 |
+
do_test autoinc-2.11 {
|
| 149 |
+
execsql {
|
| 150 |
+
INSERT INTO t1 VALUES(-1234567,-1);
|
| 151 |
+
SELECT * FROM sqlite_sequence;
|
| 152 |
+
}
|
| 153 |
+
} {t1 125}
|
| 154 |
+
do_test autoinc-2.12 {
|
| 155 |
+
execsql {
|
| 156 |
+
INSERT INTO t1 VALUES(234,5678);
|
| 157 |
+
SELECT * FROM sqlite_sequence;
|
| 158 |
+
}
|
| 159 |
+
} {t1 234}
|
| 160 |
+
do_test autoinc-2.13 {
|
| 161 |
+
execsql {
|
| 162 |
+
DELETE FROM t1;
|
| 163 |
+
INSERT INTO t1 VALUES(NULL,1);
|
| 164 |
+
SELECT * FROM sqlite_sequence;
|
| 165 |
+
}
|
| 166 |
+
} {t1 235}
|
| 167 |
+
do_test autoinc-2.14 {
|
| 168 |
+
execsql {
|
| 169 |
+
SELECT * FROM t1;
|
| 170 |
+
}
|
| 171 |
+
} {235 1}
|
| 172 |
+
|
| 173 |
+
# Manually change the autoincrement values in sqlite_sequence.
|
| 174 |
+
#
|
| 175 |
+
do_test autoinc-2.20 {
|
| 176 |
+
execsql {
|
| 177 |
+
UPDATE sqlite_sequence SET seq=1234 WHERE name='t1';
|
| 178 |
+
INSERT INTO t1 VALUES(NULL,2);
|
| 179 |
+
SELECT * FROM t1;
|
| 180 |
+
}
|
| 181 |
+
} {235 1 1235 2}
|
| 182 |
+
do_test autoinc-2.21 {
|
| 183 |
+
execsql {
|
| 184 |
+
SELECT * FROM sqlite_sequence;
|
| 185 |
+
}
|
| 186 |
+
} {t1 1235}
|
| 187 |
+
do_test autoinc-2.22 {
|
| 188 |
+
execsql {
|
| 189 |
+
UPDATE sqlite_sequence SET seq=NULL WHERE name='t1';
|
| 190 |
+
INSERT INTO t1 VALUES(NULL,3);
|
| 191 |
+
SELECT * FROM t1;
|
| 192 |
+
}
|
| 193 |
+
} {235 1 1235 2 1236 3}
|
| 194 |
+
do_test autoinc-2.23 {
|
| 195 |
+
execsql {
|
| 196 |
+
SELECT * FROM sqlite_sequence;
|
| 197 |
+
}
|
| 198 |
+
} {t1 1236}
|
| 199 |
+
do_test autoinc-2.24 {
|
| 200 |
+
execsql {
|
| 201 |
+
UPDATE sqlite_sequence SET seq='a-string' WHERE name='t1';
|
| 202 |
+
INSERT INTO t1 VALUES(NULL,4);
|
| 203 |
+
SELECT * FROM t1;
|
| 204 |
+
}
|
| 205 |
+
} {235 1 1235 2 1236 3 1237 4}
|
| 206 |
+
do_test autoinc-2.25 {
|
| 207 |
+
execsql {
|
| 208 |
+
SELECT * FROM sqlite_sequence;
|
| 209 |
+
}
|
| 210 |
+
} {t1 1237}
|
| 211 |
+
do_test autoinc-2.26 {
|
| 212 |
+
execsql {
|
| 213 |
+
DELETE FROM sqlite_sequence WHERE name='t1';
|
| 214 |
+
INSERT INTO t1 VALUES(NULL,5);
|
| 215 |
+
SELECT * FROM t1;
|
| 216 |
+
}
|
| 217 |
+
} {235 1 1235 2 1236 3 1237 4 1238 5}
|
| 218 |
+
do_test autoinc-2.27 {
|
| 219 |
+
execsql {
|
| 220 |
+
SELECT * FROM sqlite_sequence;
|
| 221 |
+
}
|
| 222 |
+
} {t1 1238}
|
| 223 |
+
do_test autoinc-2.28 {
|
| 224 |
+
execsql {
|
| 225 |
+
UPDATE sqlite_sequence SET seq='-12345678901234567890'
|
| 226 |
+
WHERE name='t1';
|
| 227 |
+
INSERT INTO t1 VALUES(NULL,6);
|
| 228 |
+
SELECT * FROM t1;
|
| 229 |
+
}
|
| 230 |
+
} {235 1 1235 2 1236 3 1237 4 1238 5 1239 6}
|
| 231 |
+
do_test autoinc-2.29 {
|
| 232 |
+
execsql {
|
| 233 |
+
SELECT * FROM sqlite_sequence;
|
| 234 |
+
}
|
| 235 |
+
} {t1 1239}
|
| 236 |
+
|
| 237 |
+
# Test multi-row inserts
|
| 238 |
+
#
|
| 239 |
+
do_test autoinc-2.50 {
|
| 240 |
+
execsql {
|
| 241 |
+
DELETE FROM t1 WHERE y>=3;
|
| 242 |
+
INSERT INTO t1 SELECT NULL, y+2 FROM t1;
|
| 243 |
+
SELECT * FROM t1;
|
| 244 |
+
}
|
| 245 |
+
} {235 1 1235 2 1240 3 1241 4}
|
| 246 |
+
do_test autoinc-2.51 {
|
| 247 |
+
execsql {
|
| 248 |
+
SELECT * FROM sqlite_sequence
|
| 249 |
+
}
|
| 250 |
+
} {t1 1241}
|
| 251 |
+
|
| 252 |
+
ifcapable tempdb {
|
| 253 |
+
do_test autoinc-2.52 {
|
| 254 |
+
execsql {
|
| 255 |
+
CREATE TEMP TABLE t2 AS SELECT y FROM t1;
|
| 256 |
+
}
|
| 257 |
+
execsql {
|
| 258 |
+
INSERT INTO t1 SELECT NULL, y+4 FROM t2;
|
| 259 |
+
SELECT * FROM t1;
|
| 260 |
+
}
|
| 261 |
+
} {235 1 1235 2 1240 3 1241 4 1242 5 1243 6 1244 7 1245 8}
|
| 262 |
+
do_test autoinc-2.53 {
|
| 263 |
+
execsql {
|
| 264 |
+
SELECT * FROM sqlite_sequence
|
| 265 |
+
}
|
| 266 |
+
} {t1 1245}
|
| 267 |
+
do_test autoinc-2.54 {
|
| 268 |
+
execsql {
|
| 269 |
+
DELETE FROM t1;
|
| 270 |
+
INSERT INTO t1 SELECT NULL, y FROM t2;
|
| 271 |
+
SELECT * FROM t1;
|
| 272 |
+
}
|
| 273 |
+
} {1246 1 1247 2 1248 3 1249 4}
|
| 274 |
+
do_test autoinc-2.55 {
|
| 275 |
+
execsql {
|
| 276 |
+
SELECT * FROM sqlite_sequence
|
| 277 |
+
}
|
| 278 |
+
} {t1 1249}
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
# Create multiple AUTOINCREMENT tables. Make sure all sequences are
|
| 282 |
+
# tracked separately and do not interfere with one another.
|
| 283 |
+
#
|
| 284 |
+
do_test autoinc-2.70 {
|
| 285 |
+
catchsql {
|
| 286 |
+
DROP TABLE t2;
|
| 287 |
+
}
|
| 288 |
+
execsql {
|
| 289 |
+
CREATE TABLE t2(d, e INTEGER PRIMARY KEY AUTOINCREMENT, f);
|
| 290 |
+
INSERT INTO t2(d) VALUES(1);
|
| 291 |
+
SELECT * FROM sqlite_sequence;
|
| 292 |
+
}
|
| 293 |
+
} [ifcapable tempdb {list t1 1249 t2 1} else {list t1 1241 t2 1}]
|
| 294 |
+
do_test autoinc-2.71 {
|
| 295 |
+
execsql {
|
| 296 |
+
INSERT INTO t2(d) VALUES(2);
|
| 297 |
+
SELECT * FROM sqlite_sequence;
|
| 298 |
+
}
|
| 299 |
+
} [ifcapable tempdb {list t1 1249 t2 2} else {list t1 1241 t2 2}]
|
| 300 |
+
do_test autoinc-2.72 {
|
| 301 |
+
execsql {
|
| 302 |
+
INSERT INTO t1(x) VALUES(10000);
|
| 303 |
+
SELECT * FROM sqlite_sequence;
|
| 304 |
+
}
|
| 305 |
+
} {t1 10000 t2 2}
|
| 306 |
+
do_test autoinc-2.73 {
|
| 307 |
+
execsql {
|
| 308 |
+
CREATE TABLE t3(g INTEGER PRIMARY KEY AUTOINCREMENT, h);
|
| 309 |
+
INSERT INTO t3(h) VALUES(1);
|
| 310 |
+
SELECT * FROM sqlite_sequence;
|
| 311 |
+
}
|
| 312 |
+
} {t1 10000 t2 2 t3 1}
|
| 313 |
+
do_test autoinc-2.74 {
|
| 314 |
+
execsql {
|
| 315 |
+
INSERT INTO t2(d,e) VALUES(3,100);
|
| 316 |
+
SELECT * FROM sqlite_sequence;
|
| 317 |
+
}
|
| 318 |
+
} {t1 10000 t2 100 t3 1}
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
# When a table with an AUTOINCREMENT is deleted, the corresponding entry
|
| 322 |
+
# in the SQLITE_SEQUENCE table should also be deleted. But the SQLITE_SEQUENCE
|
| 323 |
+
# table itself should remain behind.
|
| 324 |
+
#
|
| 325 |
+
do_test autoinc-3.1 {
|
| 326 |
+
execsql {SELECT name FROM sqlite_sequence}
|
| 327 |
+
} {t1 t2 t3}
|
| 328 |
+
do_test autoinc-3.2 {
|
| 329 |
+
execsql {
|
| 330 |
+
DROP TABLE t1;
|
| 331 |
+
SELECT name FROM sqlite_sequence;
|
| 332 |
+
}
|
| 333 |
+
} {t2 t3}
|
| 334 |
+
do_test autoinc-3.3 {
|
| 335 |
+
execsql {
|
| 336 |
+
DROP TABLE t3;
|
| 337 |
+
SELECT name FROM sqlite_sequence;
|
| 338 |
+
}
|
| 339 |
+
} {t2}
|
| 340 |
+
do_test autoinc-3.4 {
|
| 341 |
+
execsql {
|
| 342 |
+
DROP TABLE t2;
|
| 343 |
+
SELECT name FROM sqlite_sequence;
|
| 344 |
+
}
|
| 345 |
+
} {}
|
| 346 |
+
|
| 347 |
+
# AUTOINCREMENT on TEMP tables.
|
| 348 |
+
#
|
| 349 |
+
ifcapable tempdb {
|
| 350 |
+
do_test autoinc-4.1 {
|
| 351 |
+
execsql {
|
| 352 |
+
SELECT 1, name FROM sqlite_master WHERE type='table';
|
| 353 |
+
SELECT 2, name FROM temp.sqlite_master WHERE type='table';
|
| 354 |
+
}
|
| 355 |
+
} {1 sqlite_sequence}
|
| 356 |
+
do_test autoinc-4.2 {
|
| 357 |
+
execsql {
|
| 358 |
+
CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
|
| 359 |
+
CREATE TEMP TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
|
| 360 |
+
SELECT 1, name FROM sqlite_master WHERE type='table';
|
| 361 |
+
SELECT 2, name FROM sqlite_temp_master WHERE type='table';
|
| 362 |
+
}
|
| 363 |
+
} {1 sqlite_sequence 1 t1 2 t3 2 sqlite_sequence}
|
| 364 |
+
do_test autoinc-4.3 {
|
| 365 |
+
execsql {
|
| 366 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 367 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 368 |
+
}
|
| 369 |
+
} {}
|
| 370 |
+
do_test autoinc-4.4 {
|
| 371 |
+
execsql {
|
| 372 |
+
INSERT INTO t1 VALUES(10,1);
|
| 373 |
+
INSERT INTO t3 VALUES(20,2);
|
| 374 |
+
INSERT INTO t1 VALUES(NULL,3);
|
| 375 |
+
INSERT INTO t3 VALUES(NULL,4);
|
| 376 |
+
}
|
| 377 |
+
} {}
|
| 378 |
+
|
| 379 |
+
ifcapable compound {
|
| 380 |
+
do_test autoinc-4.4.1 {
|
| 381 |
+
execsql {
|
| 382 |
+
SELECT * FROM t1 UNION ALL SELECT * FROM t3;
|
| 383 |
+
}
|
| 384 |
+
} {10 1 11 3 20 2 21 4}
|
| 385 |
+
} ;# ifcapable compound
|
| 386 |
+
|
| 387 |
+
do_test autoinc-4.5 {
|
| 388 |
+
execsql {
|
| 389 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 390 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 391 |
+
}
|
| 392 |
+
} {1 t1 11 2 t3 21}
|
| 393 |
+
do_test autoinc-4.6 {
|
| 394 |
+
execsql {
|
| 395 |
+
INSERT INTO t1 SELECT * FROM t3;
|
| 396 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 397 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 398 |
+
}
|
| 399 |
+
} {1 t1 21 2 t3 21}
|
| 400 |
+
do_test autoinc-4.7 {
|
| 401 |
+
execsql {
|
| 402 |
+
INSERT INTO t3 SELECT x+100, y FROM t1;
|
| 403 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 404 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 405 |
+
}
|
| 406 |
+
} {1 t1 21 2 t3 121}
|
| 407 |
+
do_test autoinc-4.8 {
|
| 408 |
+
execsql {
|
| 409 |
+
DROP TABLE t3;
|
| 410 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 411 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 412 |
+
}
|
| 413 |
+
} {1 t1 21}
|
| 414 |
+
do_test autoinc-4.9 {
|
| 415 |
+
execsql {
|
| 416 |
+
CREATE TEMP TABLE t2(p INTEGER PRIMARY KEY AUTOINCREMENT, q);
|
| 417 |
+
INSERT INTO t2 SELECT * FROM t1;
|
| 418 |
+
DROP TABLE t1;
|
| 419 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 420 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 421 |
+
}
|
| 422 |
+
} {2 t2 21}
|
| 423 |
+
do_test autoinc-4.10 {
|
| 424 |
+
execsql {
|
| 425 |
+
DROP TABLE t2;
|
| 426 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 427 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 428 |
+
}
|
| 429 |
+
} {}
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
# Make sure AUTOINCREMENT works on ATTACH-ed tables.
|
| 433 |
+
#
|
| 434 |
+
ifcapable tempdb&&attach {
|
| 435 |
+
do_test autoinc-5.1 {
|
| 436 |
+
forcedelete test2.db
|
| 437 |
+
forcedelete test2.db-journal
|
| 438 |
+
sqlite3 db2 test2.db
|
| 439 |
+
execsql {
|
| 440 |
+
CREATE TABLE t4(m INTEGER PRIMARY KEY AUTOINCREMENT, n);
|
| 441 |
+
CREATE TABLE t5(o, p INTEGER PRIMARY KEY AUTOINCREMENT);
|
| 442 |
+
} db2;
|
| 443 |
+
execsql {
|
| 444 |
+
ATTACH 'test2.db' as aux;
|
| 445 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 446 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 447 |
+
SELECT 3, * FROM aux.sqlite_sequence;
|
| 448 |
+
}
|
| 449 |
+
} {}
|
| 450 |
+
do_test autoinc-5.2 {
|
| 451 |
+
execsql {
|
| 452 |
+
INSERT INTO t4 VALUES(NULL,1);
|
| 453 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 454 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 455 |
+
SELECT 3, * FROM aux.sqlite_sequence;
|
| 456 |
+
}
|
| 457 |
+
} {3 t4 1}
|
| 458 |
+
do_test autoinc-5.3 {
|
| 459 |
+
execsql {
|
| 460 |
+
INSERT INTO t5 VALUES(100,200);
|
| 461 |
+
SELECT * FROM sqlite_sequence
|
| 462 |
+
} db2
|
| 463 |
+
} {t4 1 t5 200}
|
| 464 |
+
do_test autoinc-5.4 {
|
| 465 |
+
execsql {
|
| 466 |
+
SELECT 1, * FROM main.sqlite_sequence;
|
| 467 |
+
SELECT 2, * FROM temp.sqlite_sequence;
|
| 468 |
+
SELECT 3, * FROM aux.sqlite_sequence;
|
| 469 |
+
}
|
| 470 |
+
} {3 t4 1 3 t5 200}
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
# Requirement REQ00310: Make sure an insert fails if the sequence is
|
| 474 |
+
# already at its maximum value.
|
| 475 |
+
#
|
| 476 |
+
ifcapable {rowid32} {
|
| 477 |
+
do_test autoinc-6.1 {
|
| 478 |
+
execsql {
|
| 479 |
+
CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
|
| 480 |
+
INSERT INTO t6 VALUES(2147483647,1);
|
| 481 |
+
SELECT seq FROM main.sqlite_sequence WHERE name='t6';
|
| 482 |
+
}
|
| 483 |
+
} 2147483647
|
| 484 |
+
}
|
| 485 |
+
ifcapable {!rowid32} {
|
| 486 |
+
do_test autoinc-6.1 {
|
| 487 |
+
execsql {
|
| 488 |
+
CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
|
| 489 |
+
INSERT INTO t6 VALUES(9223372036854775807,1);
|
| 490 |
+
SELECT seq FROM main.sqlite_sequence WHERE name='t6';
|
| 491 |
+
}
|
| 492 |
+
} 9223372036854775807
|
| 493 |
+
}
|
| 494 |
+
do_test autoinc-6.2 {
|
| 495 |
+
catchsql {
|
| 496 |
+
INSERT INTO t6 VALUES(NULL,1);
|
| 497 |
+
}
|
| 498 |
+
} {1 {database or disk is full}}
|
| 499 |
+
|
| 500 |
+
# Allow the AUTOINCREMENT keyword inside the parentheses
|
| 501 |
+
# on a separate PRIMARY KEY designation.
|
| 502 |
+
#
|
| 503 |
+
do_test autoinc-7.1 {
|
| 504 |
+
execsql {
|
| 505 |
+
CREATE TABLE t7(x INTEGER, y REAL, PRIMARY KEY(x AUTOINCREMENT));
|
| 506 |
+
INSERT INTO t7(y) VALUES(123);
|
| 507 |
+
INSERT INTO t7(y) VALUES(234);
|
| 508 |
+
DELETE FROM t7;
|
| 509 |
+
INSERT INTO t7(y) VALUES(345);
|
| 510 |
+
SELECT * FROM t7;
|
| 511 |
+
}
|
| 512 |
+
} {3 345.0}
|
| 513 |
+
|
| 514 |
+
# Test that if the AUTOINCREMENT is applied to a non integer primary key
|
| 515 |
+
# the error message is sensible.
|
| 516 |
+
do_test autoinc-7.2 {
|
| 517 |
+
catchsql {
|
| 518 |
+
CREATE TABLE t8(x TEXT PRIMARY KEY AUTOINCREMENT);
|
| 519 |
+
}
|
| 520 |
+
} {1 {AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY}}
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
# Ticket #1283. Make sure that preparing but never running a statement
|
| 524 |
+
# that creates the sqlite_sequence table does not mess up the database.
|
| 525 |
+
#
|
| 526 |
+
do_test autoinc-8.1 {
|
| 527 |
+
catch {db2 close}
|
| 528 |
+
catch {db close}
|
| 529 |
+
forcedelete test.db
|
| 530 |
+
sqlite3 db test.db
|
| 531 |
+
set DB [sqlite3_connection_pointer db]
|
| 532 |
+
set STMT [sqlite3_prepare $DB {
|
| 533 |
+
CREATE TABLE t1(
|
| 534 |
+
x INTEGER PRIMARY KEY AUTOINCREMENT
|
| 535 |
+
)
|
| 536 |
+
} -1 TAIL]
|
| 537 |
+
sqlite3_finalize $STMT
|
| 538 |
+
set STMT [sqlite3_prepare $DB {
|
| 539 |
+
CREATE TABLE t1(
|
| 540 |
+
x INTEGER PRIMARY KEY AUTOINCREMENT
|
| 541 |
+
)
|
| 542 |
+
} -1 TAIL]
|
| 543 |
+
sqlite3_step $STMT
|
| 544 |
+
sqlite3_finalize $STMT
|
| 545 |
+
execsql {
|
| 546 |
+
INSERT INTO t1 VALUES(NULL);
|
| 547 |
+
SELECT * FROM t1;
|
| 548 |
+
}
|
| 549 |
+
} {1}
|
| 550 |
+
|
| 551 |
+
# Ticket #3148
|
| 552 |
+
# Make sure the sqlite_sequence table is not damaged when doing
|
| 553 |
+
# an empty insert - an INSERT INTO ... SELECT ... where the SELECT
|
| 554 |
+
# clause returns an empty set.
|
| 555 |
+
#
|
| 556 |
+
do_test autoinc-9.1 {
|
| 557 |
+
db eval {
|
| 558 |
+
CREATE TABLE t2(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
|
| 559 |
+
INSERT INTO t2 VALUES(NULL, 1);
|
| 560 |
+
CREATE TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
|
| 561 |
+
INSERT INTO t3 SELECT * FROM t2 WHERE y>1;
|
| 562 |
+
|
| 563 |
+
SELECT * FROM sqlite_sequence WHERE name='t3';
|
| 564 |
+
}
|
| 565 |
+
} {t3 0}
|
| 566 |
+
|
| 567 |
+
ifcapable trigger {
|
| 568 |
+
catchsql { pragma recursive_triggers = off }
|
| 569 |
+
|
| 570 |
+
# Ticket #3928. Make sure that triggers to not make extra slots in
|
| 571 |
+
# the SQLITE_SEQUENCE table.
|
| 572 |
+
#
|
| 573 |
+
do_test autoinc-3928.1 {
|
| 574 |
+
db eval {
|
| 575 |
+
CREATE TABLE t3928(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
|
| 576 |
+
CREATE TRIGGER t3928r1 BEFORE INSERT ON t3928 BEGIN
|
| 577 |
+
INSERT INTO t3928(b) VALUES('before1');
|
| 578 |
+
INSERT INTO t3928(b) VALUES('before2');
|
| 579 |
+
END;
|
| 580 |
+
CREATE TRIGGER t3928r2 AFTER INSERT ON t3928 BEGIN
|
| 581 |
+
INSERT INTO t3928(b) VALUES('after1');
|
| 582 |
+
INSERT INTO t3928(b) VALUES('after2');
|
| 583 |
+
END;
|
| 584 |
+
INSERT INTO t3928(b) VALUES('test');
|
| 585 |
+
SELECT * FROM t3928 ORDER BY a;
|
| 586 |
+
}
|
| 587 |
+
} {1 before1 2 after1 3 after2 4 before2 5 after1 6 after2 7 test 8 before1 9 before2 10 after1 11 before1 12 before2 13 after2}
|
| 588 |
+
do_test autoinc-3928.2 {
|
| 589 |
+
db eval {
|
| 590 |
+
SELECT * FROM sqlite_sequence WHERE name='t3928'
|
| 591 |
+
}
|
| 592 |
+
} {t3928 13}
|
| 593 |
+
|
| 594 |
+
do_test autoinc-3928.3 {
|
| 595 |
+
db eval {
|
| 596 |
+
DROP TRIGGER t3928r1;
|
| 597 |
+
DROP TRIGGER t3928r2;
|
| 598 |
+
CREATE TRIGGER t3928r3 BEFORE UPDATE ON t3928
|
| 599 |
+
WHEN typeof(new.b)=='integer' BEGIN
|
| 600 |
+
INSERT INTO t3928(b) VALUES('before-int-' || new.b);
|
| 601 |
+
END;
|
| 602 |
+
CREATE TRIGGER t3928r4 AFTER UPDATE ON t3928
|
| 603 |
+
WHEN typeof(new.b)=='integer' BEGIN
|
| 604 |
+
INSERT INTO t3928(b) VALUES('after-int-' || new.b);
|
| 605 |
+
END;
|
| 606 |
+
DELETE FROM t3928 WHERE a!=1;
|
| 607 |
+
UPDATE t3928 SET b=456 WHERE a=1;
|
| 608 |
+
SELECT * FROM t3928 ORDER BY a;
|
| 609 |
+
}
|
| 610 |
+
} {1 456 14 before-int-456 15 after-int-456}
|
| 611 |
+
do_test autoinc-3928.4 {
|
| 612 |
+
db eval {
|
| 613 |
+
SELECT * FROM sqlite_sequence WHERE name='t3928'
|
| 614 |
+
}
|
| 615 |
+
} {t3928 15}
|
| 616 |
+
|
| 617 |
+
do_test autoinc-3928.5 {
|
| 618 |
+
db eval {
|
| 619 |
+
CREATE TABLE t3928b(x);
|
| 620 |
+
INSERT INTO t3928b VALUES(100);
|
| 621 |
+
INSERT INTO t3928b VALUES(200);
|
| 622 |
+
INSERT INTO t3928b VALUES(300);
|
| 623 |
+
DELETE FROM t3928;
|
| 624 |
+
CREATE TABLE t3928c(y INTEGER PRIMARY KEY AUTOINCREMENT, z);
|
| 625 |
+
CREATE TRIGGER t3928br1 BEFORE DELETE ON t3928b BEGIN
|
| 626 |
+
INSERT INTO t3928(b) VALUES('before-del-'||old.x);
|
| 627 |
+
INSERT INTO t3928c(z) VALUES('before-del-'||old.x);
|
| 628 |
+
END;
|
| 629 |
+
CREATE TRIGGER t3928br2 AFTER DELETE ON t3928b BEGIN
|
| 630 |
+
INSERT INTO t3928(b) VALUES('after-del-'||old.x);
|
| 631 |
+
INSERT INTO t3928c(z) VALUES('after-del-'||old.x);
|
| 632 |
+
END;
|
| 633 |
+
DELETE FROM t3928b;
|
| 634 |
+
SELECT * FROM t3928 ORDER BY a;
|
| 635 |
+
}
|
| 636 |
+
} {16 before-del-100 17 after-del-100 18 before-del-200 19 after-del-200 20 before-del-300 21 after-del-300}
|
| 637 |
+
do_test autoinc-3928.6 {
|
| 638 |
+
db eval {
|
| 639 |
+
SELECT * FROM t3928c ORDER BY y;
|
| 640 |
+
}
|
| 641 |
+
} {1 before-del-100 2 after-del-100 3 before-del-200 4 after-del-200 5 before-del-300 6 after-del-300}
|
| 642 |
+
do_test autoinc-3928.7 {
|
| 643 |
+
db eval {
|
| 644 |
+
SELECT * FROM sqlite_sequence WHERE name LIKE 't3928%' ORDER BY name;
|
| 645 |
+
}
|
| 646 |
+
} {t3928 21 t3928c 6}
|
| 647 |
+
|
| 648 |
+
# Ticket [a696379c1f0886615541a48b35bd8181a80e88f8]
|
| 649 |
+
do_test autoinc-a69637.1 {
|
| 650 |
+
db eval {
|
| 651 |
+
CREATE TABLE ta69637_1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
|
| 652 |
+
CREATE TABLE ta69637_2(z);
|
| 653 |
+
CREATE TRIGGER ra69637_1 AFTER INSERT ON ta69637_2 BEGIN
|
| 654 |
+
INSERT INTO ta69637_1(y) VALUES(new.z+1);
|
| 655 |
+
END;
|
| 656 |
+
INSERT INTO ta69637_2 VALUES(123);
|
| 657 |
+
SELECT * FROM ta69637_1;
|
| 658 |
+
}
|
| 659 |
+
} {1 124}
|
| 660 |
+
do_test autoinc-a69637.2 {
|
| 661 |
+
db eval {
|
| 662 |
+
CREATE VIEW va69637_2 AS SELECT * FROM ta69637_2;
|
| 663 |
+
CREATE TRIGGER ra69637_2 INSTEAD OF INSERT ON va69637_2 BEGIN
|
| 664 |
+
INSERT INTO ta69637_1(y) VALUES(new.z+10000);
|
| 665 |
+
END;
|
| 666 |
+
INSERT INTO va69637_2 VALUES(123);
|
| 667 |
+
SELECT * FROM ta69637_1;
|
| 668 |
+
}
|
| 669 |
+
} {1 124 2 10123}
|
| 670 |
+
}
|
| 671 |
+
|
| 672 |
+
# 2016-10-03 ticket https://www.sqlite.org/src/tktview/7b3328086a5c1
|
| 673 |
+
# Make sure autoincrement plays nicely with the xfer optimization
|
| 674 |
+
#
|
| 675 |
+
do_execsql_test autoinc-10.1 {
|
| 676 |
+
DELETE FROM sqlite_sequence;
|
| 677 |
+
CREATE TABLE t10a(a INTEGER PRIMARY KEY AUTOINCREMENT, b UNIQUE);
|
| 678 |
+
INSERT INTO t10a VALUES(888,9999);
|
| 679 |
+
CREATE TABLE t10b(x INTEGER PRIMARY KEY AUTOINCREMENT, y UNIQUE);
|
| 680 |
+
INSERT INTO t10b SELECT * FROM t10a;
|
| 681 |
+
SELECT * FROM sqlite_sequence;
|
| 682 |
+
} {t10a 888 t10b 888}
|
| 683 |
+
|
| 684 |
+
# 2018-04-21 autoincrement does not cause problems for upsert
|
| 685 |
+
#
|
| 686 |
+
do_execsql_test autoinc-11.1 {
|
| 687 |
+
CREATE TABLE t11(a INTEGER PRIMARY KEY AUTOINCREMENT,b UNIQUE);
|
| 688 |
+
INSERT INTO t11(a,b) VALUES(2,3),(5,6),(4,3),(1,2)
|
| 689 |
+
ON CONFLICT(b) DO UPDATE SET a=a+1000;
|
| 690 |
+
SELECT seq FROM sqlite_sequence WHERE name='t11';
|
| 691 |
+
} {5}
|
| 692 |
+
|
| 693 |
+
# 2018-05-23 ticket d8dc2b3a58cd5dc2918a1d4acbba4676a23ada4c
|
| 694 |
+
# Does not crash if the sqlite_sequence table schema is missing
|
| 695 |
+
# or corrupt.
|
| 696 |
+
#
|
| 697 |
+
do_test autoinc-12.1 {
|
| 698 |
+
db close
|
| 699 |
+
forcedelete test.db
|
| 700 |
+
sqlite3 db test.db
|
| 701 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 702 |
+
db eval {
|
| 703 |
+
CREATE TABLE fake_sequence(name TEXT PRIMARY KEY,seq) WITHOUT ROWID;
|
| 704 |
+
PRAGMA writable_schema=on;
|
| 705 |
+
UPDATE sqlite_master SET
|
| 706 |
+
sql=replace(sql,'fake_','sqlite_'),
|
| 707 |
+
name='sqlite_sequence',
|
| 708 |
+
tbl_name='sqlite_sequence'
|
| 709 |
+
WHERE name='fake_sequence';
|
| 710 |
+
}
|
| 711 |
+
db close
|
| 712 |
+
sqlite3 db test.db
|
| 713 |
+
set res [catch {db eval {
|
| 714 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
|
| 715 |
+
INSERT INTO t1(b) VALUES('one');
|
| 716 |
+
}} msg]
|
| 717 |
+
lappend res $msg
|
| 718 |
+
} {1 {database disk image is malformed}}
|
| 719 |
+
do_test autoinc-12.2 {
|
| 720 |
+
db close
|
| 721 |
+
forcedelete test.db
|
| 722 |
+
sqlite3 db test.db
|
| 723 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 724 |
+
db eval {
|
| 725 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
|
| 726 |
+
INSERT INTO t1(b) VALUES('one');
|
| 727 |
+
PRAGMA writable_schema=on;
|
| 728 |
+
UPDATE sqlite_master SET
|
| 729 |
+
sql=replace(sql,'sqlite_','x_'),
|
| 730 |
+
name='x_sequence',
|
| 731 |
+
tbl_name='x_sequence'
|
| 732 |
+
WHERE name='sqlite_sequence';
|
| 733 |
+
}
|
| 734 |
+
db close
|
| 735 |
+
sqlite3 db test.db
|
| 736 |
+
set res [catch {db eval {
|
| 737 |
+
INSERT INTO t1(b) VALUES('two');
|
| 738 |
+
}} msg]
|
| 739 |
+
lappend res $msg
|
| 740 |
+
} {1 {database disk image is malformed}}
|
| 741 |
+
ifcapable vtab {
|
| 742 |
+
set err "database disk image is malformed"
|
| 743 |
+
} else {
|
| 744 |
+
set err {malformed database schema (sqlite_sequence) - near "VIRTUAL": syntax error}
|
| 745 |
+
}
|
| 746 |
+
do_test autoinc-12.3 {
|
| 747 |
+
db close
|
| 748 |
+
forcedelete test.db
|
| 749 |
+
sqlite3 db test.db
|
| 750 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 751 |
+
db eval {
|
| 752 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
|
| 753 |
+
INSERT INTO t1(b) VALUES('one');
|
| 754 |
+
PRAGMA writable_schema=on;
|
| 755 |
+
UPDATE sqlite_master SET
|
| 756 |
+
sql='CREATE VIRTUAL TABLE sqlite_sequence USING sqlite_dbpage'
|
| 757 |
+
WHERE name='sqlite_sequence';
|
| 758 |
+
}
|
| 759 |
+
db close
|
| 760 |
+
sqlite3 db test.db
|
| 761 |
+
set res [catch {db eval {
|
| 762 |
+
INSERT INTO t1(b) VALUES('two');
|
| 763 |
+
}} msg]
|
| 764 |
+
lappend res $msg
|
| 765 |
+
} [list 1 $err]
|
| 766 |
+
do_test autoinc-12.4 {
|
| 767 |
+
db close
|
| 768 |
+
forcedelete test.db
|
| 769 |
+
sqlite3 db test.db
|
| 770 |
+
db eval {
|
| 771 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
|
| 772 |
+
INSERT INTO t1(b) VALUES('one');
|
| 773 |
+
CREATE TABLE fake(name TEXT PRIMARY KEY,seq) WITHOUT ROWID;
|
| 774 |
+
}
|
| 775 |
+
set root1 [db one {SELECT rootpage FROM sqlite_master
|
| 776 |
+
WHERE name='sqlite_sequence'}]
|
| 777 |
+
set root2 [db one {SELECT rootpage FROM sqlite_master
|
| 778 |
+
WHERE name='fake'}]
|
| 779 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 780 |
+
db eval {
|
| 781 |
+
PRAGMA writable_schema=on;
|
| 782 |
+
UPDATE sqlite_master SET rootpage=$root2
|
| 783 |
+
WHERE name='sqlite_sequence';
|
| 784 |
+
UPDATE sqlite_master SET rootpage=$root1
|
| 785 |
+
WHERE name='fake';
|
| 786 |
+
}
|
| 787 |
+
db close
|
| 788 |
+
sqlite3 db test.db
|
| 789 |
+
set res [catch {db eval {
|
| 790 |
+
INSERT INTO t1(b) VALUES('two');
|
| 791 |
+
}} msg]
|
| 792 |
+
lappend res $msg
|
| 793 |
+
} {1 {database disk image is malformed}}
|
| 794 |
+
breakpoint
|
| 795 |
+
do_test autoinc-12.5 {
|
| 796 |
+
db close
|
| 797 |
+
forcedelete test.db
|
| 798 |
+
sqlite3 db test.db
|
| 799 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 800 |
+
db eval {
|
| 801 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
|
| 802 |
+
INSERT INTO t1(b) VALUES('one');
|
| 803 |
+
PRAGMA writable_schema=on;
|
| 804 |
+
UPDATE sqlite_master SET
|
| 805 |
+
sql='CREATE TABLE sqlite_sequence(x)'
|
| 806 |
+
WHERE name='sqlite_sequence';
|
| 807 |
+
}
|
| 808 |
+
db close
|
| 809 |
+
sqlite3 db test.db
|
| 810 |
+
set res [catch {db eval {
|
| 811 |
+
INSERT INTO t1(b) VALUES('two');
|
| 812 |
+
}} msg]
|
| 813 |
+
lappend res $msg
|
| 814 |
+
} {1 {database disk image is malformed}}
|
| 815 |
+
do_test autoinc-12.6 {
|
| 816 |
+
db close
|
| 817 |
+
forcedelete test.db
|
| 818 |
+
sqlite3 db test.db
|
| 819 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 820 |
+
db eval {
|
| 821 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
|
| 822 |
+
INSERT INTO t1(b) VALUES('one');
|
| 823 |
+
PRAGMA writable_schema=on;
|
| 824 |
+
UPDATE sqlite_master SET
|
| 825 |
+
sql='CREATE TABLE sqlite_sequence(x,y INTEGER PRIMARY KEY)'
|
| 826 |
+
WHERE name='sqlite_sequence';
|
| 827 |
+
}
|
| 828 |
+
db close
|
| 829 |
+
sqlite3 db test.db
|
| 830 |
+
set res [catch {db eval {
|
| 831 |
+
INSERT INTO t1(b) VALUES('two'),('three'),('four');
|
| 832 |
+
INSERT INTO t1(b) VALUES('five');
|
| 833 |
+
PRAGMA integrity_check;
|
| 834 |
+
}} msg]
|
| 835 |
+
lappend res $msg
|
| 836 |
+
} {0 ok}
|
| 837 |
+
do_test autoinc-12.7 {
|
| 838 |
+
db close
|
| 839 |
+
forcedelete test.db
|
| 840 |
+
sqlite3 db test.db
|
| 841 |
+
sqlite3_db_config db DEFENSIVE 0
|
| 842 |
+
db eval {
|
| 843 |
+
CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
|
| 844 |
+
INSERT INTO t1(b) VALUES('one');
|
| 845 |
+
PRAGMA writable_schema=on;
|
| 846 |
+
UPDATE sqlite_master SET
|
| 847 |
+
sql='CREATE TABLE sqlite_sequence(y INTEGER PRIMARY KEY,x)'
|
| 848 |
+
WHERE name='sqlite_sequence';
|
| 849 |
+
}
|
| 850 |
+
db close
|
| 851 |
+
sqlite3 db test.db
|
| 852 |
+
set res [catch {db eval {
|
| 853 |
+
INSERT INTO t1(b) VALUES('two'),('three'),('four');
|
| 854 |
+
INSERT INTO t1(b) VALUES('five');
|
| 855 |
+
PRAGMA integrity_check;
|
| 856 |
+
}} msg]
|
| 857 |
+
lappend res $msg
|
| 858 |
+
} {0 ok}
|
| 859 |
+
|
| 860 |
+
#--------------------------------------------------------------------------
|
| 861 |
+
reset_db
|
| 862 |
+
do_execsql_test 13.0 {
|
| 863 |
+
CREATE TABLE t1(i INTEGER PRIMARY KEY AUTOINCREMENT, j);
|
| 864 |
+
CREATE TABLE t2(i INTEGER PRIMARY KEY AUTOINCREMENT, j);
|
| 865 |
+
CREATE TABLE t3(i INTEGER PRIMARY KEY AUTOINCREMENT, j);
|
| 866 |
+
|
| 867 |
+
INSERT INTO t1 VALUES(NULL, 1);
|
| 868 |
+
INSERT INTO t2 VALUES(NULL, 2);
|
| 869 |
+
INSERT INTO t3 VALUES(NULL, 3);
|
| 870 |
+
|
| 871 |
+
SELECT name FROM sqlite_sequence;
|
| 872 |
+
} {t1 t2 t3}
|
| 873 |
+
|
| 874 |
+
do_execsql_test 13.1 {
|
| 875 |
+
UPDATE sqlite_sequence SET name=NULL WHERE name='t2';
|
| 876 |
+
INSERT INTO t3 VALUES(NULL, 4);
|
| 877 |
+
DELETE FROM t3;
|
| 878 |
+
INSERT INTO t3 VALUES(NULL, 5);
|
| 879 |
+
SELECT * FROM t3;
|
| 880 |
+
} {3 5}
|
| 881 |
+
|
| 882 |
+
|
| 883 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/autoindex2.test
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2014-06-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 |
+
# This file implements regression tests for SQLite library. The
|
| 13 |
+
# focus of this script is testing automatic index creation logic.
|
| 14 |
+
#
|
| 15 |
+
# This file contains a single real-world test case that was giving
|
| 16 |
+
# suboptimal performance because of over-use of automatic indexes.
|
| 17 |
+
#
|
| 18 |
+
|
| 19 |
+
set testdir [file dirname $argv0]
|
| 20 |
+
source $testdir/tester.tcl
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
do_execsql_test autoindex2-100 {
|
| 24 |
+
CREATE TABLE t1(
|
| 25 |
+
t1_id largeint,
|
| 26 |
+
did char(9),
|
| 27 |
+
ptime largeint,
|
| 28 |
+
exbyte char(4),
|
| 29 |
+
pe_id int,
|
| 30 |
+
field_id int,
|
| 31 |
+
mass float,
|
| 32 |
+
param10 float,
|
| 33 |
+
param11 float,
|
| 34 |
+
exmass float,
|
| 35 |
+
deviation float,
|
| 36 |
+
trange float,
|
| 37 |
+
vstatus int,
|
| 38 |
+
commit_status int,
|
| 39 |
+
formula char(329),
|
| 40 |
+
tier int DEFAULT 2,
|
| 41 |
+
ssid int DEFAULT 0,
|
| 42 |
+
last_operation largeint DEFAULT 0,
|
| 43 |
+
admin_uuid int DEFAULT 0,
|
| 44 |
+
previous_value float,
|
| 45 |
+
job_id largeint,
|
| 46 |
+
last_t1 largeint DEFAULT 0,
|
| 47 |
+
data_t1 int,
|
| 48 |
+
previous_date largeint DEFAULT 0,
|
| 49 |
+
flg8 int DEFAULT 1,
|
| 50 |
+
failed_fields char(100)
|
| 51 |
+
);
|
| 52 |
+
CREATE INDEX t1x0 on t1 (t1_id);
|
| 53 |
+
CREATE INDEX t1x1 on t1 (ptime, vstatus);
|
| 54 |
+
CREATE INDEX t1x2 on t1 (did, ssid, ptime, vstatus, exbyte, t1_id);
|
| 55 |
+
CREATE INDEX t1x3 on t1 (job_id);
|
| 56 |
+
|
| 57 |
+
CREATE TABLE t2(
|
| 58 |
+
did char(9),
|
| 59 |
+
client_did char(30),
|
| 60 |
+
description char(49),
|
| 61 |
+
uid int,
|
| 62 |
+
tzid int,
|
| 63 |
+
privilege int,
|
| 64 |
+
param2 int,
|
| 65 |
+
type char(30),
|
| 66 |
+
subtype char(32),
|
| 67 |
+
dparam1 char(7) DEFAULT '',
|
| 68 |
+
param5 char(3) DEFAULT '',
|
| 69 |
+
notional float DEFAULT 0.000000,
|
| 70 |
+
create_time largeint,
|
| 71 |
+
sample_time largeint DEFAULT 0,
|
| 72 |
+
param6 largeint,
|
| 73 |
+
frequency int,
|
| 74 |
+
expiration largeint,
|
| 75 |
+
uw_status int,
|
| 76 |
+
next_sample largeint,
|
| 77 |
+
last_sample largeint,
|
| 78 |
+
reserve1 char(29) DEFAULT '',
|
| 79 |
+
reserve2 char(29) DEFAULT '',
|
| 80 |
+
reserve3 char(29) DEFAULT '',
|
| 81 |
+
bxcdr char(19) DEFAULT 'XY',
|
| 82 |
+
ssid int DEFAULT 1,
|
| 83 |
+
last_t1_id largeint,
|
| 84 |
+
reserve4 char(29) DEFAULT '',
|
| 85 |
+
reserve5 char(29) DEFAULT '',
|
| 86 |
+
param12 int DEFAULT 0,
|
| 87 |
+
long_did char(100) DEFAULT '',
|
| 88 |
+
gr_code int DEFAULT 0,
|
| 89 |
+
drx char(100) DEFAULT '',
|
| 90 |
+
parent_id char(9) DEFAULT '',
|
| 91 |
+
param13 int DEFAULT 0,
|
| 92 |
+
position float DEFAULT 1.000000,
|
| 93 |
+
client_did3 char(100) DEFAULT '',
|
| 94 |
+
client_did4 char(100) DEFAULT '',
|
| 95 |
+
dlib_id char(9) DEFAULT ''
|
| 96 |
+
);
|
| 97 |
+
CREATE INDEX t2x0 on t2 (did);
|
| 98 |
+
CREATE INDEX t2x1 on t2 (client_did);
|
| 99 |
+
CREATE INDEX t2x2 on t2 (long_did);
|
| 100 |
+
CREATE INDEX t2x3 on t2 (uid);
|
| 101 |
+
CREATE INDEX t2x4 on t2 (param2);
|
| 102 |
+
CREATE INDEX t2x5 on t2 (type);
|
| 103 |
+
CREATE INDEX t2x6 on t2 (subtype);
|
| 104 |
+
CREATE INDEX t2x7 on t2 (last_sample);
|
| 105 |
+
CREATE INDEX t2x8 on t2 (param6);
|
| 106 |
+
CREATE INDEX t2x9 on t2 (frequency);
|
| 107 |
+
CREATE INDEX t2x10 on t2 (privilege);
|
| 108 |
+
CREATE INDEX t2x11 on t2 (sample_time);
|
| 109 |
+
CREATE INDEX t2x12 on t2 (notional);
|
| 110 |
+
CREATE INDEX t2x13 on t2 (tzid);
|
| 111 |
+
CREATE INDEX t2x14 on t2 (gr_code);
|
| 112 |
+
CREATE INDEX t2x15 on t2 (parent_id);
|
| 113 |
+
|
| 114 |
+
CREATE TABLE t3(
|
| 115 |
+
uid int,
|
| 116 |
+
param3 int,
|
| 117 |
+
uuid int,
|
| 118 |
+
acc_id int,
|
| 119 |
+
cust_num int,
|
| 120 |
+
numerix_id int,
|
| 121 |
+
pfy char(29),
|
| 122 |
+
param4 char(29),
|
| 123 |
+
param15 int DEFAULT 0,
|
| 124 |
+
flg7 int DEFAULT 0,
|
| 125 |
+
param21 int DEFAULT 0,
|
| 126 |
+
bxcdr char(2) DEFAULT 'PC',
|
| 127 |
+
c31 int DEFAULT 0,
|
| 128 |
+
c33 int DEFAULT 0,
|
| 129 |
+
c35 int DEFAULT 0,
|
| 130 |
+
c37 int,
|
| 131 |
+
mgr_uuid int,
|
| 132 |
+
back_up_uuid int,
|
| 133 |
+
priv_mars int DEFAULT 0,
|
| 134 |
+
is_qc int DEFAULT 0,
|
| 135 |
+
c41 int DEFAULT 0,
|
| 136 |
+
deleted int DEFAULT 0,
|
| 137 |
+
c47 int DEFAULT 1
|
| 138 |
+
);
|
| 139 |
+
CREATE INDEX t3x0 on t3 (uid);
|
| 140 |
+
CREATE INDEX t3x1 on t3 (param3);
|
| 141 |
+
CREATE INDEX t3x2 on t3 (uuid);
|
| 142 |
+
CREATE INDEX t3x3 on t3 (acc_id);
|
| 143 |
+
CREATE INDEX t3x4 on t3 (param4);
|
| 144 |
+
CREATE INDEX t3x5 on t3 (pfy);
|
| 145 |
+
CREATE INDEX t3x6 on t3 (is_qc);
|
| 146 |
+
SELECT count(*) FROM sqlite_master;
|
| 147 |
+
} {30}
|
| 148 |
+
do_execsql_test autoindex2-110 {
|
| 149 |
+
ANALYZE sqlite_master;
|
| 150 |
+
INSERT INTO sqlite_stat1 VALUES('t1','t1x3','10747267 260');
|
| 151 |
+
INSERT INTO sqlite_stat1 VALUES('t1','t1x2','10747267 121 113 2 2 2 1');
|
| 152 |
+
INSERT INTO sqlite_stat1 VALUES('t1','t1x1','10747267 50 40');
|
| 153 |
+
INSERT INTO sqlite_stat1 VALUES('t1','t1x0','10747267 1');
|
| 154 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x15','39667 253');
|
| 155 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x14','39667 19834');
|
| 156 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x13','39667 13223');
|
| 157 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x12','39667 7');
|
| 158 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x11','39667 17');
|
| 159 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x10','39667 19834');
|
| 160 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x9','39667 7934');
|
| 161 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x8','39667 11');
|
| 162 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x7','39667 5');
|
| 163 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x6','39667 242');
|
| 164 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x5','39667 1984');
|
| 165 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x4','39667 4408');
|
| 166 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x3','39667 81');
|
| 167 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x2','39667 551');
|
| 168 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x1','39667 2');
|
| 169 |
+
INSERT INTO sqlite_stat1 VALUES('t2','t2x0','39667 1');
|
| 170 |
+
INSERT INTO sqlite_stat1 VALUES('t3','t3x6','569 285');
|
| 171 |
+
INSERT INTO sqlite_stat1 VALUES('t3','t3x5','569 2');
|
| 172 |
+
INSERT INTO sqlite_stat1 VALUES('t3','t3x4','569 2');
|
| 173 |
+
INSERT INTO sqlite_stat1 VALUES('t3','t3x3','569 5');
|
| 174 |
+
INSERT INTO sqlite_stat1 VALUES('t3','t3x2','569 3');
|
| 175 |
+
INSERT INTO sqlite_stat1 VALUES('t3','t3x1','569 6');
|
| 176 |
+
INSERT INTO sqlite_stat1 VALUES('t3','t3x0','569 1');
|
| 177 |
+
ANALYZE sqlite_master;
|
| 178 |
+
} {}
|
| 179 |
+
do_execsql_test autoindex2-120 {
|
| 180 |
+
EXPLAIN QUERY PLAN
|
| 181 |
+
SELECT
|
| 182 |
+
t1_id,
|
| 183 |
+
t1.did,
|
| 184 |
+
param2,
|
| 185 |
+
param3,
|
| 186 |
+
t1.ptime,
|
| 187 |
+
t1.trange,
|
| 188 |
+
t1.exmass,
|
| 189 |
+
t1.mass,
|
| 190 |
+
t1.vstatus,
|
| 191 |
+
type,
|
| 192 |
+
subtype,
|
| 193 |
+
t1.deviation,
|
| 194 |
+
t1.formula,
|
| 195 |
+
dparam1,
|
| 196 |
+
reserve1,
|
| 197 |
+
reserve2,
|
| 198 |
+
param4,
|
| 199 |
+
t1.last_operation,
|
| 200 |
+
t1.admin_uuid,
|
| 201 |
+
t1.previous_value,
|
| 202 |
+
t1.job_id,
|
| 203 |
+
client_did,
|
| 204 |
+
t1.last_t1,
|
| 205 |
+
t1.data_t1,
|
| 206 |
+
t1.previous_date,
|
| 207 |
+
param5,
|
| 208 |
+
param6,
|
| 209 |
+
mgr_uuid
|
| 210 |
+
FROM
|
| 211 |
+
t1,
|
| 212 |
+
t2,
|
| 213 |
+
t3
|
| 214 |
+
WHERE
|
| 215 |
+
t1.ptime > 1393520400
|
| 216 |
+
AND param3<>9001
|
| 217 |
+
AND t3.flg7 = 1
|
| 218 |
+
AND t1.did = t2.did
|
| 219 |
+
AND t2.uid = t3.uid
|
| 220 |
+
ORDER BY t1.ptime desc LIMIT 500;
|
| 221 |
+
} {~/AUTO/}
|
| 222 |
+
#
|
| 223 |
+
# ^^^--- Before being fixed, the above was using an automatic covering
|
| 224 |
+
# on t3 and reordering the tables so that t3 was in the outer loop and
|
| 225 |
+
# implementing the ORDER BY clause using a B-Tree.
|
| 226 |
+
#
|
| 227 |
+
# This test is sanitized data received from a user. The original unsanitized
|
| 228 |
+
# data and STAT4 data is found in the th3private test repository. See one of
|
| 229 |
+
# the th3private check-ins on 2016-02-25. The test is much more accurate when
|
| 230 |
+
# STAT4 data is used.
|
| 231 |
+
|
| 232 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/autovacuum.test
ADDED
|
@@ -0,0 +1,715 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/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-02/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-02/afc-sqlite3/test/avfs.test
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2021-03-06
|
| 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 |
+
# TESTRUNNER: shell
|
| 12 |
+
#
|
| 13 |
+
# This file implements tests for the appendvfs extension.
|
| 14 |
+
#
|
| 15 |
+
# Tests performed:
|
| 16 |
+
# avfs-1.0. Test that an appendvfs DB can be added to an empty (ZLF) file.
|
| 17 |
+
# avfs-1.1. Test that the DB can be read with correct content upon reopen.
|
| 18 |
+
# avfs-1.2. Test that an appendvfs DB can be added to a simple text file.
|
| 19 |
+
# avfs-1.3. Test that the DB can be read with correct content upon reopen.
|
| 20 |
+
# avfs-1.4. Test that appended DB is aligned to default page boundary.
|
| 21 |
+
# avfs-2.1. Test that the simple text file retains its initial text.
|
| 22 |
+
# avfs-3.1. Test that the appendvfs can grow and shrink, remaining intact.
|
| 23 |
+
# avfs-3.2. Test that appendvfs is intact after grow/shrink/close/reopen.
|
| 24 |
+
# avfs-3.3. Test that appendvfs can grow by many pages and be written.
|
| 25 |
+
# avfs-3.4. Test that grown appendvfs can be reopened and appear intact.
|
| 26 |
+
# avfs-3.5. Test that much grown appendvfs can shrink and reopen intact.
|
| 27 |
+
# avfs-4.1. Test shell's ability to append to a non-appendvfs file.
|
| 28 |
+
# avfs-4.2. Test shell's ability to append to empty or nonexistent file.
|
| 29 |
+
# avfs-4.3. Test shell's ability to reopen and alter an appendvfs file.
|
| 30 |
+
# avfs-5.1. Test appendvfs refusal to open too-tiny DB appended onto ZLF.
|
| 31 |
+
# avfs-5.2. Test appendvfs refusal to open too-tiny DB appended on other.
|
| 32 |
+
# ...
|
| 33 |
+
# (more to come)
|
| 34 |
+
|
| 35 |
+
set testdir [file dirname $argv0]
|
| 36 |
+
source $testdir/tester.tcl
|
| 37 |
+
set ::testprefix avfs
|
| 38 |
+
|
| 39 |
+
# Do not attempt this test if SQLITE_OMIT_VIRTUALTABLE is defined.
|
| 40 |
+
#
|
| 41 |
+
ifcapable !vtab {
|
| 42 |
+
finish_test
|
| 43 |
+
return
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
set CLI [test_find_cli]
|
| 47 |
+
db close
|
| 48 |
+
# forcedelete test.db
|
| 49 |
+
|
| 50 |
+
load_static_extension db appendvfs
|
| 51 |
+
|
| 52 |
+
set ::fa avfs.adb
|
| 53 |
+
set ::fza avfs.sdb
|
| 54 |
+
forcedelete $::fa $::fza
|
| 55 |
+
set ::result {}
|
| 56 |
+
|
| 57 |
+
proc shellDoesAr {} {
|
| 58 |
+
set shdo "sh_app1.sql"
|
| 59 |
+
forcedelete $shdo
|
| 60 |
+
set fd [open $shdo w]
|
| 61 |
+
puts $fd ".help\n.q"
|
| 62 |
+
close $fd
|
| 63 |
+
set res [catchcmd "-batch -cmd \".read $shdo\""]
|
| 64 |
+
return [regexp {^.archive} [lindex $res 1]]
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
set ::vf "&vfs=apndvfs"
|
| 68 |
+
|
| 69 |
+
# Return file offset of appendvfs portion of a file, or {} if none such.
|
| 70 |
+
proc fosAvfs {fname} {
|
| 71 |
+
if {[file size $fname] < 25} {
|
| 72 |
+
return {}
|
| 73 |
+
}
|
| 74 |
+
if {[catch {set fd [open $fname rb]}]} {
|
| 75 |
+
return {}
|
| 76 |
+
}
|
| 77 |
+
seek $fd -25 end
|
| 78 |
+
set am [read $fd 17]
|
| 79 |
+
set ao [read $fd 8]
|
| 80 |
+
close $fd
|
| 81 |
+
if {$am ne "Start-Of-SQLite3-"} {
|
| 82 |
+
return {}
|
| 83 |
+
}
|
| 84 |
+
binary scan $ao "W" rvo
|
| 85 |
+
return $rvo
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
do_test 1.0 {
|
| 89 |
+
set results {}
|
| 90 |
+
set out [open $::fza wb]
|
| 91 |
+
close $out
|
| 92 |
+
sqlite3 adb "file:$::fza?mode=rwc$::vf" -uri 1
|
| 93 |
+
adb eval {
|
| 94 |
+
PRAGMA page_size=1024;
|
| 95 |
+
PRAGMA cache_size=10;
|
| 96 |
+
CREATE TABLE t1(a TEXT);
|
| 97 |
+
INSERT INTO t1 VALUES ('dog'),('cat');
|
| 98 |
+
SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a);
|
| 99 |
+
} { lappend results $pets }
|
| 100 |
+
adb close
|
| 101 |
+
lappend results [fosAvfs $fza]
|
| 102 |
+
set ::result [join $results " | "]
|
| 103 |
+
} {cat,dog | 0}
|
| 104 |
+
|
| 105 |
+
do_test 1.1 {
|
| 106 |
+
set results {}
|
| 107 |
+
sqlite3 adb "file:$::fza?mode=rw$::vf" -uri 1
|
| 108 |
+
adb eval {
|
| 109 |
+
SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a DESC);
|
| 110 |
+
} { lappend results $pets }
|
| 111 |
+
adb close
|
| 112 |
+
set ::result [join $results " | "]
|
| 113 |
+
} {dog,cat}
|
| 114 |
+
|
| 115 |
+
do_test 1.2 {
|
| 116 |
+
set results {}
|
| 117 |
+
set out [open $::fa wb]
|
| 118 |
+
set ::tlo { "Just some text," "and more text," "ending at 3 lines." }
|
| 119 |
+
puts $out [join $::tlo "\n"]
|
| 120 |
+
close $out
|
| 121 |
+
set adbSz [file size $::fa]
|
| 122 |
+
sqlite3 adb "file:$::fa?mode=rwc$::vf" -uri 1
|
| 123 |
+
adb eval {
|
| 124 |
+
PRAGMA auto_vacuum = 0;
|
| 125 |
+
PRAGMA page_size=512;
|
| 126 |
+
PRAGMA cache_size=0;
|
| 127 |
+
CREATE TABLE t1(a TEXT);
|
| 128 |
+
INSERT INTO t1 VALUES ('dog'),('cat'),('pig');
|
| 129 |
+
SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a);
|
| 130 |
+
} { lappend results $pets }
|
| 131 |
+
adb close
|
| 132 |
+
set adaSz [file size $::fa]
|
| 133 |
+
lappend results "Bytes before/after $adbSz/$adaSz"
|
| 134 |
+
set ::result [join $results " | "]
|
| 135 |
+
} {cat,dog,pig | Bytes before/after 50/5145}
|
| 136 |
+
|
| 137 |
+
do_test 1.3 {
|
| 138 |
+
set results {}
|
| 139 |
+
sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
|
| 140 |
+
adb eval {
|
| 141 |
+
SELECT group_concat(a) as pets FROM (SELECT a FROM t1 ORDER BY a DESC);
|
| 142 |
+
} { lappend results $pets }
|
| 143 |
+
adb close
|
| 144 |
+
set ::result [join $results " | "]
|
| 145 |
+
} {pig,dog,cat}
|
| 146 |
+
|
| 147 |
+
do_test 1.4 {
|
| 148 |
+
set ::result [fosAvfs $fa]
|
| 149 |
+
} {4096}
|
| 150 |
+
|
| 151 |
+
do_test 2.1 {
|
| 152 |
+
set in [open $::fa r]
|
| 153 |
+
set tli {}
|
| 154 |
+
for {set i [llength $::tlo]} {$i > 0} {incr i -1} {
|
| 155 |
+
lappend tli [gets $in]
|
| 156 |
+
}
|
| 157 |
+
close $in
|
| 158 |
+
if { [join $tli ":"] ne [join $::tlo ":"] } {
|
| 159 |
+
set ::result "Appendee changed."
|
| 160 |
+
} else {
|
| 161 |
+
set ::result "Appendee intact."
|
| 162 |
+
}
|
| 163 |
+
} {Appendee intact.}
|
| 164 |
+
|
| 165 |
+
# Set of repeatable random integers for a couple tests.
|
| 166 |
+
set ::nrint 50000
|
| 167 |
+
proc rint {v} {
|
| 168 |
+
return [::tcl::mathfunc::int [expr $v * 100000]]
|
| 169 |
+
}
|
| 170 |
+
array set ::randints [list 0 [rint [::tcl::mathfunc::srand 0]]]
|
| 171 |
+
for {set i 1} {$i < $::nrint} {incr i} {
|
| 172 |
+
set ::randints($i) [rint [::tcl::mathfunc::rand]]
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
do_test 3.1 {
|
| 176 |
+
set results {}
|
| 177 |
+
sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
|
| 178 |
+
adb eval {
|
| 179 |
+
DROP TABLE t1;
|
| 180 |
+
PRAGMA cache_size=10;
|
| 181 |
+
CREATE TABLE ri (i INTEGER);
|
| 182 |
+
BEGIN;
|
| 183 |
+
}
|
| 184 |
+
for {set i 0} {$i < $::nrint} {incr i} {
|
| 185 |
+
set r $::randints($i)
|
| 186 |
+
set s $::randints([incr i])
|
| 187 |
+
set t $::randints([incr i])
|
| 188 |
+
set u $::randints([incr i])
|
| 189 |
+
set v $::randints([incr i])
|
| 190 |
+
adb eval {
|
| 191 |
+
INSERT INTO ri VALUES ($r),($s),($t),($u),($v)
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
+
adb eval {
|
| 195 |
+
COMMIT;
|
| 196 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 197 |
+
} { lappend results $ic }
|
| 198 |
+
set adbSz [file size $::fa]
|
| 199 |
+
set qr {}
|
| 200 |
+
adb eval {
|
| 201 |
+
SELECT count(*) as ic FROM ri;
|
| 202 |
+
DELETE FROM ri WHERE (i % 50) <> 25;
|
| 203 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 204 |
+
VACUUM;
|
| 205 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 206 |
+
SELECT count(*) as ic FROM ri;
|
| 207 |
+
} { lappend qr $ic }
|
| 208 |
+
adb close
|
| 209 |
+
set adaSz [file size $::fa]
|
| 210 |
+
set adba [expr ($adbSz + 0.1)/$adaSz]
|
| 211 |
+
# lappend results $adba
|
| 212 |
+
set results [concat $results [lrange $qr 0 2]]
|
| 213 |
+
lappend results [expr {$adba > 10.0}]
|
| 214 |
+
set ::result [join $results " | "]
|
| 215 |
+
} "ok | $::nrint | ok | ok | 1"
|
| 216 |
+
|
| 217 |
+
do_test 3.2 {
|
| 218 |
+
set results {}
|
| 219 |
+
sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
|
| 220 |
+
adb eval {
|
| 221 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 222 |
+
} { lappend results $ic }
|
| 223 |
+
adb close
|
| 224 |
+
set ::result [join $results " | "]
|
| 225 |
+
} {ok}
|
| 226 |
+
|
| 227 |
+
# avfs-3.3. Test that appendvfs can grow by many pages and be written.
|
| 228 |
+
do_test 3.3 {
|
| 229 |
+
set results {}
|
| 230 |
+
sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
|
| 231 |
+
set npages 300
|
| 232 |
+
adb eval { BEGIN }
|
| 233 |
+
while {$npages > 0} {
|
| 234 |
+
adb eval { INSERT INTO ri VALUES (randomblob(1500)) }
|
| 235 |
+
incr npages -1
|
| 236 |
+
}
|
| 237 |
+
adb eval { COMMIT }
|
| 238 |
+
adb eval {
|
| 239 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 240 |
+
} { lappend results $ic }
|
| 241 |
+
adb close
|
| 242 |
+
set adaSzr [expr [file size $::fa] / 300.0 / 1500 ]
|
| 243 |
+
set okSzr [expr $adaSzr > 1.0 && $adaSzr < 1.3 ]
|
| 244 |
+
lappend results $okSzr
|
| 245 |
+
set ::result [join $results " | "]
|
| 246 |
+
} {ok | 1}
|
| 247 |
+
|
| 248 |
+
# avfs-3.4. Test that grown appendvfs can be reopened and appear intact.
|
| 249 |
+
do_test 3.4 {
|
| 250 |
+
set results {}
|
| 251 |
+
sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
|
| 252 |
+
adb eval {
|
| 253 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 254 |
+
} { lappend results $ic }
|
| 255 |
+
adb close
|
| 256 |
+
set ::result $ic
|
| 257 |
+
} {ok}
|
| 258 |
+
|
| 259 |
+
# avfs-3.5. Test that much grown appendvfs can shrink and reopen intact.
|
| 260 |
+
do_test 3.5 {
|
| 261 |
+
set results {}
|
| 262 |
+
set adbsz [file size $::fa]
|
| 263 |
+
sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
|
| 264 |
+
adb eval {
|
| 265 |
+
DELETE FROM ri WHERE rowid % 8 <> 0;
|
| 266 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 267 |
+
VACUUM;
|
| 268 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 269 |
+
} { lappend results $ic }
|
| 270 |
+
adb close
|
| 271 |
+
set adasz [file size $::fa]
|
| 272 |
+
lappend results [expr {$adbsz/$adasz > 5}]
|
| 273 |
+
sqlite3 adb "file:$::fa?mode=rw$::vf" -uri 1
|
| 274 |
+
adb eval {
|
| 275 |
+
SELECT integrity_check as ic FROM pragma_integrity_check();
|
| 276 |
+
} { lappend results $ic }
|
| 277 |
+
adb close
|
| 278 |
+
set ::result [join $results " | "]
|
| 279 |
+
} {ok | ok | 1 | ok}
|
| 280 |
+
|
| 281 |
+
set ::cliDoesAr [shellDoesAr]
|
| 282 |
+
|
| 283 |
+
do_test 4.1 {
|
| 284 |
+
set shdo "sh_app1.sql"
|
| 285 |
+
set shod "sh_app1.adb"
|
| 286 |
+
forcedelete $shdo $shod
|
| 287 |
+
set ofd [open $shdo w]
|
| 288 |
+
if {$::cliDoesAr} {
|
| 289 |
+
puts $ofd ".ar -c"
|
| 290 |
+
} else {
|
| 291 |
+
puts $ofd "pragma page_size=512;"
|
| 292 |
+
puts $ofd "create table sqlar (a);"
|
| 293 |
+
}
|
| 294 |
+
puts $ofd ".tables"
|
| 295 |
+
puts $ofd ".q"
|
| 296 |
+
close $ofd
|
| 297 |
+
set ofd [open $shod wb]
|
| 298 |
+
puts $ofd "Some text."
|
| 299 |
+
close $ofd
|
| 300 |
+
set res [catchcmd "-append -batch -init $shdo $shod" ""]
|
| 301 |
+
lappend res [fosAvfs $shod]
|
| 302 |
+
forcedelete $shdo $shod
|
| 303 |
+
set ::result [join $res " | "]
|
| 304 |
+
} {0 | sqlar | 4096}
|
| 305 |
+
|
| 306 |
+
do_test 4.2 {
|
| 307 |
+
set shdo "sh_app1.sql"
|
| 308 |
+
set shod "sh_app1.adb"
|
| 309 |
+
forcedelete $shdo $shod
|
| 310 |
+
set ofd [open $shdo w]
|
| 311 |
+
if {$::cliDoesAr} {
|
| 312 |
+
puts $ofd ".ar -c"
|
| 313 |
+
} else {
|
| 314 |
+
puts $ofd "pragma page_size=512;"
|
| 315 |
+
puts $ofd "create table sqlar (a);"
|
| 316 |
+
}
|
| 317 |
+
puts $ofd ".tables"
|
| 318 |
+
puts $ofd ".q"
|
| 319 |
+
close $ofd
|
| 320 |
+
set ofd [open $shod wb]
|
| 321 |
+
close $ofd
|
| 322 |
+
set res [catchcmd "-append -batch -init $shdo $shod" ""]
|
| 323 |
+
lappend res [fosAvfs $shod]
|
| 324 |
+
forcedelete $shdo ; # Leave $shod for next test.
|
| 325 |
+
set ::result [join $res " | "]
|
| 326 |
+
} {0 | sqlar | 0}
|
| 327 |
+
|
| 328 |
+
do_test 4.3 {
|
| 329 |
+
set shdo "sh_app1.sql"
|
| 330 |
+
set shod "sh_app1.adb" ; # Same as test 4.2, reusing ADB.
|
| 331 |
+
forcedelete $shdo
|
| 332 |
+
set ofd [open $shdo w]
|
| 333 |
+
if {$::cliDoesAr} {
|
| 334 |
+
puts $ofd ".ar -u $shdo"
|
| 335 |
+
puts $ofd "select count(*) from sqlar where name = '$shdo';"
|
| 336 |
+
} else {
|
| 337 |
+
puts $ofd "insert into sqlar values (1);"
|
| 338 |
+
puts $ofd "select count(*) from sqlar;"
|
| 339 |
+
}
|
| 340 |
+
puts $ofd ".q"
|
| 341 |
+
close $ofd
|
| 342 |
+
set res [catchcmd "-append -batch -init $shdo $shod" ""]
|
| 343 |
+
sqlite3 adb "file:$shod?mode=rw$::vf" -uri 1
|
| 344 |
+
adb eval {
|
| 345 |
+
SELECT count(*) as n FROM sqlar
|
| 346 |
+
} { lappend res $n }
|
| 347 |
+
adb close
|
| 348 |
+
forcedelete $shdo $shod;
|
| 349 |
+
set ::result [join $res " | "]
|
| 350 |
+
} {0 | 1 | 1}
|
| 351 |
+
|
| 352 |
+
do_test 5.1 {
|
| 353 |
+
set fake "faketiny.sdb"
|
| 354 |
+
forcedelete $fake
|
| 355 |
+
set ofd [open $fake wb]
|
| 356 |
+
puts -nonewline $ofd "SQLite format 3"
|
| 357 |
+
puts -nonewline $ofd [binary format "c" 0]
|
| 358 |
+
puts -nonewline $ofd "Start-Of-SQLite3-"
|
| 359 |
+
puts -nonewline $ofd [binary format "W" 0]
|
| 360 |
+
close $ofd
|
| 361 |
+
if {[catch {sqlite3 adb "file:$fake?mode=rw$::vf" -uri 1}]} {
|
| 362 |
+
set res "Open failed."
|
| 363 |
+
} else {
|
| 364 |
+
adb close
|
| 365 |
+
set res "Opened when should not."
|
| 366 |
+
}
|
| 367 |
+
forcedelete $fake
|
| 368 |
+
set ::result $res
|
| 369 |
+
} {Open failed.}
|
| 370 |
+
|
| 371 |
+
do_test 5.2 {
|
| 372 |
+
set fake "faketiny.sdb"
|
| 373 |
+
forcedelete $fake
|
| 374 |
+
set ofd [open $fake wb]
|
| 375 |
+
set fakeAppendee "Dog ate my homework.\n"
|
| 376 |
+
puts -nonewline $ofd $fakeAppendee
|
| 377 |
+
puts -nonewline $ofd "SQLite format 3"
|
| 378 |
+
puts -nonewline $ofd [binary format "c" 0]
|
| 379 |
+
puts -nonewline $ofd "Start-Of-SQLite3-"
|
| 380 |
+
puts -nonewline $ofd [binary format "W" [string length $fakeAppendee]]
|
| 381 |
+
close $ofd
|
| 382 |
+
if {[catch {sqlite3 adb "file:$fake?mode=rw$::vf" -uri 1}]} {
|
| 383 |
+
set res "Open failed."
|
| 384 |
+
} else {
|
| 385 |
+
adb close
|
| 386 |
+
set res "Opened when should not."
|
| 387 |
+
}
|
| 388 |
+
forcedelete $fake
|
| 389 |
+
set ::result $res
|
| 390 |
+
} {Open failed.}
|
| 391 |
+
|
| 392 |
+
forcedelete $::fa $::fza
|
| 393 |
+
|
| 394 |
+
unset -nocomplain ::fa ::fza ::tlo ::result ::randints ::nrint ::cliDoesAr
|
| 395 |
+
|
| 396 |
+
finish_test
|
local-test-sqlite3-delta-02/afc-sqlite3/test/avtrans.test
ADDED
|
@@ -0,0 +1,927 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/afc-sqlite3/test/backup.test
ADDED
|
@@ -0,0 +1,1004 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/afc-sqlite3/test/backup_ioerr.test
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 handling of IO errors by the
|
| 13 |
+
# sqlite3_backup_XXX APIs.
|
| 14 |
+
#
|
| 15 |
+
# $Id: backup_ioerr.test,v 1.3 2009/04/10 18:41:01 danielk1977 Exp $
|
| 16 |
+
|
| 17 |
+
set testdir [file dirname $argv0]
|
| 18 |
+
source $testdir/tester.tcl
|
| 19 |
+
|
| 20 |
+
proc data_checksum {db file} {
|
| 21 |
+
$db one "SELECT md5sum(a, b) FROM ${file}.t1"
|
| 22 |
+
}
|
| 23 |
+
proc test_contents {name db1 file1 db2 file2} {
|
| 24 |
+
$db2 eval {select * from sqlite_master}
|
| 25 |
+
$db1 eval {select * from sqlite_master}
|
| 26 |
+
set checksum [data_checksum $db2 $file2]
|
| 27 |
+
uplevel [list do_test $name [list data_checksum $db1 $file1] $checksum]
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
#--------------------------------------------------------------------
|
| 31 |
+
# This proc creates a database of approximately 290 pages. Depending
|
| 32 |
+
# on whether or not auto-vacuum is configured. Test cases backup_ioerr-1.*
|
| 33 |
+
# verify nothing more than this assumption.
|
| 34 |
+
#
|
| 35 |
+
proc populate_database {db {xtra_large 0}} {
|
| 36 |
+
execsql {
|
| 37 |
+
BEGIN;
|
| 38 |
+
CREATE TABLE t1(a, b);
|
| 39 |
+
INSERT INTO t1 VALUES(1, randstr(1000,1000));
|
| 40 |
+
INSERT INTO t1 SELECT a+ 1, randstr(1000,1000) FROM t1;
|
| 41 |
+
INSERT INTO t1 SELECT a+ 2, randstr(1000,1000) FROM t1;
|
| 42 |
+
INSERT INTO t1 SELECT a+ 4, randstr(1000,1000) FROM t1;
|
| 43 |
+
INSERT INTO t1 SELECT a+ 8, randstr(1000,1000) FROM t1;
|
| 44 |
+
INSERT INTO t1 SELECT a+16, randstr(1000,1000) FROM t1;
|
| 45 |
+
INSERT INTO t1 SELECT a+32, randstr(1000,1000) FROM t1;
|
| 46 |
+
CREATE INDEX i1 ON t1(b);
|
| 47 |
+
COMMIT;
|
| 48 |
+
} $db
|
| 49 |
+
if {$xtra_large} {
|
| 50 |
+
execsql { INSERT INTO t1 SELECT a+64, randstr(1000,1000) FROM t1 } $db
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
do_test backup_ioerr-1.1 {
|
| 54 |
+
populate_database db
|
| 55 |
+
set nPage [expr {[file size test.db] / 1024}]
|
| 56 |
+
expr {$nPage>130 && $nPage<160}
|
| 57 |
+
} {1}
|
| 58 |
+
do_test backup_ioerr-1.2 {
|
| 59 |
+
expr {[file size test.db] > $sqlite_pending_byte}
|
| 60 |
+
} {1}
|
| 61 |
+
do_test backup_ioerr-1.3 {
|
| 62 |
+
db close
|
| 63 |
+
forcedelete test.db
|
| 64 |
+
} {}
|
| 65 |
+
|
| 66 |
+
# Turn off IO error simulation.
|
| 67 |
+
#
|
| 68 |
+
proc clear_ioerr_simulation {} {
|
| 69 |
+
set ::sqlite_io_error_hit 0
|
| 70 |
+
set ::sqlite_io_error_hardhit 0
|
| 71 |
+
set ::sqlite_io_error_pending 0
|
| 72 |
+
set ::sqlite_io_error_persist 0
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
#--------------------------------------------------------------------
|
| 76 |
+
# The following procedure runs with SQLite's IO error simulation
|
| 77 |
+
# enabled.
|
| 78 |
+
#
|
| 79 |
+
# 1) Start with a reasonably sized database. One that includes the
|
| 80 |
+
# pending-byte (locking) page.
|
| 81 |
+
#
|
| 82 |
+
# 2) Open a backup process. Set the cache-size for the destination
|
| 83 |
+
# database to 10 pages only.
|
| 84 |
+
#
|
| 85 |
+
# 3) Step the backup process N times to partially backup the database
|
| 86 |
+
# file. If an IO error is reported, then the backup process is
|
| 87 |
+
# concluded with a call to backup_finish().
|
| 88 |
+
#
|
| 89 |
+
# If an IO error occurs, verify that:
|
| 90 |
+
#
|
| 91 |
+
# * the call to backup_step() returns an SQLITE_IOERR_XXX error code.
|
| 92 |
+
#
|
| 93 |
+
# * after the failed call to backup_step() but before the call to
|
| 94 |
+
# backup_finish() the destination database handle error code and
|
| 95 |
+
# error message remain unchanged.
|
| 96 |
+
#
|
| 97 |
+
# * the call to backup_finish() returns an SQLITE_IOERR_XXX error code.
|
| 98 |
+
#
|
| 99 |
+
# * following the call to backup_finish(), the destination database
|
| 100 |
+
# handle has been populated with an error code and error message.
|
| 101 |
+
#
|
| 102 |
+
# 4) Write to the database via the source database connection. Check
|
| 103 |
+
# that:
|
| 104 |
+
#
|
| 105 |
+
# * If an IO error occurs while writing the source database, the
|
| 106 |
+
# write operation should report an IO error. The backup should
|
| 107 |
+
# proceed as normal.
|
| 108 |
+
#
|
| 109 |
+
# * If an IO error occurs while updating the backup, the write
|
| 110 |
+
# operation should proceed normally. The error should be reported
|
| 111 |
+
# from the next call to backup_step() (in step 5 of this test
|
| 112 |
+
# procedure).
|
| 113 |
+
#
|
| 114 |
+
# 5) Step the backup process to finish the backup. If an IO error is
|
| 115 |
+
# reported, then the backup process is concluded with a call to
|
| 116 |
+
# backup_finish().
|
| 117 |
+
#
|
| 118 |
+
# Test that if an IO error occurs, or if one occurred while updating
|
| 119 |
+
# the backup database during step 4, then the conditions listed
|
| 120 |
+
# under step 3 are all true.
|
| 121 |
+
#
|
| 122 |
+
# 6) Finish the backup process.
|
| 123 |
+
#
|
| 124 |
+
# * If the backup succeeds (backup_finish() returns SQLITE_OK), then
|
| 125 |
+
# the contents of the backup database should match that of the
|
| 126 |
+
# source database.
|
| 127 |
+
#
|
| 128 |
+
# * If the backup fails (backup_finish() returns other than SQLITE_OK),
|
| 129 |
+
# then the contents of the backup database should be as they were
|
| 130 |
+
# before the operation was started.
|
| 131 |
+
#
|
| 132 |
+
# The following factors are varied:
|
| 133 |
+
#
|
| 134 |
+
# * Destination database is initially larger than the source database, OR
|
| 135 |
+
# * Destination database is initially smaller than the source database.
|
| 136 |
+
#
|
| 137 |
+
# * IO errors are transient, OR
|
| 138 |
+
# * IO errors are persistent.
|
| 139 |
+
#
|
| 140 |
+
# * Destination page-size is smaller than the source.
|
| 141 |
+
# * Destination page-size is the same as the source.
|
| 142 |
+
# * Destination page-size is larger than the source.
|
| 143 |
+
#
|
| 144 |
+
|
| 145 |
+
set iTest 1
|
| 146 |
+
foreach bPersist {0 1} {
|
| 147 |
+
foreach iDestPagesize {512 1024 4096} {
|
| 148 |
+
foreach zSetupBak [list "" {populate_database ddb 1}] {
|
| 149 |
+
|
| 150 |
+
incr iTest
|
| 151 |
+
set bStop 0
|
| 152 |
+
for {set iError 1} {$bStop == 0} {incr iError} {
|
| 153 |
+
# Disable IO error simulation.
|
| 154 |
+
clear_ioerr_simulation
|
| 155 |
+
|
| 156 |
+
catch { ddb close }
|
| 157 |
+
catch { sdb close }
|
| 158 |
+
catch { forcedelete test.db }
|
| 159 |
+
catch { forcedelete bak.db }
|
| 160 |
+
|
| 161 |
+
# Open the source and destination databases.
|
| 162 |
+
sqlite3 sdb test.db
|
| 163 |
+
sqlite3 ddb bak.db
|
| 164 |
+
|
| 165 |
+
# Step 1: Populate the source and destination databases.
|
| 166 |
+
populate_database sdb
|
| 167 |
+
ddb eval "PRAGMA page_size = $iDestPagesize"
|
| 168 |
+
ddb eval "PRAGMA cache_size = 10"
|
| 169 |
+
eval $zSetupBak
|
| 170 |
+
|
| 171 |
+
# Step 2: Open the backup process.
|
| 172 |
+
sqlite3_backup B ddb main sdb main
|
| 173 |
+
|
| 174 |
+
# Enable IO error simulation.
|
| 175 |
+
set ::sqlite_io_error_pending $iError
|
| 176 |
+
set ::sqlite_io_error_persist $bPersist
|
| 177 |
+
|
| 178 |
+
# Step 3: Partially backup the database. If an IO error occurs, check
|
| 179 |
+
# a few things then skip to the next iteration of the loop.
|
| 180 |
+
#
|
| 181 |
+
set rc [B step 100]
|
| 182 |
+
if {$::sqlite_io_error_hardhit} {
|
| 183 |
+
|
| 184 |
+
do_test backup_ioerr-$iTest.$iError.1 {
|
| 185 |
+
string match SQLITE_IOERR* $rc
|
| 186 |
+
} {1}
|
| 187 |
+
do_test backup_ioerr-$iTest.$iError.2 {
|
| 188 |
+
list [sqlite3_errcode ddb] [sqlite3_errmsg ddb]
|
| 189 |
+
} {SQLITE_OK {not an error}}
|
| 190 |
+
|
| 191 |
+
set rc [B finish]
|
| 192 |
+
do_test backup_ioerr-$iTest.$iError.3 {
|
| 193 |
+
string match SQLITE_IOERR* $rc
|
| 194 |
+
} {1}
|
| 195 |
+
|
| 196 |
+
do_test backup_ioerr-$iTest.$iError.4 {
|
| 197 |
+
sqlite3_errmsg ddb
|
| 198 |
+
} {disk I/O error}
|
| 199 |
+
|
| 200 |
+
clear_ioerr_simulation
|
| 201 |
+
sqlite3 ddb bak.db
|
| 202 |
+
integrity_check backup_ioerr-$iTest.$iError.5 ddb
|
| 203 |
+
|
| 204 |
+
continue
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
# No IO error was encountered during step 3. Check that backup_step()
|
| 208 |
+
# returned SQLITE_OK before proceding.
|
| 209 |
+
do_test backup_ioerr-$iTest.$iError.6 {
|
| 210 |
+
expr {$rc eq "SQLITE_OK"}
|
| 211 |
+
} {1}
|
| 212 |
+
|
| 213 |
+
# Step 4: Write to the source database.
|
| 214 |
+
set rc [catchsql { UPDATE t1 SET b = randstr(1000,1000) WHERE a < 50 } sdb]
|
| 215 |
+
|
| 216 |
+
if {[lindex $rc 0] && $::sqlite_io_error_persist==0} {
|
| 217 |
+
# The IO error occurred while updating the source database. In this
|
| 218 |
+
# case the backup should be able to continue.
|
| 219 |
+
set rc [B step 5000]
|
| 220 |
+
if { $rc != "SQLITE_IOERR_UNLOCK" } {
|
| 221 |
+
do_test backup_ioerr-$iTest.$iError.7 {
|
| 222 |
+
list [B step 5000] [B finish]
|
| 223 |
+
} {SQLITE_DONE SQLITE_OK}
|
| 224 |
+
|
| 225 |
+
clear_ioerr_simulation
|
| 226 |
+
test_contents backup_ioerr-$iTest.$iError.8 ddb main sdb main
|
| 227 |
+
integrity_check backup_ioerr-$iTest.$iError.9 ddb
|
| 228 |
+
} else {
|
| 229 |
+
do_test backup_ioerr-$iTest.$iError.10 {
|
| 230 |
+
B finish
|
| 231 |
+
} {SQLITE_IOERR_UNLOCK}
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
clear_ioerr_simulation
|
| 235 |
+
sqlite3 ddb bak.db
|
| 236 |
+
integrity_check backup_ioerr-$iTest.$iError.11 ddb
|
| 237 |
+
|
| 238 |
+
continue
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
# Step 5: Finish the backup operation. If an IO error occurs, check that
|
| 242 |
+
# it is reported correctly and skip to the next iteration of the loop.
|
| 243 |
+
#
|
| 244 |
+
set rc [B step 5000]
|
| 245 |
+
if {$rc != "SQLITE_DONE"} {
|
| 246 |
+
do_test backup_ioerr-$iTest.$iError.12 {
|
| 247 |
+
string match SQLITE_IOERR* $rc
|
| 248 |
+
} {1}
|
| 249 |
+
do_test backup_ioerr-$iTest.$iError.13 {
|
| 250 |
+
list [sqlite3_errcode ddb] [sqlite3_errmsg ddb]
|
| 251 |
+
} {SQLITE_OK {not an error}}
|
| 252 |
+
|
| 253 |
+
set rc [B finish]
|
| 254 |
+
do_test backup_ioerr-$iTest.$iError.14 {
|
| 255 |
+
string match SQLITE_IOERR* $rc
|
| 256 |
+
} {1}
|
| 257 |
+
do_test backup_ioerr-$iTest.$iError.15 {
|
| 258 |
+
sqlite3_errmsg ddb
|
| 259 |
+
} {disk I/O error}
|
| 260 |
+
|
| 261 |
+
clear_ioerr_simulation
|
| 262 |
+
sqlite3 ddb bak.db
|
| 263 |
+
integrity_check backup_ioerr-$iTest.$iError.16 ddb
|
| 264 |
+
|
| 265 |
+
continue
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
# The backup was successfully completed.
|
| 269 |
+
#
|
| 270 |
+
do_test backup_ioerr-$iTest.$iError.17 {
|
| 271 |
+
list [set rc] [B finish]
|
| 272 |
+
} {SQLITE_DONE SQLITE_OK}
|
| 273 |
+
|
| 274 |
+
clear_ioerr_simulation
|
| 275 |
+
sqlite3 sdb test.db
|
| 276 |
+
sqlite3 ddb bak.db
|
| 277 |
+
|
| 278 |
+
test_contents backup_ioerr-$iTest.$iError.18 ddb main sdb main
|
| 279 |
+
integrity_check backup_ioerr-$iTest.$iError.19 ddb
|
| 280 |
+
|
| 281 |
+
set bStop [expr $::sqlite_io_error_pending<=0]
|
| 282 |
+
}}}}
|
| 283 |
+
|
| 284 |
+
catch { sdb close }
|
| 285 |
+
catch { ddb close }
|
| 286 |
+
finish_test
|
local-test-sqlite3-delta-02/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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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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-02/afc-sqlite3/test/bc_common.tcl
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
proc bc_find_binaries {zCaption} {
|
| 5 |
+
# Search for binaries to test against. Any executable files that match
|
| 6 |
+
# our naming convention are assumed to be testfixture binaries to test
|
| 7 |
+
# against.
|
| 8 |
+
#
|
| 9 |
+
set binaries [list]
|
| 10 |
+
set self [info nameofexec]
|
| 11 |
+
set pattern "$self?*"
|
| 12 |
+
if {$::tcl_platform(platform)=="windows"} {
|
| 13 |
+
set pattern [string map {\.exe {}} $pattern]
|
| 14 |
+
}
|
| 15 |
+
foreach file [glob -nocomplain $pattern] {
|
| 16 |
+
if {$file==$self} continue
|
| 17 |
+
if {[file executable $file] && [file isfile $file]} {lappend binaries $file}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
if {[llength $binaries]==0} {
|
| 21 |
+
puts "WARNING: No historical binaries to test against."
|
| 22 |
+
puts "WARNING: Omitting backwards-compatibility tests"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
foreach bin $binaries {
|
| 26 |
+
puts -nonewline "Testing against $bin - "
|
| 27 |
+
flush stdout
|
| 28 |
+
puts "version [get_version $bin]"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
set ::BC(binaries) $binaries
|
| 32 |
+
return $binaries
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
proc get_version {binary} {
|
| 36 |
+
set chan [launch_testfixture $binary]
|
| 37 |
+
set v [testfixture $chan { sqlite3 -version }]
|
| 38 |
+
close $chan
|
| 39 |
+
set v
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
proc do_bc_test {bin script} {
|
| 43 |
+
|
| 44 |
+
forcedelete test.db
|
| 45 |
+
set ::bc_chan [launch_testfixture $bin]
|
| 46 |
+
|
| 47 |
+
proc code1 {tcl} { uplevel #0 $tcl }
|
| 48 |
+
proc code2 {tcl} { testfixture $::bc_chan $tcl }
|
| 49 |
+
proc sql1 sql { code1 [list db eval $sql] }
|
| 50 |
+
proc sql2 sql { code2 [list db eval $sql] }
|
| 51 |
+
|
| 52 |
+
code1 { sqlite3 db test.db }
|
| 53 |
+
code2 { sqlite3 db test.db }
|
| 54 |
+
|
| 55 |
+
set bintag $bin
|
| 56 |
+
regsub {.*testfixture\.} $bintag {} bintag
|
| 57 |
+
set bintag [string map {\.exe {}} $bintag]
|
| 58 |
+
if {$bintag == ""} {set bintag self}
|
| 59 |
+
set saved_prefix $::testprefix
|
| 60 |
+
append ::testprefix ".$bintag"
|
| 61 |
+
|
| 62 |
+
uplevel $script
|
| 63 |
+
|
| 64 |
+
set ::testprefix $saved_prefix
|
| 65 |
+
|
| 66 |
+
catch { code1 { db close } }
|
| 67 |
+
catch { code2 { db close } }
|
| 68 |
+
catch { close $::bc_chan }
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
proc do_all_bc_test {script} {
|
| 72 |
+
foreach bin $::BC(binaries) {
|
| 73 |
+
uplevel [list do_bc_test $bin $script]
|
| 74 |
+
}
|
| 75 |
+
}
|
local-test-sqlite3-delta-02/afc-sqlite3/test/bestindex1.test
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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:
|
| 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 |
+
|
| 14 |
+
set testdir [file dirname $argv0]
|
| 15 |
+
source $testdir/tester.tcl
|
| 16 |
+
set testprefix bestindex1
|
| 17 |
+
|
| 18 |
+
ifcapable !vtab {
|
| 19 |
+
finish_test
|
| 20 |
+
return
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
register_tcl_module db
|
| 24 |
+
|
| 25 |
+
proc vtab_command {method args} {
|
| 26 |
+
switch -- $method {
|
| 27 |
+
xConnect {
|
| 28 |
+
return "CREATE TABLE t1(a, b, c)"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
xBestIndex {
|
| 32 |
+
set hdl [lindex $args 0]
|
| 33 |
+
set clist [$hdl constraints]
|
| 34 |
+
set orderby [$hdl orderby]
|
| 35 |
+
|
| 36 |
+
if {[llength $clist]!=1} { error "unexpected constraint list" }
|
| 37 |
+
catch { array unset C }
|
| 38 |
+
array set C [lindex $clist 0]
|
| 39 |
+
if {$C(usable)} {
|
| 40 |
+
return "omit 0 cost 0 rows 1 idxnum 555 idxstr eq!"
|
| 41 |
+
} else {
|
| 42 |
+
return "cost 1000000 rows 0 idxnum 0 idxstr scan..."
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
return {}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
do_execsql_test 1.0 {
|
| 52 |
+
CREATE VIRTUAL TABLE x1 USING tcl(vtab_command);
|
| 53 |
+
} {}
|
| 54 |
+
|
| 55 |
+
do_eqp_test 1.1 {
|
| 56 |
+
SELECT * FROM x1 WHERE a = 'abc'
|
| 57 |
+
} {SCAN x1 VIRTUAL TABLE INDEX 555:eq!}
|
| 58 |
+
|
| 59 |
+
do_eqp_test 1.2 {
|
| 60 |
+
SELECT * FROM x1 WHERE a IN ('abc', 'def');
|
| 61 |
+
} {SCAN x1 VIRTUAL TABLE INDEX 555:eq!}
|
| 62 |
+
|
| 63 |
+
#-------------------------------------------------------------------------
|
| 64 |
+
#
|
| 65 |
+
reset_db
|
| 66 |
+
register_tcl_module db
|
| 67 |
+
|
| 68 |
+
# Parameter $mode may be one of:
|
| 69 |
+
#
|
| 70 |
+
# "omit" - Implement filtering. Set the omit flag.
|
| 71 |
+
# "use" - Implement filtering. Use the constraint, but do not set omit.
|
| 72 |
+
# "use2" - Do not implement filtering. Use the constraint anyway.
|
| 73 |
+
#
|
| 74 |
+
#
|
| 75 |
+
proc t1_vtab {mode method args} {
|
| 76 |
+
switch -- $method {
|
| 77 |
+
xConnect {
|
| 78 |
+
return "CREATE TABLE t1(a, b)"
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
xBestIndex {
|
| 82 |
+
set hdl [lindex $args 0]
|
| 83 |
+
set clist [$hdl constraints]
|
| 84 |
+
set orderby [$hdl orderby]
|
| 85 |
+
|
| 86 |
+
set SQL_FILTER {SELECT * FROM t1x WHERE a='%1%'}
|
| 87 |
+
set SQL_SCAN {SELECT * FROM t1x}
|
| 88 |
+
|
| 89 |
+
set idx 0
|
| 90 |
+
for {set idx 0} {$idx < [llength $clist]} {incr idx} {
|
| 91 |
+
array unset C
|
| 92 |
+
array set C [lindex $clist $idx]
|
| 93 |
+
if {$C(column)==0 && $C(op)=="eq" && $C(usable)} {
|
| 94 |
+
switch -- $mode {
|
| 95 |
+
"omit" {
|
| 96 |
+
return [list omit $idx rows 10 cost 10 idxstr $SQL_FILTER]
|
| 97 |
+
}
|
| 98 |
+
"use" {
|
| 99 |
+
return [list use $idx rows 10 cost 10 idxstr $SQL_FILTER]
|
| 100 |
+
}
|
| 101 |
+
"use2" {
|
| 102 |
+
return [list use $idx rows 10 cost 10 idxstr $SQL_SCAN]
|
| 103 |
+
}
|
| 104 |
+
default {
|
| 105 |
+
error "Bad mode - $mode"
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
return [list idxstr {SELECT * FROM t1x}]
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
xFilter {
|
| 115 |
+
set map [list %1% [lindex $args 2 0]]
|
| 116 |
+
set sql [string map $map [lindex $args 1]]
|
| 117 |
+
return [list sql $sql]
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
return {}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
do_execsql_test 2.1 {
|
| 125 |
+
CREATE TABLE t1x(i INTEGER PRIMARY KEY, a, b);
|
| 126 |
+
INSERT INTO t1x VALUES(1, 'one', 1);
|
| 127 |
+
INSERT INTO t1x VALUES(2, 'two', 2);
|
| 128 |
+
INSERT INTO t1x VALUES(3, 'three', 3);
|
| 129 |
+
INSERT INTO t1x VALUES(4, 'four', 4);
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
foreach {tn mode} {
|
| 133 |
+
1 use 2 omit 3 use2
|
| 134 |
+
} {
|
| 135 |
+
do_execsql_test 2.2.$mode.1 "
|
| 136 |
+
DROP TABLE IF EXISTS t1;
|
| 137 |
+
CREATE VIRTUAL TABLE t1 USING tcl(t1_vtab $mode);
|
| 138 |
+
"
|
| 139 |
+
|
| 140 |
+
do_execsql_test 2.2.$mode.2 {SELECT * FROM t1} {one 1 two 2 three 3 four 4}
|
| 141 |
+
do_execsql_test 2.2.$mode.3 {SELECT rowid FROM t1} {1 2 3 4}
|
| 142 |
+
do_execsql_test 2.2.$mode.4 {SELECT rowid FROM t1 WHERE a='two'} {2}
|
| 143 |
+
|
| 144 |
+
do_execsql_test 2.2.$mode.5 {
|
| 145 |
+
SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid
|
| 146 |
+
} {1 4}
|
| 147 |
+
|
| 148 |
+
set plan(use) {
|
| 149 |
+
QUERY PLAN
|
| 150 |
+
|--SCAN t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x WHERE a='%1%'
|
| 151 |
+
`--USE TEMP B-TREE FOR ORDER BY
|
| 152 |
+
}
|
| 153 |
+
set plan(omit) {
|
| 154 |
+
QUERY PLAN
|
| 155 |
+
|--SCAN t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x WHERE a='%1%'
|
| 156 |
+
`--USE TEMP B-TREE FOR ORDER BY
|
| 157 |
+
}
|
| 158 |
+
set plan(use2) {
|
| 159 |
+
QUERY PLAN
|
| 160 |
+
|--SCAN t1 VIRTUAL TABLE INDEX 0:SELECT * FROM t1x
|
| 161 |
+
`--USE TEMP B-TREE FOR ORDER BY
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
do_eqp_test 2.2.$mode.6 {
|
| 165 |
+
SELECT rowid FROM t1 WHERE a IN ('one', 'four') ORDER BY +rowid
|
| 166 |
+
} [string map {"\n " "\n"} $plan($mode)]
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
# 2016-04-09.
|
| 170 |
+
# Demonstrate a register overwrite problem when using two virtual
|
| 171 |
+
# tables where the outer loop uses the IN operator.
|
| 172 |
+
#
|
| 173 |
+
set G(collist) [list PrimaryKey flagA columnA]
|
| 174 |
+
set G(cols) [join $G(collist) ,]
|
| 175 |
+
set G(nulls) "NULL"
|
| 176 |
+
|
| 177 |
+
proc vtab_command {method args} {
|
| 178 |
+
global G
|
| 179 |
+
|
| 180 |
+
switch -- $method {
|
| 181 |
+
xConnect {
|
| 182 |
+
return "CREATE TABLE t1($G(cols))"
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
xBestIndex {
|
| 186 |
+
set hdl [lindex $args 0]
|
| 187 |
+
set clist [$hdl constraints]
|
| 188 |
+
set orderby [$hdl orderby]
|
| 189 |
+
|
| 190 |
+
#puts $clist
|
| 191 |
+
set W [list]
|
| 192 |
+
set U [list]
|
| 193 |
+
|
| 194 |
+
set i 0
|
| 195 |
+
for {set idx 0} {$idx < [llength $clist]} {incr idx} {
|
| 196 |
+
array set c [lindex $clist $idx]
|
| 197 |
+
if {$c(op)=="eq" && $c(usable)} {
|
| 198 |
+
lappend W "[lindex $G(collist) $c(column)] = %$i%"
|
| 199 |
+
lappend U use $idx
|
| 200 |
+
incr i
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
if {$W==""} {
|
| 205 |
+
set sql "SELECT rowid, * FROM t1"
|
| 206 |
+
} else {
|
| 207 |
+
set sql "SELECT rowid, * FROM t1 WHERE [join $W { AND }]"
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
return [concat [list idxstr $sql] $U]
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
xFilter {
|
| 214 |
+
foreach {idxnum idxstr vals} $args {}
|
| 215 |
+
|
| 216 |
+
set map [list]
|
| 217 |
+
for {set i 0} {$i < [llength $vals]} {incr i} {
|
| 218 |
+
lappend map "%$i%"
|
| 219 |
+
set v [lindex $vals $i]
|
| 220 |
+
if {[string is integer $v]} {
|
| 221 |
+
lappend map $v
|
| 222 |
+
} else {
|
| 223 |
+
lappend map "'$v'"
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
set sql [string map $map $idxstr]
|
| 227 |
+
|
| 228 |
+
#puts "SQL: $sql"
|
| 229 |
+
return [list sql $sql]
|
| 230 |
+
}
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
return {}
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
db close
|
| 237 |
+
forcedelete test.db
|
| 238 |
+
sqlite3 db test.db
|
| 239 |
+
register_tcl_module db
|
| 240 |
+
|
| 241 |
+
do_execsql_test 3.1 "
|
| 242 |
+
CREATE TABLE t1($G(cols));
|
| 243 |
+
INSERT INTO t1 VALUES(1, 0, 'ValueA');
|
| 244 |
+
INSERT INTO t1 VALUES(2, 0, 'ValueA');
|
| 245 |
+
INSERT INTO t1 VALUES(3, 0, 'ValueB');
|
| 246 |
+
INSERT INTO t1 VALUES(4, 0, 'ValueB');
|
| 247 |
+
"
|
| 248 |
+
|
| 249 |
+
do_execsql_test 3.2 {
|
| 250 |
+
CREATE VIRTUAL TABLE VirtualTableA USING tcl(vtab_command);
|
| 251 |
+
CREATE VIRTUAL TABLE VirtualTableB USING tcl(vtab_command);
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
do_execsql_test 3.3 { SELECT primarykey FROM VirtualTableA } {1 2 3 4}
|
| 255 |
+
|
| 256 |
+
do_execsql_test 3.4 {
|
| 257 |
+
SELECT * FROM
|
| 258 |
+
VirtualTableA a CROSS JOIN VirtualTableB b ON b.PrimaryKey=a.PrimaryKey
|
| 259 |
+
WHERE a.ColumnA IN ('ValueA', 'ValueB') AND a.FlagA=0
|
| 260 |
+
} {
|
| 261 |
+
1 0 ValueA 1 0 ValueA
|
| 262 |
+
2 0 ValueA 2 0 ValueA
|
| 263 |
+
3 0 ValueB 3 0 ValueB
|
| 264 |
+
4 0 ValueB 4 0 ValueB
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
do_execsql_test 3.5 {
|
| 268 |
+
SELECT * FROM
|
| 269 |
+
VirtualTableA a CROSS JOIN VirtualTableB b ON b.PrimaryKey=a.PrimaryKey
|
| 270 |
+
WHERE a.FlagA=0 AND a.ColumnA IN ('ValueA', 'ValueB')
|
| 271 |
+
} {
|
| 272 |
+
1 0 ValueA 1 0 ValueA
|
| 273 |
+
2 0 ValueA 2 0 ValueA
|
| 274 |
+
3 0 ValueB 3 0 ValueB
|
| 275 |
+
4 0 ValueB 4 0 ValueB
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
#-------------------------------------------------------------------------
|
| 279 |
+
# If there is an IN(..) condition in the WHERE clause of a query on a
|
| 280 |
+
# virtual table, the xBestIndex method is first invoked with the IN(...)
|
| 281 |
+
# represented by a "usable" SQLITE_INDEX_CONSTRAINT_EQ constraint. If
|
| 282 |
+
# the virtual table elects to use the IN(...) constraint, then the
|
| 283 |
+
# xBestIndex method is invoked again, this time with the IN(...) marked
|
| 284 |
+
# as "not usable". Depending on the relative costs of the two plans as
|
| 285 |
+
# defined by the virtual table implementation, and the cardinality of the
|
| 286 |
+
# IN(...) operator, SQLite chooses the most efficient plan.
|
| 287 |
+
#
|
| 288 |
+
# At one point the second invocation of xBestIndex() was only being made
|
| 289 |
+
# for join queries. The following tests check that this problem has been
|
| 290 |
+
# fixed.
|
| 291 |
+
#
|
| 292 |
+
proc vtab_command {method args} {
|
| 293 |
+
switch -- $method {
|
| 294 |
+
xConnect {
|
| 295 |
+
return "CREATE TABLE t1(a, b, c, d)"
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
xBestIndex {
|
| 299 |
+
set hdl [lindex $args 0]
|
| 300 |
+
set clist [$hdl constraints]
|
| 301 |
+
set orderby [$hdl orderby]
|
| 302 |
+
|
| 303 |
+
lappend ::bestindex_calls $clist
|
| 304 |
+
set ret "cost 1000000 idxnum 555"
|
| 305 |
+
for {set i 0} {$i < [llength $clist]} {incr i} {
|
| 306 |
+
array set C [lindex $clist $i]
|
| 307 |
+
if {$C(usable)} {
|
| 308 |
+
lappend ret use $i
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
return $ret
|
| 312 |
+
}
|
| 313 |
+
}
|
| 314 |
+
return {}
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
do_execsql_test 4.0 {
|
| 318 |
+
CREATE VIRTUAL TABLE x1 USING tcl(vtab_command);
|
| 319 |
+
} {}
|
| 320 |
+
|
| 321 |
+
do_test 4.1 {
|
| 322 |
+
set ::bestindex_calls [list]
|
| 323 |
+
execsql {
|
| 324 |
+
SELECT * FROM x1 WHERE a=? AND b BETWEEN ? AND ? AND c IN (1, 2, 3, 4);
|
| 325 |
+
}
|
| 326 |
+
set ::bestindex_calls
|
| 327 |
+
} [list \
|
| 328 |
+
[list {op eq column 0 usable 1} \
|
| 329 |
+
{op eq column 2 usable 1} \
|
| 330 |
+
{op ge column 1 usable 1} \
|
| 331 |
+
{op le column 1 usable 1} \
|
| 332 |
+
] \
|
| 333 |
+
[list {op eq column 0 usable 1} \
|
| 334 |
+
{op eq column 2 usable 0} \
|
| 335 |
+
{op ge column 1 usable 1} \
|
| 336 |
+
{op le column 1 usable 1}
|
| 337 |
+
]
|
| 338 |
+
]
|
| 339 |
+
|
| 340 |
+
do_catchsql_test 5.0 {
|
| 341 |
+
SELECT * FROM tcl('abc');
|
| 342 |
+
} {1 {wrong number of arguments}}
|
| 343 |
+
|
| 344 |
+
finish_test
|