blob: bfdcb2370ac134c20d22ed8d4798baa626edacab [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
19** unrecognized string argument.
drhc11d4f92003-04-06 21:08:24 +000020**
21** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000022** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000023** to support legacy SQL code. The safety level used to be boolean
24** and older scripts may have used numbers 0 for OFF and 1 for ON.
25*/
drh4f21c4a2008-12-10 22:15:00 +000026static u8 getSafetyLevel(const char *z){
drh722e95a2004-10-25 20:33:44 +000027 /* 123456789 123456789 */
28 static const char zText[] = "onoffalseyestruefull";
29 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
30 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
31 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
32 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000033 if( sqlite3Isdigit(*z) ){
drh60ac3f42010-11-23 18:59:27 +000034 return (u8)sqlite3Atoi(z);
drhc11d4f92003-04-06 21:08:24 +000035 }
drhea678832008-12-10 19:26:22 +000036 n = sqlite3Strlen30(z);
danielk197700e13612008-11-17 19:18:54 +000037 for(i=0; i<ArraySize(iLength); i++){
drh722e95a2004-10-25 20:33:44 +000038 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
39 return iValue[i];
40 }
drhc11d4f92003-04-06 21:08:24 +000041 }
42 return 1;
43}
44
45/*
drh722e95a2004-10-25 20:33:44 +000046** Interpret the given string as a boolean value.
47*/
drh81cc5162011-05-17 20:36:21 +000048u8 sqlite3GetBoolean(const char *z){
drh722e95a2004-10-25 20:33:44 +000049 return getSafetyLevel(z)&1;
50}
51
drhc7dc9bf2011-06-03 13:02:57 +000052/* The sqlite3GetBoolean() function is used by other modules but the
53** remainder of this file is specific to PRAGMA processing. So omit
54** the rest of the file if PRAGMAs are omitted from the build.
55*/
56#if !defined(SQLITE_OMIT_PRAGMA)
57
danielk197741483462007-03-24 16:45:04 +000058/*
59** Interpret the given string as a locking mode value.
60*/
61static int getLockingMode(const char *z){
62 if( z ){
63 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
64 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
65 }
66 return PAGER_LOCKINGMODE_QUERY;
67}
68
danielk1977dddbcdc2007-04-26 14:42:34 +000069#ifndef SQLITE_OMIT_AUTOVACUUM
70/*
71** Interpret the given string as an auto-vacuum mode value.
72**
73** The following strings, "none", "full" and "incremental" are
74** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
75*/
76static int getAutoVacuum(const char *z){
77 int i;
78 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
79 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
80 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
drh60ac3f42010-11-23 18:59:27 +000081 i = sqlite3Atoi(z);
drh4f21c4a2008-12-10 22:15:00 +000082 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +000083}
84#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
85
danielk1977b84f96f2005-01-20 11:32:23 +000086#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000087/*
drh90f5ecb2004-07-22 01:19:35 +000088** Interpret the given string as a temp db location. Return 1 for file
89** backed temporary databases, 2 for the Red-Black tree in memory database
90** and 0 to use the compile-time default.
91*/
92static int getTempStore(const char *z){
93 if( z[0]>='0' && z[0]<='2' ){
94 return z[0] - '0';
95 }else if( sqlite3StrICmp(z, "file")==0 ){
96 return 1;
97 }else if( sqlite3StrICmp(z, "memory")==0 ){
98 return 2;
99 }else{
100 return 0;
101 }
102}
drhbf216272005-02-26 18:10:44 +0000103#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000104
drhbf216272005-02-26 18:10:44 +0000105#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000106/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000107** Invalidate temp storage, either when the temp storage is changed
108** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000109*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000110static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000111 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000112 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000113 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000114 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
115 "from within a transaction");
116 return SQLITE_ERROR;
117 }
118 sqlite3BtreeClose(db->aDb[1].pBt);
119 db->aDb[1].pBt = 0;
drhc7792fa2011-04-02 16:28:52 +0000120 sqlite3ResetInternalSchema(db, -1);
drh90f5ecb2004-07-22 01:19:35 +0000121 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000122 return SQLITE_OK;
123}
drhbf216272005-02-26 18:10:44 +0000124#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000125
drhbf216272005-02-26 18:10:44 +0000126#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000127/*
128** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000129** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000130** or DEFAULT_TEMP_STORE pragmas.
131*/
132static int changeTempStorage(Parse *pParse, const char *zStorageType){
133 int ts = getTempStore(zStorageType);
134 sqlite3 *db = pParse->db;
135 if( db->temp_store==ts ) return SQLITE_OK;
136 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
137 return SQLITE_ERROR;
138 }
drh4f21c4a2008-12-10 22:15:00 +0000139 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000140 return SQLITE_OK;
141}
drhbf216272005-02-26 18:10:44 +0000142#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000143
144/*
145** Generate code to return a single integer value.
146*/
drh3c713642009-04-04 16:02:32 +0000147static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000148 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000149 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000150 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
151 if( pI64 ){
152 memcpy(pI64, &value, sizeof(value));
153 }
154 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000155 sqlite3VdbeSetNumCols(v, 1);
156 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000157 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000158}
159
drhbf216272005-02-26 18:10:44 +0000160#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000161/*
drh87223182004-02-21 14:00:29 +0000162** Check to see if zRight and zLeft refer to a pragma that queries
163** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
164** Also, implement the pragma.
165*/
166static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000167 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000168 const char *zName; /* Name of the pragma */
169 int mask; /* Mask for the db->flags value */
170 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000171 { "full_column_names", SQLITE_FullColNames },
172 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000173 { "count_changes", SQLITE_CountRows },
174 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000175 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000176 { "fullfsync", SQLITE_FullFSync },
drhc97d8462010-11-19 18:23:35 +0000177 { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
drh699b3d42009-02-23 16:52:07 +0000178 { "reverse_unordered_selects", SQLITE_ReverseOrder },
drhc6339082010-04-07 16:54:58 +0000179#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
180 { "automatic_index", SQLITE_AutoIndex },
181#endif
drhcf1023c2007-05-08 20:59:49 +0000182#ifdef SQLITE_DEBUG
183 { "sql_trace", SQLITE_SqlTrace },
184 { "vdbe_listing", SQLITE_VdbeListing },
185 { "vdbe_trace", SQLITE_VdbeTrace },
186#endif
drh0cd2d4c2005-11-03 02:15:02 +0000187#ifndef SQLITE_OMIT_CHECK
188 { "ignore_check_constraints", SQLITE_IgnoreChecks },
189#endif
drh722e95a2004-10-25 20:33:44 +0000190 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000191 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
drh7bec5052005-02-06 02:45:41 +0000192 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000193
194 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
195 ** flag if there are any active statements. */
196 { "read_uncommitted", SQLITE_ReadUncommitted },
dan5bde73c2009-09-01 17:11:07 +0000197 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000198
dan75cbd982009-09-21 16:06:03 +0000199 /* This flag may only be set if both foreign-key and trigger support
200 ** are present in the build. */
201#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan1da40a32009-09-19 17:00:31 +0000202 { "foreign_keys", SQLITE_ForeignKeys },
dan75cbd982009-09-21 16:06:03 +0000203#endif
drh87223182004-02-21 14:00:29 +0000204 };
205 int i;
drh722e95a2004-10-25 20:33:44 +0000206 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000207 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000208 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000209 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000210 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000211 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000212 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
213 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000214 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000215 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000216 }else{
dan75cbd982009-09-21 16:06:03 +0000217 int mask = p->mask; /* Mask of bits to set or clear. */
218 if( db->autoCommit==0 ){
219 /* Foreign key support may not be enabled or disabled while not
220 ** in auto-commit mode. */
221 mask &= ~(SQLITE_ForeignKeys);
222 }
223
drh81cc5162011-05-17 20:36:21 +0000224 if( sqlite3GetBoolean(zRight) ){
dan75cbd982009-09-21 16:06:03 +0000225 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000226 }else{
dan75cbd982009-09-21 16:06:03 +0000227 db->flags &= ~mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000228 }
danielk19778e556522007-11-13 10:30:24 +0000229
230 /* Many of the flag-pragmas modify the code generated by the SQL
231 ** compiler (eg. count_changes). So add an opcode to expire all
232 ** compiled SQL statements after modifying a pragma value.
233 */
drh66a51672008-01-03 00:01:23 +0000234 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000235 }
drh87223182004-02-21 14:00:29 +0000236 }
danielk19778e556522007-11-13 10:30:24 +0000237
drh87223182004-02-21 14:00:29 +0000238 return 1;
239 }
240 }
241 return 0;
242}
drhbf216272005-02-26 18:10:44 +0000243#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000244
drhd2cb50b2009-01-09 21:41:17 +0000245/*
246** Return a human-readable name for a constraint resolution action.
247*/
danba9108b2009-09-22 07:13:42 +0000248#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000249static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000250 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000251 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000252 case OE_SetNull: zName = "SET NULL"; break;
253 case OE_SetDflt: zName = "SET DEFAULT"; break;
254 case OE_Cascade: zName = "CASCADE"; break;
255 case OE_Restrict: zName = "RESTRICT"; break;
256 default: zName = "NO ACTION";
257 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000258 }
drhd2cb50b2009-01-09 21:41:17 +0000259 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000260}
danba9108b2009-09-22 07:13:42 +0000261#endif
danielk197750af3e12008-10-10 17:47:21 +0000262
dane04dc882010-04-20 18:53:15 +0000263
264/*
265** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
266** defined in pager.h. This function returns the associated lowercase
267** journal-mode name.
268*/
269const char *sqlite3JournalModename(int eMode){
270 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000271 "delete", "persist", "off", "truncate", "memory"
272#ifndef SQLITE_OMIT_WAL
273 , "wal"
274#endif
dane04dc882010-04-20 18:53:15 +0000275 };
276 assert( PAGER_JOURNALMODE_DELETE==0 );
277 assert( PAGER_JOURNALMODE_PERSIST==1 );
278 assert( PAGER_JOURNALMODE_OFF==2 );
279 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
280 assert( PAGER_JOURNALMODE_MEMORY==4 );
281 assert( PAGER_JOURNALMODE_WAL==5 );
282 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
283
284 if( eMode==ArraySize(azModeName) ) return 0;
285 return azModeName[eMode];
286}
287
drh87223182004-02-21 14:00:29 +0000288/*
drhc11d4f92003-04-06 21:08:24 +0000289** Process a pragma statement.
290**
291** Pragmas are of this form:
292**
danielk197791cf71b2004-06-26 06:37:06 +0000293** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000294**
295** The identifier might also be a string. The value is a string, and
296** identifier, or a number. If minusFlag is true, then the value is
297** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000298**
299** If the left side is "database.id" then pId1 is the database name
300** and pId2 is the id. If the left side is just "id" then pId1 is the
301** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000302*/
danielk197791cf71b2004-06-26 06:37:06 +0000303void sqlite3Pragma(
304 Parse *pParse,
305 Token *pId1, /* First part of [database.]id field */
306 Token *pId2, /* Second part of [database.]id field, or NULL */
307 Token *pValue, /* Token for <value>, or NULL */
308 int minusFlag /* True if a '-' sign preceded <value> */
309){
310 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
311 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
312 const char *zDb = 0; /* The database name */
313 Token *pId; /* Pointer to <id> token */
314 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000315 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000316 Db *pDb;
drh949f9cd2008-01-12 21:35:57 +0000317 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
drhc11d4f92003-04-06 21:08:24 +0000318 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000319 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000320 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000321
danielk197791cf71b2004-06-26 06:37:06 +0000322 /* Interpret the [database.] part of the pragma statement. iDb is the
323 ** index of the database this pragma is being applied to in db.aDb[]. */
324 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
325 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000326 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000327
danielk1977ddfb2f02006-02-17 12:25:14 +0000328 /* If the temp database has been explicitly named as part of the
329 ** pragma, make sure it is open.
330 */
331 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
332 return;
333 }
334
drh17435752007-08-16 04:30:38 +0000335 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000336 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000337 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000338 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000339 }else{
drh17435752007-08-16 04:30:38 +0000340 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000341 }
danielk197791cf71b2004-06-26 06:37:06 +0000342
drhd2cb50b2009-01-09 21:41:17 +0000343 assert( pId2 );
344 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000345 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000346 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000347 }
348
drhe73c9142011-11-09 16:12:24 +0000349#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
drhc11d4f92003-04-06 21:08:24 +0000350 /*
drh90f5ecb2004-07-22 01:19:35 +0000351 ** PRAGMA [database.]default_cache_size
352 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000353 **
354 ** The first form reports the current persistent setting for the
355 ** page cache size. The value returned is the maximum number of
356 ** pages in the page cache. The second form sets both the current
357 ** page cache size value and the persistent page cache size value
358 ** stored in the database file.
359 **
drh93791ea2010-04-26 17:36:35 +0000360 ** Older versions of SQLite would set the default cache size to a
361 ** negative number to indicate synchronous=OFF. These days, synchronous
362 ** is always on by default regardless of the sign of the default cache
363 ** size. But continue to take the absolute value of the default cache
364 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000365 */
danielk19774adee202004-05-08 08:23:19 +0000366 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000367 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000368 { OP_Transaction, 0, 0, 0}, /* 0 */
369 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
370 { OP_IfPos, 1, 7, 0},
drh3c84ddf2008-01-09 02:15:38 +0000371 { OP_Integer, 0, 2, 0},
372 { OP_Subtract, 1, 2, 1},
danielk1977602b4662009-07-02 07:47:33 +0000373 { OP_IfPos, 1, 7, 0},
374 { OP_Integer, 0, 1, 0}, /* 6 */
drh3c84ddf2008-01-09 02:15:38 +0000375 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000376 };
drh905793e2004-02-21 13:31:09 +0000377 int addr;
danielk19778a414492004-06-29 08:59:35 +0000378 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000379 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000380 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000381 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000382 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000383 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000384 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000385 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000386 sqlite3VdbeChangeP1(v, addr+1, iDb);
387 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000388 }else{
drhd50ffc42011-03-08 02:38:28 +0000389 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197791cf71b2004-06-26 06:37:06 +0000390 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000391 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000392 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
drh21206082011-04-04 18:22:02 +0000393 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197714db2662006-01-09 16:12:04 +0000394 pDb->pSchema->cache_size = size;
395 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000396 }
397 }else
drhe73c9142011-11-09 16:12:24 +0000398#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
drhc11d4f92003-04-06 21:08:24 +0000399
drhe73c9142011-11-09 16:12:24 +0000400#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
drhc11d4f92003-04-06 21:08:24 +0000401 /*
drh90f5ecb2004-07-22 01:19:35 +0000402 ** PRAGMA [database.]page_size
403 ** PRAGMA [database.]page_size=N
404 **
405 ** The first form reports the current setting for the
406 ** database page size in bytes. The second form sets the
407 ** database page size value. The value can only be set if
408 ** the database has not yet been created.
409 */
410 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
411 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000412 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000413 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000414 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000415 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000416 }else{
danielk1977992772c2007-08-30 10:07:38 +0000417 /* Malloc may fail when setting the page-size, as there is an internal
418 ** buffer that the pager module resizes using sqlite3_realloc().
419 */
drh60ac3f42010-11-23 18:59:27 +0000420 db->nextPagesize = sqlite3Atoi(zRight);
drhe73c9142011-11-09 16:12:24 +0000421 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
danielk1977992772c2007-08-30 10:07:38 +0000422 db->mallocFailed = 1;
423 }
drh90f5ecb2004-07-22 01:19:35 +0000424 }
425 }else
danielk197741483462007-03-24 16:45:04 +0000426
427 /*
drh5b47efa2010-02-12 18:18:39 +0000428 ** PRAGMA [database.]secure_delete
429 ** PRAGMA [database.]secure_delete=ON/OFF
430 **
431 ** The first form reports the current setting for the
432 ** secure_delete flag. The second form changes the secure_delete
433 ** flag setting and reports thenew value.
434 */
435 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
436 Btree *pBt = pDb->pBt;
437 int b = -1;
438 assert( pBt!=0 );
439 if( zRight ){
drh81cc5162011-05-17 20:36:21 +0000440 b = sqlite3GetBoolean(zRight);
drh5b47efa2010-02-12 18:18:39 +0000441 }
drhaf034ed2010-02-12 19:46:26 +0000442 if( pId2->n==0 && b>=0 ){
443 int ii;
444 for(ii=0; ii<db->nDb; ii++){
445 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
446 }
447 }
drh5b47efa2010-02-12 18:18:39 +0000448 b = sqlite3BtreeSecureDelete(pBt, b);
449 returnSingleInt(pParse, "secure_delete", b);
450 }else
451
452 /*
drh60ac3f42010-11-23 18:59:27 +0000453 ** PRAGMA [database.]max_page_count
454 ** PRAGMA [database.]max_page_count=N
455 **
456 ** The first form reports the current setting for the
457 ** maximum number of pages in the database file. The
458 ** second form attempts to change this setting. Both
459 ** forms return the current setting.
460 **
drhe73c9142011-11-09 16:12:24 +0000461 ** The absolute value of N is used. This is undocumented and might
462 ** change. The only purpose is to provide an easy way to test
463 ** the sqlite3AbsInt32() function.
464 **
danielk197759a93792008-05-15 17:48:20 +0000465 ** PRAGMA [database.]page_count
466 **
467 ** Return the number of pages in the specified database.
468 */
drh60ac3f42010-11-23 18:59:27 +0000469 if( sqlite3StrICmp(zLeft,"page_count")==0
470 || sqlite3StrICmp(zLeft,"max_page_count")==0
471 ){
danielk197759a93792008-05-15 17:48:20 +0000472 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000473 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197759a93792008-05-15 17:48:20 +0000474 sqlite3CodeVerifySchema(pParse, iDb);
475 iReg = ++pParse->nMem;
drhc5227312011-10-13 17:09:01 +0000476 if( sqlite3Tolower(zLeft[0])=='p' ){
drh60ac3f42010-11-23 18:59:27 +0000477 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
478 }else{
drhe73c9142011-11-09 16:12:24 +0000479 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
480 sqlite3AbsInt32(sqlite3Atoi(zRight)));
drh60ac3f42010-11-23 18:59:27 +0000481 }
danielk197759a93792008-05-15 17:48:20 +0000482 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
483 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000484 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk197759a93792008-05-15 17:48:20 +0000485 }else
486
487 /*
danielk197741483462007-03-24 16:45:04 +0000488 ** PRAGMA [database.]locking_mode
489 ** PRAGMA [database.]locking_mode = (normal|exclusive)
490 */
491 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
492 const char *zRet = "normal";
493 int eMode = getLockingMode(zRight);
494
495 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
496 /* Simple "PRAGMA locking_mode;" statement. This is a query for
497 ** the current default locking mode (which may be different to
498 ** the locking-mode of the main database).
499 */
500 eMode = db->dfltLockMode;
501 }else{
502 Pager *pPager;
503 if( pId2->n==0 ){
504 /* This indicates that no database name was specified as part
505 ** of the PRAGMA command. In this case the locking-mode must be
506 ** set on all attached databases, as well as the main db file.
507 **
508 ** Also, the sqlite3.dfltLockMode variable is set so that
509 ** any subsequently attached databases also use the specified
510 ** locking mode.
511 */
512 int ii;
513 assert(pDb==&db->aDb[0]);
514 for(ii=2; ii<db->nDb; ii++){
515 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
516 sqlite3PagerLockingMode(pPager, eMode);
517 }
drh4f21c4a2008-12-10 22:15:00 +0000518 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000519 }
520 pPager = sqlite3BtreePager(pDb->pBt);
521 eMode = sqlite3PagerLockingMode(pPager, eMode);
522 }
523
524 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
525 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
526 zRet = "exclusive";
527 }
528 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000529 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000530 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
531 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000532 }else
drh3b020132008-04-17 17:02:01 +0000533
534 /*
535 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000536 ** PRAGMA [database.]journal_mode =
537 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000538 */
539 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000540 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
541 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000542
drh94714062011-10-10 12:04:14 +0000543 /* Force the schema to be loaded on all databases. This causes all
544 ** database files to be opened and the journal_modes set. This is
545 ** necessary because subsequent processing must know if the databases
546 ** are in WAL mode. */
danf6c61472010-07-07 13:54:28 +0000547 if( sqlite3ReadSchema(pParse) ){
548 goto pragma_out;
549 }
550
dane04dc882010-04-20 18:53:15 +0000551 sqlite3VdbeSetNumCols(v, 1);
552 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000553
554 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000555 /* If there is no "=MODE" part of the pragma, do a query for the
556 ** current mode */
drh3b020132008-04-17 17:02:01 +0000557 eMode = PAGER_JOURNALMODE_QUERY;
558 }else{
dane04dc882010-04-20 18:53:15 +0000559 const char *zMode;
drhea678832008-12-10 19:26:22 +0000560 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000561 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000562 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
563 }
564 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000565 /* If the "=MODE" part does not match any known journal mode,
566 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000567 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000568 }
569 }
drhc6b2a0f2010-07-08 17:40:37 +0000570 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
571 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
572 iDb = 0;
573 pId2->n = 1;
574 }
575 for(ii=db->nDb-1; ii>=0; ii--){
576 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
577 sqlite3VdbeUsesBtree(v, ii);
578 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000579 }
drh3b020132008-04-17 17:02:01 +0000580 }
drh3b020132008-04-17 17:02:01 +0000581 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
582 }else
danielk1977b53e4962008-06-04 06:45:59 +0000583
584 /*
585 ** PRAGMA [database.]journal_size_limit
586 ** PRAGMA [database.]journal_size_limit=N
587 **
drha9e364f2009-01-13 20:14:15 +0000588 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000589 */
590 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
591 Pager *pPager = sqlite3BtreePager(pDb->pBt);
592 i64 iLimit = -2;
593 if( zRight ){
drh9339da12010-09-30 00:50:49 +0000594 sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +0000595 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000596 }
597 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000598 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000599 }else
600
drh13d70422004-11-13 15:59:14 +0000601#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000602
603 /*
danielk1977951af802004-11-05 15:45:09 +0000604 ** PRAGMA [database.]auto_vacuum
605 ** PRAGMA [database.]auto_vacuum=N
606 **
drha9e364f2009-01-13 20:14:15 +0000607 ** Get or set the value of the database 'auto-vacuum' parameter.
608 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000609 */
610#ifndef SQLITE_OMIT_AUTOVACUUM
611 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
612 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000613 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000614 if( sqlite3ReadSchema(pParse) ){
615 goto pragma_out;
616 }
danielk1977951af802004-11-05 15:45:09 +0000617 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000618 int auto_vacuum;
619 if( ALWAYS(pBt) ){
620 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
621 }else{
622 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
623 }
danielk1977951af802004-11-05 15:45:09 +0000624 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
625 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000626 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000627 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000628 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000629 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000630 /* Call SetAutoVacuum() to set initialize the internal auto and
631 ** incr-vacuum flags. This is required in case this connection
632 ** creates the database file. It is important that it is created
633 ** as an auto-vacuum capable db.
634 */
635 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
636 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
637 /* When setting the auto_vacuum mode to either "full" or
638 ** "incremental", write the value of meta[6] in the database
639 ** file. Before writing to meta[6], check that meta[3] indicates
640 ** that this really is an auto-vacuum capable database.
641 */
642 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000643 { OP_Transaction, 0, 1, 0}, /* 0 */
644 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
645 { OP_If, 1, 0, 0}, /* 2 */
646 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
647 { OP_Integer, 0, 1, 0}, /* 4 */
648 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000649 };
650 int iAddr;
651 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
652 sqlite3VdbeChangeP1(v, iAddr, iDb);
653 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
654 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
655 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
656 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000657 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000658 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000659 }
danielk1977951af802004-11-05 15:45:09 +0000660 }
661 }else
662#endif
663
drhca5557f2007-05-04 18:30:40 +0000664 /*
665 ** PRAGMA [database.]incremental_vacuum(N)
666 **
667 ** Do N steps of incremental vacuuming on a database.
668 */
669#ifndef SQLITE_OMIT_AUTOVACUUM
670 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
671 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000672 if( sqlite3ReadSchema(pParse) ){
673 goto pragma_out;
674 }
drhca5557f2007-05-04 18:30:40 +0000675 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
676 iLimit = 0x7fffffff;
677 }
678 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000679 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000680 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000681 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000682 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000683 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000684 sqlite3VdbeJumpHere(v, addr);
685 }else
686#endif
687
drh13d70422004-11-13 15:59:14 +0000688#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000689 /*
drh90f5ecb2004-07-22 01:19:35 +0000690 ** PRAGMA [database.]cache_size
691 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000692 **
693 ** The first form reports the current local setting for the
drh3b42abb2011-11-09 14:23:04 +0000694 ** page cache size. The second form sets the local
695 ** page cache size value. If N is positive then that is the
696 ** number of pages in the cache. If N is negative, then the
697 ** number of pages is adjusted so that the cache uses -N kibibytes
698 ** of memory.
drhc11d4f92003-04-06 21:08:24 +0000699 */
danielk19774adee202004-05-08 08:23:19 +0000700 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000701 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh21206082011-04-04 18:22:02 +0000702 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk197791cf71b2004-06-26 06:37:06 +0000703 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000704 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000705 }else{
drh3b42abb2011-11-09 14:23:04 +0000706 int size = sqlite3Atoi(zRight);
danielk197714db2662006-01-09 16:12:04 +0000707 pDb->pSchema->cache_size = size;
708 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000709 }
710 }else
711
712 /*
drh90f5ecb2004-07-22 01:19:35 +0000713 ** PRAGMA temp_store
714 ** PRAGMA temp_store = "default"|"memory"|"file"
715 **
716 ** Return or set the local value of the temp_store flag. Changing
717 ** the local value does not make changes to the disk file and the default
718 ** value will be restored the next time the database is opened.
719 **
720 ** Note that it is possible for the library compile-time options to
721 ** override this setting
722 */
723 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
724 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000725 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000726 }else{
727 changeTempStorage(pParse, zRight);
728 }
729 }else
730
731 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000732 ** PRAGMA temp_store_directory
733 ** PRAGMA temp_store_directory = ""|"directory_name"
734 **
735 ** Return or set the local value of the temp_store_directory flag. Changing
736 ** the value sets a specific directory to be used for temporary files.
737 ** Setting to a null string reverts to the default temporary directory search.
738 ** If temporary directory is changed, then invalidateTempStorage.
739 **
740 */
741 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
742 if( !zRight ){
743 if( sqlite3_temp_directory ){
744 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000745 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000746 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000747 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
748 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000749 }
750 }else{
drh78f82d12008-09-02 00:52:52 +0000751#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000752 if( zRight[0] ){
danielk1977fab11272008-09-16 14:38:02 +0000753 int rc;
danielk1977861f7452008-06-05 11:39:11 +0000754 int res;
danielk1977fab11272008-09-16 14:38:02 +0000755 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
756 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000757 sqlite3ErrorMsg(pParse, "not a writable directory");
758 goto pragma_out;
759 }
drh268283b2005-01-08 15:44:25 +0000760 }
danielk1977b06a0b62008-06-26 10:54:12 +0000761 if( SQLITE_TEMP_STORE==0
762 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
763 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000764 ){
765 invalidateTempStorage(pParse);
766 }
drh17435752007-08-16 04:30:38 +0000767 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000768 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000769 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000770 }else{
drh268283b2005-01-08 15:44:25 +0000771 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000772 }
drh78f82d12008-09-02 00:52:52 +0000773#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000774 }
775 }else
776
drhd2cb50b2009-01-09 21:41:17 +0000777#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
778# if defined(__APPLE__)
779# define SQLITE_ENABLE_LOCKING_STYLE 1
780# else
781# define SQLITE_ENABLE_LOCKING_STYLE 0
782# endif
783#endif
784#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000785 /*
aswiftaebf4132008-11-21 00:10:35 +0000786 ** PRAGMA [database.]lock_proxy_file
787 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
788 **
789 ** Return or set the value of the lock_proxy_file flag. Changing
790 ** the value sets a specific file to be used for database access locks.
791 **
792 */
793 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
794 if( !zRight ){
795 Pager *pPager = sqlite3BtreePager(pDb->pBt);
796 char *proxy_file_path = NULL;
797 sqlite3_file *pFile = sqlite3PagerFile(pPager);
798 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
799 &proxy_file_path);
800
801 if( proxy_file_path ){
802 sqlite3VdbeSetNumCols(v, 1);
803 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
804 "lock_proxy_file", SQLITE_STATIC);
805 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
806 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
807 }
808 }else{
809 Pager *pPager = sqlite3BtreePager(pDb->pBt);
810 sqlite3_file *pFile = sqlite3PagerFile(pPager);
811 int res;
812 if( zRight[0] ){
813 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
814 zRight);
815 } else {
816 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
817 NULL);
818 }
819 if( res!=SQLITE_OK ){
820 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
821 goto pragma_out;
822 }
823 }
824 }else
drhd2cb50b2009-01-09 21:41:17 +0000825#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000826
827 /*
drh90f5ecb2004-07-22 01:19:35 +0000828 ** PRAGMA [database.]synchronous
829 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000830 **
831 ** Return or set the local value of the synchronous flag. Changing
832 ** the local value does not make changes to the disk file and the
833 ** default value will be restored the next time the database is
834 ** opened.
835 */
danielk19774adee202004-05-08 08:23:19 +0000836 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000837 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000838 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000839 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000840 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000841 if( !db->autoCommit ){
842 sqlite3ErrorMsg(pParse,
843 "Safety level may not be changed inside a transaction");
844 }else{
drh90f5ecb2004-07-22 01:19:35 +0000845 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000846 }
drhc11d4f92003-04-06 21:08:24 +0000847 }
848 }else
drh13d70422004-11-13 15:59:14 +0000849#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000850
drhbf216272005-02-26 18:10:44 +0000851#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000852 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000853 /* The flagPragma() subroutine also generates any necessary code
854 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000855 }else
drhbf216272005-02-26 18:10:44 +0000856#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000857
drh13d70422004-11-13 15:59:14 +0000858#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000859 /*
860 ** PRAGMA table_info(<table>)
861 **
862 ** Return a single row for each column of the named table. The columns of
863 ** the returned data set are:
864 **
865 ** cid: Column id (numbered from left to right, starting at 0)
866 ** name: Column name
867 ** type: Column declaration type.
868 ** notnull: True if 'NOT NULL' is part of column declaration
869 ** dflt_value: The default value for the column, if any.
870 */
drh5260f7e2004-06-26 19:35:29 +0000871 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000872 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000873 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000874 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000875 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000876 int i;
danielk1977034ca142007-06-26 10:38:54 +0000877 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000878 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000879 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000880 pParse->nMem = 6;
danielk197710fb7492008-10-31 10:53:22 +0000881 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
882 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
883 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
884 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
885 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
886 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000887 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000888 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +0000889 if( IsHiddenColumn(pCol) ){
890 nHidden++;
891 continue;
892 }
drh2d401ab2008-01-10 23:50:11 +0000893 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
894 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
895 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000896 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000897 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +0000898 if( pCol->zDflt ){
899 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +0000900 }else{
drh2d401ab2008-01-10 23:50:11 +0000901 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000902 }
drh2d401ab2008-01-10 23:50:11 +0000903 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
904 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000905 }
906 }
907 }else
908
drh5260f7e2004-06-26 19:35:29 +0000909 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000910 Index *pIdx;
911 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000912 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000913 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000914 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000915 int i;
916 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000917 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000918 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000919 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
920 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
921 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000922 for(i=0; i<pIdx->nColumn; i++){
923 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000924 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
925 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000926 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000927 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
928 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000929 }
930 }
931 }else
932
drh5260f7e2004-06-26 19:35:29 +0000933 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000934 Index *pIdx;
935 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000936 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000937 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000938 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000939 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000940 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000941 if( pIdx ){
942 int i = 0;
943 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000944 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000945 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
946 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
947 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000948 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000949 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
950 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
951 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
952 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000953 ++i;
954 pIdx = pIdx->pNext;
955 }
drhc11d4f92003-04-06 21:08:24 +0000956 }
957 }
958 }else
959
drh13d70422004-11-13 15:59:14 +0000960 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
961 int i;
962 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
963 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000964 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000965 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
966 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
967 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +0000968 for(i=0; i<db->nDb; i++){
969 if( db->aDb[i].pBt==0 ) continue;
970 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000971 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
972 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
973 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000974 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000975 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +0000976 }
977 }else
danielk197748af65a2005-02-09 03:20:37 +0000978
979 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
980 int i = 0;
981 HashElem *p;
982 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +0000983 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +0000984 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
985 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000986 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
987 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +0000988 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
989 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
990 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +0000991 }
992 }else
drh13d70422004-11-13 15:59:14 +0000993#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
994
drhb7f91642004-10-31 02:22:47 +0000995#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000996 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000997 FKey *pFK;
998 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000999 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001000 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001001 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001002 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001003 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001004 if( pFK ){
1005 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001006 sqlite3VdbeSetNumCols(v, 8);
1007 pParse->nMem = 8;
danielk197710fb7492008-10-31 10:53:22 +00001008 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1009 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1010 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1011 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1012 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1013 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1014 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1015 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001016 while(pFK){
1017 int j;
1018 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001019 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001020 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1021 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001022 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1023 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1024 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1025 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001026 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001027 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001028 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1029 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1030 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1031 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001032 }
1033 ++i;
1034 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001035 }
drh78100cc2003-08-23 22:40:53 +00001036 }
1037 }
1038 }else
drhb7f91642004-10-31 02:22:47 +00001039#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001040
drhc11d4f92003-04-06 21:08:24 +00001041#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001042 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001043 if( zRight ){
drh81cc5162011-05-17 20:36:21 +00001044 if( sqlite3GetBoolean(zRight) ){
drh55ef4d92005-08-14 01:20:37 +00001045 sqlite3ParserTrace(stderr, "parser: ");
1046 }else{
1047 sqlite3ParserTrace(0, 0);
1048 }
drhc11d4f92003-04-06 21:08:24 +00001049 }
1050 }else
1051#endif
1052
drh55ef4d92005-08-14 01:20:37 +00001053 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1054 ** used will be case sensitive or not depending on the RHS.
1055 */
1056 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1057 if( zRight ){
drh81cc5162011-05-17 20:36:21 +00001058 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight));
drh55ef4d92005-08-14 01:20:37 +00001059 }
1060 }else
1061
drh1dcdbc02007-01-27 02:24:54 +00001062#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1063# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1064#endif
1065
drhb7f91642004-10-31 02:22:47 +00001066#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +00001067 /* Pragma "quick_check" is an experimental reduced version of
1068 ** integrity_check designed to detect most database corruption
1069 ** without most of the overhead of a full integrity-check.
1070 */
1071 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1072 || sqlite3StrICmp(zLeft, "quick_check")==0
1073 ){
drh1dcdbc02007-01-27 02:24:54 +00001074 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001075
drhed717fe2003-06-15 23:42:24 +00001076 /* Code that appears at the end of the integrity check. If no error
1077 ** messages have been generated, output OK. Otherwise output the
1078 ** error message
1079 */
drh57196282004-10-06 15:41:16 +00001080 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001081 { OP_AddImm, 1, 0, 0}, /* 0 */
1082 { OP_IfNeg, 1, 0, 0}, /* 1 */
1083 { OP_String8, 0, 3, 0}, /* 2 */
1084 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001085 };
drhed717fe2003-06-15 23:42:24 +00001086
drhc5227312011-10-13 17:09:01 +00001087 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
danielk197741c58b72007-12-29 13:39:19 +00001088
drhed717fe2003-06-15 23:42:24 +00001089 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001090 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001091 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001092 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001093 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001094
1095 /* Set the maximum error count */
1096 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1097 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001098 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001099 if( mxErr<=0 ){
1100 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1101 }
1102 }
drh2d401ab2008-01-10 23:50:11 +00001103 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001104
1105 /* Do an integrity check on each database file */
1106 for(i=0; i<db->nDb; i++){
1107 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001108 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001109 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001110
danielk197753c0f742005-03-29 03:10:59 +00001111 if( OMIT_TEMPDB && i==1 ) continue;
1112
drh80242052004-06-09 00:48:12 +00001113 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001114 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001115 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001116 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001117
drhed717fe2003-06-15 23:42:24 +00001118 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001119 **
1120 ** Begin by filling registers 2, 3, ... with the root pages numbers
1121 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001122 */
drh21206082011-04-04 18:22:02 +00001123 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00001124 pTbls = &db->aDb[i].pSchema->tblHash;
1125 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001126 Table *pTab = sqliteHashData(x);
1127 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001128 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001129 cnt++;
1130 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001131 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001132 cnt++;
1133 }
1134 }
drh2d401ab2008-01-10 23:50:11 +00001135
1136 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001137 if( pParse->nMem < cnt+4 ){
1138 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001139 }
drh2d401ab2008-01-10 23:50:11 +00001140
1141 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001142 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001143 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001144 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1145 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001146 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001147 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001148 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001149 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001150 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001151 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001152
1153 /* Make sure all the indices are constructed correctly.
1154 */
danielk197741c58b72007-12-29 13:39:19 +00001155 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001156 Table *pTab = sqliteHashData(x);
1157 Index *pIdx;
1158 int loopTop;
1159
1160 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001161 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001162 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001163 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001164 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001165 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001166 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001167 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001168 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001169 int jmp2;
drh91fc4a02009-11-12 20:39:03 +00001170 int r1;
drh57196282004-10-06 15:41:16 +00001171 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001172 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001173 { OP_String8, 0, 3, 0}, /* 1 */
1174 { OP_Rowid, 1, 4, 0},
1175 { OP_String8, 0, 5, 0}, /* 3 */
1176 { OP_String8, 0, 6, 0}, /* 4 */
1177 { OP_Concat, 4, 3, 3},
1178 { OP_Concat, 5, 3, 3},
1179 { OP_Concat, 6, 3, 3},
1180 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001181 { OP_IfPos, 1, 0, 0}, /* 9 */
1182 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001183 };
drh91fc4a02009-11-12 20:39:03 +00001184 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
1185 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001186 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001187 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1188 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001189 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
drhbb8a2792008-03-19 00:21:30 +00001190 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001191 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001192 }
drh66a51672008-01-03 00:01:23 +00001193 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001194 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001195 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001196 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001197 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001198 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001199 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001200 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001201 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001202 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001203 { OP_String8, 0, 2, 0}, /* 6 */
1204 { OP_String8, 0, 3, 0}, /* 7 */
1205 { OP_Concat, 3, 2, 2},
1206 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001207 };
drh3c84ddf2008-01-09 02:15:38 +00001208 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001209 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001210 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001211 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001212 sqlite3VdbeChangeP1(v, addr+1, j+2);
1213 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1214 sqlite3VdbeChangeP1(v, addr+3, j+2);
1215 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001216 sqlite3VdbeJumpHere(v, addr+4);
1217 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001218 "wrong # of entries in index ", P4_STATIC);
drh8d129422011-04-05 12:25:19 +00001219 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
drhed717fe2003-06-15 23:42:24 +00001220 }
1221 }
1222 }
danielk19774adee202004-05-08 08:23:19 +00001223 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001224 sqlite3VdbeChangeP2(v, addr, -mxErr);
1225 sqlite3VdbeJumpHere(v, addr+1);
1226 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001227 }else
drhb7f91642004-10-31 02:22:47 +00001228#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1229
drh13d70422004-11-13 15:59:14 +00001230#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001231 /*
1232 ** PRAGMA encoding
1233 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1234 **
drh85b623f2007-12-13 21:54:09 +00001235 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001236 ** database. If the database is not initialized, it is initialized now.
1237 **
1238 ** The second form of this pragma is a no-op if the main database file
1239 ** has not already been initialized. In this case it sets the default
1240 ** encoding that will be used for the main database file if a new file
1241 ** is created. If an existing main database file is opened, then the
1242 ** default text encoding for the existing database is used.
1243 **
1244 ** In all cases new databases created using the ATTACH command are
1245 ** created to use the same default text encoding as the main database. If
1246 ** the main database has not been initialized and/or created when ATTACH
1247 ** is executed, this is done before the ATTACH operation.
1248 **
1249 ** In the second form this pragma sets the text encoding to be used in
1250 ** new database files created using this database handle. It is only
1251 ** useful if invoked immediately after the main database i
1252 */
1253 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001254 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001255 char *zName;
1256 u8 enc;
1257 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001258 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001259 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1260 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1261 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001262 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001263 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001264 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1265 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001266 { 0, 0 }
1267 };
drh0f7eb612006-08-08 13:51:43 +00001268 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001269 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001270 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001271 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001272 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001273 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001274 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1275 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1276 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1277 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001278 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001279 }else{ /* "PRAGMA encoding = XXX" */
1280 /* Only change the value of sqlite.enc if the database handle is not
1281 ** initialized. If the main database exists, the new sqlite.enc value
1282 ** will be overwritten when the schema is next loaded. If it does not
1283 ** already exists, it will be created to use the new encoding value.
1284 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001285 if(
1286 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1287 DbHasProperty(db, 0, DB_Empty)
1288 ){
danielk19778e227872004-06-07 07:52:17 +00001289 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1290 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001291 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001292 break;
1293 }
1294 }
1295 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001296 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001297 }
1298 }
1299 }
1300 }else
drh13d70422004-11-13 15:59:14 +00001301#endif /* SQLITE_OMIT_UTF16 */
1302
1303#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001304 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001305 ** PRAGMA [database.]schema_version
1306 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001307 **
danielk1977b92b70b2004-11-12 16:11:59 +00001308 ** PRAGMA [database.]user_version
1309 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001310 **
danielk1977b92b70b2004-11-12 16:11:59 +00001311 ** The pragma's schema_version and user_version are used to set or get
1312 ** the value of the schema-version and user-version, respectively. Both
1313 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001314 ** stored in the database header.
1315 **
1316 ** The schema-cookie is usually only manipulated internally by SQLite. It
1317 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001318 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001319 ** SQLite each time a query is executed to ensure that the internal cache
1320 ** of the schema used when compiling the SQL query matches the schema of
1321 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001322 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1323 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001324 ** crashes or database corruption. Use with caution!
1325 **
danielk1977b92b70b2004-11-12 16:11:59 +00001326 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001327 ** applications for any purpose.
1328 */
danielk1977180b56a2007-06-24 08:00:42 +00001329 if( sqlite3StrICmp(zLeft, "schema_version")==0
1330 || sqlite3StrICmp(zLeft, "user_version")==0
1331 || sqlite3StrICmp(zLeft, "freelist_count")==0
1332 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001333 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001334 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001335 switch( zLeft[0] ){
danielk1977180b56a2007-06-24 08:00:42 +00001336 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001337 iCookie = BTREE_FREE_PAGE_COUNT;
1338 break;
1339 case 's': case 'S':
1340 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001341 break;
1342 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001343 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001344 break;
danielk1977dae24952004-11-11 05:10:43 +00001345 }
1346
danielk19770d19f7a2009-06-03 11:25:07 +00001347 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001348 /* Write the specified cookie value */
1349 static const VdbeOpList setCookie[] = {
1350 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001351 { OP_Integer, 0, 1, 0}, /* 1 */
1352 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001353 };
1354 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1355 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001356 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001357 sqlite3VdbeChangeP1(v, addr+2, iDb);
1358 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1359 }else{
1360 /* Read the specified cookie value */
1361 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001362 { OP_Transaction, 0, 0, 0}, /* 0 */
1363 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001364 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001365 };
1366 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1367 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001368 sqlite3VdbeChangeP1(v, addr+1, iDb);
1369 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001370 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001371 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001372 }
drh6e345992007-03-30 11:12:08 +00001373 }else
drh13d70422004-11-13 15:59:14 +00001374#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001375
shanehdc97a8c2010-02-23 20:08:35 +00001376#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1377 /*
1378 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001379 **
drh71caabf2010-02-26 15:39:24 +00001380 ** Return the names of all compile-time options used in this build,
1381 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001382 */
drh264a2d42010-02-25 15:28:41 +00001383 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
shanehdc97a8c2010-02-23 20:08:35 +00001384 int i = 0;
1385 const char *zOpt;
1386 sqlite3VdbeSetNumCols(v, 1);
1387 pParse->nMem = 1;
1388 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1389 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1390 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1391 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1392 }
1393 }else
1394#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1395
dan5cf53532010-05-01 16:40:20 +00001396#ifndef SQLITE_OMIT_WAL
1397 /*
dancdc1f042010-11-18 12:11:05 +00001398 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00001399 **
1400 ** Checkpoint the database.
1401 */
dan5a299f92010-05-03 11:05:08 +00001402 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
dana58f26f2010-11-16 18:56:51 +00001403 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001404 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1405 if( zRight ){
1406 if( sqlite3StrICmp(zRight, "full")==0 ){
1407 eMode = SQLITE_CHECKPOINT_FULL;
1408 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1409 eMode = SQLITE_CHECKPOINT_RESTART;
1410 }
1411 }
dan5a299f92010-05-03 11:05:08 +00001412 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
dancdc1f042010-11-18 12:11:05 +00001413 sqlite3VdbeSetNumCols(v, 3);
1414 pParse->nMem = 3;
1415 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1416 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1417 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1418
drh30aa3b92011-02-07 23:56:01 +00001419 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001420 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
dan7c246102010-04-12 19:00:29 +00001421 }else
dan5a299f92010-05-03 11:05:08 +00001422
1423 /*
1424 ** PRAGMA wal_autocheckpoint
1425 ** PRAGMA wal_autocheckpoint = N
1426 **
1427 ** Configure a database connection to automatically checkpoint a database
1428 ** after accumulating N frames in the log. Or query for the current value
1429 ** of N.
1430 */
1431 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
1432 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001433 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001434 }
drhb033d8b2010-05-03 13:37:30 +00001435 returnSingleInt(pParse, "wal_autocheckpoint",
1436 db->xWalCallback==sqlite3WalDefaultHook ?
1437 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
dan5a299f92010-05-03 11:05:08 +00001438 }else
dan5cf53532010-05-01 16:40:20 +00001439#endif
dan7c246102010-04-12 19:00:29 +00001440
drh09419b42011-11-16 19:29:17 +00001441 /*
1442 ** PRAGMA shrink_memory
1443 **
1444 ** This pragma attempts to free as much memory as possible from the
1445 ** current database connection.
1446 */
1447 if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
1448 sqlite3_db_release_memory(db);
1449 }else
1450
dougcurrie81c95ef2004-06-18 23:21:47 +00001451#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001452 /*
1453 ** Report the current state of file logs for all databases
1454 */
1455 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001456 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001457 "unlocked", "shared", "reserved", "pending", "exclusive"
1458 };
1459 int i;
drh89ac8c12004-06-09 14:17:20 +00001460 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001461 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001462 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1463 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001464 for(i=0; i<db->nDb; i++){
1465 Btree *pBt;
1466 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001467 const char *zState = "unknown";
1468 int j;
drh89ac8c12004-06-09 14:17:20 +00001469 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001470 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001471 pBt = db->aDb[i].pBt;
1472 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001473 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001474 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001475 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1476 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001477 }
drh2d401ab2008-01-10 23:50:11 +00001478 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1479 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001480 }
danielk1977a4de4532008-09-02 15:44:08 +00001481
drh89ac8c12004-06-09 14:17:20 +00001482 }else
1483#endif
1484
shanehad9f9f62010-02-17 17:48:46 +00001485#ifdef SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001486 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhea678832008-12-10 19:26:22 +00001487 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001488 }else
drhd2cb50b2009-01-09 21:41:17 +00001489 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1490 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1491 }else
1492 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1493 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1494 int i, h1, h2;
1495 char zKey[40];
1496 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1497 h1 += 9*(1&(h1>>6));
1498 h2 += 9*(1&(h2>>6));
1499 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1500 }
1501 if( (zLeft[3] & 0xf)==0xb ){
1502 sqlite3_key(db, zKey, i/2);
1503 }else{
1504 sqlite3_rekey(db, zKey, i/2);
1505 }
1506 }else
drh3c4f2a42005-12-08 18:12:56 +00001507#endif
shanehad9f9f62010-02-17 17:48:46 +00001508#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh21e2cab2006-09-25 18:01:57 +00001509 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
shanehad9f9f62010-02-17 17:48:46 +00001510#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001511 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001512 sqlite3_activate_see(&zRight[4]);
1513 }
1514#endif
1515#ifdef SQLITE_ENABLE_CEROD
1516 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001517 sqlite3_activate_cerod(&zRight[6]);
1518 }
1519#endif
drhd2cb50b2009-01-09 21:41:17 +00001520 }else
drh21e2cab2006-09-25 18:01:57 +00001521#endif
drh3c4f2a42005-12-08 18:12:56 +00001522
drhd2cb50b2009-01-09 21:41:17 +00001523
1524 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001525
drhd2cb50b2009-01-09 21:41:17 +00001526 /*
1527 ** Reset the safety level, in case the fullfsync flag or synchronous
1528 ** setting changed.
1529 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001530#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhd2cb50b2009-01-09 21:41:17 +00001531 if( db->autoCommit ){
1532 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
drhc97d8462010-11-19 18:23:35 +00001533 (db->flags&SQLITE_FullFSync)!=0,
1534 (db->flags&SQLITE_CkptFullFSync)!=0);
danielk1977a21c6b62005-01-24 10:25:59 +00001535 }
drhd2cb50b2009-01-09 21:41:17 +00001536#endif
danielk1977e0048402004-06-15 16:51:01 +00001537pragma_out:
drh633e6d52008-07-28 19:34:53 +00001538 sqlite3DbFree(db, zLeft);
1539 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001540}
drh13d70422004-11-13 15:59:14 +00001541
drh8bfdf722009-06-19 14:06:03 +00001542#endif /* SQLITE_OMIT_PRAGMA */