blob: a586370b8d41423d3918965f091e92b474143908 [file] [log] [blame]
drhc11d4f92003-04-06 21:08:24 +00001/*
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.
drhc11d4f92003-04-06 21:08:24 +000013*/
14#include "sqliteInt.h"
15
drh9ccd8652013-09-13 16:36:46 +000016#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
drh55e85ca2013-09-13 21:01:56 +000058#define PragTyp_SOFT_HEAP_LIMIT 27
drh3ef26152013-10-12 20:22:00 +000059#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
drhf63936e2013-10-03 14:08:07 +000072#define PragFlag_NeedSchema 0x01
drh9ccd8652013-09-13 16:36:46 +000073static const struct sPragmaNames {
drh77ff23f2013-09-13 21:03:45 +000074 const char *const zName; /* Name of pragma */
drhd49c3582013-09-13 19:00:06 +000075 u8 ePragTyp; /* PragTyp_XXX value */
drhf63936e2013-10-03 14:08:07 +000076 u8 mPragFlag; /* Zero or more PragFlag_XXX values */
drh9ccd8652013-09-13 16:36:46 +000077 u32 iArg; /* Extra argument */
78} aPragmaNames[] = {
79#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drhf63936e2013-10-03 14:08:07 +000080 { /* zName: */ "activate_extensions",
81 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
82 /* ePragFlag: */ 0,
83 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +000084#endif
85#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +000086 { /* zName: */ "application_id",
87 /* ePragTyp: */ PragTyp_HEADER_VALUE,
88 /* ePragFlag: */ 0,
89 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +000090#endif
91#if !defined(SQLITE_OMIT_AUTOVACUUM)
drhf63936e2013-10-03 14:08:07 +000092 { /* zName: */ "auto_vacuum",
93 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
94 /* ePragFlag: */ PragFlag_NeedSchema,
95 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +000096#endif
mistachkindd197832013-10-21 23:17:23 +000097#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh9ccd8652013-09-13 16:36:46 +000098#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
drhf63936e2013-10-03 14:08:07 +000099 { /* zName: */ "automatic_index",
100 /* ePragTyp: */ PragTyp_FLAG,
101 /* ePragFlag: */ 0,
102 /* iArg: */ SQLITE_AutoIndex },
drh9ccd8652013-09-13 16:36:46 +0000103#endif
mistachkindd197832013-10-21 23:17:23 +0000104#endif
drhf63936e2013-10-03 14:08:07 +0000105 { /* zName: */ "busy_timeout",
106 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
107 /* ePragFlag: */ 0,
108 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000109#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000110 { /* zName: */ "cache_size",
111 /* ePragTyp: */ PragTyp_CACHE_SIZE,
112 /* ePragFlag: */ PragFlag_NeedSchema,
113 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000114#endif
mistachkindd197832013-10-21 23:17:23 +0000115#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000116 { /* zName: */ "cache_spill",
117 /* ePragTyp: */ PragTyp_FLAG,
118 /* ePragFlag: */ 0,
119 /* iArg: */ SQLITE_CacheSpill },
mistachkindd197832013-10-21 23:17:23 +0000120#endif
drhf63936e2013-10-03 14:08:07 +0000121 { /* zName: */ "case_sensitive_like",
122 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
123 /* ePragFlag: */ 0,
124 /* iArg: */ 0 },
mistachkindd197832013-10-21 23:17:23 +0000125#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000126 { /* zName: */ "checkpoint_fullfsync",
127 /* ePragTyp: */ PragTyp_FLAG,
128 /* ePragFlag: */ 0,
129 /* iArg: */ SQLITE_CkptFullFSync },
mistachkindd197832013-10-21 23:17:23 +0000130#endif
drh9ccd8652013-09-13 16:36:46 +0000131#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000132 { /* zName: */ "collation_list",
133 /* ePragTyp: */ PragTyp_COLLATION_LIST,
134 /* ePragFlag: */ 0,
135 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000136#endif
137#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
drhf63936e2013-10-03 14:08:07 +0000138 { /* zName: */ "compile_options",
139 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
140 /* ePragFlag: */ 0,
141 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000142#endif
mistachkindd197832013-10-21 23:17:23 +0000143#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000144 { /* zName: */ "count_changes",
145 /* ePragTyp: */ PragTyp_FLAG,
146 /* ePragFlag: */ 0,
147 /* iArg: */ SQLITE_CountRows },
mistachkindd197832013-10-21 23:17:23 +0000148#endif
drh9ccd8652013-09-13 16:36:46 +0000149#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
drhf63936e2013-10-03 14:08:07 +0000150 { /* zName: */ "data_store_directory",
151 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
152 /* ePragFlag: */ 0,
153 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000154#endif
155#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000156 { /* zName: */ "database_list",
157 /* ePragTyp: */ PragTyp_DATABASE_LIST,
158 /* ePragFlag: */ PragFlag_NeedSchema,
159 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000160#endif
161#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhf63936e2013-10-03 14:08:07 +0000162 { /* zName: */ "default_cache_size",
163 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
164 /* ePragFlag: */ PragFlag_NeedSchema,
165 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000166#endif
mistachkindd197832013-10-21 23:17:23 +0000167#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh9ccd8652013-09-13 16:36:46 +0000168#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
drhf63936e2013-10-03 14:08:07 +0000169 { /* zName: */ "defer_foreign_keys",
170 /* ePragTyp: */ PragTyp_FLAG,
171 /* ePragFlag: */ 0,
172 /* iArg: */ SQLITE_DeferFKs },
drh9ccd8652013-09-13 16:36:46 +0000173#endif
mistachkindd197832013-10-21 23:17:23 +0000174#endif
175#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000176 { /* zName: */ "empty_result_callbacks",
177 /* ePragTyp: */ PragTyp_FLAG,
178 /* ePragFlag: */ 0,
179 /* iArg: */ SQLITE_NullCallback },
mistachkindd197832013-10-21 23:17:23 +0000180#endif
drh9ccd8652013-09-13 16:36:46 +0000181#if !defined(SQLITE_OMIT_UTF16)
drhf63936e2013-10-03 14:08:07 +0000182 { /* zName: */ "encoding",
183 /* ePragTyp: */ PragTyp_ENCODING,
184 /* ePragFlag: */ 0,
185 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000186#endif
187#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
drhf63936e2013-10-03 14:08:07 +0000188 { /* zName: */ "foreign_key_check",
189 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
190 /* ePragFlag: */ PragFlag_NeedSchema,
191 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000192#endif
193#if !defined(SQLITE_OMIT_FOREIGN_KEY)
drhf63936e2013-10-03 14:08:07 +0000194 { /* zName: */ "foreign_key_list",
195 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
196 /* ePragFlag: */ PragFlag_NeedSchema,
197 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000198#endif
mistachkindd197832013-10-21 23:17:23 +0000199#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh9ccd8652013-09-13 16:36:46 +0000200#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
drhf63936e2013-10-03 14:08:07 +0000201 { /* zName: */ "foreign_keys",
202 /* ePragTyp: */ PragTyp_FLAG,
203 /* ePragFlag: */ 0,
204 /* iArg: */ SQLITE_ForeignKeys },
drh9ccd8652013-09-13 16:36:46 +0000205#endif
mistachkindd197832013-10-21 23:17:23 +0000206#endif
drh9ccd8652013-09-13 16:36:46 +0000207#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000208 { /* zName: */ "freelist_count",
209 /* ePragTyp: */ PragTyp_HEADER_VALUE,
210 /* ePragFlag: */ 0,
211 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000212#endif
mistachkindd197832013-10-21 23:17:23 +0000213#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000214 { /* 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 },
mistachkindd197832013-10-21 23:17:23 +0000222#endif
drh9ccd8652013-09-13 16:36:46 +0000223#if defined(SQLITE_HAS_CODEC)
drhf63936e2013-10-03 14:08:07 +0000224 { /* zName: */ "hexkey",
225 /* ePragTyp: */ PragTyp_HEXKEY,
226 /* ePragFlag: */ 0,
227 /* iArg: */ 0 },
drh8ab88322013-10-07 00:36:01 +0000228 { /* zName: */ "hexrekey",
229 /* ePragTyp: */ PragTyp_HEXKEY,
230 /* ePragFlag: */ 0,
231 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000232#endif
mistachkindd197832013-10-21 23:17:23 +0000233#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh9ccd8652013-09-13 16:36:46 +0000234#if !defined(SQLITE_OMIT_CHECK)
drhf63936e2013-10-03 14:08:07 +0000235 { /* zName: */ "ignore_check_constraints",
236 /* ePragTyp: */ PragTyp_FLAG,
237 /* ePragFlag: */ 0,
238 /* iArg: */ SQLITE_IgnoreChecks },
drh9ccd8652013-09-13 16:36:46 +0000239#endif
mistachkindd197832013-10-21 23:17:23 +0000240#endif
drh9ccd8652013-09-13 16:36:46 +0000241#if !defined(SQLITE_OMIT_AUTOVACUUM)
drhf63936e2013-10-03 14:08:07 +0000242 { /* zName: */ "incremental_vacuum",
243 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
244 /* ePragFlag: */ PragFlag_NeedSchema,
245 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000246#endif
247#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000248 { /* 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 },
drh9ccd8652013-09-13 16:36:46 +0000256#endif
257#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
drhf63936e2013-10-03 14:08:07 +0000258 { /* zName: */ "integrity_check",
259 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
260 /* ePragFlag: */ PragFlag_NeedSchema,
261 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000262#endif
263#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000264 { /* 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 },
drh9ccd8652013-09-13 16:36:46 +0000272#endif
273#if defined(SQLITE_HAS_CODEC)
drhf63936e2013-10-03 14:08:07 +0000274 { /* zName: */ "key",
275 /* ePragTyp: */ PragTyp_KEY,
276 /* ePragFlag: */ 0,
277 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000278#endif
mistachkindd197832013-10-21 23:17:23 +0000279#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000280 { /* zName: */ "legacy_file_format",
281 /* ePragTyp: */ PragTyp_FLAG,
282 /* ePragFlag: */ 0,
283 /* iArg: */ SQLITE_LegacyFileFmt },
mistachkindd197832013-10-21 23:17:23 +0000284#endif
drh9ccd8652013-09-13 16:36:46 +0000285#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
drhf63936e2013-10-03 14:08:07 +0000286 { /* zName: */ "lock_proxy_file",
287 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
288 /* ePragFlag: */ 0,
289 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000290#endif
291#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drhf63936e2013-10-03 14:08:07 +0000292 { /* zName: */ "lock_status",
293 /* ePragTyp: */ PragTyp_LOCK_STATUS,
294 /* ePragFlag: */ 0,
295 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000296#endif
297#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000298 { /* 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 },
drh9ccd8652013-09-13 16:36:46 +0000318#endif
319#if defined(SQLITE_DEBUG)
drhf63936e2013-10-03 14:08:07 +0000320 { /* zName: */ "parser_trace",
321 /* ePragTyp: */ PragTyp_PARSER_TRACE,
322 /* ePragFlag: */ 0,
323 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000324#endif
mistachkindd197832013-10-21 23:17:23 +0000325#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000326 { /* zName: */ "query_only",
327 /* ePragTyp: */ PragTyp_FLAG,
328 /* ePragFlag: */ 0,
329 /* iArg: */ SQLITE_QueryOnly },
mistachkindd197832013-10-21 23:17:23 +0000330#endif
drh9ccd8652013-09-13 16:36:46 +0000331#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
drhf63936e2013-10-03 14:08:07 +0000332 { /* zName: */ "quick_check",
333 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
334 /* ePragFlag: */ PragFlag_NeedSchema,
335 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000336#endif
mistachkindd197832013-10-21 23:17:23 +0000337#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000338 { /* 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 },
mistachkindd197832013-10-21 23:17:23 +0000346#endif
drh9ccd8652013-09-13 16:36:46 +0000347#if defined(SQLITE_HAS_CODEC)
drhf63936e2013-10-03 14:08:07 +0000348 { /* zName: */ "rekey",
349 /* ePragTyp: */ PragTyp_REKEY,
350 /* ePragFlag: */ 0,
351 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000352#endif
mistachkindd197832013-10-21 23:17:23 +0000353#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000354 { /* zName: */ "reverse_unordered_selects",
355 /* ePragTyp: */ PragTyp_FLAG,
356 /* ePragFlag: */ 0,
357 /* iArg: */ SQLITE_ReverseOrder },
mistachkindd197832013-10-21 23:17:23 +0000358#endif
drh9ccd8652013-09-13 16:36:46 +0000359#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000360 { /* zName: */ "schema_version",
361 /* ePragTyp: */ PragTyp_HEADER_VALUE,
362 /* ePragFlag: */ 0,
363 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000364#endif
365#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000366 { /* zName: */ "secure_delete",
367 /* ePragTyp: */ PragTyp_SECURE_DELETE,
368 /* ePragFlag: */ 0,
369 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000370#endif
mistachkindd197832013-10-21 23:17:23 +0000371#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000372 { /* zName: */ "short_column_names",
373 /* ePragTyp: */ PragTyp_FLAG,
374 /* ePragFlag: */ 0,
375 /* iArg: */ SQLITE_ShortColNames },
mistachkindd197832013-10-21 23:17:23 +0000376#endif
drhf63936e2013-10-03 14:08:07 +0000377 { /* 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 },
mistachkindd197832013-10-21 23:17:23 +0000385#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh9ccd8652013-09-13 16:36:46 +0000386#if defined(SQLITE_DEBUG)
drhf63936e2013-10-03 14:08:07 +0000387 { /* zName: */ "sql_trace",
388 /* ePragTyp: */ PragTyp_FLAG,
389 /* ePragFlag: */ 0,
390 /* iArg: */ SQLITE_SqlTrace },
drh9ccd8652013-09-13 16:36:46 +0000391#endif
mistachkindd197832013-10-21 23:17:23 +0000392#endif
drh3ef26152013-10-12 20:22:00 +0000393#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
394 { /* zName: */ "stats",
395 /* ePragTyp: */ PragTyp_STATS,
396 /* ePragFlag: */ PragFlag_NeedSchema,
397 /* iArg: */ 0 },
398#endif
drh9ccd8652013-09-13 16:36:46 +0000399#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000400 { /* zName: */ "synchronous",
401 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
402 /* ePragFlag: */ PragFlag_NeedSchema,
403 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000404#endif
405#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000406 { /* zName: */ "table_info",
407 /* ePragTyp: */ PragTyp_TABLE_INFO,
408 /* ePragFlag: */ PragFlag_NeedSchema,
409 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000410#endif
411#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000412 { /* 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 },
drh9ccd8652013-09-13 16:36:46 +0000420#endif
421#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000422 { /* zName: */ "user_version",
423 /* ePragTyp: */ PragTyp_HEADER_VALUE,
424 /* ePragFlag: */ 0,
425 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000426#endif
mistachkindd197832013-10-21 23:17:23 +0000427#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drh9ccd8652013-09-13 16:36:46 +0000428#if defined(SQLITE_DEBUG)
drhf63936e2013-10-03 14:08:07 +0000429 { /* 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 },
drh84e55a82013-11-13 17:58:23 +0000437 { /* zName: */ "vdbe_eqp",
438 /* ePragTyp: */ PragTyp_FLAG,
439 /* ePragFlag: */ 0,
440 /* iArg: */ SQLITE_VdbeEQP },
drhf63936e2013-10-03 14:08:07 +0000441 { /* 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 },
drh9ccd8652013-09-13 16:36:46 +0000449#endif
mistachkindd197832013-10-21 23:17:23 +0000450#endif
drh9ccd8652013-09-13 16:36:46 +0000451#if !defined(SQLITE_OMIT_WAL)
drhf63936e2013-10-03 14:08:07 +0000452 { /* 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 },
drh9ccd8652013-09-13 16:36:46 +0000460#endif
mistachkindd197832013-10-21 23:17:23 +0000461#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000462 { /* zName: */ "writable_schema",
463 /* ePragTyp: */ PragTyp_FLAG,
464 /* ePragFlag: */ 0,
465 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
mistachkindd197832013-10-21 23:17:23 +0000466#endif
drh9ccd8652013-09-13 16:36:46 +0000467};
drh84e55a82013-11-13 17:58:23 +0000468/* Number of pragmas: 56 on by default, 69 total. */
drh9ccd8652013-09-13 16:36:46 +0000469/* End of the automatically generated pragma table.
470***************************************************************************/
471
drhc11d4f92003-04-06 21:08:24 +0000472/*
drhc11d4f92003-04-06 21:08:24 +0000473** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +0000474** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
drh908c0052012-01-30 18:40:55 +0000475** unrecognized string argument. The FULL option is disallowed
476** if the omitFull parameter it 1.
drhc11d4f92003-04-06 21:08:24 +0000477**
478** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +0000479** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +0000480** 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*/
drh908c0052012-01-30 18:40:55 +0000483static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
drh722e95a2004-10-25 20:33:44 +0000484 /* 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;
danielk197778ca0e72009-01-20 16:53:39 +0000490 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +0000491 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +0000492 }
drhea678832008-12-10 19:26:22 +0000493 n = sqlite3Strlen30(z);
drh908c0052012-01-30 18:40:55 +0000494 for(i=0; i<ArraySize(iLength)-omitFull; i++){
drh722e95a2004-10-25 20:33:44 +0000495 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
496 return iValue[i];
497 }
drhc11d4f92003-04-06 21:08:24 +0000498 }
drh908c0052012-01-30 18:40:55 +0000499 return dflt;
drhc11d4f92003-04-06 21:08:24 +0000500}
501
502/*
drh722e95a2004-10-25 20:33:44 +0000503** Interpret the given string as a boolean value.
504*/
drh38d9c612012-01-31 14:24:47 +0000505u8 sqlite3GetBoolean(const char *z, int dflt){
506 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:44 +0000507}
508
drhc7dc9bf2011-06-03 13:02:57 +0000509/* 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
danielk197741483462007-03-24 16:45:04 +0000515/*
516** Interpret the given string as a locking mode value.
517*/
518static 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
danielk1977dddbcdc2007-04-26 14:42:34 +0000526#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*/
533static 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;
drh60ac3f42010-11-23 18:59:27 +0000538 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +0000539 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +0000540}
541#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
542
danielk1977b84f96f2005-01-20 11:32:23 +0000543#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +0000544/*
drh90f5ecb2004-07-22 01:19:35 +0000545** 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*/
549static 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}
drhbf216272005-02-26 18:10:44 +0000560#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000561
drhbf216272005-02-26 18:10:44 +0000562#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000563/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000564** Invalidate temp storage, either when the temp storage is changed
565** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000566*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000567static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000568 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000569 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000570 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000571 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;
drh81028a42012-05-15 18:28:27 +0000577 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35 +0000578 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000579 return SQLITE_OK;
580}
drhbf216272005-02-26 18:10:44 +0000581#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000582
drhbf216272005-02-26 18:10:44 +0000583#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000584/*
585** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000586** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000587** or DEFAULT_TEMP_STORE pragmas.
588*/
589static 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 }
drh4f21c4a2008-12-10 22:15:00 +0000596 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000597 return SQLITE_OK;
598}
drhbf216272005-02-26 18:10:44 +0000599#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000600
601/*
602** Generate code to return a single integer value.
603*/
drh3c713642009-04-04 16:02:32 +0000604static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000605 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000606 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000607 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);
drh10861722009-04-07 00:49:16 +0000612 sqlite3VdbeSetNumCols(v, 1);
613 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000614 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000615}
616
drhd3605a42013-08-17 15:42:29 +0000617
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
623static 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}
drhfeb56e02013-08-23 17:33:46 +0000642#else
643# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000644#endif
645
646
drhd2cb50b2009-01-09 21:41:17 +0000647/*
648** Return a human-readable name for a constraint resolution action.
649*/
danba9108b2009-09-22 07:13:42 +0000650#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000651static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000652 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000653 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000654 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;
danielk197750af3e12008-10-10 17:47:21 +0000660 }
drhd2cb50b2009-01-09 21:41:17 +0000661 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000662}
danba9108b2009-09-22 07:13:42 +0000663#endif
danielk197750af3e12008-10-10 17:47:21 +0000664
dane04dc882010-04-20 18:53:15 +0000665
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*/
671const char *sqlite3JournalModename(int eMode){
672 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000673 "delete", "persist", "off", "truncate", "memory"
674#ifndef SQLITE_OMIT_WAL
675 , "wal"
676#endif
dane04dc882010-04-20 18:53:15 +0000677 };
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
drh87223182004-02-21 14:00:29 +0000690/*
drhc11d4f92003-04-06 21:08:24 +0000691** Process a pragma statement.
692**
693** Pragmas are of this form:
694**
danielk197791cf71b2004-06-26 06:37:06 +0000695** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000696**
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.
drh90f5ecb2004-07-22 01:19:35 +0000700**
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.
drhc11d4f92003-04-06 21:08:24 +0000704*/
danielk197791cf71b2004-06-26 06:37:06 +0000705void 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 */
drh3fa97302012-02-22 16:58:36 +0000716 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000717 int iDb; /* Database index for <database> */
718 int lwr, upr, mid; /* Binary search bounds */
drh06fd5d62012-02-22 14:45:19 +0000719 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
720 sqlite3 *db = pParse->db; /* The database connection */
721 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000722 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh06fd5d62012-02-22 14:45:19 +0000723
drhc11d4f92003-04-06 21:08:24 +0000724 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000725 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000726 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000727
danielk197791cf71b2004-06-26 06:37:06 +0000728 /* 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;
drh90f5ecb2004-07-22 01:19:35 +0000732 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000733
danielk1977ddfb2f02006-02-17 12:25:14 +0000734 /* 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
drh17435752007-08-16 04:30:38 +0000741 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000742 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000743 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000744 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000745 }else{
drh17435752007-08-16 04:30:38 +0000746 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000747 }
danielk197791cf71b2004-06-26 06:37:06 +0000748
drhd2cb50b2009-01-09 21:41:17 +0000749 assert( pId2 );
750 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000751 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000752 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000753 }
drh06fd5d62012-02-22 14:45:19 +0000754
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 */
drh3fa97302012-02-22 16:58:36 +0000759 aFcntl[0] = 0;
760 aFcntl[1] = zLeft;
761 aFcntl[2] = zRight;
762 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000763 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000764 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
765 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000766 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 }
drh9ccd8652013-09-13 16:36:46 +0000774 goto pragma_out;
775 }
776 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000777 if( aFcntl[0] ){
778 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
779 sqlite3_free(aFcntl[0]);
780 }
781 pParse->nErr++;
782 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000783 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
drhf63936e2013-10-03 14:08:07 +0000801 /* 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
drh9ccd8652013-09-13 16:36:46 +0000806 /* Jump to the appropriate pragma handler */
807 switch( aPragmaNames[mid].ePragTyp ){
808
drhe73c9142011-11-09 16:12:24 +0000809#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000810 /*
drh90f5ecb2004-07-22 01:19:35 +0000811 ** PRAGMA [database.]default_cache_size
812 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000813 **
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 **
drh93791ea2010-04-26 17:36:35 +0000820 ** 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.
drhc11d4f92003-04-06 21:08:24 +0000825 */
drh9ccd8652013-09-13 16:36:46 +0000826 case PragTyp_DEFAULT_CACHE_SIZE: {
drh688852a2014-02-17 22:40:43 +0000827 static const int iLn = __LINE__+2;
drh57196282004-10-06 15:41:16 +0000828 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000829 { OP_Transaction, 0, 0, 0}, /* 0 */
830 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000831 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000832 { OP_Integer, 0, 2, 0},
833 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000834 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000835 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000836 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000837 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000838 };
drh905793e2004-02-21 13:31:09 +0000839 int addr;
drhfb982642007-08-30 01:19:59 +0000840 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000841 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000842 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000843 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000844 pParse->nMem += 2;
drh688852a2014-02-17 22:40:43 +0000845 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
danielk197791cf71b2004-06-26 06:37:06 +0000846 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000847 sqlite3VdbeChangeP1(v, addr+1, iDb);
848 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000849 }else{
drhd50ffc42011-03-08 02:38:28 +0000850 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000851 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000852 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000853 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000854 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000855 pDb->pSchema->cache_size = size;
856 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000857 }
drh9ccd8652013-09-13 16:36:46 +0000858 break;
859 }
drhe73c9142011-11-09 16:12:24 +0000860#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000861
drhe73c9142011-11-09 16:12:24 +0000862#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000863 /*
drh90f5ecb2004-07-22 01:19:35 +0000864 ** PRAGMA [database.]page_size
865 ** PRAGMA [database.]page_size=N
866 **
867 ** The first form reports the current setting for the
868 ** database page size in bytes. The second form sets the
869 ** database page size value. The value can only be set if
870 ** the database has not yet been created.
871 */
drh9ccd8652013-09-13 16:36:46 +0000872 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000873 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000874 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000875 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000876 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000877 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000878 }else{
danielk1977992772c2007-08-30 10:07:38 +0000879 /* Malloc may fail when setting the page-size, as there is an internal
880 ** buffer that the pager module resizes using sqlite3_realloc().
881 */
drh60ac3f42010-11-23 18:59:27 +0000882 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000883 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000884 db->mallocFailed = 1;
885 }
drh90f5ecb2004-07-22 01:19:35 +0000886 }
drh9ccd8652013-09-13 16:36:46 +0000887 break;
888 }
danielk197741483462007-03-24 16:45:04 +0000889
890 /*
drh5b47efa2010-02-12 18:18:39 +0000891 ** PRAGMA [database.]secure_delete
892 ** PRAGMA [database.]secure_delete=ON/OFF
893 **
894 ** The first form reports the current setting for the
895 ** secure_delete flag. The second form changes the secure_delete
896 ** flag setting and reports thenew value.
897 */
drh9ccd8652013-09-13 16:36:46 +0000898 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000899 Btree *pBt = pDb->pBt;
900 int b = -1;
901 assert( pBt!=0 );
902 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000903 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000904 }
drhaf034ed2010-02-12 19:46:26 +0000905 if( pId2->n==0 && b>=0 ){
906 int ii;
907 for(ii=0; ii<db->nDb; ii++){
908 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
909 }
910 }
drh5b47efa2010-02-12 18:18:39 +0000911 b = sqlite3BtreeSecureDelete(pBt, b);
912 returnSingleInt(pParse, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000913 break;
914 }
drh5b47efa2010-02-12 18:18:39 +0000915
916 /*
drh60ac3f42010-11-23 18:59:27 +0000917 ** PRAGMA [database.]max_page_count
918 ** PRAGMA [database.]max_page_count=N
919 **
920 ** The first form reports the current setting for the
921 ** maximum number of pages in the database file. The
922 ** second form attempts to change this setting. Both
923 ** forms return the current setting.
924 **
drhe73c9142011-11-09 16:12:24 +0000925 ** The absolute value of N is used. This is undocumented and might
926 ** change. The only purpose is to provide an easy way to test
927 ** the sqlite3AbsInt32() function.
928 **
danielk197759a93792008-05-15 17:48:20 +0000929 ** PRAGMA [database.]page_count
930 **
931 ** Return the number of pages in the specified database.
932 */
drh9ccd8652013-09-13 16:36:46 +0000933 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000934 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000935 sqlite3CodeVerifySchema(pParse, iDb);
936 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000937 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000938 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
939 }else{
drhe73c9142011-11-09 16:12:24 +0000940 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
941 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000942 }
danielk197759a93792008-05-15 17:48:20 +0000943 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
944 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000945 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000946 break;
947 }
danielk197759a93792008-05-15 17:48:20 +0000948
949 /*
danielk197741483462007-03-24 16:45:04 +0000950 ** PRAGMA [database.]locking_mode
951 ** PRAGMA [database.]locking_mode = (normal|exclusive)
952 */
drh9ccd8652013-09-13 16:36:46 +0000953 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000954 const char *zRet = "normal";
955 int eMode = getLockingMode(zRight);
956
957 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
958 /* Simple "PRAGMA locking_mode;" statement. This is a query for
959 ** the current default locking mode (which may be different to
960 ** the locking-mode of the main database).
961 */
962 eMode = db->dfltLockMode;
963 }else{
964 Pager *pPager;
965 if( pId2->n==0 ){
966 /* This indicates that no database name was specified as part
967 ** of the PRAGMA command. In this case the locking-mode must be
968 ** set on all attached databases, as well as the main db file.
969 **
970 ** Also, the sqlite3.dfltLockMode variable is set so that
971 ** any subsequently attached databases also use the specified
972 ** locking mode.
973 */
974 int ii;
975 assert(pDb==&db->aDb[0]);
976 for(ii=2; ii<db->nDb; ii++){
977 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
978 sqlite3PagerLockingMode(pPager, eMode);
979 }
drh4f21c4a2008-12-10 22:15:00 +0000980 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000981 }
982 pPager = sqlite3BtreePager(pDb->pBt);
983 eMode = sqlite3PagerLockingMode(pPager, eMode);
984 }
985
drh9ccd8652013-09-13 16:36:46 +0000986 assert( eMode==PAGER_LOCKINGMODE_NORMAL
987 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000988 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
989 zRet = "exclusive";
990 }
991 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000992 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000993 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
994 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000995 break;
996 }
drh3b020132008-04-17 17:02:01 +0000997
998 /*
999 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +00001000 ** PRAGMA [database.]journal_mode =
1001 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +00001002 */
drh9ccd8652013-09-13 16:36:46 +00001003 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +00001004 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
1005 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +00001006
1007 sqlite3VdbeSetNumCols(v, 1);
1008 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +00001009
1010 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +00001011 /* If there is no "=MODE" part of the pragma, do a query for the
1012 ** current mode */
drh3b020132008-04-17 17:02:01 +00001013 eMode = PAGER_JOURNALMODE_QUERY;
1014 }else{
dane04dc882010-04-20 18:53:15 +00001015 const char *zMode;
drhea678832008-12-10 19:26:22 +00001016 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +00001017 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +00001018 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
1019 }
1020 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +00001021 /* If the "=MODE" part does not match any known journal mode,
1022 ** then do a query */
dane04dc882010-04-20 18:53:15 +00001023 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +00001024 }
1025 }
drhc6b2a0f2010-07-08 17:40:37 +00001026 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
1027 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
1028 iDb = 0;
1029 pId2->n = 1;
1030 }
1031 for(ii=db->nDb-1; ii>=0; ii--){
1032 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
1033 sqlite3VdbeUsesBtree(v, ii);
1034 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +00001035 }
drh3b020132008-04-17 17:02:01 +00001036 }
drh3b020132008-04-17 17:02:01 +00001037 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +00001038 break;
1039 }
danielk1977b53e4962008-06-04 06:45:59 +00001040
1041 /*
1042 ** PRAGMA [database.]journal_size_limit
1043 ** PRAGMA [database.]journal_size_limit=N
1044 **
drha9e364f2009-01-13 20:14:15 +00001045 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +00001046 */
drh9ccd8652013-09-13 16:36:46 +00001047 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +00001048 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1049 i64 iLimit = -2;
1050 if( zRight ){
mistachkine98844f2013-08-24 00:59:24 +00001051 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +00001052 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +00001053 }
1054 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +00001055 returnSingleInt(pParse, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +00001056 break;
1057 }
danielk1977b53e4962008-06-04 06:45:59 +00001058
drh13d70422004-11-13 15:59:14 +00001059#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +00001060
1061 /*
danielk1977951af802004-11-05 15:45:09 +00001062 ** PRAGMA [database.]auto_vacuum
1063 ** PRAGMA [database.]auto_vacuum=N
1064 **
drha9e364f2009-01-13 20:14:15 +00001065 ** Get or set the value of the database 'auto-vacuum' parameter.
1066 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +00001067 */
1068#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001069 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +00001070 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +00001071 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +00001072 if( !zRight ){
drhf63936e2013-10-03 14:08:07 +00001073 returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +00001074 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +00001075 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +00001076 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +00001077 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +00001078 /* Call SetAutoVacuum() to set initialize the internal auto and
1079 ** incr-vacuum flags. This is required in case this connection
1080 ** creates the database file. It is important that it is created
1081 ** as an auto-vacuum capable db.
1082 */
1083 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
1084 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
1085 /* When setting the auto_vacuum mode to either "full" or
1086 ** "incremental", write the value of meta[6] in the database
1087 ** file. Before writing to meta[6], check that meta[3] indicates
1088 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +00001089 */
drh688852a2014-02-17 22:40:43 +00001090 static const int iLn = __LINE__+2;
drhf63936e2013-10-03 14:08:07 +00001091 static const VdbeOpList setMeta6[] = {
1092 { OP_Transaction, 0, 1, 0}, /* 0 */
1093 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
1094 { OP_If, 1, 0, 0}, /* 2 */
1095 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
1096 { OP_Integer, 0, 1, 0}, /* 4 */
1097 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
1098 };
1099 int iAddr;
drh688852a2014-02-17 22:40:43 +00001100 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
drhf63936e2013-10-03 14:08:07 +00001101 sqlite3VdbeChangeP1(v, iAddr, iDb);
1102 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
1103 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
1104 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
1105 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
1106 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +00001107 }
danielk1977951af802004-11-05 15:45:09 +00001108 }
drh9ccd8652013-09-13 16:36:46 +00001109 break;
1110 }
danielk1977951af802004-11-05 15:45:09 +00001111#endif
1112
drhca5557f2007-05-04 18:30:40 +00001113 /*
1114 ** PRAGMA [database.]incremental_vacuum(N)
1115 **
1116 ** Do N steps of incremental vacuuming on a database.
1117 */
1118#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001119 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +00001120 int iLimit, addr;
1121 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
1122 iLimit = 0x7fffffff;
1123 }
1124 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +00001125 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh688852a2014-02-17 22:40:43 +00001126 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:11 +00001127 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +00001128 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh688852a2014-02-17 22:40:43 +00001129 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
drhca5557f2007-05-04 18:30:40 +00001130 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +00001131 break;
1132 }
drhca5557f2007-05-04 18:30:40 +00001133#endif
1134
drh13d70422004-11-13 15:59:14 +00001135#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +00001136 /*
drh90f5ecb2004-07-22 01:19:35 +00001137 ** PRAGMA [database.]cache_size
1138 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +00001139 **
1140 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +00001141 ** page cache size. The second form sets the local
1142 ** page cache size value. If N is positive then that is the
1143 ** number of pages in the cache. If N is negative, then the
1144 ** number of pages is adjusted so that the cache uses -N kibibytes
1145 ** of memory.
drhc11d4f92003-04-06 21:08:24 +00001146 */
drh9ccd8652013-09-13 16:36:46 +00001147 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +00001148 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +00001149 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +00001150 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001151 }else{
drh3b42abb2011-11-09 14:23:04 +00001152 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +00001153 pDb->pSchema->cache_size = size;
1154 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001155 }
drh9ccd8652013-09-13 16:36:46 +00001156 break;
1157 }
drhc11d4f92003-04-06 21:08:24 +00001158
1159 /*
drh9b4c59f2013-04-15 17:03:42 +00001160 ** PRAGMA [database.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +00001161 **
drh0d0614b2013-03-25 23:09:28 +00001162 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +00001163 ** used to limit the aggregate size of all memory mapped regions of the
1164 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +00001165 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +00001166 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +00001167 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +00001168 **
drh0d0614b2013-03-25 23:09:28 +00001169 ** This value is advisory. The underlying VFS is free to memory map
1170 ** as little or as much as it wants. Except, if N is set to 0 then the
1171 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +00001172 */
drh9ccd8652013-09-13 16:36:46 +00001173 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +00001174 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +00001175#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +00001176 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +00001177 if( zRight ){
drha1f42c72013-04-01 22:38:06 +00001178 int ii;
mistachkine98844f2013-08-24 00:59:24 +00001179 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh9b4c59f2013-04-15 17:03:42 +00001180 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
1181 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +00001182 for(ii=db->nDb-1; ii>=0; ii--){
1183 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +00001184 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +00001185 }
1186 }
dan5d8a1372013-03-19 19:28:06 +00001187 }
drh9b4c59f2013-04-15 17:03:42 +00001188 sz = -1;
dan3719f5f2013-05-23 10:13:18 +00001189 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +00001190#else
dan3719f5f2013-05-23 10:13:18 +00001191 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +00001192 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +00001193#endif
dan3719f5f2013-05-23 10:13:18 +00001194 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:42 +00001195 returnSingleInt(pParse, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +00001196 }else if( rc!=SQLITE_NOTFOUND ){
1197 pParse->nErr++;
1198 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +00001199 }
drh9ccd8652013-09-13 16:36:46 +00001200 break;
1201 }
dan5d8a1372013-03-19 19:28:06 +00001202
1203 /*
drh90f5ecb2004-07-22 01:19:35 +00001204 ** PRAGMA temp_store
1205 ** PRAGMA temp_store = "default"|"memory"|"file"
1206 **
1207 ** Return or set the local value of the temp_store flag. Changing
1208 ** the local value does not make changes to the disk file and the default
1209 ** value will be restored the next time the database is opened.
1210 **
1211 ** Note that it is possible for the library compile-time options to
1212 ** override this setting
1213 */
drh9ccd8652013-09-13 16:36:46 +00001214 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +00001215 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001216 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +00001217 }else{
1218 changeTempStorage(pParse, zRight);
1219 }
drh9ccd8652013-09-13 16:36:46 +00001220 break;
1221 }
drh90f5ecb2004-07-22 01:19:35 +00001222
1223 /*
tpoindex9a09a3c2004-12-20 19:01:32 +00001224 ** PRAGMA temp_store_directory
1225 ** PRAGMA temp_store_directory = ""|"directory_name"
1226 **
1227 ** Return or set the local value of the temp_store_directory flag. Changing
1228 ** the value sets a specific directory to be used for temporary files.
1229 ** Setting to a null string reverts to the default temporary directory search.
1230 ** If temporary directory is changed, then invalidateTempStorage.
1231 **
1232 */
drh9ccd8652013-09-13 16:36:46 +00001233 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +00001234 if( !zRight ){
1235 if( sqlite3_temp_directory ){
1236 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +00001237 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +00001238 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001239 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
1240 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +00001241 }
1242 }else{
drh78f82d12008-09-02 00:52:52 +00001243#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +00001244 if( zRight[0] ){
1245 int res;
danielk1977fab11272008-09-16 14:38:02 +00001246 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1247 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +00001248 sqlite3ErrorMsg(pParse, "not a writable directory");
1249 goto pragma_out;
1250 }
drh268283b2005-01-08 15:44:25 +00001251 }
danielk1977b06a0b62008-06-26 10:54:12 +00001252 if( SQLITE_TEMP_STORE==0
1253 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
1254 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +00001255 ){
1256 invalidateTempStorage(pParse);
1257 }
drh17435752007-08-16 04:30:38 +00001258 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +00001259 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +00001260 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +00001261 }else{
drh268283b2005-01-08 15:44:25 +00001262 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +00001263 }
drh78f82d12008-09-02 00:52:52 +00001264#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +00001265 }
drh9ccd8652013-09-13 16:36:46 +00001266 break;
1267 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001268
drhcc716452012-06-06 23:23:23 +00001269#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +00001270 /*
1271 ** PRAGMA data_store_directory
1272 ** PRAGMA data_store_directory = ""|"directory_name"
1273 **
1274 ** Return or set the local value of the data_store_directory flag. Changing
1275 ** the value sets a specific directory to be used for database files that
1276 ** were specified with a relative pathname. Setting to a null string reverts
1277 ** to the default database directory, which for database files specified with
1278 ** a relative path will probably be based on the current directory for the
1279 ** process. Database file specified with an absolute path are not impacted
1280 ** by this setting, regardless of its value.
1281 **
1282 */
drh9ccd8652013-09-13 16:36:46 +00001283 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +00001284 if( !zRight ){
1285 if( sqlite3_data_directory ){
1286 sqlite3VdbeSetNumCols(v, 1);
1287 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1288 "data_store_directory", SQLITE_STATIC);
1289 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
1290 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1291 }
1292 }else{
1293#ifndef SQLITE_OMIT_WSD
1294 if( zRight[0] ){
1295 int res;
1296 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1297 if( rc!=SQLITE_OK || res==0 ){
1298 sqlite3ErrorMsg(pParse, "not a writable directory");
1299 goto pragma_out;
1300 }
1301 }
1302 sqlite3_free(sqlite3_data_directory);
1303 if( zRight[0] ){
1304 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1305 }else{
1306 sqlite3_data_directory = 0;
1307 }
1308#endif /* SQLITE_OMIT_WSD */
1309 }
drh9ccd8652013-09-13 16:36:46 +00001310 break;
1311 }
drhcc716452012-06-06 23:23:23 +00001312#endif
mistachkina112d142012-03-14 00:44:01 +00001313
drhd2cb50b2009-01-09 21:41:17 +00001314#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +00001315 /*
drh9ccd8652013-09-13 16:36:46 +00001316 ** PRAGMA [database.]lock_proxy_file
1317 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
1318 **
1319 ** Return or set the value of the lock_proxy_file flag. Changing
1320 ** the value sets a specific file to be used for database access locks.
1321 **
1322 */
1323 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +00001324 if( !zRight ){
1325 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1326 char *proxy_file_path = NULL;
1327 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +00001328 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +00001329 &proxy_file_path);
1330
1331 if( proxy_file_path ){
1332 sqlite3VdbeSetNumCols(v, 1);
1333 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1334 "lock_proxy_file", SQLITE_STATIC);
1335 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
1336 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1337 }
1338 }else{
1339 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1340 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1341 int res;
1342 if( zRight[0] ){
1343 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1344 zRight);
1345 } else {
1346 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1347 NULL);
1348 }
1349 if( res!=SQLITE_OK ){
1350 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1351 goto pragma_out;
1352 }
1353 }
drh9ccd8652013-09-13 16:36:46 +00001354 break;
1355 }
drhd2cb50b2009-01-09 21:41:17 +00001356#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001357
1358 /*
drh90f5ecb2004-07-22 01:19:35 +00001359 ** PRAGMA [database.]synchronous
1360 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +00001361 **
1362 ** Return or set the local value of the synchronous flag. Changing
1363 ** the local value does not make changes to the disk file and the
1364 ** default value will be restored the next time the database is
1365 ** opened.
1366 */
drh9ccd8652013-09-13 16:36:46 +00001367 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001368 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001369 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001370 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001371 if( !db->autoCommit ){
1372 sqlite3ErrorMsg(pParse,
1373 "Safety level may not be changed inside a transaction");
1374 }else{
drh908c0052012-01-30 18:40:55 +00001375 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
drhd3605a42013-08-17 15:42:29 +00001376 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001377 }
drhc11d4f92003-04-06 21:08:24 +00001378 }
drh9ccd8652013-09-13 16:36:46 +00001379 break;
1380 }
drh13d70422004-11-13 15:59:14 +00001381#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001382
drhbf216272005-02-26 18:10:44 +00001383#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001384 case PragTyp_FLAG: {
1385 if( zRight==0 ){
1386 returnSingleInt(pParse, aPragmaNames[mid].zName,
1387 (db->flags & aPragmaNames[mid].iArg)!=0 );
1388 }else{
1389 int mask = aPragmaNames[mid].iArg; /* Mask of bits to set or clear. */
1390 if( db->autoCommit==0 ){
1391 /* Foreign key support may not be enabled or disabled while not
1392 ** in auto-commit mode. */
1393 mask &= ~(SQLITE_ForeignKeys);
1394 }
1395
1396 if( sqlite3GetBoolean(zRight, 0) ){
1397 db->flags |= mask;
1398 }else{
1399 db->flags &= ~mask;
1400 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1401 }
1402
1403 /* Many of the flag-pragmas modify the code generated by the SQL
1404 ** compiler (eg. count_changes). So add an opcode to expire all
1405 ** compiled SQL statements after modifying a pragma value.
1406 */
1407 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1408 setAllPagerFlags(db);
1409 }
1410 break;
1411 }
drhbf216272005-02-26 18:10:44 +00001412#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001413
drh13d70422004-11-13 15:59:14 +00001414#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001415 /*
1416 ** PRAGMA table_info(<table>)
1417 **
1418 ** Return a single row for each column of the named table. The columns of
1419 ** the returned data set are:
1420 **
1421 ** cid: Column id (numbered from left to right, starting at 0)
1422 ** name: Column name
1423 ** type: Column declaration type.
1424 ** notnull: True if 'NOT NULL' is part of column declaration
1425 ** dflt_value: The default value for the column, if any.
1426 */
drh9ccd8652013-09-13 16:36:46 +00001427 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001428 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001429 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001430 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001431 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001432 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001433 Column *pCol;
drh44156282013-10-23 22:23:03 +00001434 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh9f6696a2006-02-09 16:52:23 +00001435 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +00001436 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001437 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001438 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1439 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1440 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1441 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1442 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1443 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001444 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001445 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001446 if( IsHiddenColumn(pCol) ){
1447 nHidden++;
1448 continue;
1449 }
drh2d401ab2008-01-10 23:50:11 +00001450 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1451 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1452 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +00001453 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +00001454 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001455 if( pCol->zDflt ){
1456 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001457 }else{
drh2d401ab2008-01-10 23:50:11 +00001458 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001459 }
drh384b7fe2013-01-01 13:55:31 +00001460 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1461 k = 0;
1462 }else if( pPk==0 ){
1463 k = 1;
1464 }else{
1465 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1466 }
1467 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001468 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001469 }
1470 }
drh9ccd8652013-09-13 16:36:46 +00001471 }
1472 break;
drhc11d4f92003-04-06 21:08:24 +00001473
drh3ef26152013-10-12 20:22:00 +00001474 case PragTyp_STATS: {
1475 Index *pIdx;
1476 HashElem *i;
1477 v = sqlite3GetVdbe(pParse);
1478 sqlite3VdbeSetNumCols(v, 4);
1479 pParse->nMem = 4;
1480 sqlite3CodeVerifySchema(pParse, iDb);
1481 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1482 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
1483 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
1484 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
1485 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1486 Table *pTab = sqliteHashData(i);
1487 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
1488 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
1489 sqlite3VdbeAddOp2(v, OP_Integer,
1490 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
1491 sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
1492 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1493 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1494 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1495 sqlite3VdbeAddOp2(v, OP_Integer,
1496 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
1497 sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
1498 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1499 }
1500 }
1501 }
1502 break;
1503
drh9ccd8652013-09-13 16:36:46 +00001504 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001505 Index *pIdx;
1506 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001507 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001508 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001509 int i;
1510 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +00001511 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001512 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001513 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001514 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1515 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1516 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhbbbdc832013-10-22 18:01:40 +00001517 for(i=0; i<pIdx->nKeyCol; i++){
1518 i16 cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001519 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1520 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +00001521 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +00001522 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1523 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001524 }
1525 }
drh9ccd8652013-09-13 16:36:46 +00001526 }
1527 break;
drhc11d4f92003-04-06 21:08:24 +00001528
drh9ccd8652013-09-13 16:36:46 +00001529 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001530 Index *pIdx;
1531 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001532 int i;
drh2783e4b2004-10-05 15:42:53 +00001533 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001534 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001535 v = sqlite3GetVdbe(pParse);
drh3ef26152013-10-12 20:22:00 +00001536 sqlite3VdbeSetNumCols(v, 3);
1537 pParse->nMem = 3;
drhe13e9f52013-10-05 19:18:00 +00001538 sqlite3CodeVerifySchema(pParse, iDb);
1539 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1540 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1541 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
drh3ef26152013-10-12 20:22:00 +00001542 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhe13e9f52013-10-05 19:18:00 +00001543 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1544 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1545 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
drh3ef26152013-10-12 20:22:00 +00001546 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001547 }
1548 }
drh9ccd8652013-09-13 16:36:46 +00001549 }
1550 break;
drhc11d4f92003-04-06 21:08:24 +00001551
drh9ccd8652013-09-13 16:36:46 +00001552 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001553 int i;
drh13d70422004-11-13 15:59:14 +00001554 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001555 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001556 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1557 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1558 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001559 for(i=0; i<db->nDb; i++){
1560 if( db->aDb[i].pBt==0 ) continue;
1561 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001562 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1563 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1564 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001565 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001566 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001567 }
drh9ccd8652013-09-13 16:36:46 +00001568 }
1569 break;
danielk197748af65a2005-02-09 03:20:37 +00001570
drh9ccd8652013-09-13 16:36:46 +00001571 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001572 int i = 0;
1573 HashElem *p;
1574 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001575 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001576 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1577 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001578 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1579 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001580 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1581 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1582 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001583 }
drh9ccd8652013-09-13 16:36:46 +00001584 }
1585 break;
drh13d70422004-11-13 15:59:14 +00001586#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1587
drhb7f91642004-10-31 02:22:47 +00001588#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001589 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001590 FKey *pFK;
1591 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001592 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001593 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001594 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001595 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001596 if( pFK ){
1597 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001598 sqlite3VdbeSetNumCols(v, 8);
1599 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001600 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001601 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1602 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1603 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1604 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1605 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1606 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1607 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1608 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001609 while(pFK){
1610 int j;
1611 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001612 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001613 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1614 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001615 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1616 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1617 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1618 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001619 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001620 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001621 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1622 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1623 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1624 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001625 }
1626 ++i;
1627 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001628 }
drh78100cc2003-08-23 22:40:53 +00001629 }
1630 }
drh9ccd8652013-09-13 16:36:46 +00001631 }
1632 break;
drhb7f91642004-10-31 02:22:47 +00001633#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001634
drh6c5b9152012-12-17 16:46:37 +00001635#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001636#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001637 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001638 FKey *pFK; /* A foreign key constraint */
1639 Table *pTab; /* Child table contain "REFERENCES" keyword */
1640 Table *pParent; /* Parent table that child points to */
1641 Index *pIdx; /* Index in the parent table */
1642 int i; /* Loop counter: Foreign key number for pTab */
1643 int j; /* Loop counter: Field of the foreign key */
1644 HashElem *k; /* Loop counter: Next table in schema */
1645 int x; /* result variable */
1646 int regResult; /* 3 registers to hold a result row */
1647 int regKey; /* Register to hold key for checking the FK */
1648 int regRow; /* Registers to hold a row from pTab */
1649 int addrTop; /* Top of a loop checking foreign keys */
1650 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001651 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001652
drh613028b2012-12-17 18:43:02 +00001653 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001654 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001655 regKey = ++pParse->nMem;
1656 regRow = ++pParse->nMem;
1657 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001658 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001659 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1660 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001661 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1662 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001663 sqlite3CodeVerifySchema(pParse, iDb);
1664 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1665 while( k ){
1666 if( zRight ){
1667 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1668 k = 0;
1669 }else{
1670 pTab = (Table*)sqliteHashData(k);
1671 k = sqliteHashNext(k);
1672 }
drh9148def2012-12-17 20:40:39 +00001673 if( pTab==0 || pTab->pFKey==0 ) continue;
1674 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001675 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001676 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001677 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1678 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001679 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001680 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1681 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001682 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001683 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001684 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001685 if( x==0 ){
1686 if( pIdx==0 ){
1687 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1688 }else{
drh6c5b9152012-12-17 16:46:37 +00001689 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001690 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001691 }
1692 }else{
drh613028b2012-12-17 18:43:02 +00001693 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001694 break;
1695 }
drh6c5b9152012-12-17 16:46:37 +00001696 }
dan5e878302013-10-12 19:06:48 +00001697 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001698 if( pFK ) break;
1699 if( pParse->nTab<i ) pParse->nTab = i;
drh688852a2014-02-17 22:40:43 +00001700 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001701 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001702 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001703 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001704 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001705 if( pParent ){
1706 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1707 assert( x==0 );
1708 }
drh613028b2012-12-17 18:43:02 +00001709 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001710 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001711 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001712 assert( iKey>=0 && iKey<pTab->nCol );
1713 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001714 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1715 sqlite3ColumnDefault(v, pTab, iKey, regRow);
drh688852a2014-02-17 22:40:43 +00001716 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
1717 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1718 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
drh6c5b9152012-12-17 16:46:37 +00001719 }else{
drh613028b2012-12-17 18:43:02 +00001720 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001721 }
drh688852a2014-02-17 22:40:43 +00001722 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001723 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1724 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1725 }else{
1726 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001727 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001728 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh688852a2014-02-17 22:40:43 +00001729 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001730 }
dan5e878302013-10-12 19:06:48 +00001731 if( pParent ){
drh57bf4a82014-02-17 14:59:22 +00001732 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
1733 sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
dan5e878302013-10-12 19:06:48 +00001734 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh688852a2014-02-17 22:40:43 +00001735 VdbeCoverage(v);
dan5e878302013-10-12 19:06:48 +00001736 }
drh6c5b9152012-12-17 16:46:37 +00001737 }
drh613028b2012-12-17 18:43:02 +00001738 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001739 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1740 pFK->zTo, P4_TRANSIENT);
1741 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1742 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001743 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001744 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001745 }
drh688852a2014-02-17 22:40:43 +00001746 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
drh613028b2012-12-17 18:43:02 +00001747 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001748 }
drh9ccd8652013-09-13 16:36:46 +00001749 }
1750 break;
dan09ff9e12013-03-11 11:49:03 +00001751#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001752#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1753
drhc11d4f92003-04-06 21:08:24 +00001754#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001755 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001756 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001757 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001758 sqlite3ParserTrace(stderr, "parser: ");
1759 }else{
1760 sqlite3ParserTrace(0, 0);
1761 }
drhc11d4f92003-04-06 21:08:24 +00001762 }
drh9ccd8652013-09-13 16:36:46 +00001763 }
1764 break;
drhc11d4f92003-04-06 21:08:24 +00001765#endif
1766
drh55ef4d92005-08-14 01:20:37 +00001767 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1768 ** used will be case sensitive or not depending on the RHS.
1769 */
drh9ccd8652013-09-13 16:36:46 +00001770 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001771 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001772 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001773 }
drh9ccd8652013-09-13 16:36:46 +00001774 }
1775 break;
drh55ef4d92005-08-14 01:20:37 +00001776
drh1dcdbc02007-01-27 02:24:54 +00001777#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1778# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1779#endif
1780
drhb7f91642004-10-31 02:22:47 +00001781#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001782 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001783 ** integrity_check designed to detect most database corruption
1784 ** without most of the overhead of a full integrity-check.
1785 */
drh9ccd8652013-09-13 16:36:46 +00001786 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001787 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001788
drhed717fe2003-06-15 23:42:24 +00001789 /* Code that appears at the end of the integrity check. If no error
1790 ** messages have been generated, output OK. Otherwise output the
1791 ** error message
1792 */
drh688852a2014-02-17 22:40:43 +00001793 static const int iLn = __LINE__+2;
drh57196282004-10-06 15:41:16 +00001794 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001795 { OP_AddImm, 1, 0, 0}, /* 0 */
1796 { OP_IfNeg, 1, 0, 0}, /* 1 */
1797 { OP_String8, 0, 3, 0}, /* 2 */
1798 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001799 };
drhed717fe2003-06-15 23:42:24 +00001800
drhc5227312011-10-13 17:09:01 +00001801 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001802
dan5885e762012-07-16 10:06:12 +00001803 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1804 ** then iDb is set to the index of the database identified by <db>.
1805 ** In this case, the integrity of database iDb only is verified by
1806 ** the VDBE created below.
1807 **
1808 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1809 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1810 ** to -1 here, to indicate that the VDBE should verify the integrity
1811 ** of all attached databases. */
1812 assert( iDb>=0 );
1813 assert( iDb==0 || pId2->z );
1814 if( pId2->z==0 ) iDb = -1;
1815
drhed717fe2003-06-15 23:42:24 +00001816 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001817 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001818 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001819 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001820
1821 /* Set the maximum error count */
1822 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1823 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001824 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001825 if( mxErr<=0 ){
1826 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1827 }
1828 }
drh2d401ab2008-01-10 23:50:11 +00001829 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001830
1831 /* Do an integrity check on each database file */
1832 for(i=0; i<db->nDb; i++){
1833 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001834 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001835 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001836
danielk197753c0f742005-03-29 03:10:59 +00001837 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001838 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001839
drh80242052004-06-09 00:48:12 +00001840 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001841 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh688852a2014-02-17 22:40:43 +00001842 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001843 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001844 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001845
drhed717fe2003-06-15 23:42:24 +00001846 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001847 **
1848 ** Begin by filling registers 2, 3, ... with the root pages numbers
1849 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001850 */
dan5885e762012-07-16 10:06:12 +00001851 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001852 pTbls = &db->aDb[i].pSchema->tblHash;
1853 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001854 Table *pTab = sqliteHashData(x);
1855 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001856 if( HasRowid(pTab) ){
1857 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001858 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001859 cnt++;
1860 }
drh79069752004-05-22 21:30:40 +00001861 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001862 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001863 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001864 cnt++;
1865 }
1866 }
drh2d401ab2008-01-10 23:50:11 +00001867
1868 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001869 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001870
1871 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001872 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001873 sqlite3VdbeChangeP5(v, (u8)i);
drh688852a2014-02-17 22:40:43 +00001874 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
drh98757152008-01-09 23:04:12 +00001875 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001876 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001877 P4_DYNAMIC);
drhe8e4af72012-09-21 00:04:28 +00001878 sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
danielk1977a7a8e142008-02-13 18:25:27 +00001879 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001880 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001881 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001882
1883 /* Make sure all the indices are constructed correctly.
1884 */
danielk197741c58b72007-12-29 13:39:19 +00001885 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001886 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001887 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001888 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001889 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001890 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001891 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001892
1893 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001894 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001895 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh688852a2014-02-17 22:40:43 +00001896 VdbeCoverage(v);
drh66a51672008-01-03 00:01:23 +00001897 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001898 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001899 sqlite3ExprCacheClear(pParse);
drh26198bb2013-10-31 11:15:09 +00001900 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
drh6a534992013-11-16 20:13:39 +00001901 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001902 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001903 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001904 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001905 }
drh6fbe41a2013-10-30 20:22:55 +00001906 pParse->nMem = MAX(pParse->nMem, 8+j);
drh688852a2014-02-17 22:40:43 +00001907 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001908 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001909 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001910 int jmp2, jmp3, jmp4;
drh6fbe41a2013-10-30 20:22:55 +00001911 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001912 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1913 pPrior, r1);
1914 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001915 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drh26198bb2013-10-31 11:15:09 +00001916 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
drh688852a2014-02-17 22:40:43 +00001917 pIdx->nColumn); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001918 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1919 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
1920 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1921 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
1922 P4_STATIC);
1923 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1924 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
1925 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1926 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
drh688852a2014-02-17 22:40:43 +00001927 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
drh6fbe41a2013-10-30 20:22:55 +00001928 sqlite3VdbeAddOp0(v, OP_Halt);
1929 sqlite3VdbeJumpHere(v, jmp4);
drhd654be82005-09-20 17:42:23 +00001930 sqlite3VdbeJumpHere(v, jmp2);
drhb2b9d3d2013-08-01 01:14:43 +00001931 sqlite3VdbeResolveLabel(v, jmp3);
drhed717fe2003-06-15 23:42:24 +00001932 }
drh688852a2014-02-17 22:40:43 +00001933 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001934 sqlite3VdbeJumpHere(v, loopTop-1);
1935#ifndef SQLITE_OMIT_BTREECOUNT
1936 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
drh24003452008-01-03 01:28:59 +00001937 "wrong # of entries in index ", P4_STATIC);
drh8a9789b2013-08-01 03:36:59 +00001938 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001939 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001940 addr = sqlite3VdbeCurrentAddr(v);
drh688852a2014-02-17 22:40:43 +00001941 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001942 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001943 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh688852a2014-02-17 22:40:43 +00001944 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
drh8a9789b2013-08-01 03:36:59 +00001945 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1946 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
1947 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1948 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001949 }
drh8a9789b2013-08-01 03:36:59 +00001950#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001951 }
1952 }
drh688852a2014-02-17 22:40:43 +00001953 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
drh2d401ab2008-01-10 23:50:11 +00001954 sqlite3VdbeChangeP2(v, addr, -mxErr);
1955 sqlite3VdbeJumpHere(v, addr+1);
1956 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001957 }
1958 break;
drhb7f91642004-10-31 02:22:47 +00001959#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1960
drh13d70422004-11-13 15:59:14 +00001961#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001962 /*
1963 ** PRAGMA encoding
1964 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1965 **
drh85b623f2007-12-13 21:54:09 +00001966 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001967 ** database. If the database is not initialized, it is initialized now.
1968 **
1969 ** The second form of this pragma is a no-op if the main database file
1970 ** has not already been initialized. In this case it sets the default
1971 ** encoding that will be used for the main database file if a new file
1972 ** is created. If an existing main database file is opened, then the
1973 ** default text encoding for the existing database is used.
1974 **
1975 ** In all cases new databases created using the ATTACH command are
1976 ** created to use the same default text encoding as the main database. If
1977 ** the main database has not been initialized and/or created when ATTACH
1978 ** is executed, this is done before the ATTACH operation.
1979 **
1980 ** In the second form this pragma sets the text encoding to be used in
1981 ** new database files created using this database handle. It is only
1982 ** useful if invoked immediately after the main database i
1983 */
drh9ccd8652013-09-13 16:36:46 +00001984 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001985 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001986 char *zName;
1987 u8 enc;
1988 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001989 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001990 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1991 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1992 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001993 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001994 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001995 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1996 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001997 { 0, 0 }
1998 };
drh0f7eb612006-08-08 13:51:43 +00001999 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00002000 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00002001 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00002002 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00002003 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00002004 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00002005 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
2006 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
2007 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
2008 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00002009 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00002010 }else{ /* "PRAGMA encoding = XXX" */
2011 /* Only change the value of sqlite.enc if the database handle is not
2012 ** initialized. If the main database exists, the new sqlite.enc value
2013 ** will be overwritten when the schema is next loaded. If it does not
2014 ** already exists, it will be created to use the new encoding value.
2015 */
danielk1977b82e7ed2006-01-11 14:09:31 +00002016 if(
2017 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
2018 DbHasProperty(db, 0, DB_Empty)
2019 ){
danielk19778e227872004-06-07 07:52:17 +00002020 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
2021 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00002022 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00002023 break;
2024 }
2025 }
2026 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00002027 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00002028 }
2029 }
2030 }
drh9ccd8652013-09-13 16:36:46 +00002031 }
2032 break;
drh13d70422004-11-13 15:59:14 +00002033#endif /* SQLITE_OMIT_UTF16 */
2034
2035#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00002036 /*
danielk1977b92b70b2004-11-12 16:11:59 +00002037 ** PRAGMA [database.]schema_version
2038 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002039 **
danielk1977b92b70b2004-11-12 16:11:59 +00002040 ** PRAGMA [database.]user_version
2041 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002042 **
drh4ee09b42013-05-01 19:49:27 +00002043 ** PRAGMA [database.]freelist_count = <integer>
2044 **
2045 ** PRAGMA [database.]application_id
2046 ** PRAGMA [database.]application_id = <integer>
2047 **
danielk1977b92b70b2004-11-12 16:11:59 +00002048 ** The pragma's schema_version and user_version are used to set or get
2049 ** the value of the schema-version and user-version, respectively. Both
2050 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00002051 ** stored in the database header.
2052 **
2053 ** The schema-cookie is usually only manipulated internally by SQLite. It
2054 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00002055 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00002056 ** SQLite each time a query is executed to ensure that the internal cache
2057 ** of the schema used when compiling the SQL query matches the schema of
2058 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00002059 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2060 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00002061 ** crashes or database corruption. Use with caution!
2062 **
danielk1977b92b70b2004-11-12 16:11:59 +00002063 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00002064 ** applications for any purpose.
2065 */
drh9ccd8652013-09-13 16:36:46 +00002066 case PragTyp_HEADER_VALUE: {
danielk19770d19f7a2009-06-03 11:25:07 +00002067 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00002068 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00002069 switch( zLeft[0] ){
drh4ee09b42013-05-01 19:49:27 +00002070 case 'a': case 'A':
2071 iCookie = BTREE_APPLICATION_ID;
2072 break;
danielk1977180b56a2007-06-24 08:00:42 +00002073 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00002074 iCookie = BTREE_FREE_PAGE_COUNT;
2075 break;
2076 case 's': case 'S':
2077 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00002078 break;
2079 default:
danielk19770d19f7a2009-06-03 11:25:07 +00002080 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00002081 break;
danielk1977dae24952004-11-11 05:10:43 +00002082 }
2083
danielk19770d19f7a2009-06-03 11:25:07 +00002084 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00002085 /* Write the specified cookie value */
2086 static const VdbeOpList setCookie[] = {
2087 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00002088 { OP_Integer, 0, 1, 0}, /* 1 */
2089 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00002090 };
drh688852a2014-02-17 22:40:43 +00002091 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00002092 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00002093 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00002094 sqlite3VdbeChangeP1(v, addr+2, iDb);
2095 sqlite3VdbeChangeP2(v, addr+2, iCookie);
2096 }else{
2097 /* Read the specified cookie value */
2098 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00002099 { OP_Transaction, 0, 0, 0}, /* 0 */
2100 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00002101 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00002102 };
drh688852a2014-02-17 22:40:43 +00002103 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
danielk1977dae24952004-11-11 05:10:43 +00002104 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00002105 sqlite3VdbeChangeP1(v, addr+1, iDb);
2106 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00002107 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00002108 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00002109 }
drh9ccd8652013-09-13 16:36:46 +00002110 }
2111 break;
drh13d70422004-11-13 15:59:14 +00002112#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00002113
shanehdc97a8c2010-02-23 20:08:35 +00002114#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2115 /*
2116 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00002117 **
drh71caabf2010-02-26 15:39:24 +00002118 ** Return the names of all compile-time options used in this build,
2119 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00002120 */
drh9ccd8652013-09-13 16:36:46 +00002121 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00002122 int i = 0;
2123 const char *zOpt;
2124 sqlite3VdbeSetNumCols(v, 1);
2125 pParse->nMem = 1;
2126 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
2127 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
2128 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
2129 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2130 }
drh9ccd8652013-09-13 16:36:46 +00002131 }
2132 break;
shanehdc97a8c2010-02-23 20:08:35 +00002133#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2134
dan5cf53532010-05-01 16:40:20 +00002135#ifndef SQLITE_OMIT_WAL
2136 /*
dancdc1f042010-11-18 12:11:05 +00002137 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00002138 **
2139 ** Checkpoint the database.
2140 */
drh9ccd8652013-09-13 16:36:46 +00002141 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00002142 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00002143 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2144 if( zRight ){
2145 if( sqlite3StrICmp(zRight, "full")==0 ){
2146 eMode = SQLITE_CHECKPOINT_FULL;
2147 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2148 eMode = SQLITE_CHECKPOINT_RESTART;
2149 }
2150 }
dancdc1f042010-11-18 12:11:05 +00002151 sqlite3VdbeSetNumCols(v, 3);
2152 pParse->nMem = 3;
2153 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
2154 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
2155 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
2156
drh30aa3b92011-02-07 23:56:01 +00002157 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00002158 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00002159 }
2160 break;
dan5a299f92010-05-03 11:05:08 +00002161
2162 /*
2163 ** PRAGMA wal_autocheckpoint
2164 ** PRAGMA wal_autocheckpoint = N
2165 **
2166 ** Configure a database connection to automatically checkpoint a database
2167 ** after accumulating N frames in the log. Or query for the current value
2168 ** of N.
2169 */
drh9ccd8652013-09-13 16:36:46 +00002170 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00002171 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00002172 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00002173 }
drhb033d8b2010-05-03 13:37:30 +00002174 returnSingleInt(pParse, "wal_autocheckpoint",
2175 db->xWalCallback==sqlite3WalDefaultHook ?
2176 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00002177 }
2178 break;
dan5cf53532010-05-01 16:40:20 +00002179#endif
dan7c246102010-04-12 19:00:29 +00002180
drh09419b42011-11-16 19:29:17 +00002181 /*
2182 ** PRAGMA shrink_memory
2183 **
2184 ** This pragma attempts to free as much memory as possible from the
2185 ** current database connection.
2186 */
drh9ccd8652013-09-13 16:36:46 +00002187 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00002188 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00002189 break;
2190 }
drh09419b42011-11-16 19:29:17 +00002191
drhf3603962012-09-07 16:46:59 +00002192 /*
2193 ** PRAGMA busy_timeout
2194 ** PRAGMA busy_timeout = N
2195 **
2196 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002197 ** if one is set. If no busy handler or a different busy handler is set
2198 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2199 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002200 */
drhd49c3582013-09-13 19:00:06 +00002201 /*case PragTyp_BUSY_TIMEOUT*/ default: {
2202 assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002203 if( zRight ){
2204 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2205 }
2206 returnSingleInt(pParse, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002207 break;
2208 }
drhf3603962012-09-07 16:46:59 +00002209
drh55e85ca2013-09-13 21:01:56 +00002210 /*
2211 ** PRAGMA soft_heap_limit
2212 ** PRAGMA soft_heap_limit = N
2213 **
2214 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
2215 ** use -1.
2216 */
2217 case PragTyp_SOFT_HEAP_LIMIT: {
2218 sqlite3_int64 N;
2219 if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
2220 sqlite3_soft_heap_limit64(N);
2221 }
2222 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
2223 break;
2224 }
2225
dougcurrie81c95ef2004-06-18 23:21:47 +00002226#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002227 /*
2228 ** Report the current state of file logs for all databases
2229 */
drh9ccd8652013-09-13 16:36:46 +00002230 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002231 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002232 "unlocked", "shared", "reserved", "pending", "exclusive"
2233 };
2234 int i;
drh89ac8c12004-06-09 14:17:20 +00002235 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00002236 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00002237 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
2238 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002239 for(i=0; i<db->nDb; i++){
2240 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002241 const char *zState = "unknown";
2242 int j;
drh89ac8c12004-06-09 14:17:20 +00002243 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00002244 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002245 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002246 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002247 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00002248 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002249 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2250 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002251 }
drh2d401ab2008-01-10 23:50:11 +00002252 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
2253 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00002254 }
drh9ccd8652013-09-13 16:36:46 +00002255 break;
2256 }
drh89ac8c12004-06-09 14:17:20 +00002257#endif
2258
shanehad9f9f62010-02-17 17:48:46 +00002259#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00002260 case PragTyp_KEY: {
2261 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2262 break;
2263 }
2264 case PragTyp_REKEY: {
2265 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2266 break;
2267 }
2268 case PragTyp_HEXKEY: {
2269 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00002270 u8 iByte;
2271 int i;
drh9ccd8652013-09-13 16:36:46 +00002272 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00002273 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
2274 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
2275 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00002276 }
2277 if( (zLeft[3] & 0xf)==0xb ){
2278 sqlite3_key_v2(db, zDb, zKey, i/2);
2279 }else{
2280 sqlite3_rekey_v2(db, zDb, zKey, i/2);
2281 }
drhd2cb50b2009-01-09 21:41:17 +00002282 }
drh9ccd8652013-09-13 16:36:46 +00002283 break;
2284 }
drh3c4f2a42005-12-08 18:12:56 +00002285#endif
shanehad9f9f62010-02-17 17:48:46 +00002286#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002287 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00002288#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00002289 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002290 sqlite3_activate_see(&zRight[4]);
2291 }
2292#endif
2293#ifdef SQLITE_ENABLE_CEROD
2294 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002295 sqlite3_activate_cerod(&zRight[6]);
2296 }
2297#endif
drh9ccd8652013-09-13 16:36:46 +00002298 }
2299 break;
drh21e2cab2006-09-25 18:01:57 +00002300#endif
drh3c4f2a42005-12-08 18:12:56 +00002301
drh9ccd8652013-09-13 16:36:46 +00002302 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002303
danielk1977e0048402004-06-15 16:51:01 +00002304pragma_out:
drh633e6d52008-07-28 19:34:53 +00002305 sqlite3DbFree(db, zLeft);
2306 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002307}
drh13d70422004-11-13 15:59:14 +00002308
drh8bfdf722009-06-19 14:06:03 +00002309#endif /* SQLITE_OMIT_PRAGMA */