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