xref: /sqlite-3.40.0/test/parser1.test (revision 6ab91a7a)
1bc622bc0Sdrh# 2014-08-24
2bc622bc0Sdrh#
3bc622bc0Sdrh# The author disclaims copyright to this source code.  In place of
4bc622bc0Sdrh# a legal notice, here is a blessing:
5bc622bc0Sdrh#
6bc622bc0Sdrh#    May you do good and not evil.
7bc622bc0Sdrh#    May you find forgiveness for yourself and forgive others.
8bc622bc0Sdrh#    May you share freely, never taking more than you give.
9bc622bc0Sdrh#
10bc622bc0Sdrh#***********************************************************************
11bc622bc0Sdrh# This file implements regression tests for SQLite library.
12bc622bc0Sdrh# The focus of this script is testing details of the SQL language parser.
13bc622bc0Sdrh#
14bc622bc0Sdrh
15bc622bc0Sdrhset testdir [file dirname $argv0]
16bc622bc0Sdrhsource $testdir/tester.tcl
17bc622bc0Sdrh
18bc622bc0Sdrhdo_catchsql_test parser1-1.1 {
19bc622bc0Sdrh  CREATE TABLE t1(
20bc622bc0Sdrh    a TEXT PRIMARY KEY,
21bc622bc0Sdrh    b TEXT,
22bc622bc0Sdrh    FOREIGN KEY(b COLLATE nocase DESC) REFERENCES t1(a COLLATE binary ASC)
23bc622bc0Sdrh  );
24108aa00aSdrh} {1 {syntax error after column name "b"}}
25108aa00aSdrh
26108aa00aSdrh
27108aa00aSdrh# Verify that a legacy schema in the sqlite_master file is allowed to have
28108aa00aSdrh# COLLATE, ASC, and DESC keywords on the id list of a FK constraint, and that
29108aa00aSdrh# those keywords are silently ignored.
30108aa00aSdrh#
31*6ab91a7aSdrhsqlite3_db_config db DEFENSIVE 0
32bc622bc0Sdrhdo_execsql_test parser1-1.2 {
33bc622bc0Sdrh  CREATE TABLE t1(
34bc622bc0Sdrh    a TEXT PRIMARY KEY,
35bc622bc0Sdrh    b TEXT,
36bc622bc0Sdrh    FOREIGN KEY(b) REFERENCES t1(a)
37bc622bc0Sdrh  );
38bc622bc0Sdrh  INSERT INTO t1 VALUES('abc',NULL),('xyz','abc');
39bc622bc0Sdrh  PRAGMA writable_schema=on;
40bc622bc0Sdrh  UPDATE sqlite_master SET sql='CREATE TABLE t1(
41bc622bc0Sdrh    a TEXT PRIMARY KEY,
42bc622bc0Sdrh    b TEXT,
43bc622bc0Sdrh    FOREIGN KEY(b COLLATE nocase) REFERENCES t1(a)
44bc622bc0Sdrh  )' WHERE name='t1';
45bc622bc0Sdrh  SELECT name FROM sqlite_master WHERE sql LIKE '%collate%';
46bc622bc0Sdrh} {t1}
47bc622bc0Sdrhsqlite3 db2 test.db
48bc622bc0Sdrhdo_test parser1-1.3 {
49bc622bc0Sdrh  sqlite3 db2 test.db
50bc622bc0Sdrh  db2 eval {SELECT * FROM t1 ORDER BY 1}
51bc622bc0Sdrh} {abc {} xyz abc}
52108aa00aSdrhdb2 close
53bc622bc0Sdrh
54108aa00aSdrhdo_execsql_test parser1-1.4 {
55108aa00aSdrh  UPDATE sqlite_master SET sql='CREATE TABLE t1(
56108aa00aSdrh    a TEXT PRIMARY KEY,
57108aa00aSdrh    b TEXT,
58108aa00aSdrh    FOREIGN KEY(b ASC) REFERENCES t1(a)
59108aa00aSdrh  )' WHERE name='t1';
60108aa00aSdrh  SELECT name FROM sqlite_master WHERE sql LIKE '%ASC%';
61108aa00aSdrh} {t1}
62108aa00aSdrhsqlite3 db2 test.db
63108aa00aSdrhdo_test parser1-1.5 {
64108aa00aSdrh  sqlite3 db2 test.db
65108aa00aSdrh  db2 eval {SELECT * FROM t1 ORDER BY 1}
66108aa00aSdrh} {abc {} xyz abc}
67108aa00aSdrhdb2 close
68bc622bc0Sdrh
69bc622bc0Sdrhdo_catchsql_test parser1-2.1 {
70bc622bc0Sdrh  WITH RECURSIVE
71bc622bc0Sdrh    c(x COLLATE binary) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<5)
72bc622bc0Sdrh  SELECT x FROM c;
73bc622bc0Sdrh} {1 {syntax error after column name "x"}}
74bc622bc0Sdrhdo_catchsql_test parser1-2.2 {
75bc622bc0Sdrh  WITH RECURSIVE
76bc622bc0Sdrh    c(x ASC) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<5)
77bc622bc0Sdrh  SELECT x FROM c;
78bc622bc0Sdrh} {1 {syntax error after column name "x"}}
79bc622bc0Sdrh
804aff119fSdrh# Verify that the comma between multiple table constraints is
814aff119fSdrh# optional.
824aff119fSdrh#
834aff119fSdrh# The missing comma is technically a syntax error.  But we have to support
844aff119fSdrh# it because there might be legacy databases that omit the commas in their
854aff119fSdrh# sqlite_master tables.
864aff119fSdrh#
874aff119fSdrhdo_execsql_test parser1-3.1 {
884aff119fSdrh  CREATE TABLE t300(id INTEGER PRIMARY KEY);
894aff119fSdrh  CREATE TABLE t301(
904aff119fSdrh    id INTEGER PRIMARY KEY,
914aff119fSdrh    c1 INTEGER NOT NULL,
924aff119fSdrh    c2 INTEGER NOT NULL,
934aff119fSdrh    c3 BOOLEAN NOT NULL DEFAULT 0,
944aff119fSdrh    FOREIGN KEY(c1) REFERENCES t300(id) ON DELETE CASCADE ON UPDATE RESTRICT
954aff119fSdrh        /* no comma */
964aff119fSdrh    FOREIGN KEY(c2) REFERENCES t300(id) ON DELETE CASCADE ON UPDATE RESTRICT
974aff119fSdrh        /* no comma */
984aff119fSdrh    UNIQUE(c1, c2)
994aff119fSdrh  );
1004aff119fSdrh  PRAGMA foreign_key_list(t301);
1014aff119fSdrh} {0 0 t300 c2 id RESTRICT CASCADE NONE 1 0 t300 c1 id RESTRICT CASCADE NONE}
1024aff119fSdrh
103bc622bc0Sdrhfinish_test
104