interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / shadowname.test

114 lines · 3088 bytes Raw Blame History
1# 2026 October 29
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12# Tests to verify that, in DEFENSIVE mode, ordinary objects may not be
13# created using the name of a shadow table of an existing virtual table.
14# Including when the schema has not yet been loaded, or when a TEMP
15# table or a table in some other schema has the same name as the
16# virtual table.
17#
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20set testprefix shadowname
21
22ifcapable !fts3 {
23 finish_test
24 return
25}
26
27# Create virtual table "v" in database file $file, then drop its
28# v_segments shadow table so that the name is available.
29#
30proc make_db {file} {
31 forcedelete $file
32 sqlite3 dbx $file
33 sqlite3_db_config dbx DEFENSIVE 0
34 dbx eval {
35 CREATE VIRTUAL TABLE v USING fts3;
36 DROP TABLE v_segments;
37 }
38 dbx close
39}
40
41#-------------------------------------------------------------------------
42# Schema not yet loaded when CREATE TABLE/VIEW is parsed.
43#
44foreach {tn sql} {
45 1 { CREATE TABLE v_segments(x) }
46 2 { CREATE VIEW v_segments AS SELECT 1 }
47} {
48 make_db test.db2
49 sqlite3 db2 test.db2
50 sqlite3_db_config db2 DEFENSIVE 1
51 do_test 1.$tn {
52 catchsql $sql db2
53 } {1 {object name reserved for internal use: v_segments}}
54 db2 close
55}
56
57#-------------------------------------------------------------------------
58# A TEMP table with the same name as the virtual table.
59#
60foreach {tn sql res} {
61 1 {
62 CREATE TABLE main.v_segments(x);
63 } {1 {object name reserved for internal use: v_segments}}
64
65 2 {
66 CREATE TABLE main.t(a);
67 CREATE INDEX main.v_segments ON t(a);
68 } {1 {object name reserved for internal use: v_segments}}
69
70 3 {
71 CREATE TEMP TABLE t2(x);
72 CREATE TEMP TRIGGER tr AFTER INSERT ON t2 BEGIN
73 DELETE FROM v_segments;
74 END;
75 } {1 {trigger "tr" may not write to shadow table "v_segments"}}
76
77 4 {
78 CREATE TEMP TABLE v_segments(x);
79 } {0 {}}
80} {
81 # Case 4: creating temp.v_segments is allowed, as there is no virtual
82 # table named "v" in the temp schema.
83 make_db test.db2
84 sqlite3 db2 test.db2
85 sqlite3_db_config db2 DEFENSIVE 1
86 do_test 2.$tn {
87 catchsql "CREATE TEMP TABLE v(a); $sql" db2
88 } $res
89 db2 close
90}
91
92#-------------------------------------------------------------------------
93# An ordinary table in "main" with the same name as a virtual table in
94# an attached database.
95#
96make_db test.db3
97forcedelete test.db2
98sqlite3 db2 test.db2
99sqlite3_db_config db2 DEFENSIVE 1
100do_test 3.1 {
101 catchsql {
102 ATTACH 'test.db3' AS aux;
103 CREATE TABLE main.v(a);
104 CREATE TABLE aux.v_segments(x);
105 } db2
106} {1 {object name reserved for internal use: v_segments}}
107
108# Creating main.v_segments is allowed - main.v is not a virtual table.
109do_test 3.2 {
110 catchsql { CREATE TABLE main.v_segments(x) } db2
111} {0 {}}
112db2 close
113
114finish_test