blob: 7383bce96f5750115f9b49bd0c68859e01757221 [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: {
drh57196282004-10-06 15:41:16 +0000827 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000828 { OP_Transaction, 0, 0, 0}, /* 0 */
829 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000830 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000831 { OP_Integer, 0, 2, 0},
832 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000833 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000834 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000835 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000836 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000837 };
drh905793e2004-02-21 13:31:09 +0000838 int addr;
drhfb982642007-08-30 01:19:59 +0000839 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000840 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000841 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000842 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000843 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000844 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000845 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000846 sqlite3VdbeChangeP1(v, addr+1, iDb);
847 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000848 }else{
drhd50ffc42011-03-08 02:38:28 +0000849 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000850 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000851 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000852 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000853 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000854 pDb->pSchema->cache_size = size;
855 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000856 }
drh9ccd8652013-09-13 16:36:46 +0000857 break;
858 }
drhe73c9142011-11-09 16:12:24 +0000859#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000860
drhe73c9142011-11-09 16:12:24 +0000861#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000862 /*
drh90f5ecb2004-07-22 01:19:35 +0000863 ** PRAGMA [database.]page_size
864 ** PRAGMA [database.]page_size=N
865 **
866 ** The first form reports the current setting for the
867 ** database page size in bytes. The second form sets the
868 ** database page size value. The value can only be set if
869 ** the database has not yet been created.
870 */
drh9ccd8652013-09-13 16:36:46 +0000871 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000872 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000873 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000874 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000875 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000876 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000877 }else{
danielk1977992772c2007-08-30 10:07:38 +0000878 /* Malloc may fail when setting the page-size, as there is an internal
879 ** buffer that the pager module resizes using sqlite3_realloc().
880 */
drh60ac3f42010-11-23 18:59:27 +0000881 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000882 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000883 db->mallocFailed = 1;
884 }
drh90f5ecb2004-07-22 01:19:35 +0000885 }
drh9ccd8652013-09-13 16:36:46 +0000886 break;
887 }
danielk197741483462007-03-24 16:45:04 +0000888
889 /*
drh5b47efa2010-02-12 18:18:39 +0000890 ** PRAGMA [database.]secure_delete
891 ** PRAGMA [database.]secure_delete=ON/OFF
892 **
893 ** The first form reports the current setting for the
894 ** secure_delete flag. The second form changes the secure_delete
895 ** flag setting and reports thenew value.
896 */
drh9ccd8652013-09-13 16:36:46 +0000897 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000898 Btree *pBt = pDb->pBt;
899 int b = -1;
900 assert( pBt!=0 );
901 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000902 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000903 }
drhaf034ed2010-02-12 19:46:26 +0000904 if( pId2->n==0 && b>=0 ){
905 int ii;
906 for(ii=0; ii<db->nDb; ii++){
907 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
908 }
909 }
drh5b47efa2010-02-12 18:18:39 +0000910 b = sqlite3BtreeSecureDelete(pBt, b);
911 returnSingleInt(pParse, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000912 break;
913 }
drh5b47efa2010-02-12 18:18:39 +0000914
915 /*
drh60ac3f42010-11-23 18:59:27 +0000916 ** PRAGMA [database.]max_page_count
917 ** PRAGMA [database.]max_page_count=N
918 **
919 ** The first form reports the current setting for the
920 ** maximum number of pages in the database file. The
921 ** second form attempts to change this setting. Both
922 ** forms return the current setting.
923 **
drhe73c9142011-11-09 16:12:24 +0000924 ** The absolute value of N is used. This is undocumented and might
925 ** change. The only purpose is to provide an easy way to test
926 ** the sqlite3AbsInt32() function.
927 **
danielk197759a93792008-05-15 17:48:20 +0000928 ** PRAGMA [database.]page_count
929 **
930 ** Return the number of pages in the specified database.
931 */
drh9ccd8652013-09-13 16:36:46 +0000932 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000933 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000934 sqlite3CodeVerifySchema(pParse, iDb);
935 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000936 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000937 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
938 }else{
drhe73c9142011-11-09 16:12:24 +0000939 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
940 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000941 }
danielk197759a93792008-05-15 17:48:20 +0000942 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
943 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000944 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000945 break;
946 }
danielk197759a93792008-05-15 17:48:20 +0000947
948 /*
danielk197741483462007-03-24 16:45:04 +0000949 ** PRAGMA [database.]locking_mode
950 ** PRAGMA [database.]locking_mode = (normal|exclusive)
951 */
drh9ccd8652013-09-13 16:36:46 +0000952 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000953 const char *zRet = "normal";
954 int eMode = getLockingMode(zRight);
955
956 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
957 /* Simple "PRAGMA locking_mode;" statement. This is a query for
958 ** the current default locking mode (which may be different to
959 ** the locking-mode of the main database).
960 */
961 eMode = db->dfltLockMode;
962 }else{
963 Pager *pPager;
964 if( pId2->n==0 ){
965 /* This indicates that no database name was specified as part
966 ** of the PRAGMA command. In this case the locking-mode must be
967 ** set on all attached databases, as well as the main db file.
968 **
969 ** Also, the sqlite3.dfltLockMode variable is set so that
970 ** any subsequently attached databases also use the specified
971 ** locking mode.
972 */
973 int ii;
974 assert(pDb==&db->aDb[0]);
975 for(ii=2; ii<db->nDb; ii++){
976 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
977 sqlite3PagerLockingMode(pPager, eMode);
978 }
drh4f21c4a2008-12-10 22:15:00 +0000979 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000980 }
981 pPager = sqlite3BtreePager(pDb->pBt);
982 eMode = sqlite3PagerLockingMode(pPager, eMode);
983 }
984
drh9ccd8652013-09-13 16:36:46 +0000985 assert( eMode==PAGER_LOCKINGMODE_NORMAL
986 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000987 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
988 zRet = "exclusive";
989 }
990 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000991 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000992 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
993 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000994 break;
995 }
drh3b020132008-04-17 17:02:01 +0000996
997 /*
998 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000999 ** PRAGMA [database.]journal_mode =
1000 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +00001001 */
drh9ccd8652013-09-13 16:36:46 +00001002 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +00001003 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
1004 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +00001005
1006 sqlite3VdbeSetNumCols(v, 1);
1007 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +00001008
1009 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +00001010 /* If there is no "=MODE" part of the pragma, do a query for the
1011 ** current mode */
drh3b020132008-04-17 17:02:01 +00001012 eMode = PAGER_JOURNALMODE_QUERY;
1013 }else{
dane04dc882010-04-20 18:53:15 +00001014 const char *zMode;
drhea678832008-12-10 19:26:22 +00001015 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +00001016 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +00001017 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
1018 }
1019 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +00001020 /* If the "=MODE" part does not match any known journal mode,
1021 ** then do a query */
dane04dc882010-04-20 18:53:15 +00001022 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +00001023 }
1024 }
drhc6b2a0f2010-07-08 17:40:37 +00001025 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
1026 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
1027 iDb = 0;
1028 pId2->n = 1;
1029 }
1030 for(ii=db->nDb-1; ii>=0; ii--){
1031 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
1032 sqlite3VdbeUsesBtree(v, ii);
1033 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +00001034 }
drh3b020132008-04-17 17:02:01 +00001035 }
drh3b020132008-04-17 17:02:01 +00001036 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +00001037 break;
1038 }
danielk1977b53e4962008-06-04 06:45:59 +00001039
1040 /*
1041 ** PRAGMA [database.]journal_size_limit
1042 ** PRAGMA [database.]journal_size_limit=N
1043 **
drha9e364f2009-01-13 20:14:15 +00001044 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +00001045 */
drh9ccd8652013-09-13 16:36:46 +00001046 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +00001047 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1048 i64 iLimit = -2;
1049 if( zRight ){
mistachkine98844f2013-08-24 00:59:24 +00001050 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +00001051 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +00001052 }
1053 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +00001054 returnSingleInt(pParse, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +00001055 break;
1056 }
danielk1977b53e4962008-06-04 06:45:59 +00001057
drh13d70422004-11-13 15:59:14 +00001058#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +00001059
1060 /*
danielk1977951af802004-11-05 15:45:09 +00001061 ** PRAGMA [database.]auto_vacuum
1062 ** PRAGMA [database.]auto_vacuum=N
1063 **
drha9e364f2009-01-13 20:14:15 +00001064 ** Get or set the value of the database 'auto-vacuum' parameter.
1065 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +00001066 */
1067#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001068 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +00001069 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +00001070 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +00001071 if( !zRight ){
drhf63936e2013-10-03 14:08:07 +00001072 returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +00001073 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +00001074 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +00001075 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +00001076 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +00001077 /* Call SetAutoVacuum() to set initialize the internal auto and
1078 ** incr-vacuum flags. This is required in case this connection
1079 ** creates the database file. It is important that it is created
1080 ** as an auto-vacuum capable db.
1081 */
1082 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
1083 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
1084 /* When setting the auto_vacuum mode to either "full" or
1085 ** "incremental", write the value of meta[6] in the database
1086 ** file. Before writing to meta[6], check that meta[3] indicates
1087 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +00001088 */
drhf63936e2013-10-03 14:08:07 +00001089 static const VdbeOpList setMeta6[] = {
1090 { OP_Transaction, 0, 1, 0}, /* 0 */
1091 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
1092 { OP_If, 1, 0, 0}, /* 2 */
1093 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
1094 { OP_Integer, 0, 1, 0}, /* 4 */
1095 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
1096 };
1097 int iAddr;
1098 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
1099 sqlite3VdbeChangeP1(v, iAddr, iDb);
1100 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
1101 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
1102 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
1103 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
1104 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +00001105 }
danielk1977951af802004-11-05 15:45:09 +00001106 }
drh9ccd8652013-09-13 16:36:46 +00001107 break;
1108 }
danielk1977951af802004-11-05 15:45:09 +00001109#endif
1110
drhca5557f2007-05-04 18:30:40 +00001111 /*
1112 ** PRAGMA [database.]incremental_vacuum(N)
1113 **
1114 ** Do N steps of incremental vacuuming on a database.
1115 */
1116#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001117 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +00001118 int iLimit, addr;
1119 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
1120 iLimit = 0x7fffffff;
1121 }
1122 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +00001123 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +00001124 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +00001125 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +00001126 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +00001127 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +00001128 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +00001129 break;
1130 }
drhca5557f2007-05-04 18:30:40 +00001131#endif
1132
drh13d70422004-11-13 15:59:14 +00001133#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +00001134 /*
drh90f5ecb2004-07-22 01:19:35 +00001135 ** PRAGMA [database.]cache_size
1136 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +00001137 **
1138 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +00001139 ** page cache size. The second form sets the local
1140 ** page cache size value. If N is positive then that is the
1141 ** number of pages in the cache. If N is negative, then the
1142 ** number of pages is adjusted so that the cache uses -N kibibytes
1143 ** of memory.
drhc11d4f92003-04-06 21:08:24 +00001144 */
drh9ccd8652013-09-13 16:36:46 +00001145 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +00001146 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +00001147 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +00001148 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001149 }else{
drh3b42abb2011-11-09 14:23:04 +00001150 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +00001151 pDb->pSchema->cache_size = size;
1152 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001153 }
drh9ccd8652013-09-13 16:36:46 +00001154 break;
1155 }
drhc11d4f92003-04-06 21:08:24 +00001156
1157 /*
drh9b4c59f2013-04-15 17:03:42 +00001158 ** PRAGMA [database.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +00001159 **
drh0d0614b2013-03-25 23:09:28 +00001160 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +00001161 ** used to limit the aggregate size of all memory mapped regions of the
1162 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +00001163 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +00001164 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +00001165 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +00001166 **
drh0d0614b2013-03-25 23:09:28 +00001167 ** This value is advisory. The underlying VFS is free to memory map
1168 ** as little or as much as it wants. Except, if N is set to 0 then the
1169 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +00001170 */
drh9ccd8652013-09-13 16:36:46 +00001171 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +00001172 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +00001173#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +00001174 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +00001175 if( zRight ){
drha1f42c72013-04-01 22:38:06 +00001176 int ii;
mistachkine98844f2013-08-24 00:59:24 +00001177 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh9b4c59f2013-04-15 17:03:42 +00001178 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
1179 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +00001180 for(ii=db->nDb-1; ii>=0; ii--){
1181 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +00001182 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +00001183 }
1184 }
dan5d8a1372013-03-19 19:28:06 +00001185 }
drh9b4c59f2013-04-15 17:03:42 +00001186 sz = -1;
dan3719f5f2013-05-23 10:13:18 +00001187 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +00001188#else
dan3719f5f2013-05-23 10:13:18 +00001189 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +00001190 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +00001191#endif
dan3719f5f2013-05-23 10:13:18 +00001192 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:42 +00001193 returnSingleInt(pParse, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +00001194 }else if( rc!=SQLITE_NOTFOUND ){
1195 pParse->nErr++;
1196 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +00001197 }
drh9ccd8652013-09-13 16:36:46 +00001198 break;
1199 }
dan5d8a1372013-03-19 19:28:06 +00001200
1201 /*
drh90f5ecb2004-07-22 01:19:35 +00001202 ** PRAGMA temp_store
1203 ** PRAGMA temp_store = "default"|"memory"|"file"
1204 **
1205 ** Return or set the local value of the temp_store flag. Changing
1206 ** the local value does not make changes to the disk file and the default
1207 ** value will be restored the next time the database is opened.
1208 **
1209 ** Note that it is possible for the library compile-time options to
1210 ** override this setting
1211 */
drh9ccd8652013-09-13 16:36:46 +00001212 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +00001213 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001214 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +00001215 }else{
1216 changeTempStorage(pParse, zRight);
1217 }
drh9ccd8652013-09-13 16:36:46 +00001218 break;
1219 }
drh90f5ecb2004-07-22 01:19:35 +00001220
1221 /*
tpoindex9a09a3c2004-12-20 19:01:32 +00001222 ** PRAGMA temp_store_directory
1223 ** PRAGMA temp_store_directory = ""|"directory_name"
1224 **
1225 ** Return or set the local value of the temp_store_directory flag. Changing
1226 ** the value sets a specific directory to be used for temporary files.
1227 ** Setting to a null string reverts to the default temporary directory search.
1228 ** If temporary directory is changed, then invalidateTempStorage.
1229 **
1230 */
drh9ccd8652013-09-13 16:36:46 +00001231 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +00001232 if( !zRight ){
1233 if( sqlite3_temp_directory ){
1234 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +00001235 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +00001236 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001237 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
1238 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +00001239 }
1240 }else{
drh78f82d12008-09-02 00:52:52 +00001241#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +00001242 if( zRight[0] ){
1243 int res;
danielk1977fab11272008-09-16 14:38:02 +00001244 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1245 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +00001246 sqlite3ErrorMsg(pParse, "not a writable directory");
1247 goto pragma_out;
1248 }
drh268283b2005-01-08 15:44:25 +00001249 }
danielk1977b06a0b62008-06-26 10:54:12 +00001250 if( SQLITE_TEMP_STORE==0
1251 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
1252 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +00001253 ){
1254 invalidateTempStorage(pParse);
1255 }
drh17435752007-08-16 04:30:38 +00001256 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +00001257 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +00001258 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +00001259 }else{
drh268283b2005-01-08 15:44:25 +00001260 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +00001261 }
drh78f82d12008-09-02 00:52:52 +00001262#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +00001263 }
drh9ccd8652013-09-13 16:36:46 +00001264 break;
1265 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001266
drhcc716452012-06-06 23:23:23 +00001267#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +00001268 /*
1269 ** PRAGMA data_store_directory
1270 ** PRAGMA data_store_directory = ""|"directory_name"
1271 **
1272 ** Return or set the local value of the data_store_directory flag. Changing
1273 ** the value sets a specific directory to be used for database files that
1274 ** were specified with a relative pathname. Setting to a null string reverts
1275 ** to the default database directory, which for database files specified with
1276 ** a relative path will probably be based on the current directory for the
1277 ** process. Database file specified with an absolute path are not impacted
1278 ** by this setting, regardless of its value.
1279 **
1280 */
drh9ccd8652013-09-13 16:36:46 +00001281 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +00001282 if( !zRight ){
1283 if( sqlite3_data_directory ){
1284 sqlite3VdbeSetNumCols(v, 1);
1285 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1286 "data_store_directory", SQLITE_STATIC);
1287 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
1288 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1289 }
1290 }else{
1291#ifndef SQLITE_OMIT_WSD
1292 if( zRight[0] ){
1293 int res;
1294 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1295 if( rc!=SQLITE_OK || res==0 ){
1296 sqlite3ErrorMsg(pParse, "not a writable directory");
1297 goto pragma_out;
1298 }
1299 }
1300 sqlite3_free(sqlite3_data_directory);
1301 if( zRight[0] ){
1302 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1303 }else{
1304 sqlite3_data_directory = 0;
1305 }
1306#endif /* SQLITE_OMIT_WSD */
1307 }
drh9ccd8652013-09-13 16:36:46 +00001308 break;
1309 }
drhcc716452012-06-06 23:23:23 +00001310#endif
mistachkina112d142012-03-14 00:44:01 +00001311
drhd2cb50b2009-01-09 21:41:17 +00001312#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +00001313 /*
drh9ccd8652013-09-13 16:36:46 +00001314 ** PRAGMA [database.]lock_proxy_file
1315 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
1316 **
1317 ** Return or set the value of the lock_proxy_file flag. Changing
1318 ** the value sets a specific file to be used for database access locks.
1319 **
1320 */
1321 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +00001322 if( !zRight ){
1323 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1324 char *proxy_file_path = NULL;
1325 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +00001326 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +00001327 &proxy_file_path);
1328
1329 if( proxy_file_path ){
1330 sqlite3VdbeSetNumCols(v, 1);
1331 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1332 "lock_proxy_file", SQLITE_STATIC);
1333 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
1334 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1335 }
1336 }else{
1337 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1338 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1339 int res;
1340 if( zRight[0] ){
1341 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1342 zRight);
1343 } else {
1344 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1345 NULL);
1346 }
1347 if( res!=SQLITE_OK ){
1348 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1349 goto pragma_out;
1350 }
1351 }
drh9ccd8652013-09-13 16:36:46 +00001352 break;
1353 }
drhd2cb50b2009-01-09 21:41:17 +00001354#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001355
1356 /*
drh90f5ecb2004-07-22 01:19:35 +00001357 ** PRAGMA [database.]synchronous
1358 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +00001359 **
1360 ** Return or set the local value of the synchronous flag. Changing
1361 ** the local value does not make changes to the disk file and the
1362 ** default value will be restored the next time the database is
1363 ** opened.
1364 */
drh9ccd8652013-09-13 16:36:46 +00001365 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001366 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001367 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001368 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001369 if( !db->autoCommit ){
1370 sqlite3ErrorMsg(pParse,
1371 "Safety level may not be changed inside a transaction");
1372 }else{
drh908c0052012-01-30 18:40:55 +00001373 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
drhd3605a42013-08-17 15:42:29 +00001374 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001375 }
drhc11d4f92003-04-06 21:08:24 +00001376 }
drh9ccd8652013-09-13 16:36:46 +00001377 break;
1378 }
drh13d70422004-11-13 15:59:14 +00001379#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001380
drhbf216272005-02-26 18:10:44 +00001381#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001382 case PragTyp_FLAG: {
1383 if( zRight==0 ){
1384 returnSingleInt(pParse, aPragmaNames[mid].zName,
1385 (db->flags & aPragmaNames[mid].iArg)!=0 );
1386 }else{
1387 int mask = aPragmaNames[mid].iArg; /* Mask of bits to set or clear. */
1388 if( db->autoCommit==0 ){
1389 /* Foreign key support may not be enabled or disabled while not
1390 ** in auto-commit mode. */
1391 mask &= ~(SQLITE_ForeignKeys);
1392 }
1393
1394 if( sqlite3GetBoolean(zRight, 0) ){
1395 db->flags |= mask;
1396 }else{
1397 db->flags &= ~mask;
1398 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1399 }
1400
1401 /* Many of the flag-pragmas modify the code generated by the SQL
1402 ** compiler (eg. count_changes). So add an opcode to expire all
1403 ** compiled SQL statements after modifying a pragma value.
1404 */
1405 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1406 setAllPagerFlags(db);
1407 }
1408 break;
1409 }
drhbf216272005-02-26 18:10:44 +00001410#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001411
drh13d70422004-11-13 15:59:14 +00001412#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001413 /*
1414 ** PRAGMA table_info(<table>)
1415 **
1416 ** Return a single row for each column of the named table. The columns of
1417 ** the returned data set are:
1418 **
1419 ** cid: Column id (numbered from left to right, starting at 0)
1420 ** name: Column name
1421 ** type: Column declaration type.
1422 ** notnull: True if 'NOT NULL' is part of column declaration
1423 ** dflt_value: The default value for the column, if any.
1424 */
drh9ccd8652013-09-13 16:36:46 +00001425 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001426 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001427 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001428 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001429 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001430 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001431 Column *pCol;
drh44156282013-10-23 22:23:03 +00001432 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh9f6696a2006-02-09 16:52:23 +00001433 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +00001434 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001435 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001436 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1437 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1438 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1439 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1440 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1441 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001442 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001443 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001444 if( IsHiddenColumn(pCol) ){
1445 nHidden++;
1446 continue;
1447 }
drh2d401ab2008-01-10 23:50:11 +00001448 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1449 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1450 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +00001451 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +00001452 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001453 if( pCol->zDflt ){
1454 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001455 }else{
drh2d401ab2008-01-10 23:50:11 +00001456 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001457 }
drh384b7fe2013-01-01 13:55:31 +00001458 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1459 k = 0;
1460 }else if( pPk==0 ){
1461 k = 1;
1462 }else{
1463 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1464 }
1465 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001466 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001467 }
1468 }
drh9ccd8652013-09-13 16:36:46 +00001469 }
1470 break;
drhc11d4f92003-04-06 21:08:24 +00001471
drh3ef26152013-10-12 20:22:00 +00001472 case PragTyp_STATS: {
1473 Index *pIdx;
1474 HashElem *i;
1475 v = sqlite3GetVdbe(pParse);
1476 sqlite3VdbeSetNumCols(v, 4);
1477 pParse->nMem = 4;
1478 sqlite3CodeVerifySchema(pParse, iDb);
1479 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1480 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
1481 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
1482 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
1483 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1484 Table *pTab = sqliteHashData(i);
1485 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
1486 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
1487 sqlite3VdbeAddOp2(v, OP_Integer,
1488 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
1489 sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
1490 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1491 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1492 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1493 sqlite3VdbeAddOp2(v, OP_Integer,
1494 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
1495 sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
1496 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1497 }
1498 }
1499 }
1500 break;
1501
drh9ccd8652013-09-13 16:36:46 +00001502 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001503 Index *pIdx;
1504 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001505 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001506 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001507 int i;
1508 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +00001509 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001510 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001511 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001512 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1513 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1514 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhbbbdc832013-10-22 18:01:40 +00001515 for(i=0; i<pIdx->nKeyCol; i++){
1516 i16 cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001517 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1518 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +00001519 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +00001520 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1521 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001522 }
1523 }
drh9ccd8652013-09-13 16:36:46 +00001524 }
1525 break;
drhc11d4f92003-04-06 21:08:24 +00001526
drh9ccd8652013-09-13 16:36:46 +00001527 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001528 Index *pIdx;
1529 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001530 int i;
drh2783e4b2004-10-05 15:42:53 +00001531 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001532 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001533 v = sqlite3GetVdbe(pParse);
drh3ef26152013-10-12 20:22:00 +00001534 sqlite3VdbeSetNumCols(v, 3);
1535 pParse->nMem = 3;
drhe13e9f52013-10-05 19:18:00 +00001536 sqlite3CodeVerifySchema(pParse, iDb);
1537 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1538 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1539 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
drh3ef26152013-10-12 20:22:00 +00001540 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhe13e9f52013-10-05 19:18:00 +00001541 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1542 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1543 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
drh3ef26152013-10-12 20:22:00 +00001544 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001545 }
1546 }
drh9ccd8652013-09-13 16:36:46 +00001547 }
1548 break;
drhc11d4f92003-04-06 21:08:24 +00001549
drh9ccd8652013-09-13 16:36:46 +00001550 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001551 int i;
drh13d70422004-11-13 15:59:14 +00001552 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001553 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001554 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1555 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1556 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001557 for(i=0; i<db->nDb; i++){
1558 if( db->aDb[i].pBt==0 ) continue;
1559 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001560 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1561 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1562 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001563 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001564 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001565 }
drh9ccd8652013-09-13 16:36:46 +00001566 }
1567 break;
danielk197748af65a2005-02-09 03:20:37 +00001568
drh9ccd8652013-09-13 16:36:46 +00001569 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001570 int i = 0;
1571 HashElem *p;
1572 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001573 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001574 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1575 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001576 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1577 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001578 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1579 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1580 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001581 }
drh9ccd8652013-09-13 16:36:46 +00001582 }
1583 break;
drh13d70422004-11-13 15:59:14 +00001584#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1585
drhb7f91642004-10-31 02:22:47 +00001586#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001587 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001588 FKey *pFK;
1589 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001590 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001591 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001592 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001593 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001594 if( pFK ){
1595 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001596 sqlite3VdbeSetNumCols(v, 8);
1597 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001598 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001599 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1600 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1601 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1602 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1603 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1604 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1605 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1606 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001607 while(pFK){
1608 int j;
1609 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001610 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001611 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1612 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001613 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1614 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1615 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1616 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001617 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001618 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001619 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1620 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1621 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1622 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001623 }
1624 ++i;
1625 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001626 }
drh78100cc2003-08-23 22:40:53 +00001627 }
1628 }
drh9ccd8652013-09-13 16:36:46 +00001629 }
1630 break;
drhb7f91642004-10-31 02:22:47 +00001631#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001632
drh6c5b9152012-12-17 16:46:37 +00001633#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001634#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001635 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001636 FKey *pFK; /* A foreign key constraint */
1637 Table *pTab; /* Child table contain "REFERENCES" keyword */
1638 Table *pParent; /* Parent table that child points to */
1639 Index *pIdx; /* Index in the parent table */
1640 int i; /* Loop counter: Foreign key number for pTab */
1641 int j; /* Loop counter: Field of the foreign key */
1642 HashElem *k; /* Loop counter: Next table in schema */
1643 int x; /* result variable */
1644 int regResult; /* 3 registers to hold a result row */
1645 int regKey; /* Register to hold key for checking the FK */
1646 int regRow; /* Registers to hold a row from pTab */
1647 int addrTop; /* Top of a loop checking foreign keys */
1648 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001649 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001650
drh613028b2012-12-17 18:43:02 +00001651 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001652 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001653 regKey = ++pParse->nMem;
1654 regRow = ++pParse->nMem;
1655 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001656 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001657 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1658 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001659 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1660 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001661 sqlite3CodeVerifySchema(pParse, iDb);
1662 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1663 while( k ){
1664 if( zRight ){
1665 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1666 k = 0;
1667 }else{
1668 pTab = (Table*)sqliteHashData(k);
1669 k = sqliteHashNext(k);
1670 }
drh9148def2012-12-17 20:40:39 +00001671 if( pTab==0 || pTab->pFKey==0 ) continue;
1672 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001673 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001674 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001675 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1676 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001677 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001678 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1679 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001680 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001681 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001682 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001683 if( x==0 ){
1684 if( pIdx==0 ){
1685 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1686 }else{
drh6c5b9152012-12-17 16:46:37 +00001687 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001688 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001689 }
1690 }else{
drh613028b2012-12-17 18:43:02 +00001691 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001692 break;
1693 }
drh6c5b9152012-12-17 16:46:37 +00001694 }
dan5e878302013-10-12 19:06:48 +00001695 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001696 if( pFK ) break;
1697 if( pParse->nTab<i ) pParse->nTab = i;
1698 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
1699 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001700 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001701 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001702 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001703 if( pParent ){
1704 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1705 assert( x==0 );
1706 }
drh613028b2012-12-17 18:43:02 +00001707 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001708 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001709 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001710 assert( iKey>=0 && iKey<pTab->nCol );
1711 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001712 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1713 sqlite3ColumnDefault(v, pTab, iKey, regRow);
1714 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
1715 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1716 sqlite3VdbeCurrentAddr(v)+3);
drh6c5b9152012-12-17 16:46:37 +00001717 }else{
drh613028b2012-12-17 18:43:02 +00001718 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001719 }
drh613028b2012-12-17 18:43:02 +00001720 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
1721 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1722 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1723 }else{
1724 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001725 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001726 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh613028b2012-12-17 18:43:02 +00001727 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
1728 }
dan5e878302013-10-12 19:06:48 +00001729 if( pParent ){
1730 sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
1731 sqlite3VdbeChangeP4(v, -1,
1732 sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
1733 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
1734 }
drh6c5b9152012-12-17 16:46:37 +00001735 }
drh613028b2012-12-17 18:43:02 +00001736 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001737 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1738 pFK->zTo, P4_TRANSIENT);
1739 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1740 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001741 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001742 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001743 }
drh613028b2012-12-17 18:43:02 +00001744 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
1745 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001746 }
drh9ccd8652013-09-13 16:36:46 +00001747 }
1748 break;
dan09ff9e12013-03-11 11:49:03 +00001749#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001750#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1751
drhc11d4f92003-04-06 21:08:24 +00001752#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001753 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001754 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001755 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001756 sqlite3ParserTrace(stderr, "parser: ");
1757 }else{
1758 sqlite3ParserTrace(0, 0);
1759 }
drhc11d4f92003-04-06 21:08:24 +00001760 }
drh9ccd8652013-09-13 16:36:46 +00001761 }
1762 break;
drhc11d4f92003-04-06 21:08:24 +00001763#endif
1764
drh55ef4d92005-08-14 01:20:37 +00001765 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1766 ** used will be case sensitive or not depending on the RHS.
1767 */
drh9ccd8652013-09-13 16:36:46 +00001768 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001769 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001770 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001771 }
drh9ccd8652013-09-13 16:36:46 +00001772 }
1773 break;
drh55ef4d92005-08-14 01:20:37 +00001774
drh1dcdbc02007-01-27 02:24:54 +00001775#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1776# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1777#endif
1778
drhb7f91642004-10-31 02:22:47 +00001779#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001780 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001781 ** integrity_check designed to detect most database corruption
1782 ** without most of the overhead of a full integrity-check.
1783 */
drh9ccd8652013-09-13 16:36:46 +00001784 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001785 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001786
drhed717fe2003-06-15 23:42:24 +00001787 /* Code that appears at the end of the integrity check. If no error
1788 ** messages have been generated, output OK. Otherwise output the
1789 ** error message
1790 */
drh57196282004-10-06 15:41:16 +00001791 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001792 { OP_AddImm, 1, 0, 0}, /* 0 */
1793 { OP_IfNeg, 1, 0, 0}, /* 1 */
1794 { OP_String8, 0, 3, 0}, /* 2 */
1795 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001796 };
drhed717fe2003-06-15 23:42:24 +00001797
drhc5227312011-10-13 17:09:01 +00001798 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001799
dan5885e762012-07-16 10:06:12 +00001800 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1801 ** then iDb is set to the index of the database identified by <db>.
1802 ** In this case, the integrity of database iDb only is verified by
1803 ** the VDBE created below.
1804 **
1805 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1806 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1807 ** to -1 here, to indicate that the VDBE should verify the integrity
1808 ** of all attached databases. */
1809 assert( iDb>=0 );
1810 assert( iDb==0 || pId2->z );
1811 if( pId2->z==0 ) iDb = -1;
1812
drhed717fe2003-06-15 23:42:24 +00001813 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001814 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001815 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001816 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001817
1818 /* Set the maximum error count */
1819 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1820 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001821 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001822 if( mxErr<=0 ){
1823 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1824 }
1825 }
drh2d401ab2008-01-10 23:50:11 +00001826 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001827
1828 /* Do an integrity check on each database file */
1829 for(i=0; i<db->nDb; i++){
1830 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001831 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001832 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001833
danielk197753c0f742005-03-29 03:10:59 +00001834 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001835 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001836
drh80242052004-06-09 00:48:12 +00001837 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001838 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001839 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001840 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001841
drhed717fe2003-06-15 23:42:24 +00001842 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001843 **
1844 ** Begin by filling registers 2, 3, ... with the root pages numbers
1845 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001846 */
dan5885e762012-07-16 10:06:12 +00001847 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001848 pTbls = &db->aDb[i].pSchema->tblHash;
1849 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001850 Table *pTab = sqliteHashData(x);
1851 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001852 if( HasRowid(pTab) ){
1853 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001854 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001855 cnt++;
1856 }
drh79069752004-05-22 21:30:40 +00001857 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001858 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001859 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001860 cnt++;
1861 }
1862 }
drh2d401ab2008-01-10 23:50:11 +00001863
1864 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001865 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001866
1867 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001868 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001869 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001870 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1871 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001872 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001873 P4_DYNAMIC);
drhe8e4af72012-09-21 00:04:28 +00001874 sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
danielk1977a7a8e142008-02-13 18:25:27 +00001875 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001876 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001877 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001878
1879 /* Make sure all the indices are constructed correctly.
1880 */
danielk197741c58b72007-12-29 13:39:19 +00001881 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001882 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001883 Index *pIdx, *pPk;
drh1c2c0b72014-01-04 19:27:05 +00001884 Index *pPrior = 0;
drhed717fe2003-06-15 23:42:24 +00001885 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001886 int iDataCur, iIdxCur;
drh1c2c0b72014-01-04 19:27:05 +00001887 int r1 = -1;
drhed717fe2003-06-15 23:42:24 +00001888
1889 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001890 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001891 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001892 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001893 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001894 sqlite3ExprCacheClear(pParse);
drh26198bb2013-10-31 11:15:09 +00001895 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
drh6a534992013-11-16 20:13:39 +00001896 1, 0, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001897 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001898 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001899 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001900 }
drh6fbe41a2013-10-30 20:22:55 +00001901 pParse->nMem = MAX(pParse->nMem, 8+j);
drh26198bb2013-10-31 11:15:09 +00001902 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
drh6fbe41a2013-10-30 20:22:55 +00001903 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001904 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001905 int jmp2, jmp3, jmp4;
drh6fbe41a2013-10-30 20:22:55 +00001906 if( pPk==pIdx ) continue;
drh1c2c0b72014-01-04 19:27:05 +00001907 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
1908 pPrior, r1);
1909 pPrior = pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001910 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drh26198bb2013-10-31 11:15:09 +00001911 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
drhda475b82013-11-04 21:44:54 +00001912 pIdx->nColumn);
drh6fbe41a2013-10-30 20:22:55 +00001913 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1914 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
1915 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1916 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
1917 P4_STATIC);
1918 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1919 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
1920 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1921 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1922 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
1923 sqlite3VdbeAddOp0(v, OP_Halt);
1924 sqlite3VdbeJumpHere(v, jmp4);
drhd654be82005-09-20 17:42:23 +00001925 sqlite3VdbeJumpHere(v, jmp2);
drhb2b9d3d2013-08-01 01:14:43 +00001926 sqlite3VdbeResolveLabel(v, jmp3);
drhed717fe2003-06-15 23:42:24 +00001927 }
drh6934fc72013-10-31 15:37:49 +00001928 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop);
drh8a9789b2013-08-01 03:36:59 +00001929 sqlite3VdbeJumpHere(v, loopTop-1);
1930#ifndef SQLITE_OMIT_BTREECOUNT
1931 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
drh24003452008-01-03 01:28:59 +00001932 "wrong # of entries in index ", P4_STATIC);
drh8a9789b2013-08-01 03:36:59 +00001933 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001934 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001935 addr = sqlite3VdbeCurrentAddr(v);
1936 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
1937 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001938 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh6fbe41a2013-10-30 20:22:55 +00001939 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3);
drh8a9789b2013-08-01 03:36:59 +00001940 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1941 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
1942 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1943 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001944 }
drh8a9789b2013-08-01 03:36:59 +00001945#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001946 }
1947 }
danielk19774adee202004-05-08 08:23:19 +00001948 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001949 sqlite3VdbeChangeP2(v, addr, -mxErr);
1950 sqlite3VdbeJumpHere(v, addr+1);
1951 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001952 }
1953 break;
drhb7f91642004-10-31 02:22:47 +00001954#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1955
drh13d70422004-11-13 15:59:14 +00001956#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001957 /*
1958 ** PRAGMA encoding
1959 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1960 **
drh85b623f2007-12-13 21:54:09 +00001961 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001962 ** database. If the database is not initialized, it is initialized now.
1963 **
1964 ** The second form of this pragma is a no-op if the main database file
1965 ** has not already been initialized. In this case it sets the default
1966 ** encoding that will be used for the main database file if a new file
1967 ** is created. If an existing main database file is opened, then the
1968 ** default text encoding for the existing database is used.
1969 **
1970 ** In all cases new databases created using the ATTACH command are
1971 ** created to use the same default text encoding as the main database. If
1972 ** the main database has not been initialized and/or created when ATTACH
1973 ** is executed, this is done before the ATTACH operation.
1974 **
1975 ** In the second form this pragma sets the text encoding to be used in
1976 ** new database files created using this database handle. It is only
1977 ** useful if invoked immediately after the main database i
1978 */
drh9ccd8652013-09-13 16:36:46 +00001979 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001980 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001981 char *zName;
1982 u8 enc;
1983 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001984 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001985 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1986 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1987 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001988 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001989 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001990 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1991 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001992 { 0, 0 }
1993 };
drh0f7eb612006-08-08 13:51:43 +00001994 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001995 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001996 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001997 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001998 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001999 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00002000 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
2001 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
2002 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
2003 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00002004 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00002005 }else{ /* "PRAGMA encoding = XXX" */
2006 /* Only change the value of sqlite.enc if the database handle is not
2007 ** initialized. If the main database exists, the new sqlite.enc value
2008 ** will be overwritten when the schema is next loaded. If it does not
2009 ** already exists, it will be created to use the new encoding value.
2010 */
danielk1977b82e7ed2006-01-11 14:09:31 +00002011 if(
2012 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
2013 DbHasProperty(db, 0, DB_Empty)
2014 ){
danielk19778e227872004-06-07 07:52:17 +00002015 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
2016 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00002017 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00002018 break;
2019 }
2020 }
2021 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00002022 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00002023 }
2024 }
2025 }
drh9ccd8652013-09-13 16:36:46 +00002026 }
2027 break;
drh13d70422004-11-13 15:59:14 +00002028#endif /* SQLITE_OMIT_UTF16 */
2029
2030#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00002031 /*
danielk1977b92b70b2004-11-12 16:11:59 +00002032 ** PRAGMA [database.]schema_version
2033 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002034 **
danielk1977b92b70b2004-11-12 16:11:59 +00002035 ** PRAGMA [database.]user_version
2036 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002037 **
drh4ee09b42013-05-01 19:49:27 +00002038 ** PRAGMA [database.]freelist_count = <integer>
2039 **
2040 ** PRAGMA [database.]application_id
2041 ** PRAGMA [database.]application_id = <integer>
2042 **
danielk1977b92b70b2004-11-12 16:11:59 +00002043 ** The pragma's schema_version and user_version are used to set or get
2044 ** the value of the schema-version and user-version, respectively. Both
2045 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00002046 ** stored in the database header.
2047 **
2048 ** The schema-cookie is usually only manipulated internally by SQLite. It
2049 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00002050 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00002051 ** SQLite each time a query is executed to ensure that the internal cache
2052 ** of the schema used when compiling the SQL query matches the schema of
2053 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00002054 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2055 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00002056 ** crashes or database corruption. Use with caution!
2057 **
danielk1977b92b70b2004-11-12 16:11:59 +00002058 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00002059 ** applications for any purpose.
2060 */
drh9ccd8652013-09-13 16:36:46 +00002061 case PragTyp_HEADER_VALUE: {
danielk19770d19f7a2009-06-03 11:25:07 +00002062 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00002063 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00002064 switch( zLeft[0] ){
drh4ee09b42013-05-01 19:49:27 +00002065 case 'a': case 'A':
2066 iCookie = BTREE_APPLICATION_ID;
2067 break;
danielk1977180b56a2007-06-24 08:00:42 +00002068 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00002069 iCookie = BTREE_FREE_PAGE_COUNT;
2070 break;
2071 case 's': case 'S':
2072 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00002073 break;
2074 default:
danielk19770d19f7a2009-06-03 11:25:07 +00002075 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00002076 break;
danielk1977dae24952004-11-11 05:10:43 +00002077 }
2078
danielk19770d19f7a2009-06-03 11:25:07 +00002079 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00002080 /* Write the specified cookie value */
2081 static const VdbeOpList setCookie[] = {
2082 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00002083 { OP_Integer, 0, 1, 0}, /* 1 */
2084 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00002085 };
2086 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
2087 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00002088 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00002089 sqlite3VdbeChangeP1(v, addr+2, iDb);
2090 sqlite3VdbeChangeP2(v, addr+2, iCookie);
2091 }else{
2092 /* Read the specified cookie value */
2093 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00002094 { OP_Transaction, 0, 0, 0}, /* 0 */
2095 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00002096 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00002097 };
2098 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
2099 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00002100 sqlite3VdbeChangeP1(v, addr+1, iDb);
2101 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00002102 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00002103 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00002104 }
drh9ccd8652013-09-13 16:36:46 +00002105 }
2106 break;
drh13d70422004-11-13 15:59:14 +00002107#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00002108
shanehdc97a8c2010-02-23 20:08:35 +00002109#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2110 /*
2111 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00002112 **
drh71caabf2010-02-26 15:39:24 +00002113 ** Return the names of all compile-time options used in this build,
2114 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00002115 */
drh9ccd8652013-09-13 16:36:46 +00002116 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00002117 int i = 0;
2118 const char *zOpt;
2119 sqlite3VdbeSetNumCols(v, 1);
2120 pParse->nMem = 1;
2121 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
2122 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
2123 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
2124 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2125 }
drh9ccd8652013-09-13 16:36:46 +00002126 }
2127 break;
shanehdc97a8c2010-02-23 20:08:35 +00002128#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2129
dan5cf53532010-05-01 16:40:20 +00002130#ifndef SQLITE_OMIT_WAL
2131 /*
dancdc1f042010-11-18 12:11:05 +00002132 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00002133 **
2134 ** Checkpoint the database.
2135 */
drh9ccd8652013-09-13 16:36:46 +00002136 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00002137 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00002138 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2139 if( zRight ){
2140 if( sqlite3StrICmp(zRight, "full")==0 ){
2141 eMode = SQLITE_CHECKPOINT_FULL;
2142 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2143 eMode = SQLITE_CHECKPOINT_RESTART;
2144 }
2145 }
dancdc1f042010-11-18 12:11:05 +00002146 sqlite3VdbeSetNumCols(v, 3);
2147 pParse->nMem = 3;
2148 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
2149 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
2150 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
2151
drh30aa3b92011-02-07 23:56:01 +00002152 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00002153 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00002154 }
2155 break;
dan5a299f92010-05-03 11:05:08 +00002156
2157 /*
2158 ** PRAGMA wal_autocheckpoint
2159 ** PRAGMA wal_autocheckpoint = N
2160 **
2161 ** Configure a database connection to automatically checkpoint a database
2162 ** after accumulating N frames in the log. Or query for the current value
2163 ** of N.
2164 */
drh9ccd8652013-09-13 16:36:46 +00002165 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00002166 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00002167 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00002168 }
drhb033d8b2010-05-03 13:37:30 +00002169 returnSingleInt(pParse, "wal_autocheckpoint",
2170 db->xWalCallback==sqlite3WalDefaultHook ?
2171 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00002172 }
2173 break;
dan5cf53532010-05-01 16:40:20 +00002174#endif
dan7c246102010-04-12 19:00:29 +00002175
drh09419b42011-11-16 19:29:17 +00002176 /*
2177 ** PRAGMA shrink_memory
2178 **
2179 ** This pragma attempts to free as much memory as possible from the
2180 ** current database connection.
2181 */
drh9ccd8652013-09-13 16:36:46 +00002182 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00002183 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00002184 break;
2185 }
drh09419b42011-11-16 19:29:17 +00002186
drhf3603962012-09-07 16:46:59 +00002187 /*
2188 ** PRAGMA busy_timeout
2189 ** PRAGMA busy_timeout = N
2190 **
2191 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002192 ** if one is set. If no busy handler or a different busy handler is set
2193 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2194 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002195 */
drhd49c3582013-09-13 19:00:06 +00002196 /*case PragTyp_BUSY_TIMEOUT*/ default: {
2197 assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002198 if( zRight ){
2199 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2200 }
2201 returnSingleInt(pParse, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002202 break;
2203 }
drhf3603962012-09-07 16:46:59 +00002204
drh55e85ca2013-09-13 21:01:56 +00002205 /*
2206 ** PRAGMA soft_heap_limit
2207 ** PRAGMA soft_heap_limit = N
2208 **
2209 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
2210 ** use -1.
2211 */
2212 case PragTyp_SOFT_HEAP_LIMIT: {
2213 sqlite3_int64 N;
2214 if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
2215 sqlite3_soft_heap_limit64(N);
2216 }
2217 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
2218 break;
2219 }
2220
dougcurrie81c95ef2004-06-18 23:21:47 +00002221#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002222 /*
2223 ** Report the current state of file logs for all databases
2224 */
drh9ccd8652013-09-13 16:36:46 +00002225 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002226 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002227 "unlocked", "shared", "reserved", "pending", "exclusive"
2228 };
2229 int i;
drh89ac8c12004-06-09 14:17:20 +00002230 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00002231 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00002232 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
2233 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002234 for(i=0; i<db->nDb; i++){
2235 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002236 const char *zState = "unknown";
2237 int j;
drh89ac8c12004-06-09 14:17:20 +00002238 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00002239 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002240 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002241 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002242 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00002243 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002244 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2245 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002246 }
drh2d401ab2008-01-10 23:50:11 +00002247 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
2248 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00002249 }
drh9ccd8652013-09-13 16:36:46 +00002250 break;
2251 }
drh89ac8c12004-06-09 14:17:20 +00002252#endif
2253
shanehad9f9f62010-02-17 17:48:46 +00002254#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00002255 case PragTyp_KEY: {
2256 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2257 break;
2258 }
2259 case PragTyp_REKEY: {
2260 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2261 break;
2262 }
2263 case PragTyp_HEXKEY: {
2264 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00002265 u8 iByte;
2266 int i;
drh9ccd8652013-09-13 16:36:46 +00002267 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00002268 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
2269 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
2270 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00002271 }
2272 if( (zLeft[3] & 0xf)==0xb ){
2273 sqlite3_key_v2(db, zDb, zKey, i/2);
2274 }else{
2275 sqlite3_rekey_v2(db, zDb, zKey, i/2);
2276 }
drhd2cb50b2009-01-09 21:41:17 +00002277 }
drh9ccd8652013-09-13 16:36:46 +00002278 break;
2279 }
drh3c4f2a42005-12-08 18:12:56 +00002280#endif
shanehad9f9f62010-02-17 17:48:46 +00002281#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002282 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00002283#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00002284 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002285 sqlite3_activate_see(&zRight[4]);
2286 }
2287#endif
2288#ifdef SQLITE_ENABLE_CEROD
2289 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002290 sqlite3_activate_cerod(&zRight[6]);
2291 }
2292#endif
drh9ccd8652013-09-13 16:36:46 +00002293 }
2294 break;
drh21e2cab2006-09-25 18:01:57 +00002295#endif
drh3c4f2a42005-12-08 18:12:56 +00002296
drh9ccd8652013-09-13 16:36:46 +00002297 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002298
danielk1977e0048402004-06-15 16:51:01 +00002299pragma_out:
drh633e6d52008-07-28 19:34:53 +00002300 sqlite3DbFree(db, zLeft);
2301 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002302}
drh13d70422004-11-13 15:59:14 +00002303
drh8bfdf722009-06-19 14:06:03 +00002304#endif /* SQLITE_OMIT_PRAGMA */