xref: /sqlite-3.40.0/test/e_resolve.test (revision b3366b99)
1798b0072Sdan# 2010 November 30
2798b0072Sdan#
3798b0072Sdan# The author disclaims copyright to this source code.  In place of
4798b0072Sdan# a legal notice, here is a blessing:
5798b0072Sdan#
6798b0072Sdan#    May you do good and not evil.
7798b0072Sdan#    May you find forgiveness for yourself and forgive others.
8798b0072Sdan#    May you share freely, never taking more than you give.
9798b0072Sdan#
10798b0072Sdan#***********************************************************************
11798b0072Sdan#
12798b0072Sdan# This file implements tests to verify that the "testable statements" in
13798b0072Sdan# the lang_naming.html document are correct.
14798b0072Sdan#
15798b0072Sdan
16798b0072Sdanset testdir [file dirname $argv0]
17798b0072Sdansource $testdir/tester.tcl
18798b0072Sdanset ::testprefix e_resolve
19798b0072Sdan
20798b0072Sdan# An example database schema for testing name resolution:
21798b0072Sdan#
22798b0072Sdanset schema {
23798b0072Sdan  ATTACH 'test.db2' AS at1;
24798b0072Sdan  ATTACH 'test.db3' AS at2;
25798b0072Sdan
26798b0072Sdan  CREATE TABLE   temp.n1(x, y); INSERT INTO temp.n1 VALUES('temp', 'n1');
27798b0072Sdan  CREATE TRIGGER temp.n3 AFTER INSERT ON n1 BEGIN SELECT 1; END;
28798b0072Sdan  CREATE INDEX   temp.n4 ON n1(x, y);
29798b0072Sdan
30798b0072Sdan  CREATE TABLE   main.n1(x, y); INSERT INTO main.n1 VALUES('main', 'n1');
31798b0072Sdan  CREATE TABLE   main.n2(x, y); INSERT INTO main.n2 VALUES('main', 'n2');
32798b0072Sdan  CREATE INDEX   main.n3 ON n2(y, x);
33798b0072Sdan  CREATE TRIGGER main.n4 BEFORE INSERT ON n2 BEGIN SELECT 1; END;
34798b0072Sdan
35798b0072Sdan  CREATE TABLE   at1.n1(x, y);  INSERT INTO at1.n1 VALUES('at1', 'n1');
36798b0072Sdan  CREATE TABLE   at1.n2(x, y);  INSERT INTO at1.n2 VALUES('at1', 'n2');
37798b0072Sdan  CREATE TABLE   at1.n3(x, y);  INSERT INTO at1.n3 VALUES('at1', 'n3');
38798b0072Sdan
39798b0072Sdan  CREATE TABLE   at2.n1(x, y);  INSERT INTO at2.n1 VALUES('at2', 'n1');
40798b0072Sdan  CREATE TABLE   at2.n2(x, y);  INSERT INTO at2.n2 VALUES('at2', 'n2');
41798b0072Sdan  CREATE TABLE   at2.n3(x, y);  INSERT INTO at2.n3 VALUES('at2', 'n3');
42798b0072Sdan  CREATE TABLE   at2.n4(x, y);  INSERT INTO at2.n4 VALUES('at2', 'n4');
43798b0072Sdan  CREATE TRIGGER at2.n4 BEFORE INSERT ON n4 BEGIN SELECT 1; END;
44798b0072Sdan}
45798b0072Sdan
46798b0072Sdanproc resolve_reopen_db {} {
47798b0072Sdan  db close
48798b0072Sdan  forcedelete test.db test.db2 test.db3
49798b0072Sdan  sqlite3 db test.db
50798b0072Sdan  db eval $::schema
51798b0072Sdan}
52798b0072Sdan
53798b0072Sdan
54798b0072Sdan
55798b0072Sdan# EVIDENCE-OF: R-33528-20612 If no database is specified as part of the
56798b0072Sdan# object reference, then SQLite searches the main, temp and all attached
57798b0072Sdan# databases for an object with a matching name. The temp database is
58798b0072Sdan# searched first, followed by the main database, followed all attached
59798b0072Sdan# databases in the order that they were attached. The reference resolves
60798b0072Sdan# to the first match found.
61798b0072Sdan#
62798b0072Sdanresolve_reopen_db
63798b0072Sdando_execsql_test 1.1 { SELECT * FROM n1 } {temp n1}
64798b0072Sdando_execsql_test 1.2 { SELECT * FROM n2 } {main n2}
65798b0072Sdando_execsql_test 1.3 { SELECT * FROM n3 } {at1  n3}
66798b0072Sdando_execsql_test 1.4 { SELECT * FROM n4 } {at2  n4}
67798b0072Sdan
68*b3366b99Sdrh# EVIDENCE-OF: R-00634-08585 If a schema name is specified as part of an
69*b3366b99Sdrh# object reference, it must be either "main", or "temp" or the
70*b3366b99Sdrh# schema-name of an attached database.
71798b0072Sdan#
72798b0072Sdan#   Or else it is a "no such table: xxx" error.
73798b0072Sdan#
74798b0072Sdanresolve_reopen_db
75798b0072Sdando_execsql_test 2.1.1 { SELECT * FROM main.n1 } {main n1}
76798b0072Sdando_execsql_test 2.1.2 { SELECT * FROM temp.n1 } {temp n1}
77798b0072Sdando_execsql_test 2.1.3 { SELECT * FROM at1.n1 } {at1 n1}
78798b0072Sdando_execsql_test 2.1.4 { SELECT * FROM at2.n1 } {at2 n1}
79798b0072Sdan
80798b0072Sdando_catchsql_test 2.2 { SELECT * FROM xxx.n1 } {1 {no such table: xxx.n1}}
81798b0072Sdan
82*b3366b99Sdrh# EVIDENCE-OF: R-17446-42210 Like other SQL identifiers, schema names
83798b0072Sdan# are case-insensitive.
84798b0072Sdan#
85798b0072Sdanresolve_reopen_db
86798b0072Sdando_execsql_test 3.1 { SELECT * FROM MAIN.n1 } {main n1}
87798b0072Sdando_execsql_test 3.2 { SELECT * FROM tEmP.n1 } {temp n1}
88798b0072Sdando_execsql_test 3.3 { SELECT * FROM aT1.n1 } {at1 n1}
89798b0072Sdando_execsql_test 3.4 { SELECT * FROM At2.n1 } {at2 n1}
90798b0072Sdan
91*b3366b99Sdrh# EVIDENCE-OF: R-14755-58619 If a schema name is specified, then only
92*b3366b99Sdrh# that one schema is searched for the named object.
93798b0072Sdan#
94798b0072Sdando_catchsql_test 4.1 { SELECT * FROM temp.n2 } {1 {no such table: temp.n2}}
95798b0072Sdando_catchsql_test 4.2 { SELECT * FROM main.n2 } {0 {main n2}}
96798b0072Sdando_catchsql_test 4.3 { SELECT * FROM at1.n2 }  {0 {at1 n2}}
97798b0072Sdando_catchsql_test 4.4 { SELECT * FROM at2.n2 }  {0 {at2 n2}}
98798b0072Sdan
99798b0072Sdan# EVIDENCE-OF: R-08951-19801 When searching database schemas for a named
100798b0072Sdan# object, objects of types that cannot be used in the context of the
101798b0072Sdan# reference are always ignored.
102798b0072Sdan#
103798b0072Sdan#   In this case, "types that cannot be used" are triggers and indexes.
104798b0072Sdan#   The temp and main databases both contain triggers and indexes named
105798b0072Sdan#   "n3" and "n4". Database "at2" contains a trigger called "n4". And yet:
106798b0072Sdan#
107798b0072Sdando_execsql_test 5.1 { SELECT * FROM n3 } {at1  n3}
108798b0072Sdando_execsql_test 5.2 { SELECT * FROM n4 } {at2  n4}
109798b0072Sdan
110798b0072Sdan#-------------------------------------------------------------------------
111798b0072Sdan# EVIDENCE-OF: R-37286-42536
112798b0072Sdan#
113798b0072Sdandb close
114798b0072Sdanforcedelete test.db file.db
115798b0072Sdansqlite3 db test.db
116798b0072Sdando_execsql_test 6.1 {
117798b0072Sdan  ATTACH 'file.db' AS aux;
118798b0072Sdan  CREATE TABLE t1(x, y);
119798b0072Sdan  CREATE TEMP TABLE t1(x, y);
120798b0072Sdan  CREATE TABLE aux.t1(x, y);
121798b0072Sdan}
122798b0072Sdan
123798b0072Sdando_execsql_test  6.2.0 { DROP TABLE t1 }
124798b0072Sdando_catchsql_test 6.2.1 { SELECT * FROM temp.t1 } {1 {no such table: temp.t1}}
125798b0072Sdando_catchsql_test 6.2.2 { SELECT * FROM main.t1 } {0 {}}
126798b0072Sdando_catchsql_test 6.2.3 { SELECT * FROM aux.t1  } {0 {}}
127798b0072Sdan
128798b0072Sdando_execsql_test  6.3.0 { DROP TABLE t1 }
129798b0072Sdando_catchsql_test 6.3.1 { SELECT * FROM main.t1 } {1 {no such table: main.t1}}
130798b0072Sdando_catchsql_test 6.3.3 { SELECT * FROM aux.t1  } {0 {}}
131798b0072Sdan
132798b0072Sdando_execsql_test  6.4.0 { DROP TABLE t1 }
133798b0072Sdando_catchsql_test 6.4.1 { SELECT * FROM aux.t1 } {1 {no such table: aux.t1}}
134798b0072Sdan
135798b0072Sdanfinish_test
136