1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#pragma ident "%Z%%M% %I% %E% SMI"
# 2001 September 15
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the INSERT statement that takes is
# result from a SELECT.
#
# $Id: insert2.test,v 1.10 2002/06/25 13:16:04 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Create some tables with data that we can select against
#
do_test insert2-1.0 {
execsql {CREATE TABLE d1(n int, log int);}
for {set i 1} {$i<=20} {incr i} {
for {set j 0} {pow(2,$j)<$i} {incr j} {}
execsql "INSERT INTO d1 VALUES($i,$j)"
}
execsql {SELECT * FROM d1 ORDER BY n}
} {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5}
# Insert into a new table from the old one.
#
do_test insert2-1.1.1 {
execsql {
CREATE TABLE t1(log int, cnt int);
PRAGMA count_changes=on;
INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
}
} {6}
do_test insert2-1.1.2 {
db changes
} {6}
do_test insert2-1.1.3 {
execsql {SELECT * FROM t1 ORDER BY log}
} {0 1 1 1 2 2 3 4 4 8 5 4}
do_test insert2-1.2.1 {
catch {execsql {DROP TABLE t1}}
execsql {
CREATE TABLE t1(log int, cnt int);
INSERT INTO t1
SELECT log, count(*) FROM d1 GROUP BY log
EXCEPT SELECT n-1,log FROM d1;
}
} {4}
do_test insert2-1.2.2 {
execsql {
SELECT * FROM t1 ORDER BY log;
}
} {0 1 3 4 4 8 5 4}
do_test insert2-1.3.1 {
catch {execsql {DROP TABLE t1}}
execsql {
CREATE TABLE t1(log int, cnt int);
PRAGMA count_changes=off;
INSERT INTO t1
SELECT log, count(*) FROM d1 GROUP BY log
INTERSECT SELECT n-1,log FROM d1;
}
} {}
do_test insert2-1.3.2 {
execsql {
SELECT * FROM t1 ORDER BY log;
}
} {1 1 2 2}
do_test insert2-1.4 {
catch {execsql {DROP TABLE t1}}
set r [execsql {
CREATE TABLE t1(log int, cnt int);
CREATE INDEX i1 ON t1(log);
CREATE INDEX i2 ON t1(cnt);
INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
SELECT * FROM t1 ORDER BY log;
}]
lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
do_test insert2-2.0 {
execsql {
CREATE TABLE t3(a,b,c);
CREATE TABLE t4(x,y);
INSERT INTO t4 VALUES(1,2);
SELECT * FROM t4;
}
} {1 2}
do_test insert2-2.1 {
execsql {
INSERT INTO t3(a,c) SELECT * FROM t4;
SELECT * FROM t3;
}
} {1 {} 2}
do_test insert2-2.2 {
execsql {
DELETE FROM t3;
INSERT INTO t3(c,b) SELECT * FROM t4;
SELECT * FROM t3;
}
} {{} 2 1}
do_test insert2-2.3 {
execsql {
DELETE FROM t3;
INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4;
SELECT * FROM t3;
}
} {hi 2 1}
integrity_check insert2-3.0
# File table t4 with lots of data
#
do_test insert2-3.1 {
execsql {
SELECT * from t4;
}
} {1 2}
do_test insert2-3.2 {
execsql {
BEGIN;
INSERT INTO t4 VALUES(2,4);
INSERT INTO t4 VALUES(3,6);
INSERT INTO t4 VALUES(4,8);
INSERT INTO t4 VALUES(5,10);
INSERT INTO t4 VALUES(6,12);
INSERT INTO t4 VALUES(7,14);
INSERT INTO t4 VALUES(8,16);
INSERT INTO t4 VALUES(9,18);
INSERT INTO t4 VALUES(10,20);
COMMIT;
}
db changes
} {9}
do_test insert2-3.2.1 {
execsql {
SELECT count(*) FROM t4;
}
} {10}
do_test insert2-3.3 {
execsql {
BEGIN;
INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
COMMIT;
SELECT count(*) FROM t4;
}
} {160}
do_test insert2-3.4 {
execsql {
BEGIN;
UPDATE t4 SET y='lots of data for the row where x=' || x
|| ' and y=' || y || ' - even more data to fill space';
COMMIT;
SELECT count(*) FROM t4;
}
} {160}
do_test insert2-3.5 {
execsql {
BEGIN;
INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4;
SELECT count(*) from t4;
ROLLBACK;
}
} {320}
do_test insert2-3.6 {
execsql {
SELECT count(*) FROM t4;
}
} {160}
do_test insert2-3.7 {
execsql {
BEGIN;
DELETE FROM t4 WHERE x!=123;
SELECT count(*) FROM t4;
ROLLBACK;
}
} {1}
do_test insert2-3.8 {
db changes
} {159}
integrity_check insert2-3.9
finish_test
|