Snapshot of upstream SQLite 3.8.8.3
[sqlcipher.git] / src / pragma.c
blob34830e33a6700907dad8d335848e6fee17f76d94
1 /*
2 ** 2003 April 6
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code used to implement the PRAGMA command.
14 #include "sqliteInt.h"
16 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
17 # if defined(__APPLE__)
18 # define SQLITE_ENABLE_LOCKING_STYLE 1
19 # else
20 # define SQLITE_ENABLE_LOCKING_STYLE 0
21 # endif
22 #endif
24 /***************************************************************************
25 ** The next block of code, including the PragTyp_XXXX macro definitions and
26 ** the aPragmaName[] object is composed of generated code. DO NOT EDIT.
28 ** To add new pragmas, edit the code in ../tool/mkpragmatab.tcl and rerun
29 ** that script. Then copy/paste the output in place of the following:
31 #define PragTyp_HEADER_VALUE 0
32 #define PragTyp_AUTO_VACUUM 1
33 #define PragTyp_FLAG 2
34 #define PragTyp_BUSY_TIMEOUT 3
35 #define PragTyp_CACHE_SIZE 4
36 #define PragTyp_CASE_SENSITIVE_LIKE 5
37 #define PragTyp_COLLATION_LIST 6
38 #define PragTyp_COMPILE_OPTIONS 7
39 #define PragTyp_DATA_STORE_DIRECTORY 8
40 #define PragTyp_DATABASE_LIST 9
41 #define PragTyp_DEFAULT_CACHE_SIZE 10
42 #define PragTyp_ENCODING 11
43 #define PragTyp_FOREIGN_KEY_CHECK 12
44 #define PragTyp_FOREIGN_KEY_LIST 13
45 #define PragTyp_INCREMENTAL_VACUUM 14
46 #define PragTyp_INDEX_INFO 15
47 #define PragTyp_INDEX_LIST 16
48 #define PragTyp_INTEGRITY_CHECK 17
49 #define PragTyp_JOURNAL_MODE 18
50 #define PragTyp_JOURNAL_SIZE_LIMIT 19
51 #define PragTyp_LOCK_PROXY_FILE 20
52 #define PragTyp_LOCKING_MODE 21
53 #define PragTyp_PAGE_COUNT 22
54 #define PragTyp_MMAP_SIZE 23
55 #define PragTyp_PAGE_SIZE 24
56 #define PragTyp_SECURE_DELETE 25
57 #define PragTyp_SHRINK_MEMORY 26
58 #define PragTyp_SOFT_HEAP_LIMIT 27
59 #define PragTyp_STATS 28
60 #define PragTyp_SYNCHRONOUS 29
61 #define PragTyp_TABLE_INFO 30
62 #define PragTyp_TEMP_STORE 31
63 #define PragTyp_TEMP_STORE_DIRECTORY 32
64 #define PragTyp_THREADS 33
65 #define PragTyp_WAL_AUTOCHECKPOINT 34
66 #define PragTyp_WAL_CHECKPOINT 35
67 #define PragTyp_ACTIVATE_EXTENSIONS 36
68 #define PragTyp_HEXKEY 37
69 #define PragTyp_KEY 38
70 #define PragTyp_REKEY 39
71 #define PragTyp_LOCK_STATUS 40
72 #define PragTyp_PARSER_TRACE 41
73 #define PragFlag_NeedSchema 0x01
74 #define PragFlag_ReadOnly 0x02
75 static const struct sPragmaNames {
76 const char *const zName; /* Name of pragma */
77 u8 ePragTyp; /* PragTyp_XXX value */
78 u8 mPragFlag; /* Zero or more PragFlag_XXX values */
79 u32 iArg; /* Extra argument */
80 } aPragmaNames[] = {
81 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
82 { /* zName: */ "activate_extensions",
83 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
84 /* ePragFlag: */ 0,
85 /* iArg: */ 0 },
86 #endif
87 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
88 { /* zName: */ "application_id",
89 /* ePragTyp: */ PragTyp_HEADER_VALUE,
90 /* ePragFlag: */ 0,
91 /* iArg: */ BTREE_APPLICATION_ID },
92 #endif
93 #if !defined(SQLITE_OMIT_AUTOVACUUM)
94 { /* zName: */ "auto_vacuum",
95 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
96 /* ePragFlag: */ PragFlag_NeedSchema,
97 /* iArg: */ 0 },
98 #endif
99 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
100 #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
101 { /* zName: */ "automatic_index",
102 /* ePragTyp: */ PragTyp_FLAG,
103 /* ePragFlag: */ 0,
104 /* iArg: */ SQLITE_AutoIndex },
105 #endif
106 #endif
107 { /* zName: */ "busy_timeout",
108 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
109 /* ePragFlag: */ 0,
110 /* iArg: */ 0 },
111 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
112 { /* zName: */ "cache_size",
113 /* ePragTyp: */ PragTyp_CACHE_SIZE,
114 /* ePragFlag: */ PragFlag_NeedSchema,
115 /* iArg: */ 0 },
116 #endif
117 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
118 { /* zName: */ "cache_spill",
119 /* ePragTyp: */ PragTyp_FLAG,
120 /* ePragFlag: */ 0,
121 /* iArg: */ SQLITE_CacheSpill },
122 #endif
123 { /* zName: */ "case_sensitive_like",
124 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
125 /* ePragFlag: */ 0,
126 /* iArg: */ 0 },
127 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
128 { /* zName: */ "checkpoint_fullfsync",
129 /* ePragTyp: */ PragTyp_FLAG,
130 /* ePragFlag: */ 0,
131 /* iArg: */ SQLITE_CkptFullFSync },
132 #endif
133 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
134 { /* zName: */ "collation_list",
135 /* ePragTyp: */ PragTyp_COLLATION_LIST,
136 /* ePragFlag: */ 0,
137 /* iArg: */ 0 },
138 #endif
139 #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
140 { /* zName: */ "compile_options",
141 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
142 /* ePragFlag: */ 0,
143 /* iArg: */ 0 },
144 #endif
145 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
146 { /* zName: */ "count_changes",
147 /* ePragTyp: */ PragTyp_FLAG,
148 /* ePragFlag: */ 0,
149 /* iArg: */ SQLITE_CountRows },
150 #endif
151 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
152 { /* zName: */ "data_store_directory",
153 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
154 /* ePragFlag: */ 0,
155 /* iArg: */ 0 },
156 #endif
157 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
158 { /* zName: */ "data_version",
159 /* ePragTyp: */ PragTyp_HEADER_VALUE,
160 /* ePragFlag: */ PragFlag_ReadOnly,
161 /* iArg: */ BTREE_DATA_VERSION },
162 #endif
163 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
164 { /* zName: */ "database_list",
165 /* ePragTyp: */ PragTyp_DATABASE_LIST,
166 /* ePragFlag: */ PragFlag_NeedSchema,
167 /* iArg: */ 0 },
168 #endif
169 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
170 { /* zName: */ "default_cache_size",
171 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
172 /* ePragFlag: */ PragFlag_NeedSchema,
173 /* iArg: */ 0 },
174 #endif
175 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
176 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
177 { /* zName: */ "defer_foreign_keys",
178 /* ePragTyp: */ PragTyp_FLAG,
179 /* ePragFlag: */ 0,
180 /* iArg: */ SQLITE_DeferFKs },
181 #endif
182 #endif
183 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
184 { /* zName: */ "empty_result_callbacks",
185 /* ePragTyp: */ PragTyp_FLAG,
186 /* ePragFlag: */ 0,
187 /* iArg: */ SQLITE_NullCallback },
188 #endif
189 #if !defined(SQLITE_OMIT_UTF16)
190 { /* zName: */ "encoding",
191 /* ePragTyp: */ PragTyp_ENCODING,
192 /* ePragFlag: */ 0,
193 /* iArg: */ 0 },
194 #endif
195 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
196 { /* zName: */ "foreign_key_check",
197 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
198 /* ePragFlag: */ PragFlag_NeedSchema,
199 /* iArg: */ 0 },
200 #endif
201 #if !defined(SQLITE_OMIT_FOREIGN_KEY)
202 { /* zName: */ "foreign_key_list",
203 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
204 /* ePragFlag: */ PragFlag_NeedSchema,
205 /* iArg: */ 0 },
206 #endif
207 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
208 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
209 { /* zName: */ "foreign_keys",
210 /* ePragTyp: */ PragTyp_FLAG,
211 /* ePragFlag: */ 0,
212 /* iArg: */ SQLITE_ForeignKeys },
213 #endif
214 #endif
215 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
216 { /* zName: */ "freelist_count",
217 /* ePragTyp: */ PragTyp_HEADER_VALUE,
218 /* ePragFlag: */ PragFlag_ReadOnly,
219 /* iArg: */ BTREE_FREE_PAGE_COUNT },
220 #endif
221 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
222 { /* zName: */ "full_column_names",
223 /* ePragTyp: */ PragTyp_FLAG,
224 /* ePragFlag: */ 0,
225 /* iArg: */ SQLITE_FullColNames },
226 { /* zName: */ "fullfsync",
227 /* ePragTyp: */ PragTyp_FLAG,
228 /* ePragFlag: */ 0,
229 /* iArg: */ SQLITE_FullFSync },
230 #endif
231 #if defined(SQLITE_HAS_CODEC)
232 { /* zName: */ "hexkey",
233 /* ePragTyp: */ PragTyp_HEXKEY,
234 /* ePragFlag: */ 0,
235 /* iArg: */ 0 },
236 { /* zName: */ "hexrekey",
237 /* ePragTyp: */ PragTyp_HEXKEY,
238 /* ePragFlag: */ 0,
239 /* iArg: */ 0 },
240 #endif
241 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
242 #if !defined(SQLITE_OMIT_CHECK)
243 { /* zName: */ "ignore_check_constraints",
244 /* ePragTyp: */ PragTyp_FLAG,
245 /* ePragFlag: */ 0,
246 /* iArg: */ SQLITE_IgnoreChecks },
247 #endif
248 #endif
249 #if !defined(SQLITE_OMIT_AUTOVACUUM)
250 { /* zName: */ "incremental_vacuum",
251 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
252 /* ePragFlag: */ PragFlag_NeedSchema,
253 /* iArg: */ 0 },
254 #endif
255 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
256 { /* zName: */ "index_info",
257 /* ePragTyp: */ PragTyp_INDEX_INFO,
258 /* ePragFlag: */ PragFlag_NeedSchema,
259 /* iArg: */ 0 },
260 { /* zName: */ "index_list",
261 /* ePragTyp: */ PragTyp_INDEX_LIST,
262 /* ePragFlag: */ PragFlag_NeedSchema,
263 /* iArg: */ 0 },
264 #endif
265 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
266 { /* zName: */ "integrity_check",
267 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
268 /* ePragFlag: */ PragFlag_NeedSchema,
269 /* iArg: */ 0 },
270 #endif
271 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
272 { /* zName: */ "journal_mode",
273 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
274 /* ePragFlag: */ PragFlag_NeedSchema,
275 /* iArg: */ 0 },
276 { /* zName: */ "journal_size_limit",
277 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
278 /* ePragFlag: */ 0,
279 /* iArg: */ 0 },
280 #endif
281 #if defined(SQLITE_HAS_CODEC)
282 { /* zName: */ "key",
283 /* ePragTyp: */ PragTyp_KEY,
284 /* ePragFlag: */ 0,
285 /* iArg: */ 0 },
286 #endif
287 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
288 { /* zName: */ "legacy_file_format",
289 /* ePragTyp: */ PragTyp_FLAG,
290 /* ePragFlag: */ 0,
291 /* iArg: */ SQLITE_LegacyFileFmt },
292 #endif
293 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
294 { /* zName: */ "lock_proxy_file",
295 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
296 /* ePragFlag: */ 0,
297 /* iArg: */ 0 },
298 #endif
299 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
300 { /* zName: */ "lock_status",
301 /* ePragTyp: */ PragTyp_LOCK_STATUS,
302 /* ePragFlag: */ 0,
303 /* iArg: */ 0 },
304 #endif
305 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
306 { /* zName: */ "locking_mode",
307 /* ePragTyp: */ PragTyp_LOCKING_MODE,
308 /* ePragFlag: */ 0,
309 /* iArg: */ 0 },
310 { /* zName: */ "max_page_count",
311 /* ePragTyp: */ PragTyp_PAGE_COUNT,
312 /* ePragFlag: */ PragFlag_NeedSchema,
313 /* iArg: */ 0 },
314 { /* zName: */ "mmap_size",
315 /* ePragTyp: */ PragTyp_MMAP_SIZE,
316 /* ePragFlag: */ 0,
317 /* iArg: */ 0 },
318 { /* zName: */ "page_count",
319 /* ePragTyp: */ PragTyp_PAGE_COUNT,
320 /* ePragFlag: */ PragFlag_NeedSchema,
321 /* iArg: */ 0 },
322 { /* zName: */ "page_size",
323 /* ePragTyp: */ PragTyp_PAGE_SIZE,
324 /* ePragFlag: */ 0,
325 /* iArg: */ 0 },
326 #endif
327 #if defined(SQLITE_DEBUG)
328 { /* zName: */ "parser_trace",
329 /* ePragTyp: */ PragTyp_PARSER_TRACE,
330 /* ePragFlag: */ 0,
331 /* iArg: */ 0 },
332 #endif
333 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
334 { /* zName: */ "query_only",
335 /* ePragTyp: */ PragTyp_FLAG,
336 /* ePragFlag: */ 0,
337 /* iArg: */ SQLITE_QueryOnly },
338 #endif
339 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
340 { /* zName: */ "quick_check",
341 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
342 /* ePragFlag: */ PragFlag_NeedSchema,
343 /* iArg: */ 0 },
344 #endif
345 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
346 { /* zName: */ "read_uncommitted",
347 /* ePragTyp: */ PragTyp_FLAG,
348 /* ePragFlag: */ 0,
349 /* iArg: */ SQLITE_ReadUncommitted },
350 { /* zName: */ "recursive_triggers",
351 /* ePragTyp: */ PragTyp_FLAG,
352 /* ePragFlag: */ 0,
353 /* iArg: */ SQLITE_RecTriggers },
354 #endif
355 #if defined(SQLITE_HAS_CODEC)
356 { /* zName: */ "rekey",
357 /* ePragTyp: */ PragTyp_REKEY,
358 /* ePragFlag: */ 0,
359 /* iArg: */ 0 },
360 #endif
361 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
362 { /* zName: */ "reverse_unordered_selects",
363 /* ePragTyp: */ PragTyp_FLAG,
364 /* ePragFlag: */ 0,
365 /* iArg: */ SQLITE_ReverseOrder },
366 #endif
367 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
368 { /* zName: */ "schema_version",
369 /* ePragTyp: */ PragTyp_HEADER_VALUE,
370 /* ePragFlag: */ 0,
371 /* iArg: */ BTREE_SCHEMA_VERSION },
372 #endif
373 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
374 { /* zName: */ "secure_delete",
375 /* ePragTyp: */ PragTyp_SECURE_DELETE,
376 /* ePragFlag: */ 0,
377 /* iArg: */ 0 },
378 #endif
379 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
380 { /* zName: */ "short_column_names",
381 /* ePragTyp: */ PragTyp_FLAG,
382 /* ePragFlag: */ 0,
383 /* iArg: */ SQLITE_ShortColNames },
384 #endif
385 { /* zName: */ "shrink_memory",
386 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
387 /* ePragFlag: */ 0,
388 /* iArg: */ 0 },
389 { /* zName: */ "soft_heap_limit",
390 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
391 /* ePragFlag: */ 0,
392 /* iArg: */ 0 },
393 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
394 #if defined(SQLITE_DEBUG)
395 { /* zName: */ "sql_trace",
396 /* ePragTyp: */ PragTyp_FLAG,
397 /* ePragFlag: */ 0,
398 /* iArg: */ SQLITE_SqlTrace },
399 #endif
400 #endif
401 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
402 { /* zName: */ "stats",
403 /* ePragTyp: */ PragTyp_STATS,
404 /* ePragFlag: */ PragFlag_NeedSchema,
405 /* iArg: */ 0 },
406 #endif
407 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
408 { /* zName: */ "synchronous",
409 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
410 /* ePragFlag: */ PragFlag_NeedSchema,
411 /* iArg: */ 0 },
412 #endif
413 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
414 { /* zName: */ "table_info",
415 /* ePragTyp: */ PragTyp_TABLE_INFO,
416 /* ePragFlag: */ PragFlag_NeedSchema,
417 /* iArg: */ 0 },
418 #endif
419 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
420 { /* zName: */ "temp_store",
421 /* ePragTyp: */ PragTyp_TEMP_STORE,
422 /* ePragFlag: */ 0,
423 /* iArg: */ 0 },
424 { /* zName: */ "temp_store_directory",
425 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
426 /* ePragFlag: */ 0,
427 /* iArg: */ 0 },
428 #endif
429 { /* zName: */ "threads",
430 /* ePragTyp: */ PragTyp_THREADS,
431 /* ePragFlag: */ 0,
432 /* iArg: */ 0 },
433 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
434 { /* zName: */ "user_version",
435 /* ePragTyp: */ PragTyp_HEADER_VALUE,
436 /* ePragFlag: */ 0,
437 /* iArg: */ BTREE_USER_VERSION },
438 #endif
439 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
440 #if defined(SQLITE_DEBUG)
441 { /* zName: */ "vdbe_addoptrace",
442 /* ePragTyp: */ PragTyp_FLAG,
443 /* ePragFlag: */ 0,
444 /* iArg: */ SQLITE_VdbeAddopTrace },
445 { /* zName: */ "vdbe_debug",
446 /* ePragTyp: */ PragTyp_FLAG,
447 /* ePragFlag: */ 0,
448 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
449 { /* zName: */ "vdbe_eqp",
450 /* ePragTyp: */ PragTyp_FLAG,
451 /* ePragFlag: */ 0,
452 /* iArg: */ SQLITE_VdbeEQP },
453 { /* zName: */ "vdbe_listing",
454 /* ePragTyp: */ PragTyp_FLAG,
455 /* ePragFlag: */ 0,
456 /* iArg: */ SQLITE_VdbeListing },
457 { /* zName: */ "vdbe_trace",
458 /* ePragTyp: */ PragTyp_FLAG,
459 /* ePragFlag: */ 0,
460 /* iArg: */ SQLITE_VdbeTrace },
461 #endif
462 #endif
463 #if !defined(SQLITE_OMIT_WAL)
464 { /* zName: */ "wal_autocheckpoint",
465 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
466 /* ePragFlag: */ 0,
467 /* iArg: */ 0 },
468 { /* zName: */ "wal_checkpoint",
469 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
470 /* ePragFlag: */ PragFlag_NeedSchema,
471 /* iArg: */ 0 },
472 #endif
473 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
474 { /* zName: */ "writable_schema",
475 /* ePragTyp: */ PragTyp_FLAG,
476 /* ePragFlag: */ 0,
477 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
478 #endif
480 /* Number of pragmas: 58 on by default, 71 total. */
481 /* End of the automatically generated pragma table.
482 ***************************************************************************/
485 ** Interpret the given string as a safety level. Return 0 for OFF,
486 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
487 ** unrecognized string argument. The FULL option is disallowed
488 ** if the omitFull parameter it 1.
490 ** Note that the values returned are one less that the values that
491 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
492 ** to support legacy SQL code. The safety level used to be boolean
493 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
495 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
496 /* 123456789 123456789 */
497 static const char zText[] = "onoffalseyestruefull";
498 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
499 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
500 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
501 int i, n;
502 if( sqlite3Isdigit(*z) ){
503 return (u8)sqlite3Atoi(z);
505 n = sqlite3Strlen30(z);
506 for(i=0; i<ArraySize(iLength)-omitFull; i++){
507 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
508 return iValue[i];
511 return dflt;
515 ** Interpret the given string as a boolean value.
517 u8 sqlite3GetBoolean(const char *z, u8 dflt){
518 return getSafetyLevel(z,1,dflt)!=0;
521 /* The sqlite3GetBoolean() function is used by other modules but the
522 ** remainder of this file is specific to PRAGMA processing. So omit
523 ** the rest of the file if PRAGMAs are omitted from the build.
525 #if !defined(SQLITE_OMIT_PRAGMA)
528 ** Interpret the given string as a locking mode value.
530 static int getLockingMode(const char *z){
531 if( z ){
532 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
533 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
535 return PAGER_LOCKINGMODE_QUERY;
538 #ifndef SQLITE_OMIT_AUTOVACUUM
540 ** Interpret the given string as an auto-vacuum mode value.
542 ** The following strings, "none", "full" and "incremental" are
543 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
545 static int getAutoVacuum(const char *z){
546 int i;
547 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
548 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
549 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
550 i = sqlite3Atoi(z);
551 return (u8)((i>=0&&i<=2)?i:0);
553 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
555 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
557 ** Interpret the given string as a temp db location. Return 1 for file
558 ** backed temporary databases, 2 for the Red-Black tree in memory database
559 ** and 0 to use the compile-time default.
561 static int getTempStore(const char *z){
562 if( z[0]>='0' && z[0]<='2' ){
563 return z[0] - '0';
564 }else if( sqlite3StrICmp(z, "file")==0 ){
565 return 1;
566 }else if( sqlite3StrICmp(z, "memory")==0 ){
567 return 2;
568 }else{
569 return 0;
572 #endif /* SQLITE_PAGER_PRAGMAS */
574 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
576 ** Invalidate temp storage, either when the temp storage is changed
577 ** from default, or when 'file' and the temp_store_directory has changed
579 static int invalidateTempStorage(Parse *pParse){
580 sqlite3 *db = pParse->db;
581 if( db->aDb[1].pBt!=0 ){
582 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
583 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
584 "from within a transaction");
585 return SQLITE_ERROR;
587 sqlite3BtreeClose(db->aDb[1].pBt);
588 db->aDb[1].pBt = 0;
589 sqlite3ResetAllSchemasOfConnection(db);
591 return SQLITE_OK;
593 #endif /* SQLITE_PAGER_PRAGMAS */
595 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
597 ** If the TEMP database is open, close it and mark the database schema
598 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
599 ** or DEFAULT_TEMP_STORE pragmas.
601 static int changeTempStorage(Parse *pParse, const char *zStorageType){
602 int ts = getTempStore(zStorageType);
603 sqlite3 *db = pParse->db;
604 if( db->temp_store==ts ) return SQLITE_OK;
605 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
606 return SQLITE_ERROR;
608 db->temp_store = (u8)ts;
609 return SQLITE_OK;
611 #endif /* SQLITE_PAGER_PRAGMAS */
614 ** Generate code to return a single integer value.
616 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
617 Vdbe *v = sqlite3GetVdbe(pParse);
618 int mem = ++pParse->nMem;
619 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
620 if( pI64 ){
621 memcpy(pI64, &value, sizeof(value));
623 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
624 sqlite3VdbeSetNumCols(v, 1);
625 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
626 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
631 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0
632 ** set these values for all pagers.
634 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
635 static void setAllPagerFlags(sqlite3 *db){
636 if( db->autoCommit ){
637 Db *pDb = db->aDb;
638 int n = db->nDb;
639 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
640 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
641 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
642 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
643 == PAGER_FLAGS_MASK );
644 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
645 while( (n--) > 0 ){
646 if( pDb->pBt ){
647 sqlite3BtreeSetPagerFlags(pDb->pBt,
648 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
650 pDb++;
654 #else
655 # define setAllPagerFlags(X) /* no-op */
656 #endif
660 ** Return a human-readable name for a constraint resolution action.
662 #ifndef SQLITE_OMIT_FOREIGN_KEY
663 static const char *actionName(u8 action){
664 const char *zName;
665 switch( action ){
666 case OE_SetNull: zName = "SET NULL"; break;
667 case OE_SetDflt: zName = "SET DEFAULT"; break;
668 case OE_Cascade: zName = "CASCADE"; break;
669 case OE_Restrict: zName = "RESTRICT"; break;
670 default: zName = "NO ACTION";
671 assert( action==OE_None ); break;
673 return zName;
675 #endif
679 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
680 ** defined in pager.h. This function returns the associated lowercase
681 ** journal-mode name.
683 const char *sqlite3JournalModename(int eMode){
684 static char * const azModeName[] = {
685 "delete", "persist", "off", "truncate", "memory"
686 #ifndef SQLITE_OMIT_WAL
687 , "wal"
688 #endif
690 assert( PAGER_JOURNALMODE_DELETE==0 );
691 assert( PAGER_JOURNALMODE_PERSIST==1 );
692 assert( PAGER_JOURNALMODE_OFF==2 );
693 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
694 assert( PAGER_JOURNALMODE_MEMORY==4 );
695 assert( PAGER_JOURNALMODE_WAL==5 );
696 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
698 if( eMode==ArraySize(azModeName) ) return 0;
699 return azModeName[eMode];
703 ** Process a pragma statement.
705 ** Pragmas are of this form:
707 ** PRAGMA [database.]id [= value]
709 ** The identifier might also be a string. The value is a string, and
710 ** identifier, or a number. If minusFlag is true, then the value is
711 ** a number that was preceded by a minus sign.
713 ** If the left side is "database.id" then pId1 is the database name
714 ** and pId2 is the id. If the left side is just "id" then pId1 is the
715 ** id and pId2 is any empty string.
717 void sqlite3Pragma(
718 Parse *pParse,
719 Token *pId1, /* First part of [database.]id field */
720 Token *pId2, /* Second part of [database.]id field, or NULL */
721 Token *pValue, /* Token for <value>, or NULL */
722 int minusFlag /* True if a '-' sign preceded <value> */
724 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
725 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
726 const char *zDb = 0; /* The database name */
727 Token *pId; /* Pointer to <id> token */
728 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
729 int iDb; /* Database index for <database> */
730 int lwr, upr, mid = 0; /* Binary search bounds */
731 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
732 sqlite3 *db = pParse->db; /* The database connection */
733 Db *pDb; /* The specific database being pragmaed */
734 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
736 if( v==0 ) return;
737 sqlite3VdbeRunOnlyOnce(v);
738 pParse->nMem = 2;
740 /* Interpret the [database.] part of the pragma statement. iDb is the
741 ** index of the database this pragma is being applied to in db.aDb[]. */
742 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
743 if( iDb<0 ) return;
744 pDb = &db->aDb[iDb];
746 /* If the temp database has been explicitly named as part of the
747 ** pragma, make sure it is open.
749 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
750 return;
753 zLeft = sqlite3NameFromToken(db, pId);
754 if( !zLeft ) return;
755 if( minusFlag ){
756 zRight = sqlite3MPrintf(db, "-%T", pValue);
757 }else{
758 zRight = sqlite3NameFromToken(db, pValue);
761 assert( pId2 );
762 zDb = pId2->n>0 ? pDb->zName : 0;
763 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
764 goto pragma_out;
767 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
768 ** connection. If it returns SQLITE_OK, then assume that the VFS
769 ** handled the pragma and generate a no-op prepared statement.
771 aFcntl[0] = 0;
772 aFcntl[1] = zLeft;
773 aFcntl[2] = zRight;
774 aFcntl[3] = 0;
775 db->busyHandler.nBusy = 0;
776 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
777 if( rc==SQLITE_OK ){
778 if( aFcntl[0] ){
779 int mem = ++pParse->nMem;
780 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
781 sqlite3VdbeSetNumCols(v, 1);
782 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
783 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
784 sqlite3_free(aFcntl[0]);
786 goto pragma_out;
788 if( rc!=SQLITE_NOTFOUND ){
789 if( aFcntl[0] ){
790 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
791 sqlite3_free(aFcntl[0]);
793 pParse->nErr++;
794 pParse->rc = rc;
795 goto pragma_out;
798 /* Locate the pragma in the lookup table */
799 lwr = 0;
800 upr = ArraySize(aPragmaNames)-1;
801 while( lwr<=upr ){
802 mid = (lwr+upr)/2;
803 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
804 if( rc==0 ) break;
805 if( rc<0 ){
806 upr = mid - 1;
807 }else{
808 lwr = mid + 1;
811 if( lwr>upr ) goto pragma_out;
813 /* Make sure the database schema is loaded if the pragma requires that */
814 if( (aPragmaNames[mid].mPragFlag & PragFlag_NeedSchema)!=0 ){
815 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
818 /* Jump to the appropriate pragma handler */
819 switch( aPragmaNames[mid].ePragTyp ){
821 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
823 ** PRAGMA [database.]default_cache_size
824 ** PRAGMA [database.]default_cache_size=N
826 ** The first form reports the current persistent setting for the
827 ** page cache size. The value returned is the maximum number of
828 ** pages in the page cache. The second form sets both the current
829 ** page cache size value and the persistent page cache size value
830 ** stored in the database file.
832 ** Older versions of SQLite would set the default cache size to a
833 ** negative number to indicate synchronous=OFF. These days, synchronous
834 ** is always on by default regardless of the sign of the default cache
835 ** size. But continue to take the absolute value of the default cache
836 ** size of historical compatibility.
838 case PragTyp_DEFAULT_CACHE_SIZE: {
839 static const int iLn = VDBE_OFFSET_LINENO(2);
840 static const VdbeOpList getCacheSize[] = {
841 { OP_Transaction, 0, 0, 0}, /* 0 */
842 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
843 { OP_IfPos, 1, 8, 0},
844 { OP_Integer, 0, 2, 0},
845 { OP_Subtract, 1, 2, 1},
846 { OP_IfPos, 1, 8, 0},
847 { OP_Integer, 0, 1, 0}, /* 6 */
848 { OP_Noop, 0, 0, 0},
849 { OP_ResultRow, 1, 1, 0},
851 int addr;
852 sqlite3VdbeUsesBtree(v, iDb);
853 if( !zRight ){
854 sqlite3VdbeSetNumCols(v, 1);
855 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
856 pParse->nMem += 2;
857 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
858 sqlite3VdbeChangeP1(v, addr, iDb);
859 sqlite3VdbeChangeP1(v, addr+1, iDb);
860 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
861 }else{
862 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
863 sqlite3BeginWriteOperation(pParse, 0, iDb);
864 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
865 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
866 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
867 pDb->pSchema->cache_size = size;
868 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
870 break;
872 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
874 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
876 ** PRAGMA [database.]page_size
877 ** PRAGMA [database.]page_size=N
879 ** The first form reports the current setting for the
880 ** database page size in bytes. The second form sets the
881 ** database page size value. The value can only be set if
882 ** the database has not yet been created.
884 case PragTyp_PAGE_SIZE: {
885 Btree *pBt = pDb->pBt;
886 assert( pBt!=0 );
887 if( !zRight ){
888 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
889 returnSingleInt(pParse, "page_size", size);
890 }else{
891 /* Malloc may fail when setting the page-size, as there is an internal
892 ** buffer that the pager module resizes using sqlite3_realloc().
894 db->nextPagesize = sqlite3Atoi(zRight);
895 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
896 db->mallocFailed = 1;
899 break;
903 ** PRAGMA [database.]secure_delete
904 ** PRAGMA [database.]secure_delete=ON/OFF
906 ** The first form reports the current setting for the
907 ** secure_delete flag. The second form changes the secure_delete
908 ** flag setting and reports thenew value.
910 case PragTyp_SECURE_DELETE: {
911 Btree *pBt = pDb->pBt;
912 int b = -1;
913 assert( pBt!=0 );
914 if( zRight ){
915 b = sqlite3GetBoolean(zRight, 0);
917 if( pId2->n==0 && b>=0 ){
918 int ii;
919 for(ii=0; ii<db->nDb; ii++){
920 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
923 b = sqlite3BtreeSecureDelete(pBt, b);
924 returnSingleInt(pParse, "secure_delete", b);
925 break;
929 ** PRAGMA [database.]max_page_count
930 ** PRAGMA [database.]max_page_count=N
932 ** The first form reports the current setting for the
933 ** maximum number of pages in the database file. The
934 ** second form attempts to change this setting. Both
935 ** forms return the current setting.
937 ** The absolute value of N is used. This is undocumented and might
938 ** change. The only purpose is to provide an easy way to test
939 ** the sqlite3AbsInt32() function.
941 ** PRAGMA [database.]page_count
943 ** Return the number of pages in the specified database.
945 case PragTyp_PAGE_COUNT: {
946 int iReg;
947 sqlite3CodeVerifySchema(pParse, iDb);
948 iReg = ++pParse->nMem;
949 if( sqlite3Tolower(zLeft[0])=='p' ){
950 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
951 }else{
952 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
953 sqlite3AbsInt32(sqlite3Atoi(zRight)));
955 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
956 sqlite3VdbeSetNumCols(v, 1);
957 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
958 break;
962 ** PRAGMA [database.]locking_mode
963 ** PRAGMA [database.]locking_mode = (normal|exclusive)
965 case PragTyp_LOCKING_MODE: {
966 const char *zRet = "normal";
967 int eMode = getLockingMode(zRight);
969 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
970 /* Simple "PRAGMA locking_mode;" statement. This is a query for
971 ** the current default locking mode (which may be different to
972 ** the locking-mode of the main database).
974 eMode = db->dfltLockMode;
975 }else{
976 Pager *pPager;
977 if( pId2->n==0 ){
978 /* This indicates that no database name was specified as part
979 ** of the PRAGMA command. In this case the locking-mode must be
980 ** set on all attached databases, as well as the main db file.
982 ** Also, the sqlite3.dfltLockMode variable is set so that
983 ** any subsequently attached databases also use the specified
984 ** locking mode.
986 int ii;
987 assert(pDb==&db->aDb[0]);
988 for(ii=2; ii<db->nDb; ii++){
989 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
990 sqlite3PagerLockingMode(pPager, eMode);
992 db->dfltLockMode = (u8)eMode;
994 pPager = sqlite3BtreePager(pDb->pBt);
995 eMode = sqlite3PagerLockingMode(pPager, eMode);
998 assert( eMode==PAGER_LOCKINGMODE_NORMAL
999 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
1000 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
1001 zRet = "exclusive";
1003 sqlite3VdbeSetNumCols(v, 1);
1004 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
1005 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
1006 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1007 break;
1011 ** PRAGMA [database.]journal_mode
1012 ** PRAGMA [database.]journal_mode =
1013 ** (delete|persist|off|truncate|memory|wal|off)
1015 case PragTyp_JOURNAL_MODE: {
1016 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
1017 int ii; /* Loop counter */
1019 sqlite3VdbeSetNumCols(v, 1);
1020 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
1022 if( zRight==0 ){
1023 /* If there is no "=MODE" part of the pragma, do a query for the
1024 ** current mode */
1025 eMode = PAGER_JOURNALMODE_QUERY;
1026 }else{
1027 const char *zMode;
1028 int n = sqlite3Strlen30(zRight);
1029 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
1030 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
1032 if( !zMode ){
1033 /* If the "=MODE" part does not match any known journal mode,
1034 ** then do a query */
1035 eMode = PAGER_JOURNALMODE_QUERY;
1038 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
1039 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
1040 iDb = 0;
1041 pId2->n = 1;
1043 for(ii=db->nDb-1; ii>=0; ii--){
1044 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
1045 sqlite3VdbeUsesBtree(v, ii);
1046 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
1049 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1050 break;
1054 ** PRAGMA [database.]journal_size_limit
1055 ** PRAGMA [database.]journal_size_limit=N
1057 ** Get or set the size limit on rollback journal files.
1059 case PragTyp_JOURNAL_SIZE_LIMIT: {
1060 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1061 i64 iLimit = -2;
1062 if( zRight ){
1063 sqlite3DecOrHexToI64(zRight, &iLimit);
1064 if( iLimit<-1 ) iLimit = -1;
1066 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
1067 returnSingleInt(pParse, "journal_size_limit", iLimit);
1068 break;
1071 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
1074 ** PRAGMA [database.]auto_vacuum
1075 ** PRAGMA [database.]auto_vacuum=N
1077 ** Get or set the value of the database 'auto-vacuum' parameter.
1078 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
1080 #ifndef SQLITE_OMIT_AUTOVACUUM
1081 case PragTyp_AUTO_VACUUM: {
1082 Btree *pBt = pDb->pBt;
1083 assert( pBt!=0 );
1084 if( !zRight ){
1085 returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
1086 }else{
1087 int eAuto = getAutoVacuum(zRight);
1088 assert( eAuto>=0 && eAuto<=2 );
1089 db->nextAutovac = (u8)eAuto;
1090 /* Call SetAutoVacuum() to set initialize the internal auto and
1091 ** incr-vacuum flags. This is required in case this connection
1092 ** creates the database file. It is important that it is created
1093 ** as an auto-vacuum capable db.
1095 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
1096 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
1097 /* When setting the auto_vacuum mode to either "full" or
1098 ** "incremental", write the value of meta[6] in the database
1099 ** file. Before writing to meta[6], check that meta[3] indicates
1100 ** that this really is an auto-vacuum capable database.
1102 static const int iLn = VDBE_OFFSET_LINENO(2);
1103 static const VdbeOpList setMeta6[] = {
1104 { OP_Transaction, 0, 1, 0}, /* 0 */
1105 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
1106 { OP_If, 1, 0, 0}, /* 2 */
1107 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
1108 { OP_Integer, 0, 1, 0}, /* 4 */
1109 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
1111 int iAddr;
1112 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
1113 sqlite3VdbeChangeP1(v, iAddr, iDb);
1114 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
1115 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
1116 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
1117 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
1118 sqlite3VdbeUsesBtree(v, iDb);
1121 break;
1123 #endif
1126 ** PRAGMA [database.]incremental_vacuum(N)
1128 ** Do N steps of incremental vacuuming on a database.
1130 #ifndef SQLITE_OMIT_AUTOVACUUM
1131 case PragTyp_INCREMENTAL_VACUUM: {
1132 int iLimit, addr;
1133 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
1134 iLimit = 0x7fffffff;
1136 sqlite3BeginWriteOperation(pParse, 0, iDb);
1137 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
1138 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
1139 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
1140 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1141 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
1142 sqlite3VdbeJumpHere(v, addr);
1143 break;
1145 #endif
1147 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
1149 ** PRAGMA [database.]cache_size
1150 ** PRAGMA [database.]cache_size=N
1152 ** The first form reports the current local setting for the
1153 ** page cache size. The second form sets the local
1154 ** page cache size value. If N is positive then that is the
1155 ** number of pages in the cache. If N is negative, then the
1156 ** number of pages is adjusted so that the cache uses -N kibibytes
1157 ** of memory.
1159 case PragTyp_CACHE_SIZE: {
1160 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1161 if( !zRight ){
1162 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
1163 }else{
1164 int size = sqlite3Atoi(zRight);
1165 pDb->pSchema->cache_size = size;
1166 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
1168 break;
1172 ** PRAGMA [database.]mmap_size(N)
1174 ** Used to set mapping size limit. The mapping size limit is
1175 ** used to limit the aggregate size of all memory mapped regions of the
1176 ** database file. If this parameter is set to zero, then memory mapping
1177 ** is not used at all. If N is negative, then the default memory map
1178 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
1179 ** The parameter N is measured in bytes.
1181 ** This value is advisory. The underlying VFS is free to memory map
1182 ** as little or as much as it wants. Except, if N is set to 0 then the
1183 ** upper layers will never invoke the xFetch interfaces to the VFS.
1185 case PragTyp_MMAP_SIZE: {
1186 sqlite3_int64 sz;
1187 #if SQLITE_MAX_MMAP_SIZE>0
1188 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1189 if( zRight ){
1190 int ii;
1191 sqlite3DecOrHexToI64(zRight, &sz);
1192 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
1193 if( pId2->n==0 ) db->szMmap = sz;
1194 for(ii=db->nDb-1; ii>=0; ii--){
1195 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
1196 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
1200 sz = -1;
1201 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
1202 #else
1203 sz = 0;
1204 rc = SQLITE_OK;
1205 #endif
1206 if( rc==SQLITE_OK ){
1207 returnSingleInt(pParse, "mmap_size", sz);
1208 }else if( rc!=SQLITE_NOTFOUND ){
1209 pParse->nErr++;
1210 pParse->rc = rc;
1212 break;
1216 ** PRAGMA temp_store
1217 ** PRAGMA temp_store = "default"|"memory"|"file"
1219 ** Return or set the local value of the temp_store flag. Changing
1220 ** the local value does not make changes to the disk file and the default
1221 ** value will be restored the next time the database is opened.
1223 ** Note that it is possible for the library compile-time options to
1224 ** override this setting
1226 case PragTyp_TEMP_STORE: {
1227 if( !zRight ){
1228 returnSingleInt(pParse, "temp_store", db->temp_store);
1229 }else{
1230 changeTempStorage(pParse, zRight);
1232 break;
1236 ** PRAGMA temp_store_directory
1237 ** PRAGMA temp_store_directory = ""|"directory_name"
1239 ** Return or set the local value of the temp_store_directory flag. Changing
1240 ** the value sets a specific directory to be used for temporary files.
1241 ** Setting to a null string reverts to the default temporary directory search.
1242 ** If temporary directory is changed, then invalidateTempStorage.
1245 case PragTyp_TEMP_STORE_DIRECTORY: {
1246 if( !zRight ){
1247 if( sqlite3_temp_directory ){
1248 sqlite3VdbeSetNumCols(v, 1);
1249 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1250 "temp_store_directory", SQLITE_STATIC);
1251 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
1252 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1254 }else{
1255 #ifndef SQLITE_OMIT_WSD
1256 if( zRight[0] ){
1257 int res;
1258 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1259 if( rc!=SQLITE_OK || res==0 ){
1260 sqlite3ErrorMsg(pParse, "not a writable directory");
1261 goto pragma_out;
1264 if( SQLITE_TEMP_STORE==0
1265 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
1266 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
1268 invalidateTempStorage(pParse);
1270 sqlite3_free(sqlite3_temp_directory);
1271 if( zRight[0] ){
1272 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
1273 }else{
1274 sqlite3_temp_directory = 0;
1276 #endif /* SQLITE_OMIT_WSD */
1278 break;
1281 #if SQLITE_OS_WIN
1283 ** PRAGMA data_store_directory
1284 ** PRAGMA data_store_directory = ""|"directory_name"
1286 ** Return or set the local value of the data_store_directory flag. Changing
1287 ** the value sets a specific directory to be used for database files that
1288 ** were specified with a relative pathname. Setting to a null string reverts
1289 ** to the default database directory, which for database files specified with
1290 ** a relative path will probably be based on the current directory for the
1291 ** process. Database file specified with an absolute path are not impacted
1292 ** by this setting, regardless of its value.
1295 case PragTyp_DATA_STORE_DIRECTORY: {
1296 if( !zRight ){
1297 if( sqlite3_data_directory ){
1298 sqlite3VdbeSetNumCols(v, 1);
1299 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1300 "data_store_directory", SQLITE_STATIC);
1301 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
1302 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1304 }else{
1305 #ifndef SQLITE_OMIT_WSD
1306 if( zRight[0] ){
1307 int res;
1308 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1309 if( rc!=SQLITE_OK || res==0 ){
1310 sqlite3ErrorMsg(pParse, "not a writable directory");
1311 goto pragma_out;
1314 sqlite3_free(sqlite3_data_directory);
1315 if( zRight[0] ){
1316 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1317 }else{
1318 sqlite3_data_directory = 0;
1320 #endif /* SQLITE_OMIT_WSD */
1322 break;
1324 #endif
1326 #if SQLITE_ENABLE_LOCKING_STYLE
1328 ** PRAGMA [database.]lock_proxy_file
1329 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
1331 ** Return or set the value of the lock_proxy_file flag. Changing
1332 ** the value sets a specific file to be used for database access locks.
1335 case PragTyp_LOCK_PROXY_FILE: {
1336 if( !zRight ){
1337 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1338 char *proxy_file_path = NULL;
1339 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1340 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
1341 &proxy_file_path);
1343 if( proxy_file_path ){
1344 sqlite3VdbeSetNumCols(v, 1);
1345 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1346 "lock_proxy_file", SQLITE_STATIC);
1347 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
1348 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1350 }else{
1351 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1352 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1353 int res;
1354 if( zRight[0] ){
1355 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1356 zRight);
1357 } else {
1358 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1359 NULL);
1361 if( res!=SQLITE_OK ){
1362 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1363 goto pragma_out;
1366 break;
1368 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
1371 ** PRAGMA [database.]synchronous
1372 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
1374 ** Return or set the local value of the synchronous flag. Changing
1375 ** the local value does not make changes to the disk file and the
1376 ** default value will be restored the next time the database is
1377 ** opened.
1379 case PragTyp_SYNCHRONOUS: {
1380 if( !zRight ){
1381 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
1382 }else{
1383 if( !db->autoCommit ){
1384 sqlite3ErrorMsg(pParse,
1385 "Safety level may not be changed inside a transaction");
1386 }else{
1387 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
1388 setAllPagerFlags(db);
1391 break;
1393 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
1395 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
1396 case PragTyp_FLAG: {
1397 if( zRight==0 ){
1398 returnSingleInt(pParse, aPragmaNames[mid].zName,
1399 (db->flags & aPragmaNames[mid].iArg)!=0 );
1400 }else{
1401 int mask = aPragmaNames[mid].iArg; /* Mask of bits to set or clear. */
1402 if( db->autoCommit==0 ){
1403 /* Foreign key support may not be enabled or disabled while not
1404 ** in auto-commit mode. */
1405 mask &= ~(SQLITE_ForeignKeys);
1407 #if SQLITE_USER_AUTHENTICATION
1408 if( db->auth.authLevel==UAUTH_User ){
1409 /* Do not allow non-admin users to modify the schema arbitrarily */
1410 mask &= ~(SQLITE_WriteSchema);
1412 #endif
1414 if( sqlite3GetBoolean(zRight, 0) ){
1415 db->flags |= mask;
1416 }else{
1417 db->flags &= ~mask;
1418 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1421 /* Many of the flag-pragmas modify the code generated by the SQL
1422 ** compiler (eg. count_changes). So add an opcode to expire all
1423 ** compiled SQL statements after modifying a pragma value.
1425 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1426 setAllPagerFlags(db);
1428 break;
1430 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
1432 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
1434 ** PRAGMA table_info(<table>)
1436 ** Return a single row for each column of the named table. The columns of
1437 ** the returned data set are:
1439 ** cid: Column id (numbered from left to right, starting at 0)
1440 ** name: Column name
1441 ** type: Column declaration type.
1442 ** notnull: True if 'NOT NULL' is part of column declaration
1443 ** dflt_value: The default value for the column, if any.
1445 case PragTyp_TABLE_INFO: if( zRight ){
1446 Table *pTab;
1447 pTab = sqlite3FindTable(db, zRight, zDb);
1448 if( pTab ){
1449 int i, k;
1450 int nHidden = 0;
1451 Column *pCol;
1452 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
1453 sqlite3VdbeSetNumCols(v, 6);
1454 pParse->nMem = 6;
1455 sqlite3CodeVerifySchema(pParse, iDb);
1456 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1457 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1458 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1459 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1460 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1461 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
1462 sqlite3ViewGetColumnNames(pParse, pTab);
1463 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
1464 if( IsHiddenColumn(pCol) ){
1465 nHidden++;
1466 continue;
1468 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1469 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1470 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1471 pCol->zType ? pCol->zType : "", 0);
1472 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
1473 if( pCol->zDflt ){
1474 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
1475 }else{
1476 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
1478 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1479 k = 0;
1480 }else if( pPk==0 ){
1481 k = 1;
1482 }else{
1483 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1485 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
1486 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
1490 break;
1492 case PragTyp_STATS: {
1493 Index *pIdx;
1494 HashElem *i;
1495 v = sqlite3GetVdbe(pParse);
1496 sqlite3VdbeSetNumCols(v, 4);
1497 pParse->nMem = 4;
1498 sqlite3CodeVerifySchema(pParse, iDb);
1499 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1500 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
1501 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
1502 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
1503 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1504 Table *pTab = sqliteHashData(i);
1505 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
1506 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
1507 sqlite3VdbeAddOp2(v, OP_Integer,
1508 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
1509 sqlite3VdbeAddOp2(v, OP_Integer,
1510 (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
1511 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1512 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1513 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1514 sqlite3VdbeAddOp2(v, OP_Integer,
1515 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
1516 sqlite3VdbeAddOp2(v, OP_Integer,
1517 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
1518 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1522 break;
1524 case PragTyp_INDEX_INFO: if( zRight ){
1525 Index *pIdx;
1526 Table *pTab;
1527 pIdx = sqlite3FindIndex(db, zRight, zDb);
1528 if( pIdx ){
1529 int i;
1530 pTab = pIdx->pTable;
1531 sqlite3VdbeSetNumCols(v, 3);
1532 pParse->nMem = 3;
1533 sqlite3CodeVerifySchema(pParse, iDb);
1534 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1535 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1536 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
1537 for(i=0; i<pIdx->nKeyCol; i++){
1538 i16 cnum = pIdx->aiColumn[i];
1539 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1540 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
1541 assert( pTab->nCol>cnum );
1542 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1543 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
1547 break;
1549 case PragTyp_INDEX_LIST: if( zRight ){
1550 Index *pIdx;
1551 Table *pTab;
1552 int i;
1553 pTab = sqlite3FindTable(db, zRight, zDb);
1554 if( pTab ){
1555 v = sqlite3GetVdbe(pParse);
1556 sqlite3VdbeSetNumCols(v, 3);
1557 pParse->nMem = 3;
1558 sqlite3CodeVerifySchema(pParse, iDb);
1559 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1560 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1561 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
1562 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
1563 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1564 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1565 sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
1566 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
1570 break;
1572 case PragTyp_DATABASE_LIST: {
1573 int i;
1574 sqlite3VdbeSetNumCols(v, 3);
1575 pParse->nMem = 3;
1576 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1577 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1578 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
1579 for(i=0; i<db->nDb; i++){
1580 if( db->aDb[i].pBt==0 ) continue;
1581 assert( db->aDb[i].zName!=0 );
1582 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1583 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1584 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1585 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
1586 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
1589 break;
1591 case PragTyp_COLLATION_LIST: {
1592 int i = 0;
1593 HashElem *p;
1594 sqlite3VdbeSetNumCols(v, 2);
1595 pParse->nMem = 2;
1596 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1597 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1598 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1599 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
1600 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1601 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1602 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
1605 break;
1606 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1608 #ifndef SQLITE_OMIT_FOREIGN_KEY
1609 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
1610 FKey *pFK;
1611 Table *pTab;
1612 pTab = sqlite3FindTable(db, zRight, zDb);
1613 if( pTab ){
1614 v = sqlite3GetVdbe(pParse);
1615 pFK = pTab->pFKey;
1616 if( pFK ){
1617 int i = 0;
1618 sqlite3VdbeSetNumCols(v, 8);
1619 pParse->nMem = 8;
1620 sqlite3CodeVerifySchema(pParse, iDb);
1621 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1622 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1623 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1624 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1625 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1626 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1627 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1628 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
1629 while(pFK){
1630 int j;
1631 for(j=0; j<pFK->nCol; j++){
1632 char *zCol = pFK->aCol[j].zCol;
1633 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1634 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
1635 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1636 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1637 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1638 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1639 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
1640 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
1641 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1642 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1643 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1644 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
1646 ++i;
1647 pFK = pFK->pNextFrom;
1652 break;
1653 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1655 #ifndef SQLITE_OMIT_FOREIGN_KEY
1656 #ifndef SQLITE_OMIT_TRIGGER
1657 case PragTyp_FOREIGN_KEY_CHECK: {
1658 FKey *pFK; /* A foreign key constraint */
1659 Table *pTab; /* Child table contain "REFERENCES" keyword */
1660 Table *pParent; /* Parent table that child points to */
1661 Index *pIdx; /* Index in the parent table */
1662 int i; /* Loop counter: Foreign key number for pTab */
1663 int j; /* Loop counter: Field of the foreign key */
1664 HashElem *k; /* Loop counter: Next table in schema */
1665 int x; /* result variable */
1666 int regResult; /* 3 registers to hold a result row */
1667 int regKey; /* Register to hold key for checking the FK */
1668 int regRow; /* Registers to hold a row from pTab */
1669 int addrTop; /* Top of a loop checking foreign keys */
1670 int addrOk; /* Jump here if the key is OK */
1671 int *aiCols; /* child to parent column mapping */
1673 regResult = pParse->nMem+1;
1674 pParse->nMem += 4;
1675 regKey = ++pParse->nMem;
1676 regRow = ++pParse->nMem;
1677 v = sqlite3GetVdbe(pParse);
1678 sqlite3VdbeSetNumCols(v, 4);
1679 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1680 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
1681 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1682 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
1683 sqlite3CodeVerifySchema(pParse, iDb);
1684 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1685 while( k ){
1686 if( zRight ){
1687 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1688 k = 0;
1689 }else{
1690 pTab = (Table*)sqliteHashData(k);
1691 k = sqliteHashNext(k);
1693 if( pTab==0 || pTab->pFKey==0 ) continue;
1694 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
1695 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
1696 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
1697 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1698 P4_TRANSIENT);
1699 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1700 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1701 if( pParent==0 ) continue;
1702 pIdx = 0;
1703 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
1704 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
1705 if( x==0 ){
1706 if( pIdx==0 ){
1707 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1708 }else{
1709 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
1710 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
1712 }else{
1713 k = 0;
1714 break;
1717 assert( pParse->nErr>0 || pFK==0 );
1718 if( pFK ) break;
1719 if( pParse->nTab<i ) pParse->nTab = i;
1720 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
1721 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1722 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1723 pIdx = 0;
1724 aiCols = 0;
1725 if( pParent ){
1726 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1727 assert( x==0 );
1729 addrOk = sqlite3VdbeMakeLabel(v);
1730 if( pParent && pIdx==0 ){
1731 int iKey = pFK->aCol[0].iFrom;
1732 assert( iKey>=0 && iKey<pTab->nCol );
1733 if( iKey!=pTab->iPKey ){
1734 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1735 sqlite3ColumnDefault(v, pTab, iKey, regRow);
1736 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1737 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1738 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
1739 }else{
1740 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
1742 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
1743 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1744 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1745 }else{
1746 for(j=0; j<pFK->nCol; j++){
1747 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
1748 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
1749 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
1751 if( pParent ){
1752 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1753 sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
1754 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
1755 VdbeCoverage(v);
1758 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1759 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1760 pFK->zTo, P4_TRANSIENT);
1761 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1762 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
1763 sqlite3VdbeResolveLabel(v, addrOk);
1764 sqlite3DbFree(db, aiCols);
1766 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
1767 sqlite3VdbeJumpHere(v, addrTop);
1770 break;
1771 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
1772 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1774 #ifndef NDEBUG
1775 case PragTyp_PARSER_TRACE: {
1776 if( zRight ){
1777 if( sqlite3GetBoolean(zRight, 0) ){
1778 sqlite3ParserTrace(stderr, "parser: ");
1779 }else{
1780 sqlite3ParserTrace(0, 0);
1784 break;
1785 #endif
1787 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1788 ** used will be case sensitive or not depending on the RHS.
1790 case PragTyp_CASE_SENSITIVE_LIKE: {
1791 if( zRight ){
1792 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
1795 break;
1797 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1798 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1799 #endif
1801 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
1802 /* Pragma "quick_check" is reduced version of
1803 ** integrity_check designed to detect most database corruption
1804 ** without most of the overhead of a full integrity-check.
1806 case PragTyp_INTEGRITY_CHECK: {
1807 int i, j, addr, mxErr;
1809 /* Code that appears at the end of the integrity check. If no error
1810 ** messages have been generated, output OK. Otherwise output the
1811 ** error message
1813 static const int iLn = VDBE_OFFSET_LINENO(2);
1814 static const VdbeOpList endCode[] = {
1815 { OP_IfNeg, 1, 0, 0}, /* 0 */
1816 { OP_String8, 0, 3, 0}, /* 1 */
1817 { OP_ResultRow, 3, 1, 0},
1820 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
1822 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1823 ** then iDb is set to the index of the database identified by <db>.
1824 ** In this case, the integrity of database iDb only is verified by
1825 ** the VDBE created below.
1827 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1828 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1829 ** to -1 here, to indicate that the VDBE should verify the integrity
1830 ** of all attached databases. */
1831 assert( iDb>=0 );
1832 assert( iDb==0 || pId2->z );
1833 if( pId2->z==0 ) iDb = -1;
1835 /* Initialize the VDBE program */
1836 pParse->nMem = 6;
1837 sqlite3VdbeSetNumCols(v, 1);
1838 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
1840 /* Set the maximum error count */
1841 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1842 if( zRight ){
1843 sqlite3GetInt32(zRight, &mxErr);
1844 if( mxErr<=0 ){
1845 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1848 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
1850 /* Do an integrity check on each database file */
1851 for(i=0; i<db->nDb; i++){
1852 HashElem *x;
1853 Hash *pTbls;
1854 int cnt = 0;
1856 if( OMIT_TEMPDB && i==1 ) continue;
1857 if( iDb>=0 && i!=iDb ) continue;
1859 sqlite3CodeVerifySchema(pParse, i);
1860 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
1861 VdbeCoverage(v);
1862 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1863 sqlite3VdbeJumpHere(v, addr);
1865 /* Do an integrity check of the B-Tree
1867 ** Begin by filling registers 2, 3, ... with the root pages numbers
1868 ** for all tables and indices in the database.
1870 assert( sqlite3SchemaMutexHeld(db, i, 0) );
1871 pTbls = &db->aDb[i].pSchema->tblHash;
1872 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1873 Table *pTab = sqliteHashData(x);
1874 Index *pIdx;
1875 if( HasRowid(pTab) ){
1876 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
1877 VdbeComment((v, "%s", pTab->zName));
1878 cnt++;
1880 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1881 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
1882 VdbeComment((v, "%s", pIdx->zName));
1883 cnt++;
1887 /* Make sure sufficient number of registers have been allocated */
1888 pParse->nMem = MAX( pParse->nMem, cnt+8 );
1890 /* Do the b-tree integrity checks */
1891 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
1892 sqlite3VdbeChangeP5(v, (u8)i);
1893 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
1894 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1895 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
1896 P4_DYNAMIC);
1897 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
1898 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
1899 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
1900 sqlite3VdbeJumpHere(v, addr);
1902 /* Make sure all the indices are constructed correctly.
1904 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
1905 Table *pTab = sqliteHashData(x);
1906 Index *pIdx, *pPk;
1907 Index *pPrior = 0;
1908 int loopTop;
1909 int iDataCur, iIdxCur;
1910 int r1 = -1;
1912 if( pTab->pIndex==0 ) continue;
1913 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
1914 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
1915 VdbeCoverage(v);
1916 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1917 sqlite3VdbeJumpHere(v, addr);
1918 sqlite3ExprCacheClear(pParse);
1919 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
1920 1, 0, &iDataCur, &iIdxCur);
1921 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
1922 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1923 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
1925 pParse->nMem = MAX(pParse->nMem, 8+j);
1926 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
1927 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
1928 /* Verify that all NOT NULL columns really are NOT NULL */
1929 for(j=0; j<pTab->nCol; j++){
1930 char *zErr;
1931 int jmp2, jmp3;
1932 if( j==pTab->iPKey ) continue;
1933 if( pTab->aCol[j].notNull==0 ) continue;
1934 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
1935 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
1936 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
1937 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1938 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
1939 pTab->aCol[j].zName);
1940 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1941 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1942 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1943 sqlite3VdbeAddOp0(v, OP_Halt);
1944 sqlite3VdbeJumpHere(v, jmp2);
1945 sqlite3VdbeJumpHere(v, jmp3);
1947 /* Validate index entries for the current row */
1948 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1949 int jmp2, jmp3, jmp4, jmp5;
1950 int ckUniq = sqlite3VdbeMakeLabel(v);
1951 if( pPk==pIdx ) continue;
1952 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1953 pPrior, r1);
1954 pPrior = pIdx;
1955 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
1956 /* Verify that an index entry exists for the current table row */
1957 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
1958 pIdx->nColumn); VdbeCoverage(v);
1959 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1960 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
1961 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1962 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1963 " missing from index ", P4_STATIC);
1964 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1965 jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
1966 pIdx->zName, P4_TRANSIENT);
1967 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1968 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1969 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
1970 sqlite3VdbeAddOp0(v, OP_Halt);
1971 sqlite3VdbeJumpHere(v, jmp2);
1972 /* For UNIQUE indexes, verify that only one entry exists with the
1973 ** current key. The entry is unique if (1) any column is NULL
1974 ** or (2) the next entry has a different key */
1975 if( IsUniqueIndex(pIdx) ){
1976 int uniqOk = sqlite3VdbeMakeLabel(v);
1977 int jmp6;
1978 int kk;
1979 for(kk=0; kk<pIdx->nKeyCol; kk++){
1980 int iCol = pIdx->aiColumn[kk];
1981 assert( iCol>=0 && iCol<pTab->nCol );
1982 if( pTab->aCol[iCol].notNull ) continue;
1983 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1984 VdbeCoverage(v);
1986 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1987 sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
1988 sqlite3VdbeJumpHere(v, jmp6);
1989 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1990 pIdx->nKeyCol); VdbeCoverage(v);
1991 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1992 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
1993 "non-unique entry in index ", P4_STATIC);
1994 sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
1995 sqlite3VdbeResolveLabel(v, uniqOk);
1997 sqlite3VdbeJumpHere(v, jmp4);
1998 sqlite3ResolvePartIdxLabel(pParse, jmp3);
2000 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
2001 sqlite3VdbeJumpHere(v, loopTop-1);
2002 #ifndef SQLITE_OMIT_BTREECOUNT
2003 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
2004 "wrong # of entries in index ", P4_STATIC);
2005 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
2006 if( pPk==pIdx ) continue;
2007 addr = sqlite3VdbeCurrentAddr(v);
2008 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
2009 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
2010 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
2011 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
2012 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
2013 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
2014 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
2015 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
2016 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
2018 #endif /* SQLITE_OMIT_BTREECOUNT */
2021 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
2022 sqlite3VdbeChangeP3(v, addr, -mxErr);
2023 sqlite3VdbeJumpHere(v, addr);
2024 sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
2026 break;
2027 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
2029 #ifndef SQLITE_OMIT_UTF16
2031 ** PRAGMA encoding
2032 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
2034 ** In its first form, this pragma returns the encoding of the main
2035 ** database. If the database is not initialized, it is initialized now.
2037 ** The second form of this pragma is a no-op if the main database file
2038 ** has not already been initialized. In this case it sets the default
2039 ** encoding that will be used for the main database file if a new file
2040 ** is created. If an existing main database file is opened, then the
2041 ** default text encoding for the existing database is used.
2043 ** In all cases new databases created using the ATTACH command are
2044 ** created to use the same default text encoding as the main database. If
2045 ** the main database has not been initialized and/or created when ATTACH
2046 ** is executed, this is done before the ATTACH operation.
2048 ** In the second form this pragma sets the text encoding to be used in
2049 ** new database files created using this database handle. It is only
2050 ** useful if invoked immediately after the main database i
2052 case PragTyp_ENCODING: {
2053 static const struct EncName {
2054 char *zName;
2055 u8 enc;
2056 } encnames[] = {
2057 { "UTF8", SQLITE_UTF8 },
2058 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
2059 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
2060 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
2061 { "UTF16le", SQLITE_UTF16LE },
2062 { "UTF16be", SQLITE_UTF16BE },
2063 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
2064 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
2065 { 0, 0 }
2067 const struct EncName *pEnc;
2068 if( !zRight ){ /* "PRAGMA encoding" */
2069 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
2070 sqlite3VdbeSetNumCols(v, 1);
2071 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
2072 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
2073 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
2074 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
2075 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
2076 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
2077 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2078 }else{ /* "PRAGMA encoding = XXX" */
2079 /* Only change the value of sqlite.enc if the database handle is not
2080 ** initialized. If the main database exists, the new sqlite.enc value
2081 ** will be overwritten when the schema is next loaded. If it does not
2082 ** already exists, it will be created to use the new encoding value.
2084 if(
2085 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
2086 DbHasProperty(db, 0, DB_Empty)
2088 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
2089 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
2090 SCHEMA_ENC(db) = ENC(db) =
2091 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
2092 break;
2095 if( !pEnc->zName ){
2096 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
2101 break;
2102 #endif /* SQLITE_OMIT_UTF16 */
2104 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
2106 ** PRAGMA [database.]schema_version
2107 ** PRAGMA [database.]schema_version = <integer>
2109 ** PRAGMA [database.]user_version
2110 ** PRAGMA [database.]user_version = <integer>
2112 ** PRAGMA [database.]freelist_count = <integer>
2114 ** PRAGMA [database.]application_id
2115 ** PRAGMA [database.]application_id = <integer>
2117 ** The pragma's schema_version and user_version are used to set or get
2118 ** the value of the schema-version and user-version, respectively. Both
2119 ** the schema-version and the user-version are 32-bit signed integers
2120 ** stored in the database header.
2122 ** The schema-cookie is usually only manipulated internally by SQLite. It
2123 ** is incremented by SQLite whenever the database schema is modified (by
2124 ** creating or dropping a table or index). The schema version is used by
2125 ** SQLite each time a query is executed to ensure that the internal cache
2126 ** of the schema used when compiling the SQL query matches the schema of
2127 ** the database against which the compiled query is actually executed.
2128 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2129 ** the schema-version is potentially dangerous and may lead to program
2130 ** crashes or database corruption. Use with caution!
2132 ** The user-version is not used internally by SQLite. It may be used by
2133 ** applications for any purpose.
2135 case PragTyp_HEADER_VALUE: {
2136 int iCookie = aPragmaNames[mid].iArg; /* Which cookie to read or write */
2137 sqlite3VdbeUsesBtree(v, iDb);
2138 if( zRight && (aPragmaNames[mid].mPragFlag & PragFlag_ReadOnly)==0 ){
2139 /* Write the specified cookie value */
2140 static const VdbeOpList setCookie[] = {
2141 { OP_Transaction, 0, 1, 0}, /* 0 */
2142 { OP_Integer, 0, 1, 0}, /* 1 */
2143 { OP_SetCookie, 0, 0, 1}, /* 2 */
2145 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
2146 sqlite3VdbeChangeP1(v, addr, iDb);
2147 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
2148 sqlite3VdbeChangeP1(v, addr+2, iDb);
2149 sqlite3VdbeChangeP2(v, addr+2, iCookie);
2150 }else{
2151 /* Read the specified cookie value */
2152 static const VdbeOpList readCookie[] = {
2153 { OP_Transaction, 0, 0, 0}, /* 0 */
2154 { OP_ReadCookie, 0, 1, 0}, /* 1 */
2155 { OP_ResultRow, 1, 1, 0}
2157 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
2158 sqlite3VdbeChangeP1(v, addr, iDb);
2159 sqlite3VdbeChangeP1(v, addr+1, iDb);
2160 sqlite3VdbeChangeP3(v, addr+1, iCookie);
2161 sqlite3VdbeSetNumCols(v, 1);
2162 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
2165 break;
2166 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
2168 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2170 ** PRAGMA compile_options
2172 ** Return the names of all compile-time options used in this build,
2173 ** one option per row.
2175 case PragTyp_COMPILE_OPTIONS: {
2176 int i = 0;
2177 const char *zOpt;
2178 sqlite3VdbeSetNumCols(v, 1);
2179 pParse->nMem = 1;
2180 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
2181 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
2182 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
2183 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2186 break;
2187 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2189 #ifndef SQLITE_OMIT_WAL
2191 ** PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate
2193 ** Checkpoint the database.
2195 case PragTyp_WAL_CHECKPOINT: {
2196 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
2197 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2198 if( zRight ){
2199 if( sqlite3StrICmp(zRight, "full")==0 ){
2200 eMode = SQLITE_CHECKPOINT_FULL;
2201 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2202 eMode = SQLITE_CHECKPOINT_RESTART;
2203 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
2204 eMode = SQLITE_CHECKPOINT_TRUNCATE;
2207 sqlite3VdbeSetNumCols(v, 3);
2208 pParse->nMem = 3;
2209 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
2210 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
2211 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
2213 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
2214 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
2216 break;
2219 ** PRAGMA wal_autocheckpoint
2220 ** PRAGMA wal_autocheckpoint = N
2222 ** Configure a database connection to automatically checkpoint a database
2223 ** after accumulating N frames in the log. Or query for the current value
2224 ** of N.
2226 case PragTyp_WAL_AUTOCHECKPOINT: {
2227 if( zRight ){
2228 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
2230 returnSingleInt(pParse, "wal_autocheckpoint",
2231 db->xWalCallback==sqlite3WalDefaultHook ?
2232 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
2234 break;
2235 #endif
2238 ** PRAGMA shrink_memory
2240 ** This pragma attempts to free as much memory as possible from the
2241 ** current database connection.
2243 case PragTyp_SHRINK_MEMORY: {
2244 sqlite3_db_release_memory(db);
2245 break;
2249 ** PRAGMA busy_timeout
2250 ** PRAGMA busy_timeout = N
2252 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
2253 ** if one is set. If no busy handler or a different busy handler is set
2254 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2255 ** disables the timeout.
2257 /*case PragTyp_BUSY_TIMEOUT*/ default: {
2258 assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
2259 if( zRight ){
2260 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2262 returnSingleInt(pParse, "timeout", db->busyTimeout);
2263 break;
2267 ** PRAGMA soft_heap_limit
2268 ** PRAGMA soft_heap_limit = N
2270 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
2271 ** use -1.
2273 case PragTyp_SOFT_HEAP_LIMIT: {
2274 sqlite3_int64 N;
2275 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
2276 sqlite3_soft_heap_limit64(N);
2278 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
2279 break;
2283 ** PRAGMA threads
2284 ** PRAGMA threads = N
2286 ** Configure the maximum number of worker threads. Return the new
2287 ** maximum, which might be less than requested.
2289 case PragTyp_THREADS: {
2290 sqlite3_int64 N;
2291 if( zRight
2292 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
2293 && N>=0
2295 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
2297 returnSingleInt(pParse, "threads",
2298 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
2299 break;
2302 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
2304 ** Report the current state of file logs for all databases
2306 case PragTyp_LOCK_STATUS: {
2307 static const char *const azLockName[] = {
2308 "unlocked", "shared", "reserved", "pending", "exclusive"
2310 int i;
2311 sqlite3VdbeSetNumCols(v, 2);
2312 pParse->nMem = 2;
2313 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
2314 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
2315 for(i=0; i<db->nDb; i++){
2316 Btree *pBt;
2317 const char *zState = "unknown";
2318 int j;
2319 if( db->aDb[i].zName==0 ) continue;
2320 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
2321 pBt = db->aDb[i].pBt;
2322 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
2323 zState = "closed";
2324 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
2325 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2326 zState = azLockName[j];
2328 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
2329 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
2331 break;
2333 #endif
2335 #ifdef SQLITE_HAS_CODEC
2336 case PragTyp_KEY: {
2337 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2338 break;
2340 case PragTyp_REKEY: {
2341 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2342 break;
2344 case PragTyp_HEXKEY: {
2345 if( zRight ){
2346 u8 iByte;
2347 int i;
2348 char zKey[40];
2349 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
2350 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
2351 if( (i&1)!=0 ) zKey[i/2] = iByte;
2353 if( (zLeft[3] & 0xf)==0xb ){
2354 sqlite3_key_v2(db, zDb, zKey, i/2);
2355 }else{
2356 sqlite3_rekey_v2(db, zDb, zKey, i/2);
2359 break;
2361 #endif
2362 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
2363 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
2364 #ifdef SQLITE_HAS_CODEC
2365 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
2366 sqlite3_activate_see(&zRight[4]);
2368 #endif
2369 #ifdef SQLITE_ENABLE_CEROD
2370 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
2371 sqlite3_activate_cerod(&zRight[6]);
2373 #endif
2375 break;
2376 #endif
2378 } /* End of the PRAGMA switch */
2380 pragma_out:
2381 sqlite3DbFree(db, zLeft);
2382 sqlite3DbFree(db, zRight);
2385 #endif /* SQLITE_OMIT_PRAGMA */