sl@0
|
1 |
# 2004 Jun 27
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
# This file implements regression tests for SQLite library.
|
sl@0
|
12 |
#
|
sl@0
|
13 |
# This file implements tests for miscellanous features that were
|
sl@0
|
14 |
# left out of other test files.
|
sl@0
|
15 |
#
|
sl@0
|
16 |
# $Id: misc4.test,v 1.23 2007/12/08 18:01:31 drh Exp $
|
sl@0
|
17 |
|
sl@0
|
18 |
set testdir [file dirname $argv0]
|
sl@0
|
19 |
source $testdir/tester.tcl
|
sl@0
|
20 |
|
sl@0
|
21 |
# Prepare a statement that will create a temporary table. Then do
|
sl@0
|
22 |
# a rollback. Then try to execute the prepared statement.
|
sl@0
|
23 |
#
|
sl@0
|
24 |
do_test misc4-1.1 {
|
sl@0
|
25 |
set DB [sqlite3_connection_pointer db]
|
sl@0
|
26 |
execsql {
|
sl@0
|
27 |
CREATE TABLE t1(x);
|
sl@0
|
28 |
INSERT INTO t1 VALUES(1);
|
sl@0
|
29 |
}
|
sl@0
|
30 |
} {}
|
sl@0
|
31 |
|
sl@0
|
32 |
ifcapable tempdb {
|
sl@0
|
33 |
do_test misc4-1.2 {
|
sl@0
|
34 |
set sql {CREATE TEMP TABLE t2 AS SELECT * FROM t1}
|
sl@0
|
35 |
set stmt [sqlite3_prepare $DB $sql -1 TAIL]
|
sl@0
|
36 |
execsql {
|
sl@0
|
37 |
BEGIN;
|
sl@0
|
38 |
CREATE TABLE t3(a,b,c);
|
sl@0
|
39 |
INSERT INTO t1 SELECT * FROM t1;
|
sl@0
|
40 |
ROLLBACK;
|
sl@0
|
41 |
}
|
sl@0
|
42 |
} {}
|
sl@0
|
43 |
|
sl@0
|
44 |
# Because the previous transaction included a DDL statement and
|
sl@0
|
45 |
# was rolled back, statement $stmt was marked as expired. Executing it
|
sl@0
|
46 |
# now returns SQLITE_SCHEMA.
|
sl@0
|
47 |
do_test misc4-1.2.1 {
|
sl@0
|
48 |
list [sqlite3_step $stmt] [sqlite3_finalize $stmt]
|
sl@0
|
49 |
} {SQLITE_ERROR SQLITE_SCHEMA}
|
sl@0
|
50 |
do_test misc4-1.2.2 {
|
sl@0
|
51 |
set stmt [sqlite3_prepare $DB $sql -1 TAIL]
|
sl@0
|
52 |
set TAIL
|
sl@0
|
53 |
} {}
|
sl@0
|
54 |
|
sl@0
|
55 |
do_test misc4-1.3 {
|
sl@0
|
56 |
sqlite3_step $stmt
|
sl@0
|
57 |
} SQLITE_DONE
|
sl@0
|
58 |
do_test misc4-1.4 {
|
sl@0
|
59 |
execsql {
|
sl@0
|
60 |
SELECT * FROM temp.t2;
|
sl@0
|
61 |
}
|
sl@0
|
62 |
} {1}
|
sl@0
|
63 |
|
sl@0
|
64 |
# Drop the temporary table, then rerun the prepared statement to
|
sl@0
|
65 |
# recreate it again. This recreates ticket #807.
|
sl@0
|
66 |
#
|
sl@0
|
67 |
do_test misc4-1.5 {
|
sl@0
|
68 |
execsql {DROP TABLE t2}
|
sl@0
|
69 |
sqlite3_reset $stmt
|
sl@0
|
70 |
sqlite3_step $stmt
|
sl@0
|
71 |
} {SQLITE_ERROR}
|
sl@0
|
72 |
do_test misc4-1.6 {
|
sl@0
|
73 |
sqlite3_finalize $stmt
|
sl@0
|
74 |
} {SQLITE_SCHEMA}
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
# Prepare but do not execute various CREATE statements. Then before
|
sl@0
|
78 |
# those statements are executed, try to use the tables, indices, views,
|
sl@0
|
79 |
# are triggers that were created.
|
sl@0
|
80 |
#
|
sl@0
|
81 |
do_test misc4-2.1 {
|
sl@0
|
82 |
set stmt [sqlite3_prepare $DB {CREATE TABLE t3(x);} -1 TAIL]
|
sl@0
|
83 |
catchsql {
|
sl@0
|
84 |
INSERT INTO t3 VALUES(1);
|
sl@0
|
85 |
}
|
sl@0
|
86 |
} {1 {no such table: t3}}
|
sl@0
|
87 |
do_test misc4-2.2 {
|
sl@0
|
88 |
sqlite3_step $stmt
|
sl@0
|
89 |
} SQLITE_DONE
|
sl@0
|
90 |
do_test misc4-2.3 {
|
sl@0
|
91 |
sqlite3_finalize $stmt
|
sl@0
|
92 |
} SQLITE_OK
|
sl@0
|
93 |
do_test misc4-2.4 {
|
sl@0
|
94 |
catchsql {
|
sl@0
|
95 |
INSERT INTO t3 VALUES(1);
|
sl@0
|
96 |
}
|
sl@0
|
97 |
} {0 {}}
|
sl@0
|
98 |
|
sl@0
|
99 |
# Ticket #966
|
sl@0
|
100 |
#
|
sl@0
|
101 |
do_test misc4-3.1 {
|
sl@0
|
102 |
execsql {
|
sl@0
|
103 |
CREATE TABLE Table1(ID integer primary key, Value TEXT);
|
sl@0
|
104 |
INSERT INTO Table1 VALUES(1, 'x');
|
sl@0
|
105 |
CREATE TABLE Table2(ID integer NOT NULL, Value TEXT);
|
sl@0
|
106 |
INSERT INTO Table2 VALUES(1, 'z');
|
sl@0
|
107 |
INSERT INTO Table2 VALUES (1, 'a');
|
sl@0
|
108 |
}
|
sl@0
|
109 |
catchsql {
|
sl@0
|
110 |
SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2 ORDER BY 1, 2;
|
sl@0
|
111 |
}
|
sl@0
|
112 |
} {1 {aggregate functions are not allowed in the GROUP BY clause}}
|
sl@0
|
113 |
ifcapable compound {
|
sl@0
|
114 |
do_test misc4-3.2 {
|
sl@0
|
115 |
execsql {
|
sl@0
|
116 |
SELECT ID, Value FROM Table1
|
sl@0
|
117 |
UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1
|
sl@0
|
118 |
ORDER BY 1, 2;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
} {1 x 1 z}
|
sl@0
|
121 |
do_test misc4-3.3 {
|
sl@0
|
122 |
catchsql {
|
sl@0
|
123 |
SELECT ID, Value FROM Table1
|
sl@0
|
124 |
UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
|
sl@0
|
125 |
ORDER BY 1, 2;
|
sl@0
|
126 |
}
|
sl@0
|
127 |
} {1 {aggregate functions are not allowed in the GROUP BY clause}}
|
sl@0
|
128 |
do_test misc4-3.4 {
|
sl@0
|
129 |
catchsql {
|
sl@0
|
130 |
SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
|
sl@0
|
131 |
UNION SELECT ID, Value FROM Table1
|
sl@0
|
132 |
ORDER BY 1, 2;
|
sl@0
|
133 |
}
|
sl@0
|
134 |
} {1 {aggregate functions are not allowed in the GROUP BY clause}}
|
sl@0
|
135 |
} ;# ifcapable compound
|
sl@0
|
136 |
|
sl@0
|
137 |
# Ticket #1047. Make sure column types are preserved in subqueries.
|
sl@0
|
138 |
#
|
sl@0
|
139 |
ifcapable subquery {
|
sl@0
|
140 |
do_test misc4-4.1 {
|
sl@0
|
141 |
execsql {
|
sl@0
|
142 |
create table a(key varchar, data varchar);
|
sl@0
|
143 |
create table b(key varchar, period integer);
|
sl@0
|
144 |
insert into a values('01','data01');
|
sl@0
|
145 |
insert into a values('+1','data+1');
|
sl@0
|
146 |
|
sl@0
|
147 |
insert into b values ('01',1);
|
sl@0
|
148 |
insert into b values ('01',2);
|
sl@0
|
149 |
insert into b values ('+1',3);
|
sl@0
|
150 |
insert into b values ('+1',4);
|
sl@0
|
151 |
|
sl@0
|
152 |
select a.*, x.*
|
sl@0
|
153 |
from a, (select key,sum(period) from b group by key) as x
|
sl@0
|
154 |
where a.key=x.key;
|
sl@0
|
155 |
}
|
sl@0
|
156 |
} {01 data01 01 3 +1 data+1 +1 7}
|
sl@0
|
157 |
|
sl@0
|
158 |
# This test case tests the same property as misc4-4.1, but it is
|
sl@0
|
159 |
# a bit smaller which makes it easier to work with while debugging.
|
sl@0
|
160 |
do_test misc4-4.2 {
|
sl@0
|
161 |
execsql {
|
sl@0
|
162 |
CREATE TABLE ab(a TEXT, b TEXT);
|
sl@0
|
163 |
INSERT INTO ab VALUES('01', '1');
|
sl@0
|
164 |
}
|
sl@0
|
165 |
execsql {
|
sl@0
|
166 |
select * from ab, (select b from ab) as x where x.b = ab.a;
|
sl@0
|
167 |
}
|
sl@0
|
168 |
} {}
|
sl@0
|
169 |
}
|
sl@0
|
170 |
|
sl@0
|
171 |
|
sl@0
|
172 |
# Ticket #1036. When creating tables from a SELECT on a view, use the
|
sl@0
|
173 |
# short names of columns.
|
sl@0
|
174 |
#
|
sl@0
|
175 |
ifcapable view {
|
sl@0
|
176 |
do_test misc4-5.1 {
|
sl@0
|
177 |
execsql {
|
sl@0
|
178 |
create table t4(a,b);
|
sl@0
|
179 |
create table t5(a,c);
|
sl@0
|
180 |
insert into t4 values (1,2);
|
sl@0
|
181 |
insert into t5 values (1,3);
|
sl@0
|
182 |
create view myview as select t4.a a from t4 inner join t5 on t4.a=t5.a;
|
sl@0
|
183 |
create table problem as select * from myview;
|
sl@0
|
184 |
}
|
sl@0
|
185 |
execsql2 {
|
sl@0
|
186 |
select * FROM problem;
|
sl@0
|
187 |
}
|
sl@0
|
188 |
} {a 1}
|
sl@0
|
189 |
do_test misc4-5.2 {
|
sl@0
|
190 |
execsql2 {
|
sl@0
|
191 |
create table t6 as select * from t4, t5;
|
sl@0
|
192 |
select * from t6;
|
sl@0
|
193 |
}
|
sl@0
|
194 |
} {a 1 b 2 a:1 1 c 3}
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
# Ticket #1086
|
sl@0
|
198 |
do_test misc4-6.1 {
|
sl@0
|
199 |
execsql {
|
sl@0
|
200 |
CREATE TABLE abc(a);
|
sl@0
|
201 |
INSERT INTO abc VALUES(1);
|
sl@0
|
202 |
CREATE TABLE def(d, e, f, PRIMARY KEY(d, e));
|
sl@0
|
203 |
}
|
sl@0
|
204 |
} {}
|
sl@0
|
205 |
do_test misc4-6.2 {
|
sl@0
|
206 |
execsql {
|
sl@0
|
207 |
SELECT a FROM abc LEFT JOIN def ON (abc.a=def.d);
|
sl@0
|
208 |
}
|
sl@0
|
209 |
} {1}
|
sl@0
|
210 |
|
sl@0
|
211 |
finish_test
|