blob: d4cf597ff8db03046273597531adfdd02fe01ede [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
16/*
drhc11d4f92003-04-06 21:08:24 +000017** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000018** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
drh908c0052012-01-30 18:40:55 +000019** unrecognized string argument. The FULL option is disallowed
20** if the omitFull parameter it 1.
drhc11d4f92003-04-06 21:08:24 +000021**
22** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000023** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000024** to support legacy SQL code. The safety level used to be boolean
25** and older scripts may have used numbers 0 for OFF and 1 for ON.
26*/
drh908c0052012-01-30 18:40:55 +000027static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
drh722e95a2004-10-25 20:33:44 +000028 /* 123456789 123456789 */
29 static const char zText[] = "onoffalseyestruefull";
30 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
31 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
32 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
33 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000034 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +000035 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +000036 }
drhea678832008-12-10 19:26:22 +000037 n = sqlite3Strlen30(z);
drh908c0052012-01-30 18:40:55 +000038 for(i=0; i<ArraySize(iLength)-omitFull; i++){
drh722e95a2004-10-25 20:33:44 +000039 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
40 return iValue[i];
41 }
drhc11d4f92003-04-06 21:08:24 +000042 }
drh908c0052012-01-30 18:40:55 +000043 return dflt;
drhc11d4f92003-04-06 21:08:24 +000044}
45
46/*
drh722e95a2004-10-25 20:33:44 +000047** Interpret the given string as a boolean value.
48*/
drh38d9c612012-01-31 14:24:47 +000049u8 sqlite3GetBoolean(const char *z, int dflt){
50 return getSafetyLevel(z,1,dflt)!=0;
drh722e95a2004-10-25 20:33:44 +000051}
52
drhc7dc9bf2011-06-03 13:02:57 +000053/* The sqlite3GetBoolean() function is used by other modules but the
54** remainder of this file is specific to PRAGMA processing. So omit
55** the rest of the file if PRAGMAs are omitted from the build.
56*/
57#if !defined(SQLITE_OMIT_PRAGMA)
58
danielk197741483462007-03-24 16:45:04 +000059/*
60** Interpret the given string as a locking mode value.
61*/
62static int getLockingMode(const char *z){
63 if( z ){
64 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
65 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
66 }
67 return PAGER_LOCKINGMODE_QUERY;
68}
69
danielk1977dddbcdc2007-04-26 14:42:34 +000070#ifndef SQLITE_OMIT_AUTOVACUUM
71/*
72** Interpret the given string as an auto-vacuum mode value.
73**
74** The following strings, "none", "full" and "incremental" are
75** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
76*/
77static int getAutoVacuum(const char *z){
78 int i;
79 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
80 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
81 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
drh60ac3f42010-11-23 18:59:27 +000082 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +000083 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +000084}
85#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
86
danielk1977b84f96f2005-01-20 11:32:23 +000087#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000088/*
drh90f5ecb2004-07-22 01:19:35 +000089** Interpret the given string as a temp db location. Return 1 for file
90** backed temporary databases, 2 for the Red-Black tree in memory database
91** and 0 to use the compile-time default.
92*/
93static int getTempStore(const char *z){
94 if( z[0]>='0' && z[0]<='2' ){
95 return z[0] - '0';
96 }else if( sqlite3StrICmp(z, "file")==0 ){
97 return 1;
98 }else if( sqlite3StrICmp(z, "memory")==0 ){
99 return 2;
100 }else{
101 return 0;
102 }
103}
drhbf216272005-02-26 18:10:44 +0000104#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000105
drhbf216272005-02-26 18:10:44 +0000106#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000107/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000108** Invalidate temp storage, either when the temp storage is changed
109** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000110*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000111static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000112 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000113 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000114 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000115 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
116 "from within a transaction");
117 return SQLITE_ERROR;
118 }
119 sqlite3BtreeClose(db->aDb[1].pBt);
120 db->aDb[1].pBt = 0;
drh81028a42012-05-15 18:28:27 +0000121 sqlite3ResetAllSchemasOfConnection(db);
drh90f5ecb2004-07-22 01:19:35 +0000122 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000123 return SQLITE_OK;
124}
drhbf216272005-02-26 18:10:44 +0000125#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000126
drhbf216272005-02-26 18:10:44 +0000127#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000128/*
129** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000130** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000131** or DEFAULT_TEMP_STORE pragmas.
132*/
133static int changeTempStorage(Parse *pParse, const char *zStorageType){
134 int ts = getTempStore(zStorageType);
135 sqlite3 *db = pParse->db;
136 if( db->temp_store==ts ) return SQLITE_OK;
137 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
138 return SQLITE_ERROR;
139 }
drh4f21c4a2008-12-10 22:15:00 +0000140 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000141 return SQLITE_OK;
142}
drhbf216272005-02-26 18:10:44 +0000143#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000144
145/*
146** Generate code to return a single integer value.
147*/
drh3c713642009-04-04 16:02:32 +0000148static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000149 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000150 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000151 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
152 if( pI64 ){
153 memcpy(pI64, &value, sizeof(value));
154 }
155 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000156 sqlite3VdbeSetNumCols(v, 1);
157 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000158 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000159}
160
drhd3605a42013-08-17 15:42:29 +0000161
162/*
163** Set the safety_level and pager flags for pager iDb. Or if iDb<0
164** set these values for all pagers.
165*/
166#ifndef SQLITE_OMIT_PAGER_PRAGMAS
167static void setAllPagerFlags(sqlite3 *db){
168 if( db->autoCommit ){
169 Db *pDb = db->aDb;
170 int n = db->nDb;
171 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
172 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
173 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
174 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
175 == PAGER_FLAGS_MASK );
176 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
177 while( (n--) > 0 ){
178 if( pDb->pBt ){
179 sqlite3BtreeSetPagerFlags(pDb->pBt,
180 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
181 }
182 pDb++;
183 }
184 }
185}
drhfeb56e02013-08-23 17:33:46 +0000186#else
187# define setAllPagerFlags(X) /* no-op */
drhd3605a42013-08-17 15:42:29 +0000188#endif
189
190
drhbf216272005-02-26 18:10:44 +0000191#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000192/*
drh87223182004-02-21 14:00:29 +0000193** Check to see if zRight and zLeft refer to a pragma that queries
194** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
195** Also, implement the pragma.
196*/
197static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000198 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000199 const char *zName; /* Name of the pragma */
200 int mask; /* Mask for the db->flags value */
201 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000202 { "full_column_names", SQLITE_FullColNames },
203 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000204 { "count_changes", SQLITE_CountRows },
205 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000206 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000207 { "fullfsync", SQLITE_FullFSync },
drhc97d8462010-11-19 18:23:35 +0000208 { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
drh40c39412013-08-16 20:42:20 +0000209 { "cache_spill", SQLITE_CacheSpill },
drh699b3d42009-02-23 16:52:07 +0000210 { "reverse_unordered_selects", SQLITE_ReverseOrder },
drh13447bf2013-07-10 13:33:49 +0000211 { "query_only", SQLITE_QueryOnly },
drhc6339082010-04-07 16:54:58 +0000212#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
213 { "automatic_index", SQLITE_AutoIndex },
214#endif
drhcf1023c2007-05-08 20:59:49 +0000215#ifdef SQLITE_DEBUG
216 { "sql_trace", SQLITE_SqlTrace },
217 { "vdbe_listing", SQLITE_VdbeListing },
218 { "vdbe_trace", SQLITE_VdbeTrace },
drhe0962052013-01-29 19:14:31 +0000219 { "vdbe_addoptrace", SQLITE_VdbeAddopTrace},
220 { "vdbe_debug", SQLITE_SqlTrace | SQLITE_VdbeListing
221 | SQLITE_VdbeTrace },
drhcf1023c2007-05-08 20:59:49 +0000222#endif
drh0cd2d4c2005-11-03 02:15:02 +0000223#ifndef SQLITE_OMIT_CHECK
224 { "ignore_check_constraints", SQLITE_IgnoreChecks },
225#endif
drh722e95a2004-10-25 20:33:44 +0000226 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000227 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
danielk1977da184232006-01-05 11:34:32 +0000228
229 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
230 ** flag if there are any active statements. */
231 { "read_uncommitted", SQLITE_ReadUncommitted },
drh648e2642013-07-11 15:03:32 +0000232 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000233
dan75cbd982009-09-21 16:06:03 +0000234 /* This flag may only be set if both foreign-key and trigger support
235 ** are present in the build. */
236#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
drh648e2642013-07-11 15:03:32 +0000237 { "foreign_keys", SQLITE_ForeignKeys },
238 { "defer_foreign_keys", SQLITE_DeferFKs },
dan75cbd982009-09-21 16:06:03 +0000239#endif
drh87223182004-02-21 14:00:29 +0000240 };
241 int i;
drh722e95a2004-10-25 20:33:44 +0000242 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000243 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000244 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000245 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000246 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000247 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000248 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
249 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000250 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000251 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000252 }else{
dan75cbd982009-09-21 16:06:03 +0000253 int mask = p->mask; /* Mask of bits to set or clear. */
254 if( db->autoCommit==0 ){
255 /* Foreign key support may not be enabled or disabled while not
256 ** in auto-commit mode. */
257 mask &= ~(SQLITE_ForeignKeys);
258 }
259
drh38d9c612012-01-31 14:24:47 +0000260 if( sqlite3GetBoolean(zRight, 0) ){
dan75cbd982009-09-21 16:06:03 +0000261 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000262 }else{
dan75cbd982009-09-21 16:06:03 +0000263 db->flags &= ~mask;
drh648e2642013-07-11 15:03:32 +0000264 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
danielk1977a21c6b62005-01-24 10:25:59 +0000265 }
danielk19778e556522007-11-13 10:30:24 +0000266
267 /* Many of the flag-pragmas modify the code generated by the SQL
268 ** compiler (eg. count_changes). So add an opcode to expire all
269 ** compiled SQL statements after modifying a pragma value.
270 */
drh66a51672008-01-03 00:01:23 +0000271 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000272 }
drh87223182004-02-21 14:00:29 +0000273 }
danielk19778e556522007-11-13 10:30:24 +0000274
drh87223182004-02-21 14:00:29 +0000275 return 1;
276 }
277 }
278 return 0;
279}
drhbf216272005-02-26 18:10:44 +0000280#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000281
drhd2cb50b2009-01-09 21:41:17 +0000282/*
283** Return a human-readable name for a constraint resolution action.
284*/
danba9108b2009-09-22 07:13:42 +0000285#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000286static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000287 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000288 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000289 case OE_SetNull: zName = "SET NULL"; break;
290 case OE_SetDflt: zName = "SET DEFAULT"; break;
291 case OE_Cascade: zName = "CASCADE"; break;
292 case OE_Restrict: zName = "RESTRICT"; break;
293 default: zName = "NO ACTION";
294 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000295 }
drhd2cb50b2009-01-09 21:41:17 +0000296 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000297}
danba9108b2009-09-22 07:13:42 +0000298#endif
danielk197750af3e12008-10-10 17:47:21 +0000299
dane04dc882010-04-20 18:53:15 +0000300
301/*
302** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
303** defined in pager.h. This function returns the associated lowercase
304** journal-mode name.
305*/
306const char *sqlite3JournalModename(int eMode){
307 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000308 "delete", "persist", "off", "truncate", "memory"
309#ifndef SQLITE_OMIT_WAL
310 , "wal"
311#endif
dane04dc882010-04-20 18:53:15 +0000312 };
313 assert( PAGER_JOURNALMODE_DELETE==0 );
314 assert( PAGER_JOURNALMODE_PERSIST==1 );
315 assert( PAGER_JOURNALMODE_OFF==2 );
316 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
317 assert( PAGER_JOURNALMODE_MEMORY==4 );
318 assert( PAGER_JOURNALMODE_WAL==5 );
319 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
320
321 if( eMode==ArraySize(azModeName) ) return 0;
322 return azModeName[eMode];
323}
324
drh87223182004-02-21 14:00:29 +0000325/*
drhc11d4f92003-04-06 21:08:24 +0000326** Process a pragma statement.
327**
328** Pragmas are of this form:
329**
danielk197791cf71b2004-06-26 06:37:06 +0000330** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000331**
332** The identifier might also be a string. The value is a string, and
333** identifier, or a number. If minusFlag is true, then the value is
334** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000335**
336** If the left side is "database.id" then pId1 is the database name
337** and pId2 is the id. If the left side is just "id" then pId1 is the
338** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000339*/
danielk197791cf71b2004-06-26 06:37:06 +0000340void sqlite3Pragma(
341 Parse *pParse,
342 Token *pId1, /* First part of [database.]id field */
343 Token *pId2, /* Second part of [database.]id field, or NULL */
344 Token *pValue, /* Token for <value>, or NULL */
345 int minusFlag /* True if a '-' sign preceded <value> */
346){
347 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
348 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
349 const char *zDb = 0; /* The database name */
350 Token *pId; /* Pointer to <id> token */
351 int iDb; /* Database index for <database> */
drh3fa97302012-02-22 16:58:36 +0000352 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
drh06fd5d62012-02-22 14:45:19 +0000353 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
354 sqlite3 *db = pParse->db; /* The database connection */
355 Db *pDb; /* The specific database being pragmaed */
drhef8e9862013-04-11 13:26:18 +0000356 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
drh06fd5d62012-02-22 14:45:19 +0000357
drhc11d4f92003-04-06 21:08:24 +0000358 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000359 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000360 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000361
danielk197791cf71b2004-06-26 06:37:06 +0000362 /* Interpret the [database.] part of the pragma statement. iDb is the
363 ** index of the database this pragma is being applied to in db.aDb[]. */
364 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
365 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000366 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000367
danielk1977ddfb2f02006-02-17 12:25:14 +0000368 /* If the temp database has been explicitly named as part of the
369 ** pragma, make sure it is open.
370 */
371 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
372 return;
373 }
374
drh17435752007-08-16 04:30:38 +0000375 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000376 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000377 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000378 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000379 }else{
drh17435752007-08-16 04:30:38 +0000380 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000381 }
danielk197791cf71b2004-06-26 06:37:06 +0000382
drhd2cb50b2009-01-09 21:41:17 +0000383 assert( pId2 );
384 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000385 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000386 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000387 }
drh06fd5d62012-02-22 14:45:19 +0000388
389 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
390 ** connection. If it returns SQLITE_OK, then assume that the VFS
391 ** handled the pragma and generate a no-op prepared statement.
392 */
drh3fa97302012-02-22 16:58:36 +0000393 aFcntl[0] = 0;
394 aFcntl[1] = zLeft;
395 aFcntl[2] = zRight;
396 aFcntl[3] = 0;
dan80bb6f82012-10-01 18:44:33 +0000397 db->busyHandler.nBusy = 0;
drh06fd5d62012-02-22 14:45:19 +0000398 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
399 if( rc==SQLITE_OK ){
drh3fa97302012-02-22 16:58:36 +0000400 if( aFcntl[0] ){
401 int mem = ++pParse->nMem;
402 sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
403 sqlite3VdbeSetNumCols(v, 1);
404 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
405 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
406 sqlite3_free(aFcntl[0]);
407 }
drh92c700d2012-02-22 19:56:17 +0000408 }else if( rc!=SQLITE_NOTFOUND ){
409 if( aFcntl[0] ){
410 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
411 sqlite3_free(aFcntl[0]);
412 }
413 pParse->nErr++;
414 pParse->rc = rc;
drh8d936842012-02-24 00:03:12 +0000415 }else
drh06fd5d62012-02-22 14:45:19 +0000416
drhc11d4f92003-04-06 21:08:24 +0000417
drhe73c9142011-11-09 16:12:24 +0000418#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000419 /*
drh90f5ecb2004-07-22 01:19:35 +0000420 ** PRAGMA [database.]default_cache_size
421 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000422 **
423 ** The first form reports the current persistent setting for the
424 ** page cache size. The value returned is the maximum number of
425 ** pages in the page cache. The second form sets both the current
426 ** page cache size value and the persistent page cache size value
427 ** stored in the database file.
428 **
drh93791ea2010-04-26 17:36:35 +0000429 ** Older versions of SQLite would set the default cache size to a
430 ** negative number to indicate synchronous=OFF. These days, synchronous
431 ** is always on by default regardless of the sign of the default cache
432 ** size. But continue to take the absolute value of the default cache
433 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000434 */
danielk19774adee202004-05-08 08:23:19 +0000435 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000436 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000437 { OP_Transaction, 0, 0, 0}, /* 0 */
438 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
drhef8e9862013-04-11 13:26:18 +0000439 { OP_IfPos, 1, 8, 0},
drh3c84ddf2008-01-09 02:15:38 +0000440 { OP_Integer, 0, 2, 0},
441 { OP_Subtract, 1, 2, 1},
drhef8e9862013-04-11 13:26:18 +0000442 { OP_IfPos, 1, 8, 0},
danielk1977602b4662009-07-02 07:47:33 +0000443 { OP_Integer, 0, 1, 0}, /* 6 */
drhef8e9862013-04-11 13:26:18 +0000444 { OP_Noop, 0, 0, 0},
drh3c84ddf2008-01-09 02:15:38 +0000445 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000446 };
drh905793e2004-02-21 13:31:09 +0000447 int addr;
danielk19778a414492004-06-29 08:59:35 +0000448 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000449 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000450 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000451 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000452 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000453 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000454 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000455 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000456 sqlite3VdbeChangeP1(v, addr+1, iDb);
457 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000458 }else{
drhd50ffc42011-03-08 02:38:28 +0000459 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000460 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000461 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000462 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000463 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000464 pDb->pSchema->cache_size = size;
465 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000466 }
467 }else
drhe73c9142011-11-09 16:12:24 +0000468#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000469
drhe73c9142011-11-09 16:12:24 +0000470#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000471 /*
drh90f5ecb2004-07-22 01:19:35 +0000472 ** PRAGMA [database.]page_size
473 ** PRAGMA [database.]page_size=N
474 **
475 ** The first form reports the current setting for the
476 ** database page size in bytes. The second form sets the
477 ** database page size value. The value can only be set if
478 ** the database has not yet been created.
479 */
480 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
481 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000482 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000483 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000484 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000485 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000486 }else{
danielk1977992772c2007-08-30 10:07:38 +0000487 /* Malloc may fail when setting the page-size, as there is an internal
488 ** buffer that the pager module resizes using sqlite3_realloc().
489 */
drh60ac3f42010-11-23 18:59:27 +0000490 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000491 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000492 db->mallocFailed = 1;
493 }
drh90f5ecb2004-07-22 01:19:35 +0000494 }
495 }else
danielk197741483462007-03-24 16:45:04 +0000496
497 /*
drh5b47efa2010-02-12 18:18:39 +0000498 ** PRAGMA [database.]secure_delete
499 ** PRAGMA [database.]secure_delete=ON/OFF
500 **
501 ** The first form reports the current setting for the
502 ** secure_delete flag. The second form changes the secure_delete
503 ** flag setting and reports thenew value.
504 */
505 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
506 Btree *pBt = pDb->pBt;
507 int b = -1;
508 assert( pBt!=0 );
509 if( zRight ){
drh38d9c612012-01-31 14:24:47 +0000510 b = sqlite3GetBoolean(zRight, 0);
drh5b47efa2010-02-12 18:18:39 +0000511 }
drhaf034ed2010-02-12 19:46:26 +0000512 if( pId2->n==0 && b>=0 ){
513 int ii;
514 for(ii=0; ii<db->nDb; ii++){
515 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
516 }
517 }
drh5b47efa2010-02-12 18:18:39 +0000518 b = sqlite3BtreeSecureDelete(pBt, b);
519 returnSingleInt(pParse, "secure_delete", b);
520 }else
521
522 /*
drh60ac3f42010-11-23 18:59:27 +0000523 ** PRAGMA [database.]max_page_count
524 ** PRAGMA [database.]max_page_count=N
525 **
526 ** The first form reports the current setting for the
527 ** maximum number of pages in the database file. The
528 ** second form attempts to change this setting. Both
529 ** forms return the current setting.
530 **
drhe73c9142011-11-09 16:12:24 +0000531 ** The absolute value of N is used. This is undocumented and might
532 ** change. The only purpose is to provide an easy way to test
533 ** the sqlite3AbsInt32() function.
534 **
danielk197759a93792008-05-15 17:48:20 +0000535 ** PRAGMA [database.]page_count
536 **
537 ** Return the number of pages in the specified database.
538 */
drh60ac3f42010-11-23 18:59:27 +0000539 if( sqlite3StrICmp(zLeft,"page_count")==0
540 || sqlite3StrICmp(zLeft,"max_page_count")==0
541 ){
danielk197759a93792008-05-15 17:48:20 +0000542 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000543 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197759a93792008-05-15 17:48:20 +0000544 sqlite3CodeVerifySchema(pParse, iDb);
545 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000546 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000547 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
548 }else{
drhe73c9142011-11-09 16:12:24 +0000549 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
550 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000551 }
danielk197759a93792008-05-15 17:48:20 +0000552 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
553 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000554 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk197759a93792008-05-15 17:48:20 +0000555 }else
556
557 /*
danielk197741483462007-03-24 16:45:04 +0000558 ** PRAGMA [database.]locking_mode
559 ** PRAGMA [database.]locking_mode = (normal|exclusive)
560 */
561 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
562 const char *zRet = "normal";
563 int eMode = getLockingMode(zRight);
564
565 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
566 /* Simple "PRAGMA locking_mode;" statement. This is a query for
567 ** the current default locking mode (which may be different to
568 ** the locking-mode of the main database).
569 */
570 eMode = db->dfltLockMode;
571 }else{
572 Pager *pPager;
573 if( pId2->n==0 ){
574 /* This indicates that no database name was specified as part
575 ** of the PRAGMA command. In this case the locking-mode must be
576 ** set on all attached databases, as well as the main db file.
577 **
578 ** Also, the sqlite3.dfltLockMode variable is set so that
579 ** any subsequently attached databases also use the specified
580 ** locking mode.
581 */
582 int ii;
583 assert(pDb==&db->aDb[0]);
584 for(ii=2; ii<db->nDb; ii++){
585 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
586 sqlite3PagerLockingMode(pPager, eMode);
587 }
drh4f21c4a2008-12-10 22:15:00 +0000588 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000589 }
590 pPager = sqlite3BtreePager(pDb->pBt);
591 eMode = sqlite3PagerLockingMode(pPager, eMode);
592 }
593
594 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
595 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
596 zRet = "exclusive";
597 }
598 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000599 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000600 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
601 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000602 }else
drh3b020132008-04-17 17:02:01 +0000603
604 /*
605 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000606 ** PRAGMA [database.]journal_mode =
607 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000608 */
609 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000610 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
611 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000612
drh94714062011-10-10 12:04:14 +0000613 /* Force the schema to be loaded on all databases. This causes all
614 ** database files to be opened and the journal_modes set. This is
615 ** necessary because subsequent processing must know if the databases
616 ** are in WAL mode. */
danf6c61472010-07-07 13:54:28 +0000617 if( sqlite3ReadSchema(pParse) ){
618 goto pragma_out;
619 }
620
dane04dc882010-04-20 18:53:15 +0000621 sqlite3VdbeSetNumCols(v, 1);
622 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000623
624 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000625 /* If there is no "=MODE" part of the pragma, do a query for the
626 ** current mode */
drh3b020132008-04-17 17:02:01 +0000627 eMode = PAGER_JOURNALMODE_QUERY;
628 }else{
dane04dc882010-04-20 18:53:15 +0000629 const char *zMode;
drhea678832008-12-10 19:26:22 +0000630 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000631 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000632 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
633 }
634 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000635 /* If the "=MODE" part does not match any known journal mode,
636 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000637 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000638 }
639 }
drhc6b2a0f2010-07-08 17:40:37 +0000640 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
641 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
642 iDb = 0;
643 pId2->n = 1;
644 }
645 for(ii=db->nDb-1; ii>=0; ii--){
646 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
647 sqlite3VdbeUsesBtree(v, ii);
648 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000649 }
drh3b020132008-04-17 17:02:01 +0000650 }
drh3b020132008-04-17 17:02:01 +0000651 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
652 }else
danielk1977b53e4962008-06-04 06:45:59 +0000653
654 /*
655 ** PRAGMA [database.]journal_size_limit
656 ** PRAGMA [database.]journal_size_limit=N
657 **
drha9e364f2009-01-13 20:14:15 +0000658 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000659 */
660 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
661 Pager *pPager = sqlite3BtreePager(pDb->pBt);
662 i64 iLimit = -2;
663 if( zRight ){
mistachkine98844f2013-08-24 00:59:24 +0000664 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +0000665 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000666 }
667 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000668 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000669 }else
670
drh13d70422004-11-13 15:59:14 +0000671#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000672
673 /*
danielk1977951af802004-11-05 15:45:09 +0000674 ** PRAGMA [database.]auto_vacuum
675 ** PRAGMA [database.]auto_vacuum=N
676 **
drha9e364f2009-01-13 20:14:15 +0000677 ** Get or set the value of the database 'auto-vacuum' parameter.
678 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000679 */
680#ifndef SQLITE_OMIT_AUTOVACUUM
681 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
682 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000683 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000684 if( sqlite3ReadSchema(pParse) ){
685 goto pragma_out;
686 }
danielk1977951af802004-11-05 15:45:09 +0000687 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000688 int auto_vacuum;
689 if( ALWAYS(pBt) ){
690 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
691 }else{
692 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
693 }
danielk1977951af802004-11-05 15:45:09 +0000694 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
695 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000696 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000697 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000698 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000699 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000700 /* Call SetAutoVacuum() to set initialize the internal auto and
701 ** incr-vacuum flags. This is required in case this connection
702 ** creates the database file. It is important that it is created
703 ** as an auto-vacuum capable db.
704 */
drh70708602012-02-24 14:33:28 +0000705 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
danielk197727b1f952007-06-25 08:16:58 +0000706 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
707 /* When setting the auto_vacuum mode to either "full" or
708 ** "incremental", write the value of meta[6] in the database
709 ** file. Before writing to meta[6], check that meta[3] indicates
710 ** that this really is an auto-vacuum capable database.
711 */
712 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000713 { OP_Transaction, 0, 1, 0}, /* 0 */
714 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
715 { OP_If, 1, 0, 0}, /* 2 */
716 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
717 { OP_Integer, 0, 1, 0}, /* 4 */
718 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000719 };
720 int iAddr;
721 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
722 sqlite3VdbeChangeP1(v, iAddr, iDb);
723 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
724 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
725 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
726 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000727 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000728 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000729 }
danielk1977951af802004-11-05 15:45:09 +0000730 }
731 }else
732#endif
733
drhca5557f2007-05-04 18:30:40 +0000734 /*
735 ** PRAGMA [database.]incremental_vacuum(N)
736 **
737 ** Do N steps of incremental vacuuming on a database.
738 */
739#ifndef SQLITE_OMIT_AUTOVACUUM
740 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
741 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000742 if( sqlite3ReadSchema(pParse) ){
743 goto pragma_out;
744 }
drhca5557f2007-05-04 18:30:40 +0000745 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
746 iLimit = 0x7fffffff;
747 }
748 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000749 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000750 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000751 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000752 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000753 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000754 sqlite3VdbeJumpHere(v, addr);
755 }else
756#endif
757
drh13d70422004-11-13 15:59:14 +0000758#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000759 /*
drh90f5ecb2004-07-22 01:19:35 +0000760 ** PRAGMA [database.]cache_size
761 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000762 **
763 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000764 ** page cache size. The second form sets the local
765 ** page cache size value. If N is positive then that is the
766 ** number of pages in the cache. If N is negative, then the
767 ** number of pages is adjusted so that the cache uses -N kibibytes
768 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000769 */
danielk19774adee202004-05-08 08:23:19 +0000770 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000771 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh21206082011-04-04 18:22:02 +0000772 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000773 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000774 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000775 }else{
drh3b42abb2011-11-09 14:23:04 +0000776 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000777 pDb->pSchema->cache_size = size;
778 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000779 }
780 }else
781
782 /*
drh9b4c59f2013-04-15 17:03:42 +0000783 ** PRAGMA [database.]mmap_size(N)
dan5d8a1372013-03-19 19:28:06 +0000784 **
drh0d0614b2013-03-25 23:09:28 +0000785 ** Used to set mapping size limit. The mapping size limit is
dan5d8a1372013-03-19 19:28:06 +0000786 ** used to limit the aggregate size of all memory mapped regions of the
787 ** database file. If this parameter is set to zero, then memory mapping
drha1f42c72013-04-01 22:38:06 +0000788 ** is not used at all. If N is negative, then the default memory map
drh9b4c59f2013-04-15 17:03:42 +0000789 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
drha1f42c72013-04-01 22:38:06 +0000790 ** The parameter N is measured in bytes.
dan5d8a1372013-03-19 19:28:06 +0000791 **
drh0d0614b2013-03-25 23:09:28 +0000792 ** This value is advisory. The underlying VFS is free to memory map
793 ** as little or as much as it wants. Except, if N is set to 0 then the
794 ** upper layers will never invoke the xFetch interfaces to the VFS.
dan5d8a1372013-03-19 19:28:06 +0000795 */
drh9b4c59f2013-04-15 17:03:42 +0000796 if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
797 sqlite3_int64 sz;
mistachkine98844f2013-08-24 00:59:24 +0000798#if SQLITE_MAX_MMAP_SIZE>0
dan5d8a1372013-03-19 19:28:06 +0000799 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh0d0614b2013-03-25 23:09:28 +0000800 if( zRight ){
drha1f42c72013-04-01 22:38:06 +0000801 int ii;
mistachkine98844f2013-08-24 00:59:24 +0000802 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
drh9b4c59f2013-04-15 17:03:42 +0000803 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
804 if( pId2->n==0 ) db->szMmap = sz;
drha1f42c72013-04-01 22:38:06 +0000805 for(ii=db->nDb-1; ii>=0; ii--){
806 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
drh9b4c59f2013-04-15 17:03:42 +0000807 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
drha1f42c72013-04-01 22:38:06 +0000808 }
809 }
dan5d8a1372013-03-19 19:28:06 +0000810 }
drh9b4c59f2013-04-15 17:03:42 +0000811 sz = -1;
dan3719f5f2013-05-23 10:13:18 +0000812 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
mistachkine98844f2013-08-24 00:59:24 +0000813#else
dan3719f5f2013-05-23 10:13:18 +0000814 sz = 0;
mistachkine98844f2013-08-24 00:59:24 +0000815 rc = SQLITE_OK;
drh188d4882013-04-08 20:47:49 +0000816#endif
dan3719f5f2013-05-23 10:13:18 +0000817 if( rc==SQLITE_OK ){
drh9b4c59f2013-04-15 17:03:42 +0000818 returnSingleInt(pParse, "mmap_size", sz);
dan3719f5f2013-05-23 10:13:18 +0000819 }else if( rc!=SQLITE_NOTFOUND ){
820 pParse->nErr++;
821 pParse->rc = rc;
drh34f74902013-04-03 13:09:18 +0000822 }
dan5d8a1372013-03-19 19:28:06 +0000823 }else
824
825 /*
drh90f5ecb2004-07-22 01:19:35 +0000826 ** PRAGMA temp_store
827 ** PRAGMA temp_store = "default"|"memory"|"file"
828 **
829 ** Return or set the local value of the temp_store flag. Changing
830 ** the local value does not make changes to the disk file and the default
831 ** value will be restored the next time the database is opened.
832 **
833 ** Note that it is possible for the library compile-time options to
834 ** override this setting
835 */
836 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
837 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000838 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000839 }else{
840 changeTempStorage(pParse, zRight);
841 }
842 }else
843
844 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000845 ** PRAGMA temp_store_directory
846 ** PRAGMA temp_store_directory = ""|"directory_name"
847 **
848 ** Return or set the local value of the temp_store_directory flag. Changing
849 ** the value sets a specific directory to be used for temporary files.
850 ** Setting to a null string reverts to the default temporary directory search.
851 ** If temporary directory is changed, then invalidateTempStorage.
852 **
853 */
854 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
855 if( !zRight ){
856 if( sqlite3_temp_directory ){
857 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000858 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000859 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000860 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
861 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000862 }
863 }else{
drh78f82d12008-09-02 00:52:52 +0000864#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000865 if( zRight[0] ){
866 int res;
danielk1977fab11272008-09-16 14:38:02 +0000867 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
868 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000869 sqlite3ErrorMsg(pParse, "not a writable directory");
870 goto pragma_out;
871 }
drh268283b2005-01-08 15:44:25 +0000872 }
danielk1977b06a0b62008-06-26 10:54:12 +0000873 if( SQLITE_TEMP_STORE==0
874 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
875 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000876 ){
877 invalidateTempStorage(pParse);
878 }
drh17435752007-08-16 04:30:38 +0000879 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000880 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000881 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000882 }else{
drh268283b2005-01-08 15:44:25 +0000883 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000884 }
drh78f82d12008-09-02 00:52:52 +0000885#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000886 }
887 }else
888
drhcc716452012-06-06 23:23:23 +0000889#if SQLITE_OS_WIN
mistachkina112d142012-03-14 00:44:01 +0000890 /*
891 ** PRAGMA data_store_directory
892 ** PRAGMA data_store_directory = ""|"directory_name"
893 **
894 ** Return or set the local value of the data_store_directory flag. Changing
895 ** the value sets a specific directory to be used for database files that
896 ** were specified with a relative pathname. Setting to a null string reverts
897 ** to the default database directory, which for database files specified with
898 ** a relative path will probably be based on the current directory for the
899 ** process. Database file specified with an absolute path are not impacted
900 ** by this setting, regardless of its value.
901 **
902 */
903 if( sqlite3StrICmp(zLeft, "data_store_directory")==0 ){
904 if( !zRight ){
905 if( sqlite3_data_directory ){
906 sqlite3VdbeSetNumCols(v, 1);
907 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
908 "data_store_directory", SQLITE_STATIC);
909 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
910 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
911 }
912 }else{
913#ifndef SQLITE_OMIT_WSD
914 if( zRight[0] ){
915 int res;
916 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
917 if( rc!=SQLITE_OK || res==0 ){
918 sqlite3ErrorMsg(pParse, "not a writable directory");
919 goto pragma_out;
920 }
921 }
922 sqlite3_free(sqlite3_data_directory);
923 if( zRight[0] ){
924 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
925 }else{
926 sqlite3_data_directory = 0;
927 }
928#endif /* SQLITE_OMIT_WSD */
929 }
930 }else
drhcc716452012-06-06 23:23:23 +0000931#endif
mistachkina112d142012-03-14 00:44:01 +0000932
drhd2cb50b2009-01-09 21:41:17 +0000933#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
934# if defined(__APPLE__)
935# define SQLITE_ENABLE_LOCKING_STYLE 1
936# else
937# define SQLITE_ENABLE_LOCKING_STYLE 0
938# endif
939#endif
940#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000941 /*
aswiftaebf4132008-11-21 00:10:35 +0000942 ** PRAGMA [database.]lock_proxy_file
943 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
944 **
945 ** Return or set the value of the lock_proxy_file flag. Changing
946 ** the value sets a specific file to be used for database access locks.
947 **
948 */
949 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
950 if( !zRight ){
951 Pager *pPager = sqlite3BtreePager(pDb->pBt);
952 char *proxy_file_path = NULL;
953 sqlite3_file *pFile = sqlite3PagerFile(pPager);
drhc02372c2012-01-10 17:59:59 +0000954 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
aswiftaebf4132008-11-21 00:10:35 +0000955 &proxy_file_path);
956
957 if( proxy_file_path ){
958 sqlite3VdbeSetNumCols(v, 1);
959 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
960 "lock_proxy_file", SQLITE_STATIC);
961 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
962 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
963 }
964 }else{
965 Pager *pPager = sqlite3BtreePager(pDb->pBt);
966 sqlite3_file *pFile = sqlite3PagerFile(pPager);
967 int res;
968 if( zRight[0] ){
969 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
970 zRight);
971 } else {
972 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
973 NULL);
974 }
975 if( res!=SQLITE_OK ){
976 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
977 goto pragma_out;
978 }
979 }
980 }else
drhd2cb50b2009-01-09 21:41:17 +0000981#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000982
983 /*
drh90f5ecb2004-07-22 01:19:35 +0000984 ** PRAGMA [database.]synchronous
985 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000986 **
987 ** Return or set the local value of the synchronous flag. Changing
988 ** the local value does not make changes to the disk file and the
989 ** default value will be restored the next time the database is
990 ** opened.
991 */
danielk19774adee202004-05-08 08:23:19 +0000992 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000993 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000994 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000995 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000996 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000997 if( !db->autoCommit ){
998 sqlite3ErrorMsg(pParse,
999 "Safety level may not be changed inside a transaction");
1000 }else{
drh908c0052012-01-30 18:40:55 +00001001 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
drhd3605a42013-08-17 15:42:29 +00001002 setAllPagerFlags(db);
danielk197791cf71b2004-06-26 06:37:06 +00001003 }
drhc11d4f92003-04-06 21:08:24 +00001004 }
1005 }else
drh13d70422004-11-13 15:59:14 +00001006#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001007
drhbf216272005-02-26 18:10:44 +00001008#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +00001009 if( flagPragma(pParse, zLeft, zRight) ){
drhd3605a42013-08-17 15:42:29 +00001010 setAllPagerFlags(db);
drhc11d4f92003-04-06 21:08:24 +00001011 }else
drhbf216272005-02-26 18:10:44 +00001012#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001013
drh13d70422004-11-13 15:59:14 +00001014#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +00001015 /*
1016 ** PRAGMA table_info(<table>)
1017 **
1018 ** Return a single row for each column of the named table. The columns of
1019 ** the returned data set are:
1020 **
1021 ** cid: Column id (numbered from left to right, starting at 0)
1022 ** name: Column name
1023 ** type: Column declaration type.
1024 ** notnull: True if 'NOT NULL' is part of column declaration
1025 ** dflt_value: The default value for the column, if any.
1026 */
drh5260f7e2004-06-26 19:35:29 +00001027 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +00001028 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001029 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001030 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001031 if( pTab ){
drh384b7fe2013-01-01 13:55:31 +00001032 int i, k;
danielk1977034ca142007-06-26 10:38:54 +00001033 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +00001034 Column *pCol;
drh384b7fe2013-01-01 13:55:31 +00001035 Index *pPk;
1036 for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){}
drh9f6696a2006-02-09 16:52:23 +00001037 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +00001038 pParse->nMem = 6;
drhc95e01d2013-02-14 16:16:05 +00001039 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001040 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
1041 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1042 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
1043 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
1044 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
1045 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001046 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +00001047 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +00001048 if( IsHiddenColumn(pCol) ){
1049 nHidden++;
1050 continue;
1051 }
drh2d401ab2008-01-10 23:50:11 +00001052 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
1053 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
1054 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +00001055 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +00001056 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +00001057 if( pCol->zDflt ){
1058 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +00001059 }else{
drh2d401ab2008-01-10 23:50:11 +00001060 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +00001061 }
drh384b7fe2013-01-01 13:55:31 +00001062 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1063 k = 0;
1064 }else if( pPk==0 ){
1065 k = 1;
1066 }else{
1067 for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
1068 }
1069 sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
drh2d401ab2008-01-10 23:50:11 +00001070 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +00001071 }
1072 }
1073 }else
1074
drh5260f7e2004-06-26 19:35:29 +00001075 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +00001076 Index *pIdx;
1077 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001078 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001079 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001080 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +00001081 int i;
1082 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +00001083 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001084 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001085 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001086 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
1087 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
1088 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001089 for(i=0; i<pIdx->nColumn; i++){
1090 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +00001091 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1092 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +00001093 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +00001094 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
1095 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +00001096 }
1097 }
1098 }else
1099
drh5260f7e2004-06-26 19:35:29 +00001100 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +00001101 Index *pIdx;
1102 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001103 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001104 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +00001105 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001106 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +00001107 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +00001108 if( pIdx ){
1109 int i = 0;
1110 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001111 pParse->nMem = 3;
drhc95e01d2013-02-14 16:16:05 +00001112 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001113 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1114 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1115 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001116 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +00001117 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1118 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
1119 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
1120 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +00001121 ++i;
1122 pIdx = pIdx->pNext;
1123 }
drhc11d4f92003-04-06 21:08:24 +00001124 }
1125 }
1126 }else
1127
drh13d70422004-11-13 15:59:14 +00001128 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
1129 int i;
1130 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
1131 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +00001132 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +00001133 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1134 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
1135 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +00001136 for(i=0; i<db->nDb; i++){
1137 if( db->aDb[i].pBt==0 ) continue;
1138 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +00001139 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1140 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
1141 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +00001142 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +00001143 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +00001144 }
1145 }else
danielk197748af65a2005-02-09 03:20:37 +00001146
1147 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
1148 int i = 0;
1149 HashElem *p;
1150 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001151 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001152 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
1153 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +00001154 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
1155 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001156 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1157 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1158 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001159 }
1160 }else
drh13d70422004-11-13 15:59:14 +00001161#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1162
drhb7f91642004-10-31 02:22:47 +00001163#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +00001164 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +00001165 FKey *pFK;
1166 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001167 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001168 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001169 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001170 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001171 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001172 if( pFK ){
1173 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001174 sqlite3VdbeSetNumCols(v, 8);
1175 pParse->nMem = 8;
drhc95e01d2013-02-14 16:16:05 +00001176 sqlite3CodeVerifySchema(pParse, iDb);
danielk197710fb7492008-10-31 10:53:22 +00001177 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1178 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1179 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1180 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1181 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1182 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1183 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1184 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001185 while(pFK){
1186 int j;
1187 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001188 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001189 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1190 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001191 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1192 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1193 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1194 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001195 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001196 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001197 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1198 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1199 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1200 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001201 }
1202 ++i;
1203 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001204 }
drh78100cc2003-08-23 22:40:53 +00001205 }
1206 }
1207 }else
drhb7f91642004-10-31 02:22:47 +00001208#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001209
drh6c5b9152012-12-17 16:46:37 +00001210#ifndef SQLITE_OMIT_FOREIGN_KEY
dan09ff9e12013-03-11 11:49:03 +00001211#ifndef SQLITE_OMIT_TRIGGER
drh613028b2012-12-17 18:43:02 +00001212 if( sqlite3StrICmp(zLeft, "foreign_key_check")==0 ){
1213 FKey *pFK; /* A foreign key constraint */
1214 Table *pTab; /* Child table contain "REFERENCES" keyword */
1215 Table *pParent; /* Parent table that child points to */
1216 Index *pIdx; /* Index in the parent table */
1217 int i; /* Loop counter: Foreign key number for pTab */
1218 int j; /* Loop counter: Field of the foreign key */
1219 HashElem *k; /* Loop counter: Next table in schema */
1220 int x; /* result variable */
1221 int regResult; /* 3 registers to hold a result row */
1222 int regKey; /* Register to hold key for checking the FK */
1223 int regRow; /* Registers to hold a row from pTab */
1224 int addrTop; /* Top of a loop checking foreign keys */
1225 int addrOk; /* Jump here if the key is OK */
drh7d22a4d2012-12-17 22:32:14 +00001226 int *aiCols; /* child to parent column mapping */
drh6c5b9152012-12-17 16:46:37 +00001227
1228 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh613028b2012-12-17 18:43:02 +00001229 regResult = pParse->nMem+1;
drh4b4b4732012-12-17 20:57:15 +00001230 pParse->nMem += 4;
drh613028b2012-12-17 18:43:02 +00001231 regKey = ++pParse->nMem;
1232 regRow = ++pParse->nMem;
1233 v = sqlite3GetVdbe(pParse);
drh4b4b4732012-12-17 20:57:15 +00001234 sqlite3VdbeSetNumCols(v, 4);
drh613028b2012-12-17 18:43:02 +00001235 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
1236 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
drh4b4b4732012-12-17 20:57:15 +00001237 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
1238 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
drh613028b2012-12-17 18:43:02 +00001239 sqlite3CodeVerifySchema(pParse, iDb);
1240 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1241 while( k ){
1242 if( zRight ){
1243 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1244 k = 0;
1245 }else{
1246 pTab = (Table*)sqliteHashData(k);
1247 k = sqliteHashNext(k);
1248 }
drh9148def2012-12-17 20:40:39 +00001249 if( pTab==0 || pTab->pFKey==0 ) continue;
1250 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
drh613028b2012-12-17 18:43:02 +00001251 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
drh6c5b9152012-12-17 16:46:37 +00001252 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
drh613028b2012-12-17 18:43:02 +00001253 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
1254 P4_TRANSIENT);
drh6c5b9152012-12-17 16:46:37 +00001255 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1256 pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
1257 if( pParent==0 ) break;
1258 pIdx = 0;
drh9148def2012-12-17 20:40:39 +00001259 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
drh613028b2012-12-17 18:43:02 +00001260 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
drh6c5b9152012-12-17 16:46:37 +00001261 if( x==0 ){
1262 if( pIdx==0 ){
1263 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
1264 }else{
1265 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
1266 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
1267 sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
1268 }
1269 }else{
drh613028b2012-12-17 18:43:02 +00001270 k = 0;
drh6c5b9152012-12-17 16:46:37 +00001271 break;
1272 }
drh6c5b9152012-12-17 16:46:37 +00001273 }
drh613028b2012-12-17 18:43:02 +00001274 if( pFK ) break;
1275 if( pParse->nTab<i ) pParse->nTab = i;
1276 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
1277 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
1278 pParent = sqlite3LocateTable(pParse, 0, pFK->zTo, zDb);
1279 assert( pParent!=0 );
1280 pIdx = 0;
drh7d22a4d2012-12-17 22:32:14 +00001281 aiCols = 0;
1282 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
drh613028b2012-12-17 18:43:02 +00001283 assert( x==0 );
1284 addrOk = sqlite3VdbeMakeLabel(v);
1285 if( pIdx==0 ){
1286 int iKey = pFK->aCol[0].iFrom;
drh83e0dba2012-12-20 00:32:49 +00001287 assert( iKey>=0 && iKey<pTab->nCol );
1288 if( iKey!=pTab->iPKey ){
drh613028b2012-12-17 18:43:02 +00001289 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
1290 sqlite3ColumnDefault(v, pTab, iKey, regRow);
1291 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
1292 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
1293 sqlite3VdbeCurrentAddr(v)+3);
drh6c5b9152012-12-17 16:46:37 +00001294 }else{
drh613028b2012-12-17 18:43:02 +00001295 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
drh6c5b9152012-12-17 16:46:37 +00001296 }
drh613028b2012-12-17 18:43:02 +00001297 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
1298 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
1299 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
1300 }else{
1301 for(j=0; j<pFK->nCol; j++){
drh7d22a4d2012-12-17 22:32:14 +00001302 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
1303 aiCols ? aiCols[j] : pFK->aCol[0].iFrom, regRow+j);
drh613028b2012-12-17 18:43:02 +00001304 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
1305 }
1306 sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
1307 sqlite3VdbeChangeP4(v, -1,
1308 sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
1309 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
drh6c5b9152012-12-17 16:46:37 +00001310 }
drh613028b2012-12-17 18:43:02 +00001311 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
drh4b4b4732012-12-17 20:57:15 +00001312 sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
1313 pFK->zTo, P4_TRANSIENT);
1314 sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
1315 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
drh613028b2012-12-17 18:43:02 +00001316 sqlite3VdbeResolveLabel(v, addrOk);
drh7d22a4d2012-12-17 22:32:14 +00001317 sqlite3DbFree(db, aiCols);
drh6c5b9152012-12-17 16:46:37 +00001318 }
drh613028b2012-12-17 18:43:02 +00001319 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
1320 sqlite3VdbeJumpHere(v, addrTop);
drh6c5b9152012-12-17 16:46:37 +00001321 }
1322 }else
dan09ff9e12013-03-11 11:49:03 +00001323#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh6c5b9152012-12-17 16:46:37 +00001324#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
1325
drhc11d4f92003-04-06 21:08:24 +00001326#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001327 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001328 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001329 if( sqlite3GetBoolean(zRight, 0) ){
drh55ef4d92005-08-14 01:20:37 +00001330 sqlite3ParserTrace(stderr, "parser: ");
1331 }else{
1332 sqlite3ParserTrace(0, 0);
1333 }
drhc11d4f92003-04-06 21:08:24 +00001334 }
1335 }else
1336#endif
1337
drh55ef4d92005-08-14 01:20:37 +00001338 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1339 ** used will be case sensitive or not depending on the RHS.
1340 */
1341 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1342 if( zRight ){
drh38d9c612012-01-31 14:24:47 +00001343 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
drh55ef4d92005-08-14 01:20:37 +00001344 }
1345 }else
1346
drh1dcdbc02007-01-27 02:24:54 +00001347#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1348# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1349#endif
1350
drhb7f91642004-10-31 02:22:47 +00001351#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drhf7b54962013-05-28 12:11:54 +00001352 /* Pragma "quick_check" is reduced version of
danielk197741c58b72007-12-29 13:39:19 +00001353 ** integrity_check designed to detect most database corruption
1354 ** without most of the overhead of a full integrity-check.
1355 */
1356 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1357 || sqlite3StrICmp(zLeft, "quick_check")==0
1358 ){
drh1dcdbc02007-01-27 02:24:54 +00001359 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001360
drhed717fe2003-06-15 23:42:24 +00001361 /* Code that appears at the end of the integrity check. If no error
1362 ** messages have been generated, output OK. Otherwise output the
1363 ** error message
1364 */
drh57196282004-10-06 15:41:16 +00001365 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001366 { OP_AddImm, 1, 0, 0}, /* 0 */
1367 { OP_IfNeg, 1, 0, 0}, /* 1 */
1368 { OP_String8, 0, 3, 0}, /* 2 */
1369 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001370 };
drhed717fe2003-06-15 23:42:24 +00001371
drhc5227312011-10-13 17:09:01 +00001372 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001373
dan5885e762012-07-16 10:06:12 +00001374 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
1375 ** then iDb is set to the index of the database identified by <db>.
1376 ** In this case, the integrity of database iDb only is verified by
1377 ** the VDBE created below.
1378 **
1379 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
1380 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
1381 ** to -1 here, to indicate that the VDBE should verify the integrity
1382 ** of all attached databases. */
1383 assert( iDb>=0 );
1384 assert( iDb==0 || pId2->z );
1385 if( pId2->z==0 ) iDb = -1;
1386
drhed717fe2003-06-15 23:42:24 +00001387 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001388 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001389 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001390 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001391 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001392
1393 /* Set the maximum error count */
1394 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1395 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001396 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001397 if( mxErr<=0 ){
1398 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1399 }
1400 }
drh2d401ab2008-01-10 23:50:11 +00001401 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001402
1403 /* Do an integrity check on each database file */
1404 for(i=0; i<db->nDb; i++){
1405 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001406 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001407 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001408
danielk197753c0f742005-03-29 03:10:59 +00001409 if( OMIT_TEMPDB && i==1 ) continue;
dan5885e762012-07-16 10:06:12 +00001410 if( iDb>=0 && i!=iDb ) continue;
danielk197753c0f742005-03-29 03:10:59 +00001411
drh80242052004-06-09 00:48:12 +00001412 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001413 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001414 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001415 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001416
drhed717fe2003-06-15 23:42:24 +00001417 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001418 **
1419 ** Begin by filling registers 2, 3, ... with the root pages numbers
1420 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001421 */
dan5885e762012-07-16 10:06:12 +00001422 assert( sqlite3SchemaMutexHeld(db, i, 0) );
danielk1977da184232006-01-05 11:34:32 +00001423 pTbls = &db->aDb[i].pSchema->tblHash;
1424 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001425 Table *pTab = sqliteHashData(x);
1426 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001427 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001428 cnt++;
1429 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001430 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001431 cnt++;
1432 }
1433 }
drh2d401ab2008-01-10 23:50:11 +00001434
1435 /* Make sure sufficient number of registers have been allocated */
drh66518ca2013-08-01 15:09:57 +00001436 pParse->nMem = MAX( pParse->nMem, cnt+7 );
drh2d401ab2008-01-10 23:50:11 +00001437
1438 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001439 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001440 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001441 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1442 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001443 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001444 P4_DYNAMIC);
drhe8e4af72012-09-21 00:04:28 +00001445 sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
danielk1977a7a8e142008-02-13 18:25:27 +00001446 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001447 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001448 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001449
1450 /* Make sure all the indices are constructed correctly.
1451 */
danielk197741c58b72007-12-29 13:39:19 +00001452 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001453 Table *pTab = sqliteHashData(x);
1454 Index *pIdx;
1455 int loopTop;
1456
1457 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001458 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001459 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001460 sqlite3VdbeJumpHere(v, addr);
drh66518ca2013-08-01 15:09:57 +00001461 sqlite3ExprCacheClear(pParse);
drh290c1942004-08-21 17:54:45 +00001462 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh8a9789b2013-08-01 03:36:59 +00001463 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1464 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7+j); /* index entries counter */
1465 }
1466 pParse->nMem = MAX(pParse->nMem, 7+j);
1467 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0) + 1;
drhed717fe2003-06-15 23:42:24 +00001468 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drhb2b9d3d2013-08-01 01:14:43 +00001469 int jmp2, jmp3;
drh91fc4a02009-11-12 20:39:03 +00001470 int r1;
drh57196282004-10-06 15:41:16 +00001471 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001472 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001473 { OP_String8, 0, 3, 0}, /* 1 */
1474 { OP_Rowid, 1, 4, 0},
1475 { OP_String8, 0, 5, 0}, /* 3 */
1476 { OP_String8, 0, 6, 0}, /* 4 */
1477 { OP_Concat, 4, 3, 3},
1478 { OP_Concat, 5, 3, 3},
1479 { OP_Concat, 6, 3, 3},
1480 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001481 { OP_IfPos, 1, 0, 0}, /* 9 */
1482 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001483 };
drhb2b9d3d2013-08-01 01:14:43 +00001484 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0, &jmp3);
drh8a9789b2013-08-01 03:36:59 +00001485 sqlite3VdbeAddOp2(v, OP_AddImm, 7+j, 1); /* increment entry count */
drh91fc4a02009-11-12 20:39:03 +00001486 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001487 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001488 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1489 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001490 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
drhbb8a2792008-03-19 00:21:30 +00001491 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001492 sqlite3VdbeJumpHere(v, jmp2);
drhb2b9d3d2013-08-01 01:14:43 +00001493 sqlite3VdbeResolveLabel(v, jmp3);
drhed717fe2003-06-15 23:42:24 +00001494 }
drh8a9789b2013-08-01 03:36:59 +00001495 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop);
1496 sqlite3VdbeJumpHere(v, loopTop-1);
1497#ifndef SQLITE_OMIT_BTREECOUNT
1498 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
drh24003452008-01-03 01:28:59 +00001499 "wrong # of entries in index ", P4_STATIC);
drh8a9789b2013-08-01 03:36:59 +00001500 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1501 addr = sqlite3VdbeCurrentAddr(v);
1502 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
1503 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
1504 sqlite3VdbeAddOp2(v, OP_Count, j+2, 3);
1505 sqlite3VdbeAddOp3(v, OP_Eq, 7+j, addr+8, 3);
1506 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
1507 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
1508 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
1509 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
drhed717fe2003-06-15 23:42:24 +00001510 }
drh8a9789b2013-08-01 03:36:59 +00001511#endif /* SQLITE_OMIT_BTREECOUNT */
drhed717fe2003-06-15 23:42:24 +00001512 }
1513 }
danielk19774adee202004-05-08 08:23:19 +00001514 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001515 sqlite3VdbeChangeP2(v, addr, -mxErr);
1516 sqlite3VdbeJumpHere(v, addr+1);
1517 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001518 }else
drhb7f91642004-10-31 02:22:47 +00001519#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1520
drh13d70422004-11-13 15:59:14 +00001521#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001522 /*
1523 ** PRAGMA encoding
1524 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1525 **
drh85b623f2007-12-13 21:54:09 +00001526 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001527 ** database. If the database is not initialized, it is initialized now.
1528 **
1529 ** The second form of this pragma is a no-op if the main database file
1530 ** has not already been initialized. In this case it sets the default
1531 ** encoding that will be used for the main database file if a new file
1532 ** is created. If an existing main database file is opened, then the
1533 ** default text encoding for the existing database is used.
1534 **
1535 ** In all cases new databases created using the ATTACH command are
1536 ** created to use the same default text encoding as the main database. If
1537 ** the main database has not been initialized and/or created when ATTACH
1538 ** is executed, this is done before the ATTACH operation.
1539 **
1540 ** In the second form this pragma sets the text encoding to be used in
1541 ** new database files created using this database handle. It is only
1542 ** useful if invoked immediately after the main database i
1543 */
1544 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001545 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001546 char *zName;
1547 u8 enc;
1548 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001549 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001550 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1551 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1552 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001553 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001554 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001555 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1556 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001557 { 0, 0 }
1558 };
drh0f7eb612006-08-08 13:51:43 +00001559 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001560 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001561 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001562 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001563 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001564 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001565 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1566 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1567 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1568 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001569 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001570 }else{ /* "PRAGMA encoding = XXX" */
1571 /* Only change the value of sqlite.enc if the database handle is not
1572 ** initialized. If the main database exists, the new sqlite.enc value
1573 ** will be overwritten when the schema is next loaded. If it does not
1574 ** already exists, it will be created to use the new encoding value.
1575 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001576 if(
1577 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1578 DbHasProperty(db, 0, DB_Empty)
1579 ){
danielk19778e227872004-06-07 07:52:17 +00001580 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1581 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001582 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001583 break;
1584 }
1585 }
1586 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001587 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001588 }
1589 }
1590 }
1591 }else
drh13d70422004-11-13 15:59:14 +00001592#endif /* SQLITE_OMIT_UTF16 */
1593
1594#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001595 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001596 ** PRAGMA [database.]schema_version
1597 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001598 **
danielk1977b92b70b2004-11-12 16:11:59 +00001599 ** PRAGMA [database.]user_version
1600 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001601 **
drh4ee09b42013-05-01 19:49:27 +00001602 ** PRAGMA [database.]freelist_count = <integer>
1603 **
1604 ** PRAGMA [database.]application_id
1605 ** PRAGMA [database.]application_id = <integer>
1606 **
danielk1977b92b70b2004-11-12 16:11:59 +00001607 ** The pragma's schema_version and user_version are used to set or get
1608 ** the value of the schema-version and user-version, respectively. Both
1609 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001610 ** stored in the database header.
1611 **
1612 ** The schema-cookie is usually only manipulated internally by SQLite. It
1613 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001614 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001615 ** SQLite each time a query is executed to ensure that the internal cache
1616 ** of the schema used when compiling the SQL query matches the schema of
1617 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001618 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1619 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001620 ** crashes or database corruption. Use with caution!
1621 **
danielk1977b92b70b2004-11-12 16:11:59 +00001622 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001623 ** applications for any purpose.
1624 */
danielk1977180b56a2007-06-24 08:00:42 +00001625 if( sqlite3StrICmp(zLeft, "schema_version")==0
1626 || sqlite3StrICmp(zLeft, "user_version")==0
1627 || sqlite3StrICmp(zLeft, "freelist_count")==0
drh4ee09b42013-05-01 19:49:27 +00001628 || sqlite3StrICmp(zLeft, "application_id")==0
danielk1977180b56a2007-06-24 08:00:42 +00001629 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001630 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001631 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001632 switch( zLeft[0] ){
drh4ee09b42013-05-01 19:49:27 +00001633 case 'a': case 'A':
1634 iCookie = BTREE_APPLICATION_ID;
1635 break;
danielk1977180b56a2007-06-24 08:00:42 +00001636 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001637 iCookie = BTREE_FREE_PAGE_COUNT;
1638 break;
1639 case 's': case 'S':
1640 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001641 break;
1642 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001643 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001644 break;
danielk1977dae24952004-11-11 05:10:43 +00001645 }
1646
danielk19770d19f7a2009-06-03 11:25:07 +00001647 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001648 /* Write the specified cookie value */
1649 static const VdbeOpList setCookie[] = {
1650 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001651 { OP_Integer, 0, 1, 0}, /* 1 */
1652 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001653 };
1654 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1655 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001656 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001657 sqlite3VdbeChangeP1(v, addr+2, iDb);
1658 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1659 }else{
1660 /* Read the specified cookie value */
1661 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001662 { OP_Transaction, 0, 0, 0}, /* 0 */
1663 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001664 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001665 };
1666 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1667 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001668 sqlite3VdbeChangeP1(v, addr+1, iDb);
1669 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001670 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001671 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001672 }
drh6e345992007-03-30 11:12:08 +00001673 }else
drh13d70422004-11-13 15:59:14 +00001674#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001675
shanehdc97a8c2010-02-23 20:08:35 +00001676#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1677 /*
1678 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001679 **
drh71caabf2010-02-26 15:39:24 +00001680 ** Return the names of all compile-time options used in this build,
1681 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001682 */
drh264a2d42010-02-25 15:28:41 +00001683 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
shanehdc97a8c2010-02-23 20:08:35 +00001684 int i = 0;
1685 const char *zOpt;
1686 sqlite3VdbeSetNumCols(v, 1);
1687 pParse->nMem = 1;
1688 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1689 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1690 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1691 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1692 }
1693 }else
1694#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1695
dan5cf53532010-05-01 16:40:20 +00001696#ifndef SQLITE_OMIT_WAL
1697 /*
dancdc1f042010-11-18 12:11:05 +00001698 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00001699 **
1700 ** Checkpoint the database.
1701 */
dan5a299f92010-05-03 11:05:08 +00001702 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
dana58f26f2010-11-16 18:56:51 +00001703 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001704 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1705 if( zRight ){
1706 if( sqlite3StrICmp(zRight, "full")==0 ){
1707 eMode = SQLITE_CHECKPOINT_FULL;
1708 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1709 eMode = SQLITE_CHECKPOINT_RESTART;
1710 }
1711 }
dan5a299f92010-05-03 11:05:08 +00001712 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
dancdc1f042010-11-18 12:11:05 +00001713 sqlite3VdbeSetNumCols(v, 3);
1714 pParse->nMem = 3;
1715 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1716 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1717 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1718
drh30aa3b92011-02-07 23:56:01 +00001719 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001720 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
dan7c246102010-04-12 19:00:29 +00001721 }else
dan5a299f92010-05-03 11:05:08 +00001722
1723 /*
1724 ** PRAGMA wal_autocheckpoint
1725 ** PRAGMA wal_autocheckpoint = N
1726 **
1727 ** Configure a database connection to automatically checkpoint a database
1728 ** after accumulating N frames in the log. Or query for the current value
1729 ** of N.
1730 */
1731 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
1732 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001733 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001734 }
drhb033d8b2010-05-03 13:37:30 +00001735 returnSingleInt(pParse, "wal_autocheckpoint",
1736 db->xWalCallback==sqlite3WalDefaultHook ?
1737 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
dan5a299f92010-05-03 11:05:08 +00001738 }else
dan5cf53532010-05-01 16:40:20 +00001739#endif
dan7c246102010-04-12 19:00:29 +00001740
drh09419b42011-11-16 19:29:17 +00001741 /*
1742 ** PRAGMA shrink_memory
1743 **
1744 ** This pragma attempts to free as much memory as possible from the
1745 ** current database connection.
1746 */
1747 if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
1748 sqlite3_db_release_memory(db);
1749 }else
1750
drhf3603962012-09-07 16:46:59 +00001751 /*
1752 ** PRAGMA busy_timeout
1753 ** PRAGMA busy_timeout = N
1754 **
1755 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
drhc0c7b5e2012-09-07 18:49:57 +00001756 ** if one is set. If no busy handler or a different busy handler is set
1757 ** then 0 is returned. Setting the busy_timeout to 0 or negative
1758 ** disables the timeout.
drhf3603962012-09-07 16:46:59 +00001759 */
1760 if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
1761 if( zRight ){
1762 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
1763 }
1764 returnSingleInt(pParse, "timeout", db->busyTimeout);
1765 }else
1766
dougcurrie81c95ef2004-06-18 23:21:47 +00001767#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001768 /*
1769 ** Report the current state of file logs for all databases
1770 */
1771 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001772 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001773 "unlocked", "shared", "reserved", "pending", "exclusive"
1774 };
1775 int i;
drh89ac8c12004-06-09 14:17:20 +00001776 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001777 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001778 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1779 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001780 for(i=0; i<db->nDb; i++){
1781 Btree *pBt;
drh9e33c2c2007-08-31 18:34:59 +00001782 const char *zState = "unknown";
1783 int j;
drh89ac8c12004-06-09 14:17:20 +00001784 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001785 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001786 pBt = db->aDb[i].pBt;
drh5a05be12012-10-09 18:51:44 +00001787 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001788 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001789 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001790 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1791 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001792 }
drh2d401ab2008-01-10 23:50:11 +00001793 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1794 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001795 }
danielk1977a4de4532008-09-02 15:44:08 +00001796
drh89ac8c12004-06-09 14:17:20 +00001797 }else
1798#endif
1799
shanehad9f9f62010-02-17 17:48:46 +00001800#ifdef SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001801 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhee0231e2013-05-29 17:48:28 +00001802 sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001803 }else
drhd2cb50b2009-01-09 21:41:17 +00001804 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
drhee0231e2013-05-29 17:48:28 +00001805 sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
drhd2cb50b2009-01-09 21:41:17 +00001806 }else
1807 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1808 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1809 int i, h1, h2;
1810 char zKey[40];
1811 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1812 h1 += 9*(1&(h1>>6));
1813 h2 += 9*(1&(h2>>6));
1814 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1815 }
1816 if( (zLeft[3] & 0xf)==0xb ){
drhee0231e2013-05-29 17:48:28 +00001817 sqlite3_key_v2(db, zDb, zKey, i/2);
drhd2cb50b2009-01-09 21:41:17 +00001818 }else{
drhee0231e2013-05-29 17:48:28 +00001819 sqlite3_rekey_v2(db, zDb, zKey, i/2);
drhd2cb50b2009-01-09 21:41:17 +00001820 }
1821 }else
drh3c4f2a42005-12-08 18:12:56 +00001822#endif
shanehad9f9f62010-02-17 17:48:46 +00001823#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh7fe18b42013-01-16 20:33:02 +00001824 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 && zRight ){
shanehad9f9f62010-02-17 17:48:46 +00001825#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001826 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001827 sqlite3_activate_see(&zRight[4]);
1828 }
1829#endif
1830#ifdef SQLITE_ENABLE_CEROD
1831 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001832 sqlite3_activate_cerod(&zRight[6]);
1833 }
1834#endif
drhd2cb50b2009-01-09 21:41:17 +00001835 }else
drh21e2cab2006-09-25 18:01:57 +00001836#endif
drh3c4f2a42005-12-08 18:12:56 +00001837
drhd2cb50b2009-01-09 21:41:17 +00001838
1839 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001840
danielk1977e0048402004-06-15 16:51:01 +00001841pragma_out:
drh633e6d52008-07-28 19:34:53 +00001842 sqlite3DbFree(db, zLeft);
1843 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001844}
drh13d70422004-11-13 15:59:14 +00001845
drh8bfdf722009-06-19 14:06:03 +00001846#endif /* SQLITE_OMIT_PRAGMA */