summaryrefslogtreecommitdiffstats
path: root/test/pragma4.test
blob: 97c62261b497c95fd58196dded757d78992f096f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# 2017 Jan 4
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix pragma4

proc do_pragma_ncol_test {tn sql nCol} {
  set ::stmt 0
  set ::stmt [sqlite3_prepare_v2 db $sql -1 dummy]
  uplevel [list do_test $tn { sqlite3_column_count $::stmt } $nCol]
  sqlite3_finalize $::stmt
}

# If there is no RHS argument, the following PRAGMA statements operate as
# queries, returning a single row containing a single column.
#
# Or, if there is RHS argument, they return zero rows of zero columns.
#
foreach {tn sql} {
  1 "PRAGMA application_id = 10"
  2 "PRAGMA automatic_index = 1"
  3 "PRAGMA auto_vacuum = 1"
  4 "PRAGMA cache_size = -100"
  5 "PRAGMA cache_spill = 1"
  6 "PRAGMA cell_size_check = 1"
  7 "PRAGMA checkpoint_fullfsync = 1"
  8 "PRAGMA count_changes = 1"
  9 "PRAGMA default_cache_size = 100"
 10 "PRAGMA defer_foreign_keys = 1"
 11 "PRAGMA empty_result_callbacks = 1"
 12 "PRAGMA encoding = 'utf-8'"
 13 "PRAGMA foreign_keys = 1"
 14 "PRAGMA full_column_names = 1"
 15 "PRAGMA fullfsync = 1"
 16 "PRAGMA ignore_check_constraints = 1"
 18 "PRAGMA page_size = 511"
 19 "PRAGMA page_size = 512"
 20 "PRAGMA query_only = false"
 21 "PRAGMA read_uncommitted = true"
 22 "PRAGMA recursive_triggers = false"
 23 "PRAGMA reverse_unordered_selects = false"
 24 "PRAGMA schema_version = 211"
 25 "PRAGMA short_column_names = 1"
 26 "PRAGMA synchronous = full"
 29 "PRAGMA temp_store = memory"
 30 "PRAGMA user_version = 405"
 31 "PRAGMA writable_schema = 1"
} {
  reset_db

  # Without RHS:
  do_pragma_ncol_test 1.$tn.1 [lindex [split $sql =] 0] 1

  # With RHS:
  do_pragma_ncol_test 1.$tn.2 $sql  0
}

# These pragmas should never return any values.
#
foreach {tn sql} {
  1 "PRAGMA shrink_memory"
  2 "PRAGMA shrink_memory = 10"
  3 "PRAGMA case_sensitive_like = 0"
  4 "PRAGMA case_sensitive_like = 1"
  5 "PRAGMA case_sensitive_like"
} {

  do_pragma_ncol_test 1.$tn.1 $sql 0
}

# EXPLAIN on a PRAGMA integrity_check.
# Verify that that P4_INTARRAY argument to OP_IntegrityCk is rendered
# correctly.
#
catch {db close}
forcedelete test.db
sqlite3 db test.db
do_test pragma4-2.100 {
  db eval {
    PRAGMA page_size=512;
    CREATE TABLE t1(x);
    WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10000)
    INSERT INTO t1(x) SELECT zeroblob(300) FROM c;
    CREATE TABLE t2(y);
    DROP TABLE t1;
  }
  string map {\[ x \] x \173 {} \175 {}} \
    [db eval {EXPLAIN PRAGMA integrity_check}]
} {/ IntegrityCk 2 2 1 x[0-9]+,1x /}


#--------------------------------------------------------------------------
#
reset_db
forcedelete test.db2
do_execsql_test 4.1.1 {
  CREATE TABLE t1(a, b, c);
  ATTACH 'test.db2' AS aux;
  CREATE TABLE aux.t2(d, e, f);
}
do_execsql_test 4.1.2 { PRAGMA table_info = t1 } {
  0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0
}
do_execsql_test 4.1.3 { PRAGMA table_info = t2 } {
  0 d {} 0 {} 0 1 e {} 0 {} 0 2 f {} 0 {} 0
}
do_test 4.1.4 { 
  sqlite3 db3 test.db
  sqlite3 db2 test.db2
  execsql { DROP TABLE t1 } db3
  execsql { DROP TABLE t2 } db2
} {}  
if {[permutation]=="prepare"} {
  do_catchsql_test 4.1.5a { 
    PRAGMA table_info(t1) 
  } {1 {database schema has changed}}
}
do_execsql_test 4.1.5 { 
  PRAGMA table_info(t1) 
}
do_execsql_test 4.1.6 { PRAGMA table_info(t2) }

db2 close
db3 close
reset_db
forcedelete test.db2
do_execsql_test 4.2.1 {
  CREATE TABLE t1(a, b, c);
  ATTACH 'test.db2' AS aux;
  CREATE TABLE aux.t2(d, e, f);
}
ifcapable vtab {
  do_execsql_test 4.2.2 { SELECT * FROM pragma_table_info('t1') } {
    0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0
  }
  do_execsql_test 4.2.3 { SELECT * FROM pragma_table_info('t2') } {
    0 d {} 0 {} 0 1 e {} 0 {} 0 2 f {} 0 {} 0
  }
}
do_test 4.2.4 { 
  sqlite3 db3 test.db
  sqlite3 db2 test.db2
  execsql { DROP TABLE t1 } db3
  execsql { DROP TABLE t2 } db2
} {}
ifcapable vtab {
  do_execsql_test 4.2.5 { SELECT * FROM pragma_table_info('t1') } 
  do_execsql_test 4.2.6 { SELECT * FROM pragma_table_info('t2') } 
}

db2 close
db3 close
reset_db
forcedelete test.db2
do_execsql_test 4.3.1 {
  CREATE TABLE t1(a, b, c);
  CREATE INDEX i1 ON t1(b);
  ATTACH 'test.db2' AS aux;
  CREATE TABLE aux.t2(d, e, f);
  CREATE INDEX aux.i2 ON t2(e);
}
ifcapable vtab {
  do_execsql_test 4.3.2 { SELECT * FROM pragma_index_info('i1') } {0 1 b}
  do_execsql_test 4.3.3 { SELECT * FROM pragma_index_info('i2') } {0 1 e}
}
do_test 4.3.4 { 
  sqlite3 db3 test.db
  sqlite3 db2 test.db2
  execsql { DROP INDEX i1 } db3
  execsql { DROP INDEX i2 } db2
} {}
if {[permutation]=="prepare"} { catchsql { SELECT * FROM sqlite_master } }
ifcapable vtab {
  do_execsql_test 4.3.5 { SELECT * FROM pragma_index_info('i1') } 
  do_execsql_test 4.3.6 { SELECT * FROM pragma_index_info('i2') } 
}

execsql {SELECT * FROM main.sqlite_master, aux.sqlite_master}
do_execsql_test 4.4.0 {
  CREATE INDEX main.i1 ON t1(b, c);
  CREATE INDEX aux.i2 ON t2(e, f);
}
ifcapable vtab {
  do_execsql_test 4.4.1 { SELECT * FROM pragma_index_list('t1') } {0 i1 0 c 0}
  do_execsql_test 4.4.2 { SELECT * FROM pragma_index_list('t2') } {0 i2 0 c 0}
}
do_test 4.4.3 { 
  execsql { DROP INDEX i1 } db3
  execsql { DROP INDEX i2 } db2
} {}
if {[permutation]=="prepare"} { 
  catchsql { SELECT * FROM sqlite_master, aux.sqlite_master }
}
ifcapable vtab {
  do_execsql_test 4.4.5 { SELECT * FROM pragma_index_list('t1') } {}
  do_execsql_test 4.4.6 { SELECT * FROM pragma_index_list('t2') } {}
}
execsql {SELECT * FROM main.sqlite_master, aux.sqlite_master}

do_execsql_test 4.5.0 {
  CREATE UNIQUE INDEX main.i1 ON t1(a);
  CREATE UNIQUE INDEX aux.i2 ON t2(d);
  CREATE TABLE main.c1 (a, b, c REFERENCES t1(a));
  CREATE TABLE aux.c2 (d, e, r REFERENCES t2(d));
}
ifcapable vtab {
  do_execsql_test 4.5.1 { SELECT * FROM pragma_foreign_key_list('c1') } {
    0 0 t1 c a {NO ACTION} {NO ACTION} NONE
  }
  do_execsql_test 4.5.2 { SELECT * FROM pragma_foreign_key_list('c2') } {
    0 0 t2 r d {NO ACTION} {NO ACTION} NONE
  }
}
do_test 4.5.3 { 
  execsql { DROP TABLE c1 } db3
  execsql { DROP TABLE c2 } db2
} {}
if {[permutation]=="prepare"} { 
  catchsql { SELECT * FROM sqlite_master, aux.sqlite_master }
}
ifcapable vtab {
  do_execsql_test 4.5.4 { SELECT * FROM pragma_foreign_key_list('c1') }
  do_execsql_test 4.5.5 { SELECT * FROM pragma_foreign_key_list('c2') } 
}
execsql {SELECT * FROM main.sqlite_master, aux.sqlite_master}

do_execsql_test 4.6.0 {
  CREATE TABLE main.c1 (a, b, c REFERENCES t1(a));
  CREATE TABLE aux.c2 (d, e, r REFERENCES t2(d));
  INSERT INTO main.c1 VALUES(1, 2, 3);
  INSERT INTO aux.c2 VALUES(4, 5, 6);
}
do_execsql_test 4.6.1 { pragma foreign_key_check('c1') } {
  c1 1 t1 0
}
do_execsql_test 4.6.2 { pragma foreign_key_check('c2') } {
  c2 1 t2 0
}
do_test 4.6.3 { 
  execsql { DROP TABLE c2 } db2
} {}
do_execsql_test 4.6.4 { pragma foreign_key_check('c1') } {c1 1 t1 0}
do_catchsql_test 4.6.5 { 
  pragma foreign_key_check('c2') 
} {1 {no such table: c2}}

do_execsql_test 5.0 {
  CREATE TABLE t4(a DEFAULT 'abc' /* comment */, b DEFAULT -1 -- comment
     , c DEFAULT +4.0 /* another comment */
  );
  PRAGMA table_info = t4;
} {
  0 a {} 0 'abc' 0 1 b {} 0 -1 0 2 c {} 0 +4.0 0
}

# 2024-03-24 https://sqlite.org/forum/forumpost/85b6a8b6705fb77a
#
catch {db2 close}
catch {db3 close}
ifcapable vtab {
  reset_db
  do_execsql_test 6.0 {
    CREATE TABLE t1(a INT PRIMARY KEY, b INT);
    CREATE TABLE t2(c INT PRIMARY KEY, d INT REFERENCES t1);
    SELECT t.name, f."table", f."from", i.name, i.pk
      FROM pragma_table_list() AS t
           JOIN pragma_foreign_key_list(t.name, t.schema) AS f
           JOIN pragma_table_info(f."table", t.schema) AS i
     WHERE i.pk;
  } {t2 t1 d a 1}
}

finish_test