blob: 940402bb68d5363775fc33753fe7d66343cb7882 [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
59#define PragTyp_SYNCHRONOUS 28
60#define PragTyp_TABLE_INFO 29
61#define PragTyp_TEMP_STORE 30
62#define PragTyp_TEMP_STORE_DIRECTORY 31
63#define PragTyp_WAL_AUTOCHECKPOINT 32
64#define PragTyp_WAL_CHECKPOINT 33
65#define PragTyp_ACTIVATE_EXTENSIONS 34
66#define PragTyp_HEXKEY 35
67#define PragTyp_KEY 36
68#define PragTyp_REKEY 37
69#define PragTyp_LOCK_STATUS 38
70#define PragTyp_PARSER_TRACE 39
drhf63936e2013-10-03 14:08:07 +000071#define PragFlag_NeedSchema 0x01
drh9ccd8652013-09-13 16:36:46 +000072static const struct sPragmaNames {
drh77ff23f2013-09-13 21:03:45 +000073 const char *const zName; /* Name of pragma */
drhd49c3582013-09-13 19:00:06 +000074 u8 ePragTyp; /* PragTyp_XXX value */
drhf63936e2013-10-03 14:08:07 +000075 u8 mPragFlag; /* Zero or more PragFlag_XXX values */
drh9ccd8652013-09-13 16:36:46 +000076 u32 iArg; /* Extra argument */
77} aPragmaNames[] = {
78#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drhf63936e2013-10-03 14:08:07 +000079 { /* zName: */ "activate_extensions",
80 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
81 /* ePragFlag: */ 0,
82 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +000083#endif
84#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +000085 { /* zName: */ "application_id",
86 /* ePragTyp: */ PragTyp_HEADER_VALUE,
87 /* ePragFlag: */ 0,
88 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +000089#endif
90#if !defined(SQLITE_OMIT_AUTOVACUUM)
drhf63936e2013-10-03 14:08:07 +000091 { /* zName: */ "auto_vacuum",
92 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
93 /* ePragFlag: */ PragFlag_NeedSchema,
94 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +000095#endif
96#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
drhf63936e2013-10-03 14:08:07 +000097 { /* zName: */ "automatic_index",
98 /* ePragTyp: */ PragTyp_FLAG,
99 /* ePragFlag: */ 0,
100 /* iArg: */ SQLITE_AutoIndex },
drh9ccd8652013-09-13 16:36:46 +0000101#endif
drhf63936e2013-10-03 14:08:07 +0000102 { /* zName: */ "busy_timeout",
103 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
104 /* ePragFlag: */ 0,
105 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000106#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000107 { /* zName: */ "cache_size",
108 /* ePragTyp: */ PragTyp_CACHE_SIZE,
109 /* ePragFlag: */ PragFlag_NeedSchema,
110 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000111#endif
drhf63936e2013-10-03 14:08:07 +0000112 { /* zName: */ "cache_spill",
113 /* ePragTyp: */ PragTyp_FLAG,
114 /* ePragFlag: */ 0,
115 /* iArg: */ SQLITE_CacheSpill },
116 { /* zName: */ "case_sensitive_like",
117 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
118 /* ePragFlag: */ 0,
119 /* iArg: */ 0 },
120 { /* zName: */ "checkpoint_fullfsync",
121 /* ePragTyp: */ PragTyp_FLAG,
122 /* ePragFlag: */ 0,
123 /* iArg: */ SQLITE_CkptFullFSync },
drh9ccd8652013-09-13 16:36:46 +0000124#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000125 { /* zName: */ "collation_list",
126 /* ePragTyp: */ PragTyp_COLLATION_LIST,
127 /* ePragFlag: */ 0,
128 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000129#endif
130#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
drhf63936e2013-10-03 14:08:07 +0000131 { /* zName: */ "compile_options",
132 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
133 /* ePragFlag: */ 0,
134 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000135#endif
drhf63936e2013-10-03 14:08:07 +0000136 { /* zName: */ "count_changes",
137 /* ePragTyp: */ PragTyp_FLAG,
138 /* ePragFlag: */ 0,
139 /* iArg: */ SQLITE_CountRows },
drh9ccd8652013-09-13 16:36:46 +0000140#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
drhf63936e2013-10-03 14:08:07 +0000141 { /* zName: */ "data_store_directory",
142 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
143 /* ePragFlag: */ 0,
144 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000145#endif
146#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000147 { /* zName: */ "database_list",
148 /* ePragTyp: */ PragTyp_DATABASE_LIST,
149 /* ePragFlag: */ PragFlag_NeedSchema,
150 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000151#endif
152#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhf63936e2013-10-03 14:08:07 +0000153 { /* zName: */ "default_cache_size",
154 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
155 /* ePragFlag: */ PragFlag_NeedSchema,
156 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000157#endif
158#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
drhf63936e2013-10-03 14:08:07 +0000159 { /* zName: */ "defer_foreign_keys",
160 /* ePragTyp: */ PragTyp_FLAG,
161 /* ePragFlag: */ 0,
162 /* iArg: */ SQLITE_DeferFKs },
drh9ccd8652013-09-13 16:36:46 +0000163#endif
drhf63936e2013-10-03 14:08:07 +0000164 { /* zName: */ "empty_result_callbacks",
165 /* ePragTyp: */ PragTyp_FLAG,
166 /* ePragFlag: */ 0,
167 /* iArg: */ SQLITE_NullCallback },
drh9ccd8652013-09-13 16:36:46 +0000168#if !defined(SQLITE_OMIT_UTF16)
drhf63936e2013-10-03 14:08:07 +0000169 { /* zName: */ "encoding",
170 /* ePragTyp: */ PragTyp_ENCODING,
171 /* ePragFlag: */ 0,
172 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000173#endif
174#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
drhf63936e2013-10-03 14:08:07 +0000175 { /* zName: */ "foreign_key_check",
176 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
177 /* ePragFlag: */ PragFlag_NeedSchema,
178 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000179#endif
180#if !defined(SQLITE_OMIT_FOREIGN_KEY)
drhf63936e2013-10-03 14:08:07 +0000181 { /* zName: */ "foreign_key_list",
182 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
183 /* ePragFlag: */ PragFlag_NeedSchema,
184 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000185#endif
186#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
drhf63936e2013-10-03 14:08:07 +0000187 { /* zName: */ "foreign_keys",
188 /* ePragTyp: */ PragTyp_FLAG,
189 /* ePragFlag: */ 0,
190 /* iArg: */ SQLITE_ForeignKeys },
drh9ccd8652013-09-13 16:36:46 +0000191#endif
192#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000193 { /* zName: */ "freelist_count",
194 /* ePragTyp: */ PragTyp_HEADER_VALUE,
195 /* ePragFlag: */ 0,
196 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000197#endif
drhf63936e2013-10-03 14:08:07 +0000198 { /* zName: */ "full_column_names",
199 /* ePragTyp: */ PragTyp_FLAG,
200 /* ePragFlag: */ 0,
201 /* iArg: */ SQLITE_FullColNames },
202 { /* zName: */ "fullfsync",
203 /* ePragTyp: */ PragTyp_FLAG,
204 /* ePragFlag: */ 0,
205 /* iArg: */ SQLITE_FullFSync },
drh9ccd8652013-09-13 16:36:46 +0000206#if defined(SQLITE_HAS_CODEC)
drhf63936e2013-10-03 14:08:07 +0000207 { /* zName: */ "hexkey",
208 /* ePragTyp: */ PragTyp_HEXKEY,
209 /* ePragFlag: */ 0,
210 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000211#endif
212#if !defined(SQLITE_OMIT_CHECK)
drhf63936e2013-10-03 14:08:07 +0000213 { /* zName: */ "ignore_check_constraints",
214 /* ePragTyp: */ PragTyp_FLAG,
215 /* ePragFlag: */ 0,
216 /* iArg: */ SQLITE_IgnoreChecks },
drh9ccd8652013-09-13 16:36:46 +0000217#endif
218#if !defined(SQLITE_OMIT_AUTOVACUUM)
drhf63936e2013-10-03 14:08:07 +0000219 { /* zName: */ "incremental_vacuum",
220 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
221 /* ePragFlag: */ PragFlag_NeedSchema,
222 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000223#endif
224#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000225 { /* zName: */ "index_info",
226 /* ePragTyp: */ PragTyp_INDEX_INFO,
227 /* ePragFlag: */ PragFlag_NeedSchema,
228 /* iArg: */ 0 },
229 { /* zName: */ "index_list",
230 /* ePragTyp: */ PragTyp_INDEX_LIST,
231 /* ePragFlag: */ PragFlag_NeedSchema,
232 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000233#endif
234#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
drhf63936e2013-10-03 14:08:07 +0000235 { /* zName: */ "integrity_check",
236 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
237 /* ePragFlag: */ PragFlag_NeedSchema,
238 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000239#endif
240#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000241 { /* zName: */ "journal_mode",
242 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
243 /* ePragFlag: */ PragFlag_NeedSchema,
244 /* iArg: */ 0 },
245 { /* zName: */ "journal_size_limit",
246 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
247 /* ePragFlag: */ 0,
248 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000249#endif
250#if defined(SQLITE_HAS_CODEC)
drhf63936e2013-10-03 14:08:07 +0000251 { /* zName: */ "key",
252 /* ePragTyp: */ PragTyp_KEY,
253 /* ePragFlag: */ 0,
254 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000255#endif
drhf63936e2013-10-03 14:08:07 +0000256 { /* zName: */ "legacy_file_format",
257 /* ePragTyp: */ PragTyp_FLAG,
258 /* ePragFlag: */ 0,
259 /* iArg: */ SQLITE_LegacyFileFmt },
drh9ccd8652013-09-13 16:36:46 +0000260#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
drhf63936e2013-10-03 14:08:07 +0000261 { /* zName: */ "lock_proxy_file",
262 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
263 /* ePragFlag: */ 0,
264 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000265#endif
266#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drhf63936e2013-10-03 14:08:07 +0000267 { /* zName: */ "lock_status",
268 /* ePragTyp: */ PragTyp_LOCK_STATUS,
269 /* ePragFlag: */ 0,
270 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000271#endif
272#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000273 { /* zName: */ "locking_mode",
274 /* ePragTyp: */ PragTyp_LOCKING_MODE,
275 /* ePragFlag: */ 0,
276 /* iArg: */ 0 },
277 { /* zName: */ "max_page_count",
278 /* ePragTyp: */ PragTyp_PAGE_COUNT,
279 /* ePragFlag: */ PragFlag_NeedSchema,
280 /* iArg: */ 0 },
281 { /* zName: */ "mmap_size",
282 /* ePragTyp: */ PragTyp_MMAP_SIZE,
283 /* ePragFlag: */ 0,
284 /* iArg: */ 0 },
285 { /* zName: */ "page_count",
286 /* ePragTyp: */ PragTyp_PAGE_COUNT,
287 /* ePragFlag: */ PragFlag_NeedSchema,
288 /* iArg: */ 0 },
289 { /* zName: */ "page_size",
290 /* ePragTyp: */ PragTyp_PAGE_SIZE,
291 /* ePragFlag: */ 0,
292 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000293#endif
294#if defined(SQLITE_DEBUG)
drhf63936e2013-10-03 14:08:07 +0000295 { /* zName: */ "parser_trace",
296 /* ePragTyp: */ PragTyp_PARSER_TRACE,
297 /* ePragFlag: */ 0,
298 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000299#endif
drhf63936e2013-10-03 14:08:07 +0000300 { /* zName: */ "query_only",
301 /* ePragTyp: */ PragTyp_FLAG,
302 /* ePragFlag: */ 0,
303 /* iArg: */ SQLITE_QueryOnly },
drh9ccd8652013-09-13 16:36:46 +0000304#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
drhf63936e2013-10-03 14:08:07 +0000305 { /* zName: */ "quick_check",
306 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
307 /* ePragFlag: */ PragFlag_NeedSchema,
308 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000309#endif
drhf63936e2013-10-03 14:08:07 +0000310 { /* zName: */ "read_uncommitted",
311 /* ePragTyp: */ PragTyp_FLAG,
312 /* ePragFlag: */ 0,
313 /* iArg: */ SQLITE_ReadUncommitted },
314 { /* zName: */ "recursive_triggers",
315 /* ePragTyp: */ PragTyp_FLAG,
316 /* ePragFlag: */ 0,
317 /* iArg: */ SQLITE_RecTriggers },
drh9ccd8652013-09-13 16:36:46 +0000318#if defined(SQLITE_HAS_CODEC)
drhf63936e2013-10-03 14:08:07 +0000319 { /* zName: */ "rekey",
320 /* ePragTyp: */ PragTyp_REKEY,
321 /* ePragFlag: */ 0,
322 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000323#endif
drhf63936e2013-10-03 14:08:07 +0000324 { /* zName: */ "reverse_unordered_selects",
325 /* ePragTyp: */ PragTyp_FLAG,
326 /* ePragFlag: */ 0,
327 /* iArg: */ SQLITE_ReverseOrder },
drh9ccd8652013-09-13 16:36:46 +0000328#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000329 { /* zName: */ "schema_version",
330 /* ePragTyp: */ PragTyp_HEADER_VALUE,
331 /* ePragFlag: */ 0,
332 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000333#endif
334#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000335 { /* zName: */ "secure_delete",
336 /* ePragTyp: */ PragTyp_SECURE_DELETE,
337 /* ePragFlag: */ 0,
338 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000339#endif
drhf63936e2013-10-03 14:08:07 +0000340 { /* zName: */ "short_column_names",
341 /* ePragTyp: */ PragTyp_FLAG,
342 /* ePragFlag: */ 0,
343 /* iArg: */ SQLITE_ShortColNames },
344 { /* zName: */ "shrink_memory",
345 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
346 /* ePragFlag: */ 0,
347 /* iArg: */ 0 },
348 { /* zName: */ "soft_heap_limit",
349 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
350 /* ePragFlag: */ 0,
351 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000352#if defined(SQLITE_DEBUG)
drhf63936e2013-10-03 14:08:07 +0000353 { /* zName: */ "sql_trace",
354 /* ePragTyp: */ PragTyp_FLAG,
355 /* ePragFlag: */ 0,
356 /* iArg: */ SQLITE_SqlTrace },
drh9ccd8652013-09-13 16:36:46 +0000357#endif
358#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000359 { /* zName: */ "synchronous",
360 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
361 /* ePragFlag: */ PragFlag_NeedSchema,
362 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000363#endif
364#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000365 { /* zName: */ "table_info",
366 /* ePragTyp: */ PragTyp_TABLE_INFO,
367 /* ePragFlag: */ PragFlag_NeedSchema,
368 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000369#endif
370#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000371 { /* zName: */ "temp_store",
372 /* ePragTyp: */ PragTyp_TEMP_STORE,
373 /* ePragFlag: */ 0,
374 /* iArg: */ 0 },
375 { /* zName: */ "temp_store_directory",
376 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
377 /* ePragFlag: */ 0,
378 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000379#endif
380#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
drhf63936e2013-10-03 14:08:07 +0000381 { /* zName: */ "user_version",
382 /* ePragTyp: */ PragTyp_HEADER_VALUE,
383 /* ePragFlag: */ 0,
384 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000385#endif
386#if defined(SQLITE_DEBUG)
drhf63936e2013-10-03 14:08:07 +0000387 { /* zName: */ "vdbe_addoptrace",
388 /* ePragTyp: */ PragTyp_FLAG,
389 /* ePragFlag: */ 0,
390 /* iArg: */ SQLITE_VdbeAddopTrace },
391 { /* zName: */ "vdbe_debug",
392 /* ePragTyp: */ PragTyp_FLAG,
393 /* ePragFlag: */ 0,
394 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
395 { /* zName: */ "vdbe_listing",
396 /* ePragTyp: */ PragTyp_FLAG,
397 /* ePragFlag: */ 0,
398 /* iArg: */ SQLITE_VdbeListing },
399 { /* zName: */ "vdbe_trace",
400 /* ePragTyp: */ PragTyp_FLAG,
401 /* ePragFlag: */ 0,
402 /* iArg: */ SQLITE_VdbeTrace },
drh9ccd8652013-09-13 16:36:46 +0000403#endif
404#if !defined(SQLITE_OMIT_WAL)
drhf63936e2013-10-03 14:08:07 +0000405 { /* zName: */ "wal_autocheckpoint",
406 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
407 /* ePragFlag: */ 0,
408 /* iArg: */ 0 },
409 { /* zName: */ "wal_checkpoint",
410 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
411 /* ePragFlag: */ PragFlag_NeedSchema,
412 /* iArg: */ 0 },
drh9ccd8652013-09-13 16:36:46 +0000413#endif
drhf63936e2013-10-03 14:08:07 +0000414 { /* zName: */ "writable_schema",
415 /* ePragTyp: */ PragTyp_FLAG,
416 /* ePragFlag: */ 0,
417 /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
drh9ccd8652013-09-13 16:36:46 +0000418};
drh55e85ca2013-09-13 21:01:56 +0000419/* Number of pragmas: 55 on by default, 66 total. */
drh9ccd8652013-09-13 16:36:46 +0000420/* End of the automatically generated pragma table.
421***************************************************************************/
422
drhc11d4f92003-04-06 21:08:24 +0000423/*
drhc11d4f92003-04-06 21:08:24 +0000424** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +0000425** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
drh908c0052012-01-30 18:40:55 +0000426** unrecognized string argument. The FULL option is disallowed
427** if the omitFull parameter it 1.
drhc11d4f92003-04-06 21:08:24 +0000428**
429** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +0000430** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +0000431** to support legacy SQL code. The safety level used to be boolean
432** and older scripts may have used numbers 0 for OFF and 1 for ON.
433*/
drh908c0052012-01-30 18:40:55 +0000434static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
drh722e95a2004-10-25 20:33:44 +0000435 /* 123456789 123456789 */
436 static const char zText[] = "onoffalseyestruefull";
437 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
438 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
439 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
440 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +0000441 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +0000442 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +0000443 }
drhea678832008-12-10 19:26:22 +0000444 n = sqlite3Strlen30(z);
drh908c0052012-01-30 18:40:55 +0000445 for(i=0; i<ArraySize(iLength)-omitFull; i++){
drh722e95a2004-10-25 20:33:44 +0000446 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
447 return iValue[i];
448 }
drhc11d4f92003-04-06 21:08:24 +0000449 }
drh908c0052012-01-30 18:40:55 +0000450 return dflt;
drhc11d4f92003-04-06 21:08:24 +0000451}
452
453/*
drh722e95a2004-10-25 20:33:44 +0000454** Interpret the given string as a boolean value.
455*/
drh38d9c612012-01-31 14:24:47 +0000456u8 sqlite3GetBoolean(const char *z, int dflt){
457 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:44 +0000458}
459
drhc7dc9bf2011-06-03 13:02:57 +0000460/* The sqlite3GetBoolean() function is used by other modules but the
461** remainder of this file is specific to PRAGMA processing. So omit
462** the rest of the file if PRAGMAs are omitted from the build.
463*/
464#if !defined(SQLITE_OMIT_PRAGMA)
465
danielk197741483462007-03-24 16:45:04 +0000466/*
467** Interpret the given string as a locking mode value.
468*/
469static int getLockingMode(const char *z){
470 if( z ){
471 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
472 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
473 }
474 return PAGER_LOCKINGMODE_QUERY;
475}
476
danielk1977dddbcdc2007-04-26 14:42:34 +0000477#ifndef SQLITE_OMIT_AUTOVACUUM
478/*
479** Interpret the given string as an auto-vacuum mode value.
480**
481** The following strings, "none", "full" and "incremental" are
482** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
483*/
484static int getAutoVacuum(const char *z){
485 int i;
486 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
487 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
488 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
drh60ac3f42010-11-23 18:59:27 +0000489 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +0000490 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +0000491}
492#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
493
danielk1977b84f96f2005-01-20 11:32:23 +0000494#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +0000495/*
drh90f5ecb2004-07-22 01:19:35 +0000496** Interpret the given string as a temp db location. Return 1 for file
497** backed temporary databases, 2 for the Red-Black tree in memory database
498** and 0 to use the compile-time default.
499*/
500static int getTempStore(const char *z){
501 if( z[0]>='0' && z[0]<='2' ){
502 return z[0] - '0';
503 }else if( sqlite3StrICmp(z, "file")==0 ){
504 return 1;
505 }else if( sqlite3StrICmp(z, "memory")==0 ){
506 return 2;
507 }else{
508 return 0;
509 }
510}
drhbf216272005-02-26 18:10:44 +0000511#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000512
drhbf216272005-02-26 18:10:44 +0000513#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000514/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000515** Invalidate temp storage, either when the temp storage is changed
516** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000517*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000518static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000519 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000520 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000521 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000522 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
523 "from within a transaction");
524 return SQLITE_ERROR;
525 }
526 sqlite3BtreeClose(db->aDb[1].pBt);
527 db->aDb[1].pBt = 0;
drh81028a42012-05-15 18:28:27 +0000528 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35 +0000529 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000530 return SQLITE_OK;
531}
drhbf216272005-02-26 18:10:44 +0000532#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000533
drhbf216272005-02-26 18:10:44 +0000534#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000535/*
536** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000537** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000538** or DEFAULT_TEMP_STORE pragmas.
539*/
540static int changeTempStorage(Parse *pParse, const char *zStorageType){
541 int ts = getTempStore(zStorageType);
542 sqlite3 *db = pParse->db;
543 if( db->temp_store==ts ) return SQLITE_OK;
544 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
545 return SQLITE_ERROR;
546 }
drh4f21c4a2008-12-10 22:15:00 +0000547 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000548 return SQLITE_OK;
549}
drhbf216272005-02-26 18:10:44 +0000550#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000551
552/*
553** Generate code to return a single integer value.
554*/
drh3c713642009-04-04 16:02:32 +0000555static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000556 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000557 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000558 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
559 if( pI64 ){
560 memcpy(pI64, &value, sizeof(value));
561 }
562 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000563 sqlite3VdbeSetNumCols(v, 1);
564 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000565 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000566}
567
drhd3605a42013-08-17 15:42:29 +0000568
569/*
570** Set the safety_level and pager flags for pager iDb. Or if iDb<0
571** set these values for all pagers.
572*/
573#ifndef SQLITE_OMIT_PAGER_PRAGMAS
574static void setAllPagerFlags(sqlite3 *db){
575 if( db->autoCommit ){
576 Db *pDb = db->aDb;
577 int n = db->nDb;
578 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
579 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
580 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
581 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
582 == PAGER_FLAGS_MASK );
583 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
584 while( (n--) > 0 ){
585 if( pDb->pBt ){
586 sqlite3BtreeSetPagerFlags(pDb->pBt,
587 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
588 }
589 pDb++;
590 }
591 }
592}
drhfeb56e02013-08-23 17:33:46 +0000593#else
594# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000595#endif
596
597
drhd2cb50b2009-01-09 21:41:17 +0000598/*
599** Return a human-readable name for a constraint resolution action.
600*/
danba9108b2009-09-22 07:13:42 +0000601#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000602static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000603 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000604 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000605 case OE_SetNull: zName = "SET NULL"; break;
606 case OE_SetDflt: zName = "SET DEFAULT"; break;
607 case OE_Cascade: zName = "CASCADE"; break;
608 case OE_Restrict: zName = "RESTRICT"; break;
609 default: zName = "NO ACTION";
610 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000611 }
drhd2cb50b2009-01-09 21:41:17 +0000612 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000613}
danba9108b2009-09-22 07:13:42 +0000614#endif
danielk197750af3e12008-10-10 17:47:21 +0000615
dane04dc882010-04-20 18:53:15 +0000616
617/*
618** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
619** defined in pager.h. This function returns the associated lowercase
620** journal-mode name.
621*/
622const char *sqlite3JournalModename(int eMode){
623 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000624 "delete", "persist", "off", "truncate", "memory"
625#ifndef SQLITE_OMIT_WAL
626 , "wal"
627#endif
dane04dc882010-04-20 18:53:15 +0000628 };
629 assert( PAGER_JOURNALMODE_DELETE==0 );
630 assert( PAGER_JOURNALMODE_PERSIST==1 );
631 assert( PAGER_JOURNALMODE_OFF==2 );
632 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
633 assert( PAGER_JOURNALMODE_MEMORY==4 );
634 assert( PAGER_JOURNALMODE_WAL==5 );
635 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
636
637 if( eMode==ArraySize(azModeName) ) return 0;
638 return azModeName[eMode];
639}
640
drh87223182004-02-21 14:00:29 +0000641/*
drhc11d4f92003-04-06 21:08:24 +0000642** Process a pragma statement.
643**
644** Pragmas are of this form:
645**
danielk197791cf71b2004-06-26 06:37:06 +0000646** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000647**
648** The identifier might also be a string. The value is a string, and
649** identifier, or a number. If minusFlag is true, then the value is
650** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000651**
652** If the left side is "database.id" then pId1 is the database name
653** and pId2 is the id. If the left side is just "id" then pId1 is the
654** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000655*/
danielk197791cf71b2004-06-26 06:37:06 +0000656void sqlite3Pragma(
657 Parse *pParse,
658 Token *pId1, /* First part of [database.]id field */
659 Token *pId2, /* Second part of [database.]id field, or NULL */
660 Token *pValue, /* Token for <value>, or NULL */
661 int minusFlag /* True if a '-' sign preceded <value> */
662){
663 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
664 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
665 const char *zDb = 0; /* The database name */
666 Token *pId; /* Pointer to <id> token */
drh3fa97302012-02-22 16:58:36 +0000667 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh9ccd8652013-09-13 16:36:46 +0000668 int iDb; /* Database index for <database> */
669 int lwr, upr, mid; /* Binary search bounds */
drh06fd5d62012-02-22 14:45:19 +0000670 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
671 sqlite3 *db = pParse->db; /* The database connection */
672 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000673 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh06fd5d62012-02-22 14:45:19 +0000674
drhc11d4f92003-04-06 21:08:24 +0000675 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000676 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000677 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000678
danielk197791cf71b2004-06-26 06:37:06 +0000679 /* Interpret the [database.] part of the pragma statement. iDb is the
680 ** index of the database this pragma is being applied to in db.aDb[]. */
681 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
682 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000683 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000684
danielk1977ddfb2f02006-02-17 12:25:14 +0000685 /* If the temp database has been explicitly named as part of the
686 ** pragma, make sure it is open.
687 */
688 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
689 return;
690 }
691
drh17435752007-08-16 04:30:38 +0000692 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000693 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000694 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000695 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000696 }else{
drh17435752007-08-16 04:30:38 +0000697 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000698 }
danielk197791cf71b2004-06-26 06:37:06 +0000699
drhd2cb50b2009-01-09 21:41:17 +0000700 assert( pId2 );
701 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000702 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000703 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000704 }
drh06fd5d62012-02-22 14:45:19 +0000705
706 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
707 ** connection. If it returns SQLITE_OK, then assume that the VFS
708 ** handled the pragma and generate a no-op prepared statement.
709 */
drh3fa97302012-02-22 16:58:36 +0000710 aFcntl[0] = 0;
711 aFcntl[1] = zLeft;
712 aFcntl[2] = zRight;
713 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000714 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000715 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
716 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000717 if( aFcntl[0] ){
718 int mem = ++pParse->nMem;
719 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
720 sqlite3VdbeSetNumCols(v, 1);
721 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
722 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
723 sqlite3_free(aFcntl[0]);
724 }
drh9ccd8652013-09-13 16:36:46 +0000725 goto pragma_out;
726 }
727 if( rc!=SQLITE_NOTFOUND ){
drh92c700d2012-02-22 19:56:17 +0000728 if( aFcntl[0] ){
729 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
730 sqlite3_free(aFcntl[0]);
731 }
732 pParse->nErr++;
733 pParse->rc = rc;
drh9ccd8652013-09-13 16:36:46 +0000734 goto pragma_out;
735 }
736
737 /* Locate the pragma in the lookup table */
738 lwr = 0;
739 upr = ArraySize(aPragmaNames)-1;
740 while( lwr<=upr ){
741 mid = (lwr+upr)/2;
742 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
743 if( rc==0 ) break;
744 if( rc<0 ){
745 upr = mid - 1;
746 }else{
747 lwr = mid + 1;
748 }
749 }
750 if( lwr>upr ) goto pragma_out;
751
drhf63936e2013-10-03 14:08:07 +0000752 /* Make sure the database schema is loaded if the pragma requires that */
753 if( (aPragmaNames[mid].mPragFlag & PragFlag_NeedSchema)!=0 ){
754 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
755 }
756
drh9ccd8652013-09-13 16:36:46 +0000757 /* Jump to the appropriate pragma handler */
758 switch( aPragmaNames[mid].ePragTyp ){
759
drhe73c9142011-11-09 16:12:24 +0000760#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000761 /*
drh90f5ecb2004-07-22 01:19:35 +0000762 ** PRAGMA [database.]default_cache_size
763 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000764 **
765 ** The first form reports the current persistent setting for the
766 ** page cache size. The value returned is the maximum number of
767 ** pages in the page cache. The second form sets both the current
768 ** page cache size value and the persistent page cache size value
769 ** stored in the database file.
770 **
drh93791ea2010-04-26 17:36:35 +0000771 ** Older versions of SQLite would set the default cache size to a
772 ** negative number to indicate synchronous=OFF. These days, synchronous
773 ** is always on by default regardless of the sign of the default cache
774 ** size. But continue to take the absolute value of the default cache
775 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000776 */
drh9ccd8652013-09-13 16:36:46 +0000777 case PragTyp_DEFAULT_CACHE_SIZE: {
drh57196282004-10-06 15:41:16 +0000778 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000779 { OP_Transaction, 0, 0, 0}, /* 0 */
780 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000781 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000782 { OP_Integer, 0, 2, 0},
783 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000784 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000785 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000786 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000787 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000788 };
drh905793e2004-02-21 13:31:09 +0000789 int addr;
drhfb982642007-08-30 01:19:59 +0000790 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000791 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000792 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000793 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000794 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000795 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000796 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000797 sqlite3VdbeChangeP1(v, addr+1, iDb);
798 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000799 }else{
drhd50ffc42011-03-08 02:38:28 +0000800 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000801 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000802 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000803 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000804 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000805 pDb->pSchema->cache_size = size;
806 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000807 }
drh9ccd8652013-09-13 16:36:46 +0000808 break;
809 }
drhe73c9142011-11-09 16:12:24 +0000810#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000811
drhe73c9142011-11-09 16:12:24 +0000812#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000813 /*
drh90f5ecb2004-07-22 01:19:35 +0000814 ** PRAGMA [database.]page_size
815 ** PRAGMA [database.]page_size=N
816 **
817 ** The first form reports the current setting for the
818 ** database page size in bytes. The second form sets the
819 ** database page size value. The value can only be set if
820 ** the database has not yet been created.
821 */
drh9ccd8652013-09-13 16:36:46 +0000822 case PragTyp_PAGE_SIZE: {
drh90f5ecb2004-07-22 01:19:35 +0000823 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000824 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000825 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000826 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000827 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000828 }else{
danielk1977992772c2007-08-30 10:07:38 +0000829 /* Malloc may fail when setting the page-size, as there is an internal
830 ** buffer that the pager module resizes using sqlite3_realloc().
831 */
drh60ac3f42010-11-23 18:59:27 +0000832 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000833 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000834 db->mallocFailed = 1;
835 }
drh90f5ecb2004-07-22 01:19:35 +0000836 }
drh9ccd8652013-09-13 16:36:46 +0000837 break;
838 }
danielk197741483462007-03-24 16:45:04 +0000839
840 /*
drh5b47efa2010-02-12 18:18:39 +0000841 ** PRAGMA [database.]secure_delete
842 ** PRAGMA [database.]secure_delete=ON/OFF
843 **
844 ** The first form reports the current setting for the
845 ** secure_delete flag. The second form changes the secure_delete
846 ** flag setting and reports thenew value.
847 */
drh9ccd8652013-09-13 16:36:46 +0000848 case PragTyp_SECURE_DELETE: {
drh5b47efa2010-02-12 18:18:39 +0000849 Btree *pBt = pDb->pBt;
850 int b = -1;
851 assert( pBt!=0 );
852 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000853 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000854 }
drhaf034ed2010-02-12 19:46:26 +0000855 if( pId2->n==0 && b>=0 ){
856 int ii;
857 for(ii=0; ii<db->nDb; ii++){
858 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
859 }
860 }
drh5b47efa2010-02-12 18:18:39 +0000861 b = sqlite3BtreeSecureDelete(pBt, b);
862 returnSingleInt(pParse, "secure_delete", b);
drh9ccd8652013-09-13 16:36:46 +0000863 break;
864 }
drh5b47efa2010-02-12 18:18:39 +0000865
866 /*
drh60ac3f42010-11-23 18:59:27 +0000867 ** PRAGMA [database.]max_page_count
868 ** PRAGMA [database.]max_page_count=N
869 **
870 ** The first form reports the current setting for the
871 ** maximum number of pages in the database file. The
872 ** second form attempts to change this setting. Both
873 ** forms return the current setting.
874 **
drhe73c9142011-11-09 16:12:24 +0000875 ** The absolute value of N is used. This is undocumented and might
876 ** change. The only purpose is to provide an easy way to test
877 ** the sqlite3AbsInt32() function.
878 **
danielk197759a93792008-05-15 17:48:20 +0000879 ** PRAGMA [database.]page_count
880 **
881 ** Return the number of pages in the specified database.
882 */
drh9ccd8652013-09-13 16:36:46 +0000883 case PragTyp_PAGE_COUNT: {
danielk197759a93792008-05-15 17:48:20 +0000884 int iReg;
danielk197759a93792008-05-15 17:48:20 +0000885 sqlite3CodeVerifySchema(pParse, iDb);
886 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000887 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000888 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
889 }else{
drhe73c9142011-11-09 16:12:24 +0000890 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
891 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000892 }
danielk197759a93792008-05-15 17:48:20 +0000893 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
894 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000895 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
drh9ccd8652013-09-13 16:36:46 +0000896 break;
897 }
danielk197759a93792008-05-15 17:48:20 +0000898
899 /*
danielk197741483462007-03-24 16:45:04 +0000900 ** PRAGMA [database.]locking_mode
901 ** PRAGMA [database.]locking_mode = (normal|exclusive)
902 */
drh9ccd8652013-09-13 16:36:46 +0000903 case PragTyp_LOCKING_MODE: {
danielk197741483462007-03-24 16:45:04 +0000904 const char *zRet = "normal";
905 int eMode = getLockingMode(zRight);
906
907 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
908 /* Simple "PRAGMA locking_mode;" statement. This is a query for
909 ** the current default locking mode (which may be different to
910 ** the locking-mode of the main database).
911 */
912 eMode = db->dfltLockMode;
913 }else{
914 Pager *pPager;
915 if( pId2->n==0 ){
916 /* This indicates that no database name was specified as part
917 ** of the PRAGMA command. In this case the locking-mode must be
918 ** set on all attached databases, as well as the main db file.
919 **
920 ** Also, the sqlite3.dfltLockMode variable is set so that
921 ** any subsequently attached databases also use the specified
922 ** locking mode.
923 */
924 int ii;
925 assert(pDb==&db->aDb[0]);
926 for(ii=2; ii<db->nDb; ii++){
927 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
928 sqlite3PagerLockingMode(pPager, eMode);
929 }
drh4f21c4a2008-12-10 22:15:00 +0000930 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000931 }
932 pPager = sqlite3BtreePager(pDb->pBt);
933 eMode = sqlite3PagerLockingMode(pPager, eMode);
934 }
935
drh9ccd8652013-09-13 16:36:46 +0000936 assert( eMode==PAGER_LOCKINGMODE_NORMAL
937 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
danielk197741483462007-03-24 16:45:04 +0000938 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
939 zRet = "exclusive";
940 }
941 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000942 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000943 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
944 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000945 break;
946 }
drh3b020132008-04-17 17:02:01 +0000947
948 /*
949 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000950 ** PRAGMA [database.]journal_mode =
951 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000952 */
drh9ccd8652013-09-13 16:36:46 +0000953 case PragTyp_JOURNAL_MODE: {
drhc6b2a0f2010-07-08 17:40:37 +0000954 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
955 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000956
957 sqlite3VdbeSetNumCols(v, 1);
958 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000959
960 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000961 /* If there is no "=MODE" part of the pragma, do a query for the
962 ** current mode */
drh3b020132008-04-17 17:02:01 +0000963 eMode = PAGER_JOURNALMODE_QUERY;
964 }else{
dane04dc882010-04-20 18:53:15 +0000965 const char *zMode;
drhea678832008-12-10 19:26:22 +0000966 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000967 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000968 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
969 }
970 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000971 /* If the "=MODE" part does not match any known journal mode,
972 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000973 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000974 }
975 }
drhc6b2a0f2010-07-08 17:40:37 +0000976 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
977 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
978 iDb = 0;
979 pId2->n = 1;
980 }
981 for(ii=db->nDb-1; ii>=0; ii--){
982 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
983 sqlite3VdbeUsesBtree(v, ii);
984 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000985 }
drh3b020132008-04-17 17:02:01 +0000986 }
drh3b020132008-04-17 17:02:01 +0000987 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
drh9ccd8652013-09-13 16:36:46 +0000988 break;
989 }
danielk1977b53e4962008-06-04 06:45:59 +0000990
991 /*
992 ** PRAGMA [database.]journal_size_limit
993 ** PRAGMA [database.]journal_size_limit=N
994 **
drha9e364f2009-01-13 20:14:15 +0000995 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000996 */
drh9ccd8652013-09-13 16:36:46 +0000997 case PragTyp_JOURNAL_SIZE_LIMIT: {
danielk1977b53e4962008-06-04 06:45:59 +0000998 Pager *pPager = sqlite3BtreePager(pDb->pBt);
999 i64 iLimit = -2;
1000 if( zRight ){
mistachkine98844f2013-08-24 00:59:24 +00001001 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +00001002 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +00001003 }
1004 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +00001005 returnSingleInt(pParse, "journal_size_limit", iLimit);
drh9ccd8652013-09-13 16:36:46 +00001006 break;
1007 }
danielk1977b53e4962008-06-04 06:45:59 +00001008
drh13d70422004-11-13 15:59:14 +00001009#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +00001010
1011 /*
danielk1977951af802004-11-05 15:45:09 +00001012 ** PRAGMA [database.]auto_vacuum
1013 ** PRAGMA [database.]auto_vacuum=N
1014 **
drha9e364f2009-01-13 20:14:15 +00001015 ** Get or set the value of the database 'auto-vacuum' parameter.
1016 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +00001017 */
1018#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001019 case PragTyp_AUTO_VACUUM: {
danielk1977951af802004-11-05 15:45:09 +00001020 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +00001021 assert( pBt!=0 );
danielk1977951af802004-11-05 15:45:09 +00001022 if( !zRight ){
drhf63936e2013-10-03 14:08:07 +00001023 returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
danielk1977951af802004-11-05 15:45:09 +00001024 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +00001025 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +00001026 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +00001027 db->nextAutovac = (u8)eAuto;
drhf63936e2013-10-03 14:08:07 +00001028 /* Call SetAutoVacuum() to set initialize the internal auto and
1029 ** incr-vacuum flags. This is required in case this connection
1030 ** creates the database file. It is important that it is created
1031 ** as an auto-vacuum capable db.
1032 */
1033 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
1034 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
1035 /* When setting the auto_vacuum mode to either "full" or
1036 ** "incremental", write the value of meta[6] in the database
1037 ** file. Before writing to meta[6], check that meta[3] indicates
1038 ** that this really is an auto-vacuum capable database.
danielk197727b1f952007-06-25 08:16:58 +00001039 */
drhf63936e2013-10-03 14:08:07 +00001040 static const VdbeOpList setMeta6[] = {
1041 { OP_Transaction, 0, 1, 0}, /* 0 */
1042 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
1043 { OP_If, 1, 0, 0}, /* 2 */
1044 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
1045 { OP_Integer, 0, 1, 0}, /* 4 */
1046 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
1047 };
1048 int iAddr;
1049 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
1050 sqlite3VdbeChangeP1(v, iAddr, iDb);
1051 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
1052 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
1053 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
1054 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
1055 sqlite3VdbeUsesBtree(v, iDb);
danielk1977dddbcdc2007-04-26 14:42:34 +00001056 }
danielk1977951af802004-11-05 15:45:09 +00001057 }
drh9ccd8652013-09-13 16:36:46 +00001058 break;
1059 }
danielk1977951af802004-11-05 15:45:09 +00001060#endif
1061
drhca5557f2007-05-04 18:30:40 +00001062 /*
1063 ** PRAGMA [database.]incremental_vacuum(N)
1064 **
1065 ** Do N steps of incremental vacuuming on a database.
1066 */
1067#ifndef SQLITE_OMIT_AUTOVACUUM
drh9ccd8652013-09-13 16:36:46 +00001068 case PragTyp_INCREMENTAL_VACUUM: {
drhca5557f2007-05-04 18:30:40 +00001069 int iLimit, addr;
1070 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
1071 iLimit = 0x7fffffff;
1072 }
1073 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +00001074 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +00001075 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +00001076 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +00001077 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +00001078 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +00001079 sqlite3VdbeJumpHere(v, addr);
drh9ccd8652013-09-13 16:36:46 +00001080 break;
1081 }
drhca5557f2007-05-04 18:30:40 +00001082#endif
1083
drh13d70422004-11-13 15:59:14 +00001084#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +00001085 /*
drh90f5ecb2004-07-22 01:19:35 +00001086 ** PRAGMA [database.]cache_size
1087 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +00001088 **
1089 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +00001090 ** page cache size. The second form sets the local
1091 ** page cache size value. If N is positive then that is the
1092 ** number of pages in the cache. If N is negative, then the
1093 ** number of pages is adjusted so that the cache uses -N kibibytes
1094 ** of memory.
drhc11d4f92003-04-06 21:08:24 +00001095 */
drh9ccd8652013-09-13 16:36:46 +00001096 case PragTyp_CACHE_SIZE: {
drh21206082011-04-04 18:22:02 +00001097 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +00001098 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +00001099 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001100 }else{
drh3b42abb2011-11-09 14:23:04 +00001101 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +00001102 pDb->pSchema->cache_size = size;
1103 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +00001104 }
drh9ccd8652013-09-13 16:36:46 +00001105 break;
1106 }
drhc11d4f92003-04-06 21:08:24 +00001107
1108 /*
drh9b4c59f2013-04-15 17:03:42 +00001109 ** PRAGMA [database.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +00001110 **
drh0d0614b2013-03-25 23:09:28 +00001111 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +00001112 ** used to limit the aggregate size of all memory mapped regions of the
1113 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +00001114 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +00001115 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +00001116 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +00001117 **
drh0d0614b2013-03-25 23:09:28 +00001118 ** This value is advisory. The underlying VFS is free to memory map
1119 ** as little or as much as it wants. Except, if N is set to 0 then the
1120 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +00001121 */
drh9ccd8652013-09-13 16:36:46 +00001122 case PragTyp_MMAP_SIZE: {
drh9b4c59f2013-04-15 17:03:42 +00001123 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +00001124#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +00001125 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +00001126 if( zRight ){
drha1f42c72013-04-01 22:38:06 +00001127 int ii;
mistachkine98844f2013-08-24 00:59:24 +00001128 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh9b4c59f2013-04-15 17:03:42 +00001129 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
1130 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +00001131 for(ii=db->nDb-1; ii>=0; ii--){
1132 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +00001133 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +00001134 }
1135 }
dan5d8a1372013-03-19 19:28:06 +00001136 }
drh9b4c59f2013-04-15 17:03:42 +00001137 sz = -1;
dan3719f5f2013-05-23 10:13:18 +00001138 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +00001139#else
dan3719f5f2013-05-23 10:13:18 +00001140 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +00001141 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +00001142#endif
dan3719f5f2013-05-23 10:13:18 +00001143 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:42 +00001144 returnSingleInt(pParse, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +00001145 }else if( rc!=SQLITE_NOTFOUND ){
1146 pParse->nErr++;
1147 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +00001148 }
drh9ccd8652013-09-13 16:36:46 +00001149 break;
1150 }
dan5d8a1372013-03-19 19:28:06 +00001151
1152 /*
drh90f5ecb2004-07-22 01:19:35 +00001153 ** PRAGMA temp_store
1154 ** PRAGMA temp_store = "default"|"memory"|"file"
1155 **
1156 ** Return or set the local value of the temp_store flag. Changing
1157 ** the local value does not make changes to the disk file and the default
1158 ** value will be restored the next time the database is opened.
1159 **
1160 ** Note that it is possible for the library compile-time options to
1161 ** override this setting
1162 */
drh9ccd8652013-09-13 16:36:46 +00001163 case PragTyp_TEMP_STORE: {
drh90f5ecb2004-07-22 01:19:35 +00001164 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001165 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +00001166 }else{
1167 changeTempStorage(pParse, zRight);
1168 }
drh9ccd8652013-09-13 16:36:46 +00001169 break;
1170 }
drh90f5ecb2004-07-22 01:19:35 +00001171
1172 /*
tpoindex9a09a3c2004-12-20 19:01:32 +00001173 ** PRAGMA temp_store_directory
1174 ** PRAGMA temp_store_directory = ""|"directory_name"
1175 **
1176 ** Return or set the local value of the temp_store_directory flag. Changing
1177 ** the value sets a specific directory to be used for temporary files.
1178 ** Setting to a null string reverts to the default temporary directory search.
1179 ** If temporary directory is changed, then invalidateTempStorage.
1180 **
1181 */
drh9ccd8652013-09-13 16:36:46 +00001182 case PragTyp_TEMP_STORE_DIRECTORY: {
tpoindex9a09a3c2004-12-20 19:01:32 +00001183 if( !zRight ){
1184 if( sqlite3_temp_directory ){
1185 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +00001186 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +00001187 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001188 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
1189 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +00001190 }
1191 }else{
drh78f82d12008-09-02 00:52:52 +00001192#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +00001193 if( zRight[0] ){
1194 int res;
danielk1977fab11272008-09-16 14:38:02 +00001195 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1196 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +00001197 sqlite3ErrorMsg(pParse, "not a writable directory");
1198 goto pragma_out;
1199 }
drh268283b2005-01-08 15:44:25 +00001200 }
danielk1977b06a0b62008-06-26 10:54:12 +00001201 if( SQLITE_TEMP_STORE==0
1202 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
1203 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +00001204 ){
1205 invalidateTempStorage(pParse);
1206 }
drh17435752007-08-16 04:30:38 +00001207 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +00001208 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +00001209 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +00001210 }else{
drh268283b2005-01-08 15:44:25 +00001211 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +00001212 }
drh78f82d12008-09-02 00:52:52 +00001213#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +00001214 }
drh9ccd8652013-09-13 16:36:46 +00001215 break;
1216 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001217
drhcc716452012-06-06 23:23:23 +00001218#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +00001219 /*
1220 ** PRAGMA data_store_directory
1221 ** PRAGMA data_store_directory = ""|"directory_name"
1222 **
1223 ** Return or set the local value of the data_store_directory flag. Changing
1224 ** the value sets a specific directory to be used for database files that
1225 ** were specified with a relative pathname. Setting to a null string reverts
1226 ** to the default database directory, which for database files specified with
1227 ** a relative path will probably be based on the current directory for the
1228 ** process. Database file specified with an absolute path are not impacted
1229 ** by this setting, regardless of its value.
1230 **
1231 */
drh9ccd8652013-09-13 16:36:46 +00001232 case PragTyp_DATA_STORE_DIRECTORY: {
mistachkina112d142012-03-14 00:44:01 +00001233 if( !zRight ){
1234 if( sqlite3_data_directory ){
1235 sqlite3VdbeSetNumCols(v, 1);
1236 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1237 "data_store_directory", SQLITE_STATIC);
1238 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
1239 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1240 }
1241 }else{
1242#ifndef SQLITE_OMIT_WSD
1243 if( zRight[0] ){
1244 int res;
1245 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1246 if( rc!=SQLITE_OK || res==0 ){
1247 sqlite3ErrorMsg(pParse, "not a writable directory");
1248 goto pragma_out;
1249 }
1250 }
1251 sqlite3_free(sqlite3_data_directory);
1252 if( zRight[0] ){
1253 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1254 }else{
1255 sqlite3_data_directory = 0;
1256 }
1257#endif /* SQLITE_OMIT_WSD */
1258 }
drh9ccd8652013-09-13 16:36:46 +00001259 break;
1260 }
drhcc716452012-06-06 23:23:23 +00001261#endif
mistachkina112d142012-03-14 00:44:01 +00001262
drhd2cb50b2009-01-09 21:41:17 +00001263#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +00001264 /*
drh9ccd8652013-09-13 16:36:46 +00001265 ** PRAGMA [database.]lock_proxy_file
1266 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
1267 **
1268 ** Return or set the value of the lock_proxy_file flag. Changing
1269 ** the value sets a specific file to be used for database access locks.
1270 **
1271 */
1272 case PragTyp_LOCK_PROXY_FILE: {
aswiftaebf4132008-11-21 00:10:35 +00001273 if( !zRight ){
1274 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1275 char *proxy_file_path = NULL;
1276 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +00001277 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +00001278 &proxy_file_path);
1279
1280 if( proxy_file_path ){
1281 sqlite3VdbeSetNumCols(v, 1);
1282 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
1283 "lock_proxy_file", SQLITE_STATIC);
1284 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
1285 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1286 }
1287 }else{
1288 Pager *pPager = sqlite3BtreePager(pDb->pBt);
1289 sqlite3_file *pFile = sqlite3PagerFile(pPager);
1290 int res;
1291 if( zRight[0] ){
1292 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1293 zRight);
1294 } else {
1295 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1296 NULL);
1297 }
1298 if( res!=SQLITE_OK ){
1299 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1300 goto pragma_out;
1301 }
1302 }
drh9ccd8652013-09-13 16:36:46 +00001303 break;
1304 }
drhd2cb50b2009-01-09 21:41:17 +00001305#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +00001306
1307 /*
drh90f5ecb2004-07-22 01:19:35 +00001308 ** PRAGMA [database.]synchronous
1309 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +00001310 **
1311 ** Return or set the local value of the synchronous flag. Changing
1312 ** the local value does not make changes to the disk file and the
1313 ** default value will be restored the next time the database is
1314 ** opened.
1315 */
drh9ccd8652013-09-13 16:36:46 +00001316 case PragTyp_SYNCHRONOUS: {
danielk197791cf71b2004-06-26 06:37:06 +00001317 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +00001318 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +00001319 }else{
danielk197791cf71b2004-06-26 06:37:06 +00001320 if( !db->autoCommit ){
1321 sqlite3ErrorMsg(pParse,
1322 "Safety level may not be changed inside a transaction");
1323 }else{
drh908c0052012-01-30 18:40:55 +00001324 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
drhd3605a42013-08-17 15:42:29 +00001325 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001326 }
drhc11d4f92003-04-06 21:08:24 +00001327 }
drh9ccd8652013-09-13 16:36:46 +00001328 break;
1329 }
drh13d70422004-11-13 15:59:14 +00001330#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001331
drhbf216272005-02-26 18:10:44 +00001332#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh9ccd8652013-09-13 16:36:46 +00001333 case PragTyp_FLAG: {
1334 if( zRight==0 ){
1335 returnSingleInt(pParse, aPragmaNames[mid].zName,
1336 (db->flags & aPragmaNames[mid].iArg)!=0 );
1337 }else{
1338 int mask = aPragmaNames[mid].iArg; /* Mask of bits to set or clear. */
1339 if( db->autoCommit==0 ){
1340 /* Foreign key support may not be enabled or disabled while not
1341 ** in auto-commit mode. */
1342 mask &= ~(SQLITE_ForeignKeys);
1343 }
1344
1345 if( sqlite3GetBoolean(zRight, 0) ){
1346 db->flags |= mask;
1347 }else{
1348 db->flags &= ~mask;
1349 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1350 }
1351
1352 /* Many of the flag-pragmas modify the code generated by the SQL
1353 ** compiler (eg. count_changes). So add an opcode to expire all
1354 ** compiled SQL statements after modifying a pragma value.
1355 */
1356 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
1357 setAllPagerFlags(db);
1358 }
1359 break;
1360 }
drhbf216272005-02-26 18:10:44 +00001361#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001362
drh13d70422004-11-13 15:59:14 +00001363#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001364 /*
1365 ** PRAGMA table_info(<table>)
1366 **
1367 ** Return a single row for each column of the named table. The columns of
1368 ** the returned data set are:
1369 **
1370 ** cid: Column id (numbered from left to right, starting at 0)
1371 ** name: Column name
1372 ** type: Column declaration type.
1373 ** notnull: True if 'NOT NULL' is part of column declaration
1374 ** dflt_value: The default value for the column, if any.
1375 */
drh9ccd8652013-09-13 16:36:46 +00001376 case PragTyp_TABLE_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001377 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001378 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001379 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001380 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001381 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001382 Column *pCol;
drh384b7fe2013-01-01 13:55:31 +00001383 Index *pPk;
1384 for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){}
drh9f6696a2006-02-09 16:52:23 +00001385 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +00001386 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001387 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001388 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1389 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1390 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1391 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1392 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1393 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001394 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001395 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001396 if( IsHiddenColumn(pCol) ){
1397 nHidden++;
1398 continue;
1399 }
drh2d401ab2008-01-10 23:50:11 +00001400 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1401 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1402 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +00001403 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +00001404 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001405 if( pCol->zDflt ){
1406 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001407 }else{
drh2d401ab2008-01-10 23:50:11 +00001408 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001409 }
drh384b7fe2013-01-01 13:55:31 +00001410 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1411 k = 0;
1412 }else if( pPk==0 ){
1413 k = 1;
1414 }else{
1415 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1416 }
1417 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001418 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001419 }
1420 }
drh9ccd8652013-09-13 16:36:46 +00001421 }
1422 break;
drhc11d4f92003-04-06 21:08:24 +00001423
drh9ccd8652013-09-13 16:36:46 +00001424 case PragTyp_INDEX_INFO: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001425 Index *pIdx;
1426 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001427 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001428 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001429 int i;
1430 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +00001431 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001432 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001433 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001434 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1435 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1436 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001437 for(i=0; i<pIdx->nColumn; i++){
1438 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001439 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1440 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +00001441 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +00001442 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1443 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001444 }
1445 }
drh9ccd8652013-09-13 16:36:46 +00001446 }
1447 break;
drhc11d4f92003-04-06 21:08:24 +00001448
drh9ccd8652013-09-13 16:36:46 +00001449 case PragTyp_INDEX_LIST: if( zRight ){
drhc11d4f92003-04-06 21:08:24 +00001450 Index *pIdx;
1451 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001452 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001453 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001454 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +00001455 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +00001456 if( pIdx ){
1457 int i = 0;
drhfdaac672013-10-04 15:30:21 +00001458 sqlite3VdbeSetNumCols(v, 4);
1459 pParse->nMem = 4;
drhc95e01d2013-02-14 16:16:05 +00001460 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001461 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1462 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1463 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
drhfdaac672013-10-04 15:30:21 +00001464 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "r", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001465 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +00001466 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1467 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1468 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
drh6919b072013-10-05 02:56:25 +00001469 sqlite3VdbeAddOp2(v, OP_Integer, (pIdx->iScanRatio*100+127)/128, 4);
drhfdaac672013-10-04 15:30:21 +00001470 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
danielk1977742f9472004-06-16 12:02:43 +00001471 ++i;
1472 pIdx = pIdx->pNext;
1473 }
drhc11d4f92003-04-06 21:08:24 +00001474 }
1475 }
drh9ccd8652013-09-13 16:36:46 +00001476 }
1477 break;
drhc11d4f92003-04-06 21:08:24 +00001478
drh9ccd8652013-09-13 16:36:46 +00001479 case PragTyp_DATABASE_LIST: {
drh13d70422004-11-13 15:59:14 +00001480 int i;
drh13d70422004-11-13 15:59:14 +00001481 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001482 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001483 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1484 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1485 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001486 for(i=0; i<db->nDb; i++){
1487 if( db->aDb[i].pBt==0 ) continue;
1488 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001489 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1490 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1491 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001492 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001493 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001494 }
drh9ccd8652013-09-13 16:36:46 +00001495 }
1496 break;
danielk197748af65a2005-02-09 03:20:37 +00001497
drh9ccd8652013-09-13 16:36:46 +00001498 case PragTyp_COLLATION_LIST: {
danielk197748af65a2005-02-09 03:20:37 +00001499 int i = 0;
1500 HashElem *p;
1501 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001502 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001503 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1504 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001505 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1506 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001507 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1508 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1509 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001510 }
drh9ccd8652013-09-13 16:36:46 +00001511 }
1512 break;
drh13d70422004-11-13 15:59:14 +00001513#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1514
drhb7f91642004-10-31 02:22:47 +00001515#ifndef SQLITE_OMIT_FOREIGN_KEY
drh9ccd8652013-09-13 16:36:46 +00001516 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
drh78100cc2003-08-23 22:40:53 +00001517 FKey *pFK;
1518 Table *pTab;
drh2783e4b2004-10-05 15:42:53 +00001519 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001520 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001521 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001522 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001523 if( pFK ){
1524 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001525 sqlite3VdbeSetNumCols(v, 8);
1526 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001527 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001528 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1529 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1530 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1531 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1532 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1533 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1534 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1535 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001536 while(pFK){
1537 int j;
1538 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001539 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001540 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1541 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001542 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1543 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1544 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1545 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001546 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001547 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001548 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1549 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1550 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1551 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001552 }
1553 ++i;
1554 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001555 }
drh78100cc2003-08-23 22:40:53 +00001556 }
1557 }
drh9ccd8652013-09-13 16:36:46 +00001558 }
1559 break;
drhb7f91642004-10-31 02:22:47 +00001560#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001561
drh6c5b9152012-12-17 16:46:37 +00001562#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001563#ifndef SQLITE_OMIT_TRIGGER
drh9ccd8652013-09-13 16:36:46 +00001564 case PragTyp_FOREIGN_KEY_CHECK: {
drh613028b2012-12-17 18:43:02 +00001565 FKey *pFK; /* A foreign key constraint */
1566 Table *pTab; /* Child table contain "REFERENCES" keyword */
1567 Table *pParent; /* Parent table that child points to */
1568 Index *pIdx; /* Index in the parent table */
1569 int i; /* Loop counter: Foreign key number for pTab */
1570 int j; /* Loop counter: Field of the foreign key */
1571 HashElem *k; /* Loop counter: Next table in schema */
1572 int x; /* result variable */
1573 int regResult; /* 3 registers to hold a result row */
1574 int regKey; /* Register to hold key for checking the FK */
1575 int regRow; /* Registers to hold a row from pTab */
1576 int addrTop; /* Top of a loop checking foreign keys */
1577 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001578 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001579
drh613028b2012-12-17 18:43:02 +00001580 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001581 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001582 regKey = ++pParse->nMem;
1583 regRow = ++pParse->nMem;
1584 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001585 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001586 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1587 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001588 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1589 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001590 sqlite3CodeVerifySchema(pParse, iDb);
1591 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1592 while( k ){
1593 if( zRight ){
1594 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1595 k = 0;
1596 }else{
1597 pTab = (Table*)sqliteHashData(k);
1598 k = sqliteHashNext(k);
1599 }
drh9148def2012-12-17 20:40:39 +00001600 if( pTab==0 || pTab->pFKey==0 ) continue;
1601 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001602 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001603 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001604 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1605 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001606 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1607 pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
1608 if( pParent==0 ) break;
1609 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001610 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001611 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001612 if( x==0 ){
1613 if( pIdx==0 ){
1614 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1615 }else{
1616 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
1617 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
1618 sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
1619 }
1620 }else{
drh613028b2012-12-17 18:43:02 +00001621 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001622 break;
1623 }
drh6c5b9152012-12-17 16:46:37 +00001624 }
drh613028b2012-12-17 18:43:02 +00001625 if( pFK ) break;
1626 if( pParse->nTab<i ) pParse->nTab = i;
1627 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
1628 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1629 pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
1630 assert( pParent!=0 );
1631 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001632 aiCols = 0;
1633 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
drh613028b2012-12-17 18:43:02 +00001634 assert( x==0 );
1635 addrOk = sqlite3VdbeMakeLabel(v);
1636 if( pIdx==0 ){
1637 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001638 assert( iKey>=0 && iKey<pTab->nCol );
1639 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001640 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1641 sqlite3ColumnDefault(v, pTab, iKey, regRow);
1642 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
1643 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1644 sqlite3VdbeCurrentAddr(v)+3);
drh6c5b9152012-12-17 16:46:37 +00001645 }else{
drh613028b2012-12-17 18:43:02 +00001646 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001647 }
drh613028b2012-12-17 18:43:02 +00001648 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
1649 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1650 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1651 }else{
1652 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001653 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
1654 aiCols ? aiCols[j] : pFK->aCol[0].iFrom, regRow+j);
drh613028b2012-12-17 18:43:02 +00001655 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
1656 }
1657 sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
1658 sqlite3VdbeChangeP4(v, -1,
1659 sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
1660 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh6c5b9152012-12-17 16:46:37 +00001661 }
drh613028b2012-12-17 18:43:02 +00001662 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001663 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1664 pFK->zTo, P4_TRANSIENT);
1665 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1666 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001667 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001668 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001669 }
drh613028b2012-12-17 18:43:02 +00001670 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
1671 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001672 }
drh9ccd8652013-09-13 16:36:46 +00001673 }
1674 break;
dan09ff9e12013-03-11 11:49:03 +00001675#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001676#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1677
drhc11d4f92003-04-06 21:08:24 +00001678#ifndef NDEBUG
drh9ccd8652013-09-13 16:36:46 +00001679 case PragTyp_PARSER_TRACE: {
drh55ef4d92005-08-14 01:20:37 +00001680 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001681 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001682 sqlite3ParserTrace(stderr, "parser: ");
1683 }else{
1684 sqlite3ParserTrace(0, 0);
1685 }
drhc11d4f92003-04-06 21:08:24 +00001686 }
drh9ccd8652013-09-13 16:36:46 +00001687 }
1688 break;
drhc11d4f92003-04-06 21:08:24 +00001689#endif
1690
drh55ef4d92005-08-14 01:20:37 +00001691 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1692 ** used will be case sensitive or not depending on the RHS.
1693 */
drh9ccd8652013-09-13 16:36:46 +00001694 case PragTyp_CASE_SENSITIVE_LIKE: {
drh55ef4d92005-08-14 01:20:37 +00001695 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001696 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001697 }
drh9ccd8652013-09-13 16:36:46 +00001698 }
1699 break;
drh55ef4d92005-08-14 01:20:37 +00001700
drh1dcdbc02007-01-27 02:24:54 +00001701#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1702# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1703#endif
1704
drhb7f91642004-10-31 02:22:47 +00001705#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001706 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001707 ** integrity_check designed to detect most database corruption
1708 ** without most of the overhead of a full integrity-check.
1709 */
drh9ccd8652013-09-13 16:36:46 +00001710 case PragTyp_INTEGRITY_CHECK: {
drh1dcdbc02007-01-27 02:24:54 +00001711 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001712
drhed717fe2003-06-15 23:42:24 +00001713 /* Code that appears at the end of the integrity check. If no error
1714 ** messages have been generated, output OK. Otherwise output the
1715 ** error message
1716 */
drh57196282004-10-06 15:41:16 +00001717 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001718 { OP_AddImm, 1, 0, 0}, /* 0 */
1719 { OP_IfNeg, 1, 0, 0}, /* 1 */
1720 { OP_String8, 0, 3, 0}, /* 2 */
1721 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001722 };
drhed717fe2003-06-15 23:42:24 +00001723
drhc5227312011-10-13 17:09:01 +00001724 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001725
dan5885e762012-07-16 10:06:12 +00001726 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1727 ** then iDb is set to the index of the database identified by <db>.
1728 ** In this case, the integrity of database iDb only is verified by
1729 ** the VDBE created below.
1730 **
1731 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1732 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1733 ** to -1 here, to indicate that the VDBE should verify the integrity
1734 ** of all attached databases. */
1735 assert( iDb>=0 );
1736 assert( iDb==0 || pId2->z );
1737 if( pId2->z==0 ) iDb = -1;
1738
drhed717fe2003-06-15 23:42:24 +00001739 /* Initialize the VDBE program */
drh2d401ab2008-01-10 23:50:11 +00001740 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001741 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001742 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001743
1744 /* Set the maximum error count */
1745 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1746 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001747 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001748 if( mxErr<=0 ){
1749 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1750 }
1751 }
drh2d401ab2008-01-10 23:50:11 +00001752 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001753
1754 /* Do an integrity check on each database file */
1755 for(i=0; i<db->nDb; i++){
1756 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001757 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001758 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001759
danielk197753c0f742005-03-29 03:10:59 +00001760 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001761 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001762
drh80242052004-06-09 00:48:12 +00001763 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001764 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001765 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001766 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001767
drhed717fe2003-06-15 23:42:24 +00001768 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001769 **
1770 ** Begin by filling registers 2, 3, ... with the root pages numbers
1771 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001772 */
dan5885e762012-07-16 10:06:12 +00001773 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001774 pTbls = &db->aDb[i].pSchema->tblHash;
1775 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001776 Table *pTab = sqliteHashData(x);
1777 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001778 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001779 cnt++;
1780 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001781 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001782 cnt++;
1783 }
1784 }
drh2d401ab2008-01-10 23:50:11 +00001785
1786 /* Make sure sufficient number of registers have been allocated */
drh66518ca2013-08-01 15:09:57 +00001787 pParse->nMem = MAX( pParse->nMem, cnt+7 );
drh2d401ab2008-01-10 23:50:11 +00001788
1789 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001790 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001791 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001792 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1793 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001794 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001795 P4_DYNAMIC);
drhe8e4af72012-09-21 00:04:28 +00001796 sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
danielk1977a7a8e142008-02-13 18:25:27 +00001797 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001798 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001799 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001800
1801 /* Make sure all the indices are constructed correctly.
1802 */
danielk197741c58b72007-12-29 13:39:19 +00001803 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001804 Table *pTab = sqliteHashData(x);
1805 Index *pIdx;
1806 int loopTop;
1807
1808 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001809 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001810 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001811 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001812 sqlite3ExprCacheClear(pParse);
drh290c1942004-08-21 17:54:45 +00001813 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh8a9789b2013-08-01 03:36:59 +00001814 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1815 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7+j); /* index entries counter */
1816 }
1817 pParse->nMem = MAX(pParse->nMem, 7+j);
1818 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0) + 1;
drhed717fe2003-06-15 23:42:24 +00001819 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhb2b9d3d2013-08-01 01:14:43 +00001820 int jmp2, jmp3;
drh91fc4a02009-11-12 20:39:03 +00001821 int r1;
drh57196282004-10-06 15:41:16 +00001822 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001823 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001824 { OP_String8, 0, 3, 0}, /* 1 */
1825 { OP_Rowid, 1, 4, 0},
1826 { OP_String8, 0, 5, 0}, /* 3 */
1827 { OP_String8, 0, 6, 0}, /* 4 */
1828 { OP_Concat, 4, 3, 3},
1829 { OP_Concat, 5, 3, 3},
1830 { OP_Concat, 6, 3, 3},
1831 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001832 { OP_IfPos, 1, 0, 0}, /* 9 */
1833 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001834 };
drhb2b9d3d2013-08-01 01:14:43 +00001835 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0, &jmp3);
drh8a9789b2013-08-01 03:36:59 +00001836 sqlite3VdbeAddOp2(v, OP_AddImm, 7+j, 1); /* increment entry count */
drh91fc4a02009-11-12 20:39:03 +00001837 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001838 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001839 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1840 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001841 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
drhbb8a2792008-03-19 00:21:30 +00001842 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001843 sqlite3VdbeJumpHere(v, jmp2);
drhb2b9d3d2013-08-01 01:14:43 +00001844 sqlite3VdbeResolveLabel(v, jmp3);
drhed717fe2003-06-15 23:42:24 +00001845 }
drh8a9789b2013-08-01 03:36:59 +00001846 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop);
1847 sqlite3VdbeJumpHere(v, loopTop-1);
1848#ifndef SQLITE_OMIT_BTREECOUNT
1849 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
drh24003452008-01-03 01:28:59 +00001850 "wrong # of entries in index ", P4_STATIC);
drh8a9789b2013-08-01 03:36:59 +00001851 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1852 addr = sqlite3VdbeCurrentAddr(v);
1853 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
1854 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1855 sqlite3VdbeAddOp2(v, OP_Count, j+2, 3);
1856 sqlite3VdbeAddOp3(v, OP_Eq, 7+j, addr+8, 3);
1857 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1858 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
1859 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1860 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001861 }
drh8a9789b2013-08-01 03:36:59 +00001862#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001863 }
1864 }
danielk19774adee202004-05-08 08:23:19 +00001865 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001866 sqlite3VdbeChangeP2(v, addr, -mxErr);
1867 sqlite3VdbeJumpHere(v, addr+1);
1868 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drh9ccd8652013-09-13 16:36:46 +00001869 }
1870 break;
drhb7f91642004-10-31 02:22:47 +00001871#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1872
drh13d70422004-11-13 15:59:14 +00001873#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001874 /*
1875 ** PRAGMA encoding
1876 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1877 **
drh85b623f2007-12-13 21:54:09 +00001878 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001879 ** database. If the database is not initialized, it is initialized now.
1880 **
1881 ** The second form of this pragma is a no-op if the main database file
1882 ** has not already been initialized. In this case it sets the default
1883 ** encoding that will be used for the main database file if a new file
1884 ** is created. If an existing main database file is opened, then the
1885 ** default text encoding for the existing database is used.
1886 **
1887 ** In all cases new databases created using the ATTACH command are
1888 ** created to use the same default text encoding as the main database. If
1889 ** the main database has not been initialized and/or created when ATTACH
1890 ** is executed, this is done before the ATTACH operation.
1891 **
1892 ** In the second form this pragma sets the text encoding to be used in
1893 ** new database files created using this database handle. It is only
1894 ** useful if invoked immediately after the main database i
1895 */
drh9ccd8652013-09-13 16:36:46 +00001896 case PragTyp_ENCODING: {
drh0f7eb612006-08-08 13:51:43 +00001897 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001898 char *zName;
1899 u8 enc;
1900 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001901 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001902 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1903 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1904 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001905 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001906 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001907 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1908 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001909 { 0, 0 }
1910 };
drh0f7eb612006-08-08 13:51:43 +00001911 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001912 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001913 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001914 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001915 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001916 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001917 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1918 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1919 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1920 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001921 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001922 }else{ /* "PRAGMA encoding = XXX" */
1923 /* Only change the value of sqlite.enc if the database handle is not
1924 ** initialized. If the main database exists, the new sqlite.enc value
1925 ** will be overwritten when the schema is next loaded. If it does not
1926 ** already exists, it will be created to use the new encoding value.
1927 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001928 if(
1929 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1930 DbHasProperty(db, 0, DB_Empty)
1931 ){
danielk19778e227872004-06-07 07:52:17 +00001932 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1933 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001934 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001935 break;
1936 }
1937 }
1938 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001939 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001940 }
1941 }
1942 }
drh9ccd8652013-09-13 16:36:46 +00001943 }
1944 break;
drh13d70422004-11-13 15:59:14 +00001945#endif /* SQLITE_OMIT_UTF16 */
1946
1947#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001948 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001949 ** PRAGMA [database.]schema_version
1950 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001951 **
danielk1977b92b70b2004-11-12 16:11:59 +00001952 ** PRAGMA [database.]user_version
1953 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001954 **
drh4ee09b42013-05-01 19:49:27 +00001955 ** PRAGMA [database.]freelist_count = <integer>
1956 **
1957 ** PRAGMA [database.]application_id
1958 ** PRAGMA [database.]application_id = <integer>
1959 **
danielk1977b92b70b2004-11-12 16:11:59 +00001960 ** The pragma's schema_version and user_version are used to set or get
1961 ** the value of the schema-version and user-version, respectively. Both
1962 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001963 ** stored in the database header.
1964 **
1965 ** The schema-cookie is usually only manipulated internally by SQLite. It
1966 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001967 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001968 ** SQLite each time a query is executed to ensure that the internal cache
1969 ** of the schema used when compiling the SQL query matches the schema of
1970 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001971 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1972 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001973 ** crashes or database corruption. Use with caution!
1974 **
danielk1977b92b70b2004-11-12 16:11:59 +00001975 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001976 ** applications for any purpose.
1977 */
drh9ccd8652013-09-13 16:36:46 +00001978 case PragTyp_HEADER_VALUE: {
danielk19770d19f7a2009-06-03 11:25:07 +00001979 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001980 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001981 switch( zLeft[0] ){
drh4ee09b42013-05-01 19:49:27 +00001982 case 'a': case 'A':
1983 iCookie = BTREE_APPLICATION_ID;
1984 break;
danielk1977180b56a2007-06-24 08:00:42 +00001985 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001986 iCookie = BTREE_FREE_PAGE_COUNT;
1987 break;
1988 case 's': case 'S':
1989 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001990 break;
1991 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001992 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001993 break;
danielk1977dae24952004-11-11 05:10:43 +00001994 }
1995
danielk19770d19f7a2009-06-03 11:25:07 +00001996 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001997 /* Write the specified cookie value */
1998 static const VdbeOpList setCookie[] = {
1999 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00002000 { OP_Integer, 0, 1, 0}, /* 1 */
2001 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00002002 };
2003 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
2004 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00002005 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00002006 sqlite3VdbeChangeP1(v, addr+2, iDb);
2007 sqlite3VdbeChangeP2(v, addr+2, iCookie);
2008 }else{
2009 /* Read the specified cookie value */
2010 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00002011 { OP_Transaction, 0, 0, 0}, /* 0 */
2012 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00002013 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00002014 };
2015 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
2016 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00002017 sqlite3VdbeChangeP1(v, addr+1, iDb);
2018 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00002019 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00002020 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00002021 }
drh9ccd8652013-09-13 16:36:46 +00002022 }
2023 break;
drh13d70422004-11-13 15:59:14 +00002024#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00002025
shanehdc97a8c2010-02-23 20:08:35 +00002026#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2027 /*
2028 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00002029 **
drh71caabf2010-02-26 15:39:24 +00002030 ** Return the names of all compile-time options used in this build,
2031 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00002032 */
drh9ccd8652013-09-13 16:36:46 +00002033 case PragTyp_COMPILE_OPTIONS: {
shanehdc97a8c2010-02-23 20:08:35 +00002034 int i = 0;
2035 const char *zOpt;
2036 sqlite3VdbeSetNumCols(v, 1);
2037 pParse->nMem = 1;
2038 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
2039 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
2040 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
2041 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2042 }
drh9ccd8652013-09-13 16:36:46 +00002043 }
2044 break;
shanehdc97a8c2010-02-23 20:08:35 +00002045#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2046
dan5cf53532010-05-01 16:40:20 +00002047#ifndef SQLITE_OMIT_WAL
2048 /*
dancdc1f042010-11-18 12:11:05 +00002049 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00002050 **
2051 ** Checkpoint the database.
2052 */
drh9ccd8652013-09-13 16:36:46 +00002053 case PragTyp_WAL_CHECKPOINT: {
dana58f26f2010-11-16 18:56:51 +00002054 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00002055 int eMode = SQLITE_CHECKPOINT_PASSIVE;
2056 if( zRight ){
2057 if( sqlite3StrICmp(zRight, "full")==0 ){
2058 eMode = SQLITE_CHECKPOINT_FULL;
2059 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2060 eMode = SQLITE_CHECKPOINT_RESTART;
2061 }
2062 }
dancdc1f042010-11-18 12:11:05 +00002063 sqlite3VdbeSetNumCols(v, 3);
2064 pParse->nMem = 3;
2065 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
2066 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
2067 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
2068
drh30aa3b92011-02-07 23:56:01 +00002069 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00002070 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh9ccd8652013-09-13 16:36:46 +00002071 }
2072 break;
dan5a299f92010-05-03 11:05:08 +00002073
2074 /*
2075 ** PRAGMA wal_autocheckpoint
2076 ** PRAGMA wal_autocheckpoint = N
2077 **
2078 ** Configure a database connection to automatically checkpoint a database
2079 ** after accumulating N frames in the log. Or query for the current value
2080 ** of N.
2081 */
drh9ccd8652013-09-13 16:36:46 +00002082 case PragTyp_WAL_AUTOCHECKPOINT: {
dan5a299f92010-05-03 11:05:08 +00002083 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00002084 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00002085 }
drhb033d8b2010-05-03 13:37:30 +00002086 returnSingleInt(pParse, "wal_autocheckpoint",
2087 db->xWalCallback==sqlite3WalDefaultHook ?
2088 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
drh9ccd8652013-09-13 16:36:46 +00002089 }
2090 break;
dan5cf53532010-05-01 16:40:20 +00002091#endif
dan7c246102010-04-12 19:00:29 +00002092
drh09419b42011-11-16 19:29:17 +00002093 /*
2094 ** PRAGMA shrink_memory
2095 **
2096 ** This pragma attempts to free as much memory as possible from the
2097 ** current database connection.
2098 */
drh9ccd8652013-09-13 16:36:46 +00002099 case PragTyp_SHRINK_MEMORY: {
drh09419b42011-11-16 19:29:17 +00002100 sqlite3_db_release_memory(db);
drh9ccd8652013-09-13 16:36:46 +00002101 break;
2102 }
drh09419b42011-11-16 19:29:17 +00002103
drhf3603962012-09-07 16:46:59 +00002104 /*
2105 ** PRAGMA busy_timeout
2106 ** PRAGMA busy_timeout = N
2107 **
2108 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00002109 ** if one is set. If no busy handler or a different busy handler is set
2110 ** then 0 is returned. Setting the busy_timeout to 0 or negative
2111 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00002112 */
drhd49c3582013-09-13 19:00:06 +00002113 /*case PragTyp_BUSY_TIMEOUT*/ default: {
2114 assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
drhf3603962012-09-07 16:46:59 +00002115 if( zRight ){
2116 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2117 }
2118 returnSingleInt(pParse, "timeout", db->busyTimeout);
drh9ccd8652013-09-13 16:36:46 +00002119 break;
2120 }
drhf3603962012-09-07 16:46:59 +00002121
drh55e85ca2013-09-13 21:01:56 +00002122 /*
2123 ** PRAGMA soft_heap_limit
2124 ** PRAGMA soft_heap_limit = N
2125 **
2126 ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
2127 ** use -1.
2128 */
2129 case PragTyp_SOFT_HEAP_LIMIT: {
2130 sqlite3_int64 N;
2131 if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
2132 sqlite3_soft_heap_limit64(N);
2133 }
2134 returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
2135 break;
2136 }
2137
dougcurrie81c95ef2004-06-18 23:21:47 +00002138#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00002139 /*
2140 ** Report the current state of file logs for all databases
2141 */
drh9ccd8652013-09-13 16:36:46 +00002142 case PragTyp_LOCK_STATUS: {
drh57196282004-10-06 15:41:16 +00002143 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00002144 "unlocked", "shared", "reserved", "pending", "exclusive"
2145 };
2146 int i;
drh89ac8c12004-06-09 14:17:20 +00002147 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00002148 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00002149 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
2150 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002151 for(i=0; i<db->nDb; i++){
2152 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00002153 const char *zState = "unknown";
2154 int j;
drh89ac8c12004-06-09 14:17:20 +00002155 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00002156 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00002157 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00002158 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00002159 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00002160 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00002161 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
2162 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00002163 }
drh2d401ab2008-01-10 23:50:11 +00002164 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
2165 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00002166 }
drh9ccd8652013-09-13 16:36:46 +00002167 break;
2168 }
drh89ac8c12004-06-09 14:17:20 +00002169#endif
2170
shanehad9f9f62010-02-17 17:48:46 +00002171#ifdef SQLITE_HAS_CODEC
drh9ccd8652013-09-13 16:36:46 +00002172 case PragTyp_KEY: {
2173 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2174 break;
2175 }
2176 case PragTyp_REKEY: {
2177 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
2178 break;
2179 }
2180 case PragTyp_HEXKEY: {
2181 if( zRight ){
2182 int i, h1, h2;
2183 char zKey[40];
2184 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
2185 h1 += 9*(1&(h1>>6));
2186 h2 += 9*(1&(h2>>6));
2187 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
2188 }
2189 if( (zLeft[3] & 0xf)==0xb ){
2190 sqlite3_key_v2(db, zDb, zKey, i/2);
2191 }else{
2192 sqlite3_rekey_v2(db, zDb, zKey, i/2);
2193 }
drhd2cb50b2009-01-09 21:41:17 +00002194 }
drh9ccd8652013-09-13 16:36:46 +00002195 break;
2196 }
drh3c4f2a42005-12-08 18:12:56 +00002197#endif
shanehad9f9f62010-02-17 17:48:46 +00002198#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh9ccd8652013-09-13 16:36:46 +00002199 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
shanehad9f9f62010-02-17 17:48:46 +00002200#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00002201 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002202 sqlite3_activate_see(&zRight[4]);
2203 }
2204#endif
2205#ifdef SQLITE_ENABLE_CEROD
2206 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00002207 sqlite3_activate_cerod(&zRight[6]);
2208 }
2209#endif
drh9ccd8652013-09-13 16:36:46 +00002210 }
2211 break;
drh21e2cab2006-09-25 18:01:57 +00002212#endif
drh3c4f2a42005-12-08 18:12:56 +00002213
drh9ccd8652013-09-13 16:36:46 +00002214 } /* End of the PRAGMA switch */
danielk1977a21c6b62005-01-24 10:25:59 +00002215
danielk1977e0048402004-06-15 16:51:01 +00002216pragma_out:
drh633e6d52008-07-28 19:34:53 +00002217 sqlite3DbFree(db, zLeft);
2218 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00002219}
drh13d70422004-11-13 15:59:14 +00002220
drh8bfdf722009-06-19 14:06:03 +00002221#endif /* SQLITE_OMIT_PRAGMA */