blob: 9211a2cb071838efe533cc2be52196e9d178cab5 [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 },
437 { /* zName: */ "vdbe_listing",
438 /* ePragTyp: */ PragTyp_FLAG,
439 /* ePragFlag: */ 0,
440 /* iArg: */ SQLITE_VdbeListing },
441 { /* zName: */ "vdbe_trace",
442 /* ePragTyp: */ PragTyp_FLAG,
443 /* ePragFlag: */ 0,
444 /* iArg: */ SQLITE_VdbeTrace },
drh9ccd8652013-09-13 16:36:46 +0000445#endif
mistachkindd197832013-10-21 23:17:23 +0000446#endif
drh9ccd8652013-09-13 16:36:46 +0000447#if !defined(SQLITE_OMIT_WAL)
drhf63936e2013-10-03 14:08:07 +0000448 { /* zName: */ "wal_autocheckpoint",
449 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
450 /* ePragFlag: */ 0,
451 /* iArg: */ 0 },
452 { /* zName: */ "wal_checkpoint",
453 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
454 /* ePragFlag: */ PragFlag_NeedSchema,
455 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000456#endif
mistachkindd197832013-10-21 23:17:23 +0000457#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000458 { /* zName: */ "writable_schema",
459 /* ePragTyp: */ PragTyp_FLAG,
460 /* ePragFlag: */ 0,
461 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
mistachkindd197832013-10-21 23:17:23 +0000462#endif
drh9ccd8652013-09-13 16:36:46 +0000463};
drh3ef26152013-10-12 20:22:00 +0000464/* Number of pragmas: 56 on by default, 68 total. */
drh9ccd8652013-09-13 16:36:46 +0000465/* End of the automatically generated pragma table.
466***************************************************************************/
467
drhc11d4f92003-04-06 21:08:24 +0000468/*
drhc11d4f92003-04-06 21:08:24 +0000469** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +0000470** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
drh908c0052012-01-30 18:40:55 +0000471** unrecognized string argument. The FULL option is disallowed
472** if the omitFull parameter it 1.
drhc11d4f92003-04-06 21:08:24 +0000473**
474** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +0000475** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +0000476** to support legacy SQL code. The safety level used to be boolean
477** and older scripts may have used numbers 0 for OFF and 1 for ON.
478*/
drh908c0052012-01-30 18:40:55 +0000479static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
drh722e95a2004-10-25 20:33:44 +0000480 /* 123456789 123456789 */
481 static const char zText[] = "onoffalseyestruefull";
482 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
483 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
484 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
485 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +0000486 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +0000487 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +0000488 }
drhea678832008-12-10 19:26:22 +0000489 n = sqlite3Strlen30(z);
drh908c0052012-01-30 18:40:55 +0000490 for(i=0; i<ArraySize(iLength)-omitFull; i++){
drh722e95a2004-10-25 20:33:44 +0000491 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
492 return iValue[i];
493 }
drhc11d4f92003-04-06 21:08:24 +0000494 }
drh908c0052012-01-30 18:40:55 +0000495 return dflt;
drhc11d4f92003-04-06 21:08:24 +0000496}
497
498/*
drh722e95a2004-10-25 20:33:44 +0000499** Interpret the given string as a boolean value.
500*/
drh38d9c612012-01-31 14:24:47 +0000501u8 sqlite3GetBoolean(const char *z, int dflt){
502 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:44 +0000503}
504
drhc7dc9bf2011-06-03 13:02:57 +0000505/* The sqlite3GetBoolean() function is used by other modules but the
506** remainder of this file is specific to PRAGMA processing. So omit
507** the rest of the file if PRAGMAs are omitted from the build.
508*/
509#if !defined(SQLITE_OMIT_PRAGMA)
510
danielk197741483462007-03-24 16:45:04 +0000511/*
512** Interpret the given string as a locking mode value.
513*/
514static int getLockingMode(const char *z){
515 if( z ){
516 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
517 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
518 }
519 return PAGER_LOCKINGMODE_QUERY;
520}
521
danielk1977dddbcdc2007-04-26 14:42:34 +0000522#ifndef SQLITE_OMIT_AUTOVACUUM
523/*
524** Interpret the given string as an auto-vacuum mode value.
525**
526** The following strings, "none", "full" and "incremental" are
527** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
528*/
529static int getAutoVacuum(const char *z){
530 int i;
531 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
532 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
533 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
drh60ac3f42010-11-23 18:59:27 +0000534 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +0000535 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +0000536}
537#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
538
danielk1977b84f96f2005-01-20 11:32:23 +0000539#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +0000540/*
drh90f5ecb2004-07-22 01:19:35 +0000541** Interpret the given string as a temp db location. Return 1 for file
542** backed temporary databases, 2 for the Red-Black tree in memory database
543** and 0 to use the compile-time default.
544*/
545static int getTempStore(const char *z){
546 if( z[0]>='0' && z[0]<='2' ){
547 return z[0] - '0';
548 }else if( sqlite3StrICmp(z, "file")==0 ){
549 return 1;
550 }else if( sqlite3StrICmp(z, "memory")==0 ){
551 return 2;
552 }else{
553 return 0;
554 }
555}
drhbf216272005-02-26 18:10:44 +0000556#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000557
drhbf216272005-02-26 18:10:44 +0000558#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000559/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000560** Invalidate temp storage, either when the temp storage is changed
561** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000562*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000563static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000564 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000565 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000566 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000567 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
568 "from within a transaction");
569 return SQLITE_ERROR;
570 }
571 sqlite3BtreeClose(db->aDb[1].pBt);
572 db->aDb[1].pBt = 0;
drh81028a42012-05-15 18:28:27 +0000573 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35 +0000574 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000575 return SQLITE_OK;
576}
drhbf216272005-02-26 18:10:44 +0000577#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000578
drhbf216272005-02-26 18:10:44 +0000579#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000580/*
581** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000582** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000583** or DEFAULT_TEMP_STORE pragmas.
584*/
585static int changeTempStorage(Parse *pParse, const char *zStorageType){
586 int ts = getTempStore(zStorageType);
587 sqlite3 *db = pParse->db;
588 if( db->temp_store==ts ) return SQLITE_OK;
589 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
590 return SQLITE_ERROR;
591 }
drh4f21c4a2008-12-10 22:15:00 +0000592 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000593 return SQLITE_OK;
594}
drhbf216272005-02-26 18:10:44 +0000595#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000596
597/*
598** Generate code to return a single integer value.
599*/
drh3c713642009-04-04 16:02:32 +0000600static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000601 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000602 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000603 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
604 if( pI64 ){
605 memcpy(pI64, &value, sizeof(value));
606 }
607 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000608 sqlite3VdbeSetNumCols(v, 1);
609 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000610 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000611}
612
drhd3605a42013-08-17 15:42:29 +0000613
614/*
615** Set the safety_level and pager flags for pager iDb. Or if iDb<0
616** set these values for all pagers.
617*/
618#ifndef SQLITE_OMIT_PAGER_PRAGMAS
619static void setAllPagerFlags(sqlite3 *db){
620 if( db->autoCommit ){
621 Db *pDb = db->aDb;
622 int n = db->nDb;
623 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
624 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
625 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
626 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
627 == PAGER_FLAGS_MASK );
628 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
629 while( (n--) > 0 ){
630 if( pDb->pBt ){
631 sqlite3BtreeSetPagerFlags(pDb->pBt,
632 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
633 }
634 pDb++;
635 }
636 }
637}
drhfeb56e02013-08-23 17:33:46 +0000638#else
639# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000640#endif
641
642
drhd2cb50b2009-01-09 21:41:17 +0000643/*
644** Return a human-readable name for a constraint resolution action.
645*/
danba9108b2009-09-22 07:13:42 +0000646#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000647static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000648 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000649 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000650 case OE_SetNull: zName = "SET NULL"; break;
651 case OE_SetDflt: zName = "SET DEFAULT"; break;
652 case OE_Cascade: zName = "CASCADE"; break;
653 case OE_Restrict: zName = "RESTRICT"; break;
654 default: zName = "NO ACTION";
655 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000656 }
drhd2cb50b2009-01-09 21:41:17 +0000657 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000658}
danba9108b2009-09-22 07:13:42 +0000659#endif
danielk197750af3e12008-10-10 17:47:21 +0000660
dane04dc882010-04-20 18:53:15 +0000661
662/*
663** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
664** defined in pager.h. This function returns the associated lowercase
665** journal-mode name.
666*/
667const char *sqlite3JournalModename(int eMode){
668 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000669 "delete", "persist", "off", "truncate", "memory"
670#ifndef SQLITE_OMIT_WAL
671 , "wal"
672#endif
dane04dc882010-04-20 18:53:15 +0000673 };
674 assert( PAGER_JOURNALMODE_DELETE==0 );
675 assert( PAGER_JOURNALMODE_PERSIST==1 );
676 assert( PAGER_JOURNALMODE_OFF==2 );
677 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
678 assert( PAGER_JOURNALMODE_MEMORY==4 );
679 assert( PAGER_JOURNALMODE_WAL==5 );
680 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
681
682 if( eMode==ArraySize(azModeName) ) return 0;
683 return azModeName[eMode];
684}
685
drh87223182004-02-21 14:00:29 +0000686/*
drhc11d4f92003-04-06 21:08:24 +0000687** Process a pragma statement.
688**
689** Pragmas are of this form:
690**
danielk197791cf71b2004-06-26 06:37:06 +0000691** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000692**
693** The identifier might also be a string. The value is a string, and
694** identifier, or a number. If minusFlag is true, then the value is
695** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000696**
697** If the left side is "database.id" then pId1 is the database name
698** and pId2 is the id. If the left side is just "id" then pId1 is the
699** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000700*/
danielk197791cf71b2004-06-26 06:37:06 +0000701void sqlite3Pragma(
702 Parse *pParse,
703 Token *pId1, /* First part of [database.]id field */
704 Token *pId2, /* Second part of [database.]id field, or NULL */
705 Token *pValue, /* Token for <value>, or NULL */
706 int minusFlag /* True if a '-' sign preceded <value> */
707){
708 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
709 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
710 const char *zDb = 0; /* The database name */
711 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000712 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000713 int iDb; /* Database index for <database> */
714 int lwr, upr, mid; /* Binary search bounds */
drh06fd5d62012-02-22 14:45:19 +0000715 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
716 sqlite3 *db = pParse->db; /* The database connection */
717 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000718 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh06fd5d62012-02-22 14:45:19 +0000719
drhc11d4f92003-04-06 21:08:24 +0000720 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000721 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000722 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000723
danielk197791cf71b2004-06-26 06:37:06 +0000724 /* Interpret the [database.] part of the pragma statement. iDb is the
725 ** index of the database this pragma is being applied to in db.aDb[]. */
726 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
727 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000728 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000729
danielk1977ddfb2f02006-02-17 12:25:14 +0000730 /* If the temp database has been explicitly named as part of the
731 ** pragma, make sure it is open.
732 */
733 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
734 return;
735 }
736
drh17435752007-08-16 04:30:38 +0000737 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000738 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000739 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000740 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000741 }else{
drh17435752007-08-16 04:30:38 +0000742 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000743 }
danielk197791cf71b2004-06-26 06:37:06 +0000744
drhd2cb50b2009-01-09 21:41:17 +0000745 assert( pId2 );
746 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000747 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000748 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000749 }
drh06fd5d62012-02-22 14:45:19 +0000750
751 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
752 ** connection. If it returns SQLITE_OK, then assume that the VFS
753 ** handled the pragma and generate a no-op prepared statement.
754 */
drh3fa97302012-02-22 16:58:36 +0000755 aFcntl[0] = 0;
756 aFcntl[1] = zLeft;
757 aFcntl[2] = zRight;
758 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000759 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000760 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
761 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000762 if( aFcntl[0] ){
763 int mem = ++pParse->nMem;
764 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
765 sqlite3VdbeSetNumCols(v, 1);
766 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
767 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
768 sqlite3_free(aFcntl[0]);
769 }
drh9ccd8652013-09-13 16:36:46 +0000770 goto pragma_out;
771 }
772 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000773 if( aFcntl[0] ){
774 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
775 sqlite3_free(aFcntl[0]);
776 }
777 pParse->nErr++;
778 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000779 goto pragma_out;
780 }
781
782 /* Locate the pragma in the lookup table */
783 lwr = 0;
784 upr = ArraySize(aPragmaNames)-1;
785 while( lwr<=upr ){
786 mid = (lwr+upr)/2;
787 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
788 if( rc==0 ) break;
789 if( rc<0 ){
790 upr = mid - 1;
791 }else{
792 lwr = mid + 1;
793 }
794 }
795 if( lwr>upr ) goto pragma_out;
796
drhf63936e2013-10-03 14:08:07 +0000797 /* Make sure the database schema is loaded if the pragma requires that */
798 if( (aPragmaNames[mid].mPragFlag & PragFlag_NeedSchema)!=0 ){
799 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
800 }
801
drh9ccd8652013-09-13 16:36:46 +0000802 /* Jump to the appropriate pragma handler */
803 switch( aPragmaNames[mid].ePragTyp ){
804
drhe73c9142011-11-09 16:12:24 +0000805#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000806 /*
drh90f5ecb2004-07-22 01:19:35 +0000807 ** PRAGMA [database.]default_cache_size
808 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000809 **
810 ** The first form reports the current persistent setting for the
811 ** page cache size. The value returned is the maximum number of
812 ** pages in the page cache. The second form sets both the current
813 ** page cache size value and the persistent page cache size value
814 ** stored in the database file.
815 **
drh93791ea2010-04-26 17:36:35 +0000816 ** Older versions of SQLite would set the default cache size to a
817 ** negative number to indicate synchronous=OFF. These days, synchronous
818 ** is always on by default regardless of the sign of the default cache
819 ** size. But continue to take the absolute value of the default cache
820 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000821 */
drh9ccd8652013-09-13 16:36:46 +0000822 case PragTyp_DEFAULT_CACHE_SIZE: {
drh57196282004-10-06 15:41:16 +0000823 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000824 { OP_Transaction, 0, 0, 0}, /* 0 */
825 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000826 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000827 { OP_Integer, 0, 2, 0},
828 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000829 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000830 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000831 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000832 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000833 };
drh905793e2004-02-21 13:31:09 +0000834 int addr;
drhfb982642007-08-30 01:19:59 +0000835 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000836 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000837 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000838 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000839 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000840 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000841 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000842 sqlite3VdbeChangeP1(v, addr+1, iDb);
843 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000844 }else{
drhd50ffc42011-03-08 02:38:28 +0000845 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000846 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000847 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000848 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000849 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000850 pDb->pSchema->cache_size = size;
851 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000852 }
drh9ccd8652013-09-13 16:36:46 +0000853 break;
854 }
drhe73c9142011-11-09 16:12:24 +0000855#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000856
drhe73c9142011-11-09 16:12:24 +0000857#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000858 /*
drh90f5ecb2004-07-22 01:19:35 +0000859 ** PRAGMA [database.]page_size
860 ** PRAGMA [database.]page_size=N
861 **
862 ** The first form reports the current setting for the
863 ** database page size in bytes. The second form sets the
864 ** database page size value. The value can only be set if
865 ** the database has not yet been created.
866 */
drh9ccd8652013-09-13 16:36:46 +0000867 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000868 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000869 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000870 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000871 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000872 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000873 }else{
danielk1977992772c2007-08-30 10:07:38 +0000874 /* Malloc may fail when setting the page-size, as there is an internal
875 ** buffer that the pager module resizes using sqlite3_realloc().
876 */
drh60ac3f42010-11-23 18:59:27 +0000877 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000878 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000879 db->mallocFailed = 1;
880 }
drh90f5ecb2004-07-22 01:19:35 +0000881 }
drh9ccd8652013-09-13 16:36:46 +0000882 break;
883 }
danielk197741483462007-03-24 16:45:04 +0000884
885 /*
drh5b47efa2010-02-12 18:18:39 +0000886 ** PRAGMA [database.]secure_delete
887 ** PRAGMA [database.]secure_delete=ON/OFF
888 **
889 ** The first form reports the current setting for the
890 ** secure_delete flag. The second form changes the secure_delete
891 ** flag setting and reports thenew value.
892 */
drh9ccd8652013-09-13 16:36:46 +0000893 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000894 Btree *pBt = pDb->pBt;
895 int b = -1;
896 assert( pBt!=0 );
897 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000898 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000899 }
drhaf034ed2010-02-12 19:46:26 +0000900 if( pId2->n==0 && b>=0 ){
901 int ii;
902 for(ii=0; ii<db->nDb; ii++){
903 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
904 }
905 }
drh5b47efa2010-02-12 18:18:39 +0000906 b = sqlite3BtreeSecureDelete(pBt, b);
907 returnSingleInt(pParse, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000908 break;
909 }
drh5b47efa2010-02-12 18:18:39 +0000910
911 /*
drh60ac3f42010-11-23 18:59:27 +0000912 ** PRAGMA [database.]max_page_count
913 ** PRAGMA [database.]max_page_count=N
914 **
915 ** The first form reports the current setting for the
916 ** maximum number of pages in the database file. The
917 ** second form attempts to change this setting. Both
918 ** forms return the current setting.
919 **
drhe73c9142011-11-09 16:12:24 +0000920 ** The absolute value of N is used. This is undocumented and might
921 ** change. The only purpose is to provide an easy way to test
922 ** the sqlite3AbsInt32() function.
923 **
danielk197759a93792008-05-15 17:48:20 +0000924 ** PRAGMA [database.]page_count
925 **
926 ** Return the number of pages in the specified database.
927 */
drh9ccd8652013-09-13 16:36:46 +0000928 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000929 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000930 sqlite3CodeVerifySchema(pParse, iDb);
931 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000932 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000933 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
934 }else{
drhe73c9142011-11-09 16:12:24 +0000935 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
936 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000937 }
danielk197759a93792008-05-15 17:48:20 +0000938 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
939 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000940 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000941 break;
942 }
danielk197759a93792008-05-15 17:48:20 +0000943
944 /*
danielk197741483462007-03-24 16:45:04 +0000945 ** PRAGMA [database.]locking_mode
946 ** PRAGMA [database.]locking_mode = (normal|exclusive)
947 */
drh9ccd8652013-09-13 16:36:46 +0000948 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000949 const char *zRet = "normal";
950 int eMode = getLockingMode(zRight);
951
952 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
953 /* Simple "PRAGMA locking_mode;" statement. This is a query for
954 ** the current default locking mode (which may be different to
955 ** the locking-mode of the main database).
956 */
957 eMode = db->dfltLockMode;
958 }else{
959 Pager *pPager;
960 if( pId2->n==0 ){
961 /* This indicates that no database name was specified as part
962 ** of the PRAGMA command. In this case the locking-mode must be
963 ** set on all attached databases, as well as the main db file.
964 **
965 ** Also, the sqlite3.dfltLockMode variable is set so that
966 ** any subsequently attached databases also use the specified
967 ** locking mode.
968 */
969 int ii;
970 assert(pDb==&db->aDb[0]);
971 for(ii=2; ii<db->nDb; ii++){
972 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
973 sqlite3PagerLockingMode(pPager, eMode);
974 }
drh4f21c4a2008-12-10 22:15:00 +0000975 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000976 }
977 pPager = sqlite3BtreePager(pDb->pBt);
978 eMode = sqlite3PagerLockingMode(pPager, eMode);
979 }
980
drh9ccd8652013-09-13 16:36:46 +0000981 assert( eMode==PAGER_LOCKINGMODE_NORMAL
982 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000983 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
984 zRet = "exclusive";
985 }
986 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000987 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000988 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
989 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000990 break;
991 }
drh3b020132008-04-17 17:02:01 +0000992
993 /*
994 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000995 ** PRAGMA [database.]journal_mode =
996 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000997 */
drh9ccd8652013-09-13 16:36:46 +0000998 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000999 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
1000 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +00001001
1002 sqlite3VdbeSetNumCols(v, 1);
1003 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +00001004
1005 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +00001006 /* If there is no "=MODE" part of the pragma, do a query for the
1007 ** current mode */
drh3b020132008-04-17 17:02:01 +00001008 eMode = PAGER_JOURNALMODE_QUERY;
1009 }else{
dane04dc882010-04-20 18:53:15 +00001010 const char *zMode;
drhea678832008-12-10 19:26:22 +00001011 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +00001012 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +00001013 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
1014 }
1015 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +00001016 /* If the "=MODE" part does not match any known journal mode,
1017 ** then do a query */
dane04dc882010-04-20 18:53:15 +00001018 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +00001019 }
1020 }
drhc6b2a0f2010-07-08 17:40:37 +00001021 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
1022 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
1023 iDb = 0;
1024 pId2->n = 1;
1025 }
1026 for(ii=db->nDb-1; ii>=0; ii--){
1027 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
1028 sqlite3VdbeUsesBtree(v, ii);
1029 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +00001030 }
drh3b020132008-04-17 17:02:01 +00001031 }
drh3b020132008-04-17 17:02:01 +00001032 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +00001033 break;
1034 }
danielk1977b53e4962008-06-04 06:45:59 +00001035
1036 /*
1037 ** PRAGMA [database.]journal_size_limit
1038 ** PRAGMA [database.]journal_size_limit=N
1039 **
drha9e364f2009-01-13 20:14:15 +00001040 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +00001041 */
drh9ccd8652013-09-13 16:36:46 +00001042 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +00001043 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1044 i64 iLimit = -2;
1045 if( zRight ){
mistachkine98844f2013-08-24 00:59:24 +00001046 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +00001047 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +00001048 }
1049 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +00001050 returnSingleInt(pParse, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +00001051 break;
1052 }
danielk1977b53e4962008-06-04 06:45:59 +00001053
drh13d70422004-11-13 15:59:14 +00001054#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +00001055
1056 /*
danielk1977951af802004-11-05 15:45:09 +00001057 ** PRAGMA [database.]auto_vacuum
1058 ** PRAGMA [database.]auto_vacuum=N
1059 **
drha9e364f2009-01-13 20:14:15 +00001060 ** Get or set the value of the database 'auto-vacuum' parameter.
1061 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +00001062 */
1063#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001064 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +00001065 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +00001066 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +00001067 if( !zRight ){
drhf63936e2013-10-03 14:08:07 +00001068 returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +00001069 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +00001070 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +00001071 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +00001072 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +00001073 /* Call SetAutoVacuum() to set initialize the internal auto and
1074 ** incr-vacuum flags. This is required in case this connection
1075 ** creates the database file. It is important that it is created
1076 ** as an auto-vacuum capable db.
1077 */
1078 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
1079 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
1080 /* When setting the auto_vacuum mode to either "full" or
1081 ** "incremental", write the value of meta[6] in the database
1082 ** file. Before writing to meta[6], check that meta[3] indicates
1083 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +00001084 */
drhf63936e2013-10-03 14:08:07 +00001085 static const VdbeOpList setMeta6[] = {
1086 { OP_Transaction, 0, 1, 0}, /* 0 */
1087 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
1088 { OP_If, 1, 0, 0}, /* 2 */
1089 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
1090 { OP_Integer, 0, 1, 0}, /* 4 */
1091 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
1092 };
1093 int iAddr;
1094 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
1095 sqlite3VdbeChangeP1(v, iAddr, iDb);
1096 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
1097 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
1098 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
1099 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
1100 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +00001101 }
danielk1977951af802004-11-05 15:45:09 +00001102 }
drh9ccd8652013-09-13 16:36:46 +00001103 break;
1104 }
danielk1977951af802004-11-05 15:45:09 +00001105#endif
1106
drhca5557f2007-05-04 18:30:40 +00001107 /*
1108 ** PRAGMA [database.]incremental_vacuum(N)
1109 **
1110 ** Do N steps of incremental vacuuming on a database.
1111 */
1112#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001113 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +00001114 int iLimit, addr;
1115 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
1116 iLimit = 0x7fffffff;
1117 }
1118 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +00001119 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +00001120 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +00001121 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +00001122 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +00001123 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +00001124 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +00001125 break;
1126 }
drhca5557f2007-05-04 18:30:40 +00001127#endif
1128
drh13d70422004-11-13 15:59:14 +00001129#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +00001130 /*
drh90f5ecb2004-07-22 01:19:35 +00001131 ** PRAGMA [database.]cache_size
1132 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +00001133 **
1134 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +00001135 ** page cache size. The second form sets the local
1136 ** page cache size value. If N is positive then that is the
1137 ** number of pages in the cache. If N is negative, then the
1138 ** number of pages is adjusted so that the cache uses -N kibibytes
1139 ** of memory.
drhc11d4f92003-04-06 21:08:24 +00001140 */
drh9ccd8652013-09-13 16:36:46 +00001141 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +00001142 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +00001143 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +00001144 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001145 }else{
drh3b42abb2011-11-09 14:23:04 +00001146 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +00001147 pDb->pSchema->cache_size = size;
1148 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001149 }
drh9ccd8652013-09-13 16:36:46 +00001150 break;
1151 }
drhc11d4f92003-04-06 21:08:24 +00001152
1153 /*
drh9b4c59f2013-04-15 17:03:42 +00001154 ** PRAGMA [database.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +00001155 **
drh0d0614b2013-03-25 23:09:28 +00001156 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +00001157 ** used to limit the aggregate size of all memory mapped regions of the
1158 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +00001159 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +00001160 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +00001161 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +00001162 **
drh0d0614b2013-03-25 23:09:28 +00001163 ** This value is advisory. The underlying VFS is free to memory map
1164 ** as little or as much as it wants. Except, if N is set to 0 then the
1165 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +00001166 */
drh9ccd8652013-09-13 16:36:46 +00001167 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +00001168 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +00001169#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +00001170 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +00001171 if( zRight ){
drha1f42c72013-04-01 22:38:06 +00001172 int ii;
mistachkine98844f2013-08-24 00:59:24 +00001173 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh9b4c59f2013-04-15 17:03:42 +00001174 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
1175 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +00001176 for(ii=db->nDb-1; ii>=0; ii--){
1177 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +00001178 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +00001179 }
1180 }
dan5d8a1372013-03-19 19:28:06 +00001181 }
drh9b4c59f2013-04-15 17:03:42 +00001182 sz = -1;
dan3719f5f2013-05-23 10:13:18 +00001183 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +00001184#else
dan3719f5f2013-05-23 10:13:18 +00001185 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +00001186 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +00001187#endif
dan3719f5f2013-05-23 10:13:18 +00001188 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:42 +00001189 returnSingleInt(pParse, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +00001190 }else if( rc!=SQLITE_NOTFOUND ){
1191 pParse->nErr++;
1192 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +00001193 }
drh9ccd8652013-09-13 16:36:46 +00001194 break;
1195 }
dan5d8a1372013-03-19 19:28:06 +00001196
1197 /*
drh90f5ecb2004-07-22 01:19:35 +00001198 ** PRAGMA temp_store
1199 ** PRAGMA temp_store = "default"|"memory"|"file"
1200 **
1201 ** Return or set the local value of the temp_store flag. Changing
1202 ** the local value does not make changes to the disk file and the default
1203 ** value will be restored the next time the database is opened.
1204 **
1205 ** Note that it is possible for the library compile-time options to
1206 ** override this setting
1207 */
drh9ccd8652013-09-13 16:36:46 +00001208 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +00001209 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001210 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +00001211 }else{
1212 changeTempStorage(pParse, zRight);
1213 }
drh9ccd8652013-09-13 16:36:46 +00001214 break;
1215 }
drh90f5ecb2004-07-22 01:19:35 +00001216
1217 /*
tpoindex9a09a3c2004-12-20 19:01:32 +00001218 ** PRAGMA temp_store_directory
1219 ** PRAGMA temp_store_directory = ""|"directory_name"
1220 **
1221 ** Return or set the local value of the temp_store_directory flag. Changing
1222 ** the value sets a specific directory to be used for temporary files.
1223 ** Setting to a null string reverts to the default temporary directory search.
1224 ** If temporary directory is changed, then invalidateTempStorage.
1225 **
1226 */
drh9ccd8652013-09-13 16:36:46 +00001227 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +00001228 if( !zRight ){
1229 if( sqlite3_temp_directory ){
1230 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +00001231 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +00001232 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001233 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
1234 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +00001235 }
1236 }else{
drh78f82d12008-09-02 00:52:52 +00001237#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +00001238 if( zRight[0] ){
1239 int res;
danielk1977fab11272008-09-16 14:38:02 +00001240 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1241 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +00001242 sqlite3ErrorMsg(pParse, "not a writable directory");
1243 goto pragma_out;
1244 }
drh268283b2005-01-08 15:44:25 +00001245 }
danielk1977b06a0b62008-06-26 10:54:12 +00001246 if( SQLITE_TEMP_STORE==0
1247 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
1248 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +00001249 ){
1250 invalidateTempStorage(pParse);
1251 }
drh17435752007-08-16 04:30:38 +00001252 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +00001253 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +00001254 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +00001255 }else{
drh268283b2005-01-08 15:44:25 +00001256 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +00001257 }
drh78f82d12008-09-02 00:52:52 +00001258#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +00001259 }
drh9ccd8652013-09-13 16:36:46 +00001260 break;
1261 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001262
drhcc716452012-06-06 23:23:23 +00001263#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +00001264 /*
1265 ** PRAGMA data_store_directory
1266 ** PRAGMA data_store_directory = ""|"directory_name"
1267 **
1268 ** Return or set the local value of the data_store_directory flag. Changing
1269 ** the value sets a specific directory to be used for database files that
1270 ** were specified with a relative pathname. Setting to a null string reverts
1271 ** to the default database directory, which for database files specified with
1272 ** a relative path will probably be based on the current directory for the
1273 ** process. Database file specified with an absolute path are not impacted
1274 ** by this setting, regardless of its value.
1275 **
1276 */
drh9ccd8652013-09-13 16:36:46 +00001277 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +00001278 if( !zRight ){
1279 if( sqlite3_data_directory ){
1280 sqlite3VdbeSetNumCols(v, 1);
1281 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1282 "data_store_directory", SQLITE_STATIC);
1283 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
1284 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1285 }
1286 }else{
1287#ifndef SQLITE_OMIT_WSD
1288 if( zRight[0] ){
1289 int res;
1290 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1291 if( rc!=SQLITE_OK || res==0 ){
1292 sqlite3ErrorMsg(pParse, "not a writable directory");
1293 goto pragma_out;
1294 }
1295 }
1296 sqlite3_free(sqlite3_data_directory);
1297 if( zRight[0] ){
1298 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1299 }else{
1300 sqlite3_data_directory = 0;
1301 }
1302#endif /* SQLITE_OMIT_WSD */
1303 }
drh9ccd8652013-09-13 16:36:46 +00001304 break;
1305 }
drhcc716452012-06-06 23:23:23 +00001306#endif
mistachkina112d142012-03-14 00:44:01 +00001307
drhd2cb50b2009-01-09 21:41:17 +00001308#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +00001309 /*
drh9ccd8652013-09-13 16:36:46 +00001310 ** PRAGMA [database.]lock_proxy_file
1311 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
1312 **
1313 ** Return or set the value of the lock_proxy_file flag. Changing
1314 ** the value sets a specific file to be used for database access locks.
1315 **
1316 */
1317 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +00001318 if( !zRight ){
1319 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1320 char *proxy_file_path = NULL;
1321 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +00001322 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +00001323 &proxy_file_path);
1324
1325 if( proxy_file_path ){
1326 sqlite3VdbeSetNumCols(v, 1);
1327 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1328 "lock_proxy_file", SQLITE_STATIC);
1329 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
1330 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1331 }
1332 }else{
1333 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1334 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1335 int res;
1336 if( zRight[0] ){
1337 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1338 zRight);
1339 } else {
1340 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1341 NULL);
1342 }
1343 if( res!=SQLITE_OK ){
1344 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1345 goto pragma_out;
1346 }
1347 }
drh9ccd8652013-09-13 16:36:46 +00001348 break;
1349 }
drhd2cb50b2009-01-09 21:41:17 +00001350#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001351
1352 /*
drh90f5ecb2004-07-22 01:19:35 +00001353 ** PRAGMA [database.]synchronous
1354 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +00001355 **
1356 ** Return or set the local value of the synchronous flag. Changing
1357 ** the local value does not make changes to the disk file and the
1358 ** default value will be restored the next time the database is
1359 ** opened.
1360 */
drh9ccd8652013-09-13 16:36:46 +00001361 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001362 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001363 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001364 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001365 if( !db->autoCommit ){
1366 sqlite3ErrorMsg(pParse,
1367 "Safety level may not be changed inside a transaction");
1368 }else{
drh908c0052012-01-30 18:40:55 +00001369 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
drhd3605a42013-08-17 15:42:29 +00001370 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001371 }
drhc11d4f92003-04-06 21:08:24 +00001372 }
drh9ccd8652013-09-13 16:36:46 +00001373 break;
1374 }
drh13d70422004-11-13 15:59:14 +00001375#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001376
drhbf216272005-02-26 18:10:44 +00001377#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001378 case PragTyp_FLAG: {
1379 if( zRight==0 ){
1380 returnSingleInt(pParse, aPragmaNames[mid].zName,
1381 (db->flags & aPragmaNames[mid].iArg)!=0 );
1382 }else{
1383 int mask = aPragmaNames[mid].iArg; /* Mask of bits to set or clear. */
1384 if( db->autoCommit==0 ){
1385 /* Foreign key support may not be enabled or disabled while not
1386 ** in auto-commit mode. */
1387 mask &= ~(SQLITE_ForeignKeys);
1388 }
1389
1390 if( sqlite3GetBoolean(zRight, 0) ){
1391 db->flags |= mask;
1392 }else{
1393 db->flags &= ~mask;
1394 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1395 }
1396
1397 /* Many of the flag-pragmas modify the code generated by the SQL
1398 ** compiler (eg. count_changes). So add an opcode to expire all
1399 ** compiled SQL statements after modifying a pragma value.
1400 */
1401 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1402 setAllPagerFlags(db);
1403 }
1404 break;
1405 }
drhbf216272005-02-26 18:10:44 +00001406#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001407
drh13d70422004-11-13 15:59:14 +00001408#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001409 /*
1410 ** PRAGMA table_info(<table>)
1411 **
1412 ** Return a single row for each column of the named table. The columns of
1413 ** the returned data set are:
1414 **
1415 ** cid: Column id (numbered from left to right, starting at 0)
1416 ** name: Column name
1417 ** type: Column declaration type.
1418 ** notnull: True if 'NOT NULL' is part of column declaration
1419 ** dflt_value: The default value for the column, if any.
1420 */
drh9ccd8652013-09-13 16:36:46 +00001421 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001422 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001423 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001424 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001425 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001426 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001427 Column *pCol;
drh44156282013-10-23 22:23:03 +00001428 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
drh9f6696a2006-02-09 16:52:23 +00001429 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +00001430 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001431 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001432 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1433 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1434 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1435 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1436 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1437 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001438 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001439 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001440 if( IsHiddenColumn(pCol) ){
1441 nHidden++;
1442 continue;
1443 }
drh2d401ab2008-01-10 23:50:11 +00001444 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1445 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1446 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +00001447 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +00001448 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001449 if( pCol->zDflt ){
1450 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001451 }else{
drh2d401ab2008-01-10 23:50:11 +00001452 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001453 }
drh384b7fe2013-01-01 13:55:31 +00001454 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1455 k = 0;
1456 }else if( pPk==0 ){
1457 k = 1;
1458 }else{
1459 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1460 }
1461 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001462 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001463 }
1464 }
drh9ccd8652013-09-13 16:36:46 +00001465 }
1466 break;
drhc11d4f92003-04-06 21:08:24 +00001467
drh3ef26152013-10-12 20:22:00 +00001468 case PragTyp_STATS: {
1469 Index *pIdx;
1470 HashElem *i;
1471 v = sqlite3GetVdbe(pParse);
1472 sqlite3VdbeSetNumCols(v, 4);
1473 pParse->nMem = 4;
1474 sqlite3CodeVerifySchema(pParse, iDb);
1475 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1476 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
1477 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
1478 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
1479 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
1480 Table *pTab = sqliteHashData(i);
1481 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
1482 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
1483 sqlite3VdbeAddOp2(v, OP_Integer,
1484 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
1485 sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
1486 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1487 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1488 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1489 sqlite3VdbeAddOp2(v, OP_Integer,
1490 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
1491 sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
1492 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
1493 }
1494 }
1495 }
1496 break;
1497
drh9ccd8652013-09-13 16:36:46 +00001498 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001499 Index *pIdx;
1500 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001501 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001502 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001503 int i;
1504 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +00001505 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001506 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001507 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001508 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1509 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1510 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhbbbdc832013-10-22 18:01:40 +00001511 for(i=0; i<pIdx->nKeyCol; i++){
1512 i16 cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001513 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1514 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +00001515 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +00001516 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1517 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001518 }
1519 }
drh9ccd8652013-09-13 16:36:46 +00001520 }
1521 break;
drhc11d4f92003-04-06 21:08:24 +00001522
drh9ccd8652013-09-13 16:36:46 +00001523 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001524 Index *pIdx;
1525 Table *pTab;
drhe13e9f52013-10-05 19:18:00 +00001526 int i;
drh2783e4b2004-10-05 15:42:53 +00001527 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001528 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001529 v = sqlite3GetVdbe(pParse);
drh3ef26152013-10-12 20:22:00 +00001530 sqlite3VdbeSetNumCols(v, 3);
1531 pParse->nMem = 3;
drhe13e9f52013-10-05 19:18:00 +00001532 sqlite3CodeVerifySchema(pParse, iDb);
1533 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1534 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1535 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
drh3ef26152013-10-12 20:22:00 +00001536 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
drhe13e9f52013-10-05 19:18:00 +00001537 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1538 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1539 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
drh3ef26152013-10-12 20:22:00 +00001540 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001541 }
1542 }
drh9ccd8652013-09-13 16:36:46 +00001543 }
1544 break;
drhc11d4f92003-04-06 21:08:24 +00001545
drh9ccd8652013-09-13 16:36:46 +00001546 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001547 int i;
drh13d70422004-11-13 15:59:14 +00001548 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001549 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001550 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1551 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1552 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001553 for(i=0; i<db->nDb; i++){
1554 if( db->aDb[i].pBt==0 ) continue;
1555 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001556 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1557 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1558 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001559 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001560 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001561 }
drh9ccd8652013-09-13 16:36:46 +00001562 }
1563 break;
danielk197748af65a2005-02-09 03:20:37 +00001564
drh9ccd8652013-09-13 16:36:46 +00001565 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001566 int i = 0;
1567 HashElem *p;
1568 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001569 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001570 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1571 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001572 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1573 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001574 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1575 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1576 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001577 }
drh9ccd8652013-09-13 16:36:46 +00001578 }
1579 break;
drh13d70422004-11-13 15:59:14 +00001580#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1581
drhb7f91642004-10-31 02:22:47 +00001582#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001583 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001584 FKey *pFK;
1585 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001586 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001587 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001588 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001589 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001590 if( pFK ){
1591 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001592 sqlite3VdbeSetNumCols(v, 8);
1593 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001594 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001595 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1596 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1597 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1598 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1599 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1600 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1601 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1602 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001603 while(pFK){
1604 int j;
1605 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001606 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001607 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1608 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001609 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1610 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1611 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1612 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001613 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001614 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001615 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1616 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1617 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1618 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001619 }
1620 ++i;
1621 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001622 }
drh78100cc2003-08-23 22:40:53 +00001623 }
1624 }
drh9ccd8652013-09-13 16:36:46 +00001625 }
1626 break;
drhb7f91642004-10-31 02:22:47 +00001627#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001628
drh6c5b9152012-12-17 16:46:37 +00001629#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001630#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001631 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001632 FKey *pFK; /* A foreign key constraint */
1633 Table *pTab; /* Child table contain "REFERENCES" keyword */
1634 Table *pParent; /* Parent table that child points to */
1635 Index *pIdx; /* Index in the parent table */
1636 int i; /* Loop counter: Foreign key number for pTab */
1637 int j; /* Loop counter: Field of the foreign key */
1638 HashElem *k; /* Loop counter: Next table in schema */
1639 int x; /* result variable */
1640 int regResult; /* 3 registers to hold a result row */
1641 int regKey; /* Register to hold key for checking the FK */
1642 int regRow; /* Registers to hold a row from pTab */
1643 int addrTop; /* Top of a loop checking foreign keys */
1644 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001645 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001646
drh613028b2012-12-17 18:43:02 +00001647 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001648 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001649 regKey = ++pParse->nMem;
1650 regRow = ++pParse->nMem;
1651 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001652 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001653 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1654 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001655 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1656 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001657 sqlite3CodeVerifySchema(pParse, iDb);
1658 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1659 while( k ){
1660 if( zRight ){
1661 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1662 k = 0;
1663 }else{
1664 pTab = (Table*)sqliteHashData(k);
1665 k = sqliteHashNext(k);
1666 }
drh9148def2012-12-17 20:40:39 +00001667 if( pTab==0 || pTab->pFKey==0 ) continue;
1668 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001669 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001670 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001671 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1672 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001673 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001674 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
1675 if( pParent==0 ) continue;
drh6c5b9152012-12-17 16:46:37 +00001676 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001677 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001678 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001679 if( x==0 ){
1680 if( pIdx==0 ){
1681 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1682 }else{
drh6c5b9152012-12-17 16:46:37 +00001683 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
drh2ec2fb22013-11-06 19:59:23 +00001684 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
drh6c5b9152012-12-17 16:46:37 +00001685 }
1686 }else{
drh613028b2012-12-17 18:43:02 +00001687 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001688 break;
1689 }
drh6c5b9152012-12-17 16:46:37 +00001690 }
dan5e878302013-10-12 19:06:48 +00001691 assert( pParse->nErr>0 || pFK==0 );
drh613028b2012-12-17 18:43:02 +00001692 if( pFK ) break;
1693 if( pParse->nTab<i ) pParse->nTab = i;
1694 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
1695 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
dan5e878302013-10-12 19:06:48 +00001696 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
drh613028b2012-12-17 18:43:02 +00001697 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001698 aiCols = 0;
dan5e878302013-10-12 19:06:48 +00001699 if( pParent ){
1700 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1701 assert( x==0 );
1702 }
drh613028b2012-12-17 18:43:02 +00001703 addrOk = sqlite3VdbeMakeLabel(v);
dan5e878302013-10-12 19:06:48 +00001704 if( pParent && pIdx==0 ){
drh613028b2012-12-17 18:43:02 +00001705 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001706 assert( iKey>=0 && iKey<pTab->nCol );
1707 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001708 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1709 sqlite3ColumnDefault(v, pTab, iKey, regRow);
1710 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
1711 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1712 sqlite3VdbeCurrentAddr(v)+3);
drh6c5b9152012-12-17 16:46:37 +00001713 }else{
drh613028b2012-12-17 18:43:02 +00001714 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001715 }
drh613028b2012-12-17 18:43:02 +00001716 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
1717 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1718 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1719 }else{
1720 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001721 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
dan5e878302013-10-12 19:06:48 +00001722 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
drh613028b2012-12-17 18:43:02 +00001723 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
1724 }
dan5e878302013-10-12 19:06:48 +00001725 if( pParent ){
1726 sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
1727 sqlite3VdbeChangeP4(v, -1,
1728 sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
1729 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
1730 }
drh6c5b9152012-12-17 16:46:37 +00001731 }
drh613028b2012-12-17 18:43:02 +00001732 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001733 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1734 pFK->zTo, P4_TRANSIENT);
1735 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1736 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001737 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001738 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001739 }
drh613028b2012-12-17 18:43:02 +00001740 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
1741 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001742 }
drh9ccd8652013-09-13 16:36:46 +00001743 }
1744 break;
dan09ff9e12013-03-11 11:49:03 +00001745#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001746#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1747
drhc11d4f92003-04-06 21:08:24 +00001748#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001749 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001750 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001751 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001752 sqlite3ParserTrace(stderr, "parser: ");
1753 }else{
1754 sqlite3ParserTrace(0, 0);
1755 }
drhc11d4f92003-04-06 21:08:24 +00001756 }
drh9ccd8652013-09-13 16:36:46 +00001757 }
1758 break;
drhc11d4f92003-04-06 21:08:24 +00001759#endif
1760
drh55ef4d92005-08-14 01:20:37 +00001761 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1762 ** used will be case sensitive or not depending on the RHS.
1763 */
drh9ccd8652013-09-13 16:36:46 +00001764 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001765 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001766 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001767 }
drh9ccd8652013-09-13 16:36:46 +00001768 }
1769 break;
drh55ef4d92005-08-14 01:20:37 +00001770
drh1dcdbc02007-01-27 02:24:54 +00001771#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1772# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1773#endif
1774
drhb7f91642004-10-31 02:22:47 +00001775#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001776 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001777 ** integrity_check designed to detect most database corruption
1778 ** without most of the overhead of a full integrity-check.
1779 */
drh9ccd8652013-09-13 16:36:46 +00001780 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001781 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001782
drhed717fe2003-06-15 23:42:24 +00001783 /* Code that appears at the end of the integrity check. If no error
1784 ** messages have been generated, output OK. Otherwise output the
1785 ** error message
1786 */
drh57196282004-10-06 15:41:16 +00001787 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001788 { OP_AddImm, 1, 0, 0}, /* 0 */
1789 { OP_IfNeg, 1, 0, 0}, /* 1 */
1790 { OP_String8, 0, 3, 0}, /* 2 */
1791 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001792 };
drhed717fe2003-06-15 23:42:24 +00001793
drhc5227312011-10-13 17:09:01 +00001794 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001795
dan5885e762012-07-16 10:06:12 +00001796 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1797 ** then iDb is set to the index of the database identified by <db>.
1798 ** In this case, the integrity of database iDb only is verified by
1799 ** the VDBE created below.
1800 **
1801 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1802 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1803 ** to -1 here, to indicate that the VDBE should verify the integrity
1804 ** of all attached databases. */
1805 assert( iDb>=0 );
1806 assert( iDb==0 || pId2->z );
1807 if( pId2->z==0 ) iDb = -1;
1808
drhed717fe2003-06-15 23:42:24 +00001809 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001810 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001811 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001812 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001813
1814 /* Set the maximum error count */
1815 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1816 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001817 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001818 if( mxErr<=0 ){
1819 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1820 }
1821 }
drh2d401ab2008-01-10 23:50:11 +00001822 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001823
1824 /* Do an integrity check on each database file */
1825 for(i=0; i<db->nDb; i++){
1826 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001827 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001828 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001829
danielk197753c0f742005-03-29 03:10:59 +00001830 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001831 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001832
drh80242052004-06-09 00:48:12 +00001833 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001834 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001835 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001836 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001837
drhed717fe2003-06-15 23:42:24 +00001838 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001839 **
1840 ** Begin by filling registers 2, 3, ... with the root pages numbers
1841 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001842 */
dan5885e762012-07-16 10:06:12 +00001843 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001844 pTbls = &db->aDb[i].pSchema->tblHash;
1845 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001846 Table *pTab = sqliteHashData(x);
1847 Index *pIdx;
drh6fbe41a2013-10-30 20:22:55 +00001848 if( HasRowid(pTab) ){
1849 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001850 VdbeComment((v, "%s", pTab->zName));
drh6fbe41a2013-10-30 20:22:55 +00001851 cnt++;
1852 }
drh79069752004-05-22 21:30:40 +00001853 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001854 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh58383402013-11-04 17:00:50 +00001855 VdbeComment((v, "%s", pIdx->zName));
drh79069752004-05-22 21:30:40 +00001856 cnt++;
1857 }
1858 }
drh2d401ab2008-01-10 23:50:11 +00001859
1860 /* Make sure sufficient number of registers have been allocated */
drh6fbe41a2013-10-30 20:22:55 +00001861 pParse->nMem = MAX( pParse->nMem, cnt+8 );
drh2d401ab2008-01-10 23:50:11 +00001862
1863 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001864 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001865 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001866 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1867 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001868 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001869 P4_DYNAMIC);
drhe8e4af72012-09-21 00:04:28 +00001870 sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
danielk1977a7a8e142008-02-13 18:25:27 +00001871 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001872 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001873 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001874
1875 /* Make sure all the indices are constructed correctly.
1876 */
danielk197741c58b72007-12-29 13:39:19 +00001877 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001878 Table *pTab = sqliteHashData(x);
drh6fbe41a2013-10-30 20:22:55 +00001879 Index *pIdx, *pPk;
drhed717fe2003-06-15 23:42:24 +00001880 int loopTop;
drh26198bb2013-10-31 11:15:09 +00001881 int iDataCur, iIdxCur;
drhed717fe2003-06-15 23:42:24 +00001882
1883 if( pTab->pIndex==0 ) continue;
drh26198bb2013-10-31 11:15:09 +00001884 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
drh2d401ab2008-01-10 23:50:11 +00001885 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001886 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001887 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001888 sqlite3ExprCacheClear(pParse);
drh26198bb2013-10-31 11:15:09 +00001889 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
1890 1, &iDataCur, &iIdxCur);
drh6fbe41a2013-10-30 20:22:55 +00001891 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
drh8a9789b2013-08-01 03:36:59 +00001892 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001893 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
drh8a9789b2013-08-01 03:36:59 +00001894 }
drh6fbe41a2013-10-30 20:22:55 +00001895 pParse->nMem = MAX(pParse->nMem, 8+j);
drh26198bb2013-10-31 11:15:09 +00001896 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
drh6fbe41a2013-10-30 20:22:55 +00001897 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001898 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001899 int jmp2, jmp3, jmp4;
drh91fc4a02009-11-12 20:39:03 +00001900 int r1;
drh6fbe41a2013-10-30 20:22:55 +00001901 if( pPk==pIdx ) continue;
drh26198bb2013-10-31 11:15:09 +00001902 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3);
drh6fbe41a2013-10-30 20:22:55 +00001903 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */
drh26198bb2013-10-31 11:15:09 +00001904 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
drhda475b82013-11-04 21:44:54 +00001905 pIdx->nColumn);
drh6fbe41a2013-10-30 20:22:55 +00001906 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
1907 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
1908 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1909 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
1910 P4_STATIC);
1911 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1912 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
1913 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1914 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
1915 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
1916 sqlite3VdbeAddOp0(v, OP_Halt);
1917 sqlite3VdbeJumpHere(v, jmp4);
drhd654be82005-09-20 17:42:23 +00001918 sqlite3VdbeJumpHere(v, jmp2);
drhb2b9d3d2013-08-01 01:14:43 +00001919 sqlite3VdbeResolveLabel(v, jmp3);
drhed717fe2003-06-15 23:42:24 +00001920 }
drh6934fc72013-10-31 15:37:49 +00001921 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop);
drh8a9789b2013-08-01 03:36:59 +00001922 sqlite3VdbeJumpHere(v, loopTop-1);
1923#ifndef SQLITE_OMIT_BTREECOUNT
1924 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
drh24003452008-01-03 01:28:59 +00001925 "wrong # of entries in index ", P4_STATIC);
drh8a9789b2013-08-01 03:36:59 +00001926 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh6fbe41a2013-10-30 20:22:55 +00001927 if( pPk==pIdx ) continue;
drh8a9789b2013-08-01 03:36:59 +00001928 addr = sqlite3VdbeCurrentAddr(v);
1929 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
1930 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh26198bb2013-10-31 11:15:09 +00001931 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
drh6fbe41a2013-10-30 20:22:55 +00001932 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3);
drh8a9789b2013-08-01 03:36:59 +00001933 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1934 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
1935 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1936 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001937 }
drh8a9789b2013-08-01 03:36:59 +00001938#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001939 }
1940 }
danielk19774adee202004-05-08 08:23:19 +00001941 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001942 sqlite3VdbeChangeP2(v, addr, -mxErr);
1943 sqlite3VdbeJumpHere(v, addr+1);
1944 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001945 }
1946 break;
drhb7f91642004-10-31 02:22:47 +00001947#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1948
drh13d70422004-11-13 15:59:14 +00001949#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001950 /*
1951 ** PRAGMA encoding
1952 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1953 **
drh85b623f2007-12-13 21:54:09 +00001954 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001955 ** database. If the database is not initialized, it is initialized now.
1956 **
1957 ** The second form of this pragma is a no-op if the main database file
1958 ** has not already been initialized. In this case it sets the default
1959 ** encoding that will be used for the main database file if a new file
1960 ** is created. If an existing main database file is opened, then the
1961 ** default text encoding for the existing database is used.
1962 **
1963 ** In all cases new databases created using the ATTACH command are
1964 ** created to use the same default text encoding as the main database. If
1965 ** the main database has not been initialized and/or created when ATTACH
1966 ** is executed, this is done before the ATTACH operation.
1967 **
1968 ** In the second form this pragma sets the text encoding to be used in
1969 ** new database files created using this database handle. It is only
1970 ** useful if invoked immediately after the main database i
1971 */
drh9ccd8652013-09-13 16:36:46 +00001972 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001973 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001974 char *zName;
1975 u8 enc;
1976 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001977 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001978 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1979 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1980 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001981 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001982 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001983 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1984 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001985 { 0, 0 }
1986 };
drh0f7eb612006-08-08 13:51:43 +00001987 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001988 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001989 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001990 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001991 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001992 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001993 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1994 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1995 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1996 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001997 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001998 }else{ /* "PRAGMA encoding = XXX" */
1999 /* Only change the value of sqlite.enc if the database handle is not
2000 ** initialized. If the main database exists, the new sqlite.enc value
2001 ** will be overwritten when the schema is next loaded. If it does not
2002 ** already exists, it will be created to use the new encoding value.
2003 */
danielk1977b82e7ed2006-01-11 14:09:31 +00002004 if(
2005 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
2006 DbHasProperty(db, 0, DB_Empty)
2007 ){
danielk19778e227872004-06-07 07:52:17 +00002008 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
2009 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00002010 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00002011 break;
2012 }
2013 }
2014 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00002015 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00002016 }
2017 }
2018 }
drh9ccd8652013-09-13 16:36:46 +00002019 }
2020 break;
drh13d70422004-11-13 15:59:14 +00002021#endif /* SQLITE_OMIT_UTF16 */
2022
2023#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00002024 /*
danielk1977b92b70b2004-11-12 16:11:59 +00002025 ** PRAGMA [database.]schema_version
2026 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002027 **
danielk1977b92b70b2004-11-12 16:11:59 +00002028 ** PRAGMA [database.]user_version
2029 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00002030 **
drh4ee09b42013-05-01 19:49:27 +00002031 ** PRAGMA [database.]freelist_count = <integer>
2032 **
2033 ** PRAGMA [database.]application_id
2034 ** PRAGMA [database.]application_id = <integer>
2035 **
danielk1977b92b70b2004-11-12 16:11:59 +00002036 ** The pragma's schema_version and user_version are used to set or get
2037 ** the value of the schema-version and user-version, respectively. Both
2038 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00002039 ** stored in the database header.
2040 **
2041 ** The schema-cookie is usually only manipulated internally by SQLite. It
2042 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00002043 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00002044 ** SQLite each time a query is executed to ensure that the internal cache
2045 ** of the schema used when compiling the SQL query matches the schema of
2046 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00002047 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2048 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00002049 ** crashes or database corruption. Use with caution!
2050 **
danielk1977b92b70b2004-11-12 16:11:59 +00002051 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00002052 ** applications for any purpose.
2053 */
drh9ccd8652013-09-13 16:36:46 +00002054 case PragTyp_HEADER_VALUE: {
danielk19770d19f7a2009-06-03 11:25:07 +00002055 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00002056 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00002057 switch( zLeft[0] ){
drh4ee09b42013-05-01 19:49:27 +00002058 case 'a': case 'A':
2059 iCookie = BTREE_APPLICATION_ID;
2060 break;
danielk1977180b56a2007-06-24 08:00:42 +00002061 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00002062 iCookie = BTREE_FREE_PAGE_COUNT;
2063 break;
2064 case 's': case 'S':
2065 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00002066 break;
2067 default:
danielk19770d19f7a2009-06-03 11:25:07 +00002068 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00002069 break;
danielk1977dae24952004-11-11 05:10:43 +00002070 }
2071
danielk19770d19f7a2009-06-03 11:25:07 +00002072 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00002073 /* Write the specified cookie value */
2074 static const VdbeOpList setCookie[] = {
2075 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00002076 { OP_Integer, 0, 1, 0}, /* 1 */
2077 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00002078 };
2079 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
2080 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00002081 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00002082 sqlite3VdbeChangeP1(v, addr+2, iDb);
2083 sqlite3VdbeChangeP2(v, addr+2, iCookie);
2084 }else{
2085 /* Read the specified cookie value */
2086 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00002087 { OP_Transaction, 0, 0, 0}, /* 0 */
2088 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00002089 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00002090 };
2091 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
2092 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00002093 sqlite3VdbeChangeP1(v, addr+1, iDb);
2094 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00002095 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00002096 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00002097 }
drh9ccd8652013-09-13 16:36:46 +00002098 }
2099 break;
drh13d70422004-11-13 15:59:14 +00002100#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00002101
shanehdc97a8c2010-02-23 20:08:35 +00002102#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2103 /*
2104 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00002105 **
drh71caabf2010-02-26 15:39:24 +00002106 ** Return the names of all compile-time options used in this build,
2107 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00002108 */
drh9ccd8652013-09-13 16:36:46 +00002109 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00002110 int i = 0;
2111 const char *zOpt;
2112 sqlite3VdbeSetNumCols(v, 1);
2113 pParse->nMem = 1;
2114 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
2115 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
2116 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
2117 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2118 }
drh9ccd8652013-09-13 16:36:46 +00002119 }
2120 break;
shanehdc97a8c2010-02-23 20:08:35 +00002121#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2122
dan5cf53532010-05-01 16:40:20 +00002123#ifndef SQLITE_OMIT_WAL
2124 /*
dancdc1f042010-11-18 12:11:05 +00002125 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00002126 **
2127 ** Checkpoint the database.
2128 */
drh9ccd8652013-09-13 16:36:46 +00002129 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00002130 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00002131 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2132 if( zRight ){
2133 if( sqlite3StrICmp(zRight, "full")==0 ){
2134 eMode = SQLITE_CHECKPOINT_FULL;
2135 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2136 eMode = SQLITE_CHECKPOINT_RESTART;
2137 }
2138 }
dancdc1f042010-11-18 12:11:05 +00002139 sqlite3VdbeSetNumCols(v, 3);
2140 pParse->nMem = 3;
2141 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
2142 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
2143 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
2144
drh30aa3b92011-02-07 23:56:01 +00002145 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00002146 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00002147 }
2148 break;
dan5a299f92010-05-03 11:05:08 +00002149
2150 /*
2151 ** PRAGMA wal_autocheckpoint
2152 ** PRAGMA wal_autocheckpoint = N
2153 **
2154 ** Configure a database connection to automatically checkpoint a database
2155 ** after accumulating N frames in the log. Or query for the current value
2156 ** of N.
2157 */
drh9ccd8652013-09-13 16:36:46 +00002158 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00002159 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00002160 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00002161 }
drhb033d8b2010-05-03 13:37:30 +00002162 returnSingleInt(pParse, "wal_autocheckpoint",
2163 db->xWalCallback==sqlite3WalDefaultHook ?
2164 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00002165 }
2166 break;
dan5cf53532010-05-01 16:40:20 +00002167#endif
dan7c246102010-04-12 19:00:29 +00002168
drh09419b42011-11-16 19:29:17 +00002169 /*
2170 ** PRAGMA shrink_memory
2171 **
2172 ** This pragma attempts to free as much memory as possible from the
2173 ** current database connection.
2174 */
drh9ccd8652013-09-13 16:36:46 +00002175 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00002176 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00002177 break;
2178 }
drh09419b42011-11-16 19:29:17 +00002179
drhf3603962012-09-07 16:46:59 +00002180 /*
2181 ** PRAGMA busy_timeout
2182 ** PRAGMA busy_timeout = N
2183 **
2184 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002185 ** if one is set. If no busy handler or a different busy handler is set
2186 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2187 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002188 */
drhd49c3582013-09-13 19:00:06 +00002189 /*case PragTyp_BUSY_TIMEOUT*/ default: {
2190 assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002191 if( zRight ){
2192 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2193 }
2194 returnSingleInt(pParse, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002195 break;
2196 }
drhf3603962012-09-07 16:46:59 +00002197
drh55e85ca2013-09-13 21:01:56 +00002198 /*
2199 ** PRAGMA soft_heap_limit
2200 ** PRAGMA soft_heap_limit = N
2201 **
2202 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
2203 ** use -1.
2204 */
2205 case PragTyp_SOFT_HEAP_LIMIT: {
2206 sqlite3_int64 N;
2207 if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
2208 sqlite3_soft_heap_limit64(N);
2209 }
2210 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
2211 break;
2212 }
2213
dougcurrie81c95ef2004-06-18 23:21:47 +00002214#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002215 /*
2216 ** Report the current state of file logs for all databases
2217 */
drh9ccd8652013-09-13 16:36:46 +00002218 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002219 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002220 "unlocked", "shared", "reserved", "pending", "exclusive"
2221 };
2222 int i;
drh89ac8c12004-06-09 14:17:20 +00002223 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00002224 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00002225 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
2226 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002227 for(i=0; i<db->nDb; i++){
2228 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002229 const char *zState = "unknown";
2230 int j;
drh89ac8c12004-06-09 14:17:20 +00002231 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00002232 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002233 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002234 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002235 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00002236 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002237 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2238 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002239 }
drh2d401ab2008-01-10 23:50:11 +00002240 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
2241 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00002242 }
drh9ccd8652013-09-13 16:36:46 +00002243 break;
2244 }
drh89ac8c12004-06-09 14:17:20 +00002245#endif
2246
shanehad9f9f62010-02-17 17:48:46 +00002247#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00002248 case PragTyp_KEY: {
2249 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2250 break;
2251 }
2252 case PragTyp_REKEY: {
2253 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2254 break;
2255 }
2256 case PragTyp_HEXKEY: {
2257 if( zRight ){
drh8ab88322013-10-07 00:36:01 +00002258 u8 iByte;
2259 int i;
drh9ccd8652013-09-13 16:36:46 +00002260 char zKey[40];
drh8ab88322013-10-07 00:36:01 +00002261 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
2262 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
2263 if( (i&1)!=0 ) zKey[i/2] = iByte;
drh9ccd8652013-09-13 16:36:46 +00002264 }
2265 if( (zLeft[3] & 0xf)==0xb ){
2266 sqlite3_key_v2(db, zDb, zKey, i/2);
2267 }else{
2268 sqlite3_rekey_v2(db, zDb, zKey, i/2);
2269 }
drhd2cb50b2009-01-09 21:41:17 +00002270 }
drh9ccd8652013-09-13 16:36:46 +00002271 break;
2272 }
drh3c4f2a42005-12-08 18:12:56 +00002273#endif
shanehad9f9f62010-02-17 17:48:46 +00002274#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002275 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00002276#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00002277 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002278 sqlite3_activate_see(&zRight[4]);
2279 }
2280#endif
2281#ifdef SQLITE_ENABLE_CEROD
2282 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002283 sqlite3_activate_cerod(&zRight[6]);
2284 }
2285#endif
drh9ccd8652013-09-13 16:36:46 +00002286 }
2287 break;
drh21e2cab2006-09-25 18:01:57 +00002288#endif
drh3c4f2a42005-12-08 18:12:56 +00002289
drh9ccd8652013-09-13 16:36:46 +00002290 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002291
danielk1977e0048402004-06-15 16:51:01 +00002292pragma_out:
drh633e6d52008-07-28 19:34:53 +00002293 sqlite3DbFree(db, zLeft);
2294 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002295}
drh13d70422004-11-13 15:59:14 +00002296
drh8bfdf722009-06-19 14:06:03 +00002297#endif /* SQLITE_OMIT_PRAGMA */