3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for miscellanous features that were
14 # left out of other test files.
16 # $Id: misc4.test,v 1.23 2007/12/08 18:01:31 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # Prepare a statement that will create a temporary table. Then do
22 # a rollback. Then try to execute the prepared statement.
25 set DB [sqlite3_connection_pointer db]
28 INSERT INTO t1 VALUES(1);
34 set sql {CREATE TEMP TABLE t2 AS SELECT * FROM t1}
35 set stmt [sqlite3_prepare $DB $sql -1 TAIL]
38 CREATE TABLE t3(a,b,c);
39 INSERT INTO t1 SELECT * FROM t1;
44 # Because the previous transaction included a DDL statement and
45 # was rolled back, statement $stmt was marked as expired. Executing it
46 # now returns SQLITE_SCHEMA.
48 list [sqlite3_step $stmt] [sqlite3_finalize $stmt]
49 } {SQLITE_ERROR SQLITE_SCHEMA}
51 set stmt [sqlite3_prepare $DB $sql -1 TAIL]
60 SELECT * FROM temp.t2;
64 # Drop the temporary table, then rerun the prepared statement to
65 # recreate it again. This recreates ticket #807.
68 execsql {DROP TABLE t2}
73 sqlite3_finalize $stmt
77 # Prepare but do not execute various CREATE statements. Then before
78 # those statements are executed, try to use the tables, indices, views,
79 # are triggers that were created.
82 set stmt [sqlite3_prepare $DB {CREATE TABLE t3(x);} -1 TAIL]
84 INSERT INTO t3 VALUES(1);
86 } {1 {no such table: t3}}
91 sqlite3_finalize $stmt
95 INSERT INTO t3 VALUES(1);
103 CREATE TABLE Table1(ID integer primary key, Value TEXT);
104 INSERT INTO Table1 VALUES(1, 'x');
105 CREATE TABLE Table2(ID integer NOT NULL, Value TEXT);
106 INSERT INTO Table2 VALUES(1, 'z');
107 INSERT INTO Table2 VALUES (1, 'a');
110 SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2 ORDER BY 1, 2;
112 } {1 {aggregate functions are not allowed in the GROUP BY clause}}
116 SELECT ID, Value FROM Table1
117 UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1
123 SELECT ID, Value FROM Table1
124 UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
127 } {1 {aggregate functions are not allowed in the GROUP BY clause}}
130 SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
131 UNION SELECT ID, Value FROM Table1
134 } {1 {aggregate functions are not allowed in the GROUP BY clause}}
135 } ;# ifcapable compound
137 # Ticket #1047. Make sure column types are preserved in subqueries.
142 create table a(key varchar, data varchar);
143 create table b(key varchar, period integer);
144 insert into a values('01','data01');
145 insert into a values('+1','data+1');
147 insert into b values ('01',1);
148 insert into b values ('01',2);
149 insert into b values ('+1',3);
150 insert into b values ('+1',4);
153 from a, (select key,sum(period) from b group by key) as x
154 where a.key=x.key order by 1 desc;
156 } {01 data01 01 3 +1 data+1 +1 7}
158 # This test case tests the same property as misc4-4.1, but it is
159 # a bit smaller which makes it easier to work with while debugging.
162 CREATE TABLE ab(a TEXT, b TEXT);
163 INSERT INTO ab VALUES('01', '1');
166 select * from ab, (select b from ab) as x where x.b = ab.a;
172 # Ticket #1036. When creating tables from a SELECT on a view, use the
173 # short names of columns.
178 create table t4(a,b);
179 create table t5(a,c);
180 insert into t4 values (1,2);
181 insert into t5 values (1,3);
182 create view myview as select t4.a a from t4 inner join t5 on t4.a=t5.a;
183 create table problem as select * from myview;
186 select * FROM problem;
191 create table t6 as select * from t4, t5;
194 } {a 1 b 2 a:1 1 c 3}
201 INSERT INTO abc VALUES(1);
202 CREATE TABLE def(d, e, f, PRIMARY KEY(d, e));
207 SELECT a FROM abc LEFT JOIN def ON (abc.a=def.d);
211 # 2015-05-15. Error message formatting problem.
215 sqlite3_db_config db DEFENSIVE 0
216 do_catchsql_test misc4-7.1 {
218 PRAGMA writable_schema=ON;
219 UPDATE sqlite_master SET sql='CREATE TABLE [M%s%s%s%s%s%s%s%s%s%s%s%s%s';
221 } {1 {unrecognized token: "[M%s%s%s%s%s%s%s%s%s%s%s%s%s"}}
223 # 2015-05-18. Use of ephermeral Mem content after the cursor that holds
224 # the canonical content has moved on.
226 do_execsql_test misc4-7.2 {
227 CREATE TABLE t0(a,b);
228 INSERT INTO t0 VALUES(1,0),(2,0);
229 UPDATE t0 SET b=9 WHERE a AND (SELECT a FROM t0 WHERE a);
230 SELECT * FROM t0 ORDER BY +a;