xref: /sqlite-3.40.0/test/misc4.test (revision 6ab91a7a)
183ca160aSdrh# 2004 Jun 27
283ca160aSdrh#
383ca160aSdrh# The author disclaims copyright to this source code.  In place of
483ca160aSdrh# a legal notice, here is a blessing:
583ca160aSdrh#
683ca160aSdrh#    May you do good and not evil.
783ca160aSdrh#    May you find forgiveness for yourself and forgive others.
883ca160aSdrh#    May you share freely, never taking more than you give.
983ca160aSdrh#
1083ca160aSdrh#***********************************************************************
1183ca160aSdrh# This file implements regression tests for SQLite library.
1283ca160aSdrh#
1383ca160aSdrh# This file implements tests for miscellanous features that were
1483ca160aSdrh# left out of other test files.
1583ca160aSdrh#
169213d9e5Sdrh# $Id: misc4.test,v 1.23 2007/12/08 18:01:31 drh Exp $
1783ca160aSdrh
1883ca160aSdrhset testdir [file dirname $argv0]
1983ca160aSdrhsource $testdir/tester.tcl
2083ca160aSdrh
2183ca160aSdrh# Prepare a statement that will create a temporary table.  Then do
2283ca160aSdrh# a rollback.  Then try to execute the prepared statement.
2383ca160aSdrh#
2483ca160aSdrhdo_test misc4-1.1 {
25dddca286Sdrh  set DB [sqlite3_connection_pointer db]
2683ca160aSdrh  execsql {
2783ca160aSdrh    CREATE TABLE t1(x);
2883ca160aSdrh    INSERT INTO t1 VALUES(1);
2983ca160aSdrh  }
303f7d4e49Sdrh} {}
3153c0f748Sdanielk1977
3253c0f748Sdanielk1977ifcapable tempdb {
333f7d4e49Sdrh  do_test misc4-1.2 {
3483ca160aSdrh    set sql {CREATE TEMP TABLE t2 AS SELECT * FROM t1}
3583ca160aSdrh    set stmt [sqlite3_prepare $DB $sql -1 TAIL]
3683ca160aSdrh    execsql {
3783ca160aSdrh      BEGIN;
3883ca160aSdrh      CREATE TABLE t3(a,b,c);
3983ca160aSdrh      INSERT INTO t1 SELECT * FROM t1;
4083ca160aSdrh      ROLLBACK;
4183ca160aSdrh    }
423f7d4e49Sdrh  } {}
43ea4d9e2dSdanielk1977
44ea4d9e2dSdanielk1977  # Because the previous transaction included a DDL statement and
45ea4d9e2dSdanielk1977  # was rolled back, statement $stmt was marked as expired. Executing it
46ea4d9e2dSdanielk1977  # now returns SQLITE_SCHEMA.
47ea4d9e2dSdanielk1977  do_test misc4-1.2.1 {
48ea4d9e2dSdanielk1977    list [sqlite3_step $stmt] [sqlite3_finalize $stmt]
49ea4d9e2dSdanielk1977  } {SQLITE_ERROR SQLITE_SCHEMA}
50ea4d9e2dSdanielk1977  do_test misc4-1.2.2 {
51ea4d9e2dSdanielk1977    set stmt [sqlite3_prepare $DB $sql -1 TAIL]
52ea4d9e2dSdanielk1977    set TAIL
53ea4d9e2dSdanielk1977  } {}
54ea4d9e2dSdanielk1977
553f7d4e49Sdrh  do_test misc4-1.3 {
5683ca160aSdrh    sqlite3_step $stmt
573f7d4e49Sdrh  } SQLITE_DONE
583f7d4e49Sdrh  do_test misc4-1.4 {
5983ca160aSdrh    execsql {
6083ca160aSdrh      SELECT * FROM temp.t2;
6183ca160aSdrh    }
6283ca160aSdrh  } {1}
63c275b4eaSdrh
64c275b4eaSdrh  # Drop the temporary table, then rerun the prepared  statement to
65c275b4eaSdrh  # recreate it again.  This recreates ticket #807.
66c275b4eaSdrh  #
673f7d4e49Sdrh  do_test misc4-1.5 {
68c275b4eaSdrh    execsql {DROP TABLE t2}
69c275b4eaSdrh    sqlite3_reset $stmt
70c275b4eaSdrh    sqlite3_step $stmt
71c275b4eaSdrh  } {SQLITE_ERROR}
723f7d4e49Sdrh  do_test misc4-1.6 {
73c275b4eaSdrh    sqlite3_finalize $stmt
74c275b4eaSdrh  } {SQLITE_SCHEMA}
7553c0f748Sdanielk1977}
7683ca160aSdrh
77234c39dfSdrh# Prepare but do not execute various CREATE statements.  Then before
78234c39dfSdrh# those statements are executed, try to use the tables, indices, views,
79234c39dfSdrh# are triggers that were created.
80234c39dfSdrh#
81234c39dfSdrhdo_test misc4-2.1 {
82234c39dfSdrh  set stmt [sqlite3_prepare $DB {CREATE TABLE t3(x);} -1 TAIL]
83234c39dfSdrh  catchsql {
84234c39dfSdrh    INSERT INTO t3 VALUES(1);
85234c39dfSdrh  }
86234c39dfSdrh} {1 {no such table: t3}}
87234c39dfSdrhdo_test misc4-2.2 {
88234c39dfSdrh  sqlite3_step $stmt
89234c39dfSdrh} SQLITE_DONE
90234c39dfSdrhdo_test misc4-2.3 {
91234c39dfSdrh  sqlite3_finalize $stmt
92234c39dfSdrh} SQLITE_OK
93234c39dfSdrhdo_test misc4-2.4 {
94234c39dfSdrh  catchsql {
95234c39dfSdrh    INSERT INTO t3 VALUES(1);
96234c39dfSdrh  }
97234c39dfSdrh} {0 {}}
98956bc92cSdrh
996138df56Sdrh# Ticket #966
1006138df56Sdrh#
1016138df56Sdrhdo_test misc4-3.1 {
1026138df56Sdrh  execsql {
1036138df56Sdrh    CREATE TABLE Table1(ID integer primary key, Value TEXT);
1046138df56Sdrh    INSERT INTO Table1 VALUES(1, 'x');
1056138df56Sdrh    CREATE TABLE Table2(ID integer NOT NULL, Value TEXT);
1066138df56Sdrh    INSERT INTO Table2 VALUES(1, 'z');
1076138df56Sdrh    INSERT INTO Table2 VALUES (1, 'a');
1089213d9e5Sdrh  }
1099213d9e5Sdrh  catchsql {
1109213d9e5Sdrh    SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2 ORDER BY 1, 2;
1119213d9e5Sdrh  }
1129213d9e5Sdrh} {1 {aggregate functions are not allowed in the GROUP BY clause}}
1139213d9e5Sdrhifcapable compound {
1149213d9e5Sdrh  do_test misc4-3.2 {
1159213d9e5Sdrh    execsql {
1166138df56Sdrh      SELECT ID, Value FROM Table1
11713449892Sdrh         UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1
1186138df56Sdrh      ORDER BY 1, 2;
1196138df56Sdrh    }
1205774b806Sdrh  } {1 x 1 z}
1219213d9e5Sdrh  do_test misc4-3.3 {
1229d2985c7Sdrh    catchsql {
1239d2985c7Sdrh      SELECT ID, Value FROM Table1
1249d2985c7Sdrh         UNION SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
1259d2985c7Sdrh      ORDER BY 1, 2;
1269d2985c7Sdrh    }
1279d2985c7Sdrh  } {1 {aggregate functions are not allowed in the GROUP BY clause}}
1289213d9e5Sdrh  do_test misc4-3.4 {
1299213d9e5Sdrh    catchsql {
1309213d9e5Sdrh      SELECT ID, max(Value) FROM Table2 GROUP BY 1, 2
1319213d9e5Sdrh         UNION SELECT ID, Value FROM Table1
1329213d9e5Sdrh      ORDER BY 1, 2;
1339213d9e5Sdrh    }
1349213d9e5Sdrh  } {1 {aggregate functions are not allowed in the GROUP BY clause}}
13527c77438Sdanielk1977} ;# ifcapable compound
1366138df56Sdrh
1379b3187e1Sdrh# Ticket #1047.  Make sure column types are preserved in subqueries.
1389b3187e1Sdrh#
1393e8c37e7Sdanielk1977ifcapable subquery {
1409b3187e1Sdrh  do_test misc4-4.1 {
1419b3187e1Sdrh    execsql {
1429b3187e1Sdrh      create table a(key varchar, data varchar);
1439b3187e1Sdrh      create table b(key varchar, period integer);
1449b3187e1Sdrh      insert into a values('01','data01');
1459b3187e1Sdrh      insert into a values('+1','data+1');
1469b3187e1Sdrh
1479b3187e1Sdrh      insert into b values ('01',1);
1489b3187e1Sdrh      insert into b values ('01',2);
1499b3187e1Sdrh      insert into b values ('+1',3);
1509b3187e1Sdrh      insert into b values ('+1',4);
1519b3187e1Sdrh
1529b3187e1Sdrh      select a.*, x.*
1539b3187e1Sdrh        from a, (select key,sum(period) from b group by key) as x
15415564055Sdrh        where a.key=x.key order by 1 desc;
1559b3187e1Sdrh    }
1563d1d95e6Sdrh  } {01 data01 01 3 +1 data+1 +1 7}
157b3bce662Sdanielk1977
158b3bce662Sdanielk1977  # This test case tests the same property as misc4-4.1, but it is
159b3bce662Sdanielk1977  # a bit smaller which makes it easier to work with while debugging.
160b3bce662Sdanielk1977  do_test misc4-4.2 {
161b3bce662Sdanielk1977    execsql {
162b3bce662Sdanielk1977      CREATE TABLE ab(a TEXT, b TEXT);
163b3bce662Sdanielk1977      INSERT INTO ab VALUES('01', '1');
1643e8c37e7Sdanielk1977    }
165b3bce662Sdanielk1977    execsql {
166b3bce662Sdanielk1977      select * from ab, (select b from ab) as x where x.b = ab.a;
167b3bce662Sdanielk1977    }
168b3bce662Sdanielk1977  } {}
169b3bce662Sdanielk1977}
170b3bce662Sdanielk1977
1719b3187e1Sdrh
17279d5f63fSdrh# Ticket #1036.  When creating tables from a SELECT on a view, use the
17379d5f63fSdrh# short names of columns.
17479d5f63fSdrh#
1754489f9bdSdanielk1977ifcapable view {
17679d5f63fSdrh  do_test misc4-5.1 {
17779d5f63fSdrh    execsql {
17879d5f63fSdrh      create table t4(a,b);
17979d5f63fSdrh      create table t5(a,c);
18079d5f63fSdrh      insert into t4 values (1,2);
18179d5f63fSdrh      insert into t5 values (1,3);
18279d5f63fSdrh      create view myview as select t4.a a from t4 inner join t5 on t4.a=t5.a;
18379d5f63fSdrh      create table problem as select * from myview;
18479d5f63fSdrh    }
18579d5f63fSdrh    execsql2 {
18679d5f63fSdrh      select * FROM problem;
18779d5f63fSdrh    }
18879d5f63fSdrh  } {a 1}
18979d5f63fSdrh  do_test misc4-5.2 {
19079d5f63fSdrh    execsql2 {
19179d5f63fSdrh      create table t6 as select * from t4, t5;
19279d5f63fSdrh      select * from t6;
19379d5f63fSdrh    }
19479d5f63fSdrh  } {a 1 b 2 a:1 1 c 3}
1954489f9bdSdanielk1977}
19679d5f63fSdrh
1976c18b6e0Sdanielk1977# Ticket #1086
1986c18b6e0Sdanielk1977do_test misc4-6.1 {
1996c18b6e0Sdanielk1977  execsql {
2006c18b6e0Sdanielk1977    CREATE TABLE abc(a);
2016c18b6e0Sdanielk1977    INSERT INTO abc VALUES(1);
2026c18b6e0Sdanielk1977    CREATE TABLE def(d, e, f, PRIMARY KEY(d, e));
2036c18b6e0Sdanielk1977  }
2046c18b6e0Sdanielk1977} {}
2056c18b6e0Sdanielk1977do_test misc4-6.2 {
2066c18b6e0Sdanielk1977  execsql {
2076c18b6e0Sdanielk1977    SELECT a FROM abc LEFT JOIN def ON (abc.a=def.d);
2086c18b6e0Sdanielk1977  }
2096c18b6e0Sdanielk1977} {1}
2106c18b6e0Sdanielk1977
21122c17b8bSdrh# 2015-05-15.  Error message formatting problem.
21222c17b8bSdrh#
21322c17b8bSdrhdb close
21422c17b8bSdrhsqlite3 db :memory:
215*6ab91a7aSdrhsqlite3_db_config db DEFENSIVE 0
21622c17b8bSdrhdo_catchsql_test misc4-7.1 {
21722c17b8bSdrh  CREATE TABLE t7(x);
21822c17b8bSdrh  PRAGMA writable_schema=ON;
21922c17b8bSdrh  UPDATE sqlite_master SET sql='CREATE TABLE [M%s%s%s%s%s%s%s%s%s%s%s%s%s';
22022c17b8bSdrh  VACUUM;
22122c17b8bSdrh} {1 {unrecognized token: "[M%s%s%s%s%s%s%s%s%s%s%s%s%s"}}
22222c17b8bSdrh
223df553659Sdrh# 2015-05-18.  Use of ephermeral Mem content after the cursor that holds
224df553659Sdrh# the canonical content has moved on.
225df553659Sdrh#
226df553659Sdrhdo_execsql_test misc4-7.2 {
227df553659Sdrh  CREATE TABLE t0(a,b);
228df553659Sdrh  INSERT INTO t0 VALUES(1,0),(2,0);
229df553659Sdrh  UPDATE t0 SET b=9 WHERE a AND (SELECT a FROM t0 WHERE a);
230df553659Sdrh  SELECT * FROM t0 ORDER BY +a;
231df553659Sdrh} {1 9 2 9}
232df553659Sdrh
23383ca160aSdrhfinish_test
234