blob: a6a8cd14107a6131b539702d4bd69e5e46500dcf [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
drh13d70422004-11-13 15:59:14 +000016/* Ignore this whole file if pragmas are disabled
17*/
drh8bfdf722009-06-19 14:06:03 +000018#if !defined(SQLITE_OMIT_PRAGMA)
drh13d70422004-11-13 15:59:14 +000019
drhc11d4f92003-04-06 21:08:24 +000020/*
drhc11d4f92003-04-06 21:08:24 +000021** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000022** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
23** unrecognized string argument.
drhc11d4f92003-04-06 21:08:24 +000024**
25** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000026** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000027** to support legacy SQL code. The safety level used to be boolean
28** and older scripts may have used numbers 0 for OFF and 1 for ON.
29*/
drh4f21c4a2008-12-10 22:15:00 +000030static u8 getSafetyLevel(const char *z){
drh722e95a2004-10-25 20:33:44 +000031 /* 123456789 123456789 */
32 static const char zText[] = "onoffalseyestruefull";
33 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
34 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
35 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
36 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000037 if( sqlite3Isdigit(*z) ){
drh3abbd392008-12-10 23:04:13 +000038 return (u8)atoi(z);
drhc11d4f92003-04-06 21:08:24 +000039 }
drhea678832008-12-10 19:26:22 +000040 n = sqlite3Strlen30(z);
danielk197700e13612008-11-17 19:18:54 +000041 for(i=0; i<ArraySize(iLength); i++){
drh722e95a2004-10-25 20:33:44 +000042 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
43 return iValue[i];
44 }
drhc11d4f92003-04-06 21:08:24 +000045 }
46 return 1;
47}
48
49/*
drh722e95a2004-10-25 20:33:44 +000050** Interpret the given string as a boolean value.
51*/
drh4f21c4a2008-12-10 22:15:00 +000052static u8 getBoolean(const char *z){
drh722e95a2004-10-25 20:33:44 +000053 return getSafetyLevel(z)&1;
54}
55
danielk197741483462007-03-24 16:45:04 +000056/*
57** Interpret the given string as a locking mode value.
58*/
59static int getLockingMode(const char *z){
60 if( z ){
61 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
62 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
63 }
64 return PAGER_LOCKINGMODE_QUERY;
65}
66
danielk1977dddbcdc2007-04-26 14:42:34 +000067#ifndef SQLITE_OMIT_AUTOVACUUM
68/*
69** Interpret the given string as an auto-vacuum mode value.
70**
71** The following strings, "none", "full" and "incremental" are
72** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
73*/
74static int getAutoVacuum(const char *z){
75 int i;
76 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
77 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
78 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
79 i = atoi(z);
drh4f21c4a2008-12-10 22:15:00 +000080 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +000081}
82#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
83
danielk1977b84f96f2005-01-20 11:32:23 +000084#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000085/*
drh90f5ecb2004-07-22 01:19:35 +000086** Interpret the given string as a temp db location. Return 1 for file
87** backed temporary databases, 2 for the Red-Black tree in memory database
88** and 0 to use the compile-time default.
89*/
90static int getTempStore(const char *z){
91 if( z[0]>='0' && z[0]<='2' ){
92 return z[0] - '0';
93 }else if( sqlite3StrICmp(z, "file")==0 ){
94 return 1;
95 }else if( sqlite3StrICmp(z, "memory")==0 ){
96 return 2;
97 }else{
98 return 0;
99 }
100}
drhbf216272005-02-26 18:10:44 +0000101#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000102
drhbf216272005-02-26 18:10:44 +0000103#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000104/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000105** Invalidate temp storage, either when the temp storage is changed
106** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000107*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000108static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000109 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000110 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000111 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000112 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
113 "from within a transaction");
114 return SQLITE_ERROR;
115 }
116 sqlite3BtreeClose(db->aDb[1].pBt);
117 db->aDb[1].pBt = 0;
118 sqlite3ResetInternalSchema(db, 0);
119 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000120 return SQLITE_OK;
121}
drhbf216272005-02-26 18:10:44 +0000122#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000123
drhbf216272005-02-26 18:10:44 +0000124#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000125/*
126** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000127** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000128** or DEFAULT_TEMP_STORE pragmas.
129*/
130static int changeTempStorage(Parse *pParse, const char *zStorageType){
131 int ts = getTempStore(zStorageType);
132 sqlite3 *db = pParse->db;
133 if( db->temp_store==ts ) return SQLITE_OK;
134 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
135 return SQLITE_ERROR;
136 }
drh4f21c4a2008-12-10 22:15:00 +0000137 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000138 return SQLITE_OK;
139}
drhbf216272005-02-26 18:10:44 +0000140#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000141
142/*
143** Generate code to return a single integer value.
144*/
drh3c713642009-04-04 16:02:32 +0000145static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000146 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000147 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000148 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
149 if( pI64 ){
150 memcpy(pI64, &value, sizeof(value));
151 }
152 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000153 sqlite3VdbeSetNumCols(v, 1);
154 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000155 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000156}
157
drhbf216272005-02-26 18:10:44 +0000158#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000159/*
drh87223182004-02-21 14:00:29 +0000160** Check to see if zRight and zLeft refer to a pragma that queries
161** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
162** Also, implement the pragma.
163*/
164static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000165 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000166 const char *zName; /* Name of the pragma */
167 int mask; /* Mask for the db->flags value */
168 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000169 { "full_column_names", SQLITE_FullColNames },
170 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000171 { "count_changes", SQLITE_CountRows },
172 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000173 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000174 { "fullfsync", SQLITE_FullFSync },
drh699b3d42009-02-23 16:52:07 +0000175 { "reverse_unordered_selects", SQLITE_ReverseOrder },
drhc6339082010-04-07 16:54:58 +0000176#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
177 { "automatic_index", SQLITE_AutoIndex },
178#endif
drhcf1023c2007-05-08 20:59:49 +0000179#ifdef SQLITE_DEBUG
180 { "sql_trace", SQLITE_SqlTrace },
181 { "vdbe_listing", SQLITE_VdbeListing },
182 { "vdbe_trace", SQLITE_VdbeTrace },
183#endif
drh0cd2d4c2005-11-03 02:15:02 +0000184#ifndef SQLITE_OMIT_CHECK
185 { "ignore_check_constraints", SQLITE_IgnoreChecks },
186#endif
drh722e95a2004-10-25 20:33:44 +0000187 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000188 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
drh7bec5052005-02-06 02:45:41 +0000189 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000190
191 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
192 ** flag if there are any active statements. */
193 { "read_uncommitted", SQLITE_ReadUncommitted },
dan5bde73c2009-09-01 17:11:07 +0000194 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000195
dan75cbd982009-09-21 16:06:03 +0000196 /* This flag may only be set if both foreign-key and trigger support
197 ** are present in the build. */
198#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan1da40a32009-09-19 17:00:31 +0000199 { "foreign_keys", SQLITE_ForeignKeys },
dan75cbd982009-09-21 16:06:03 +0000200#endif
drh87223182004-02-21 14:00:29 +0000201 };
202 int i;
drh722e95a2004-10-25 20:33:44 +0000203 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000204 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000205 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000206 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000207 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000208 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000209 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
210 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000211 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000212 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000213 }else{
dan75cbd982009-09-21 16:06:03 +0000214 int mask = p->mask; /* Mask of bits to set or clear. */
215 if( db->autoCommit==0 ){
216 /* Foreign key support may not be enabled or disabled while not
217 ** in auto-commit mode. */
218 mask &= ~(SQLITE_ForeignKeys);
219 }
220
danielk1977a21c6b62005-01-24 10:25:59 +0000221 if( getBoolean(zRight) ){
dan75cbd982009-09-21 16:06:03 +0000222 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000223 }else{
dan75cbd982009-09-21 16:06:03 +0000224 db->flags &= ~mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000225 }
danielk19778e556522007-11-13 10:30:24 +0000226
227 /* Many of the flag-pragmas modify the code generated by the SQL
228 ** compiler (eg. count_changes). So add an opcode to expire all
229 ** compiled SQL statements after modifying a pragma value.
230 */
drh66a51672008-01-03 00:01:23 +0000231 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000232 }
drh87223182004-02-21 14:00:29 +0000233 }
danielk19778e556522007-11-13 10:30:24 +0000234
drh87223182004-02-21 14:00:29 +0000235 return 1;
236 }
237 }
238 return 0;
239}
drhbf216272005-02-26 18:10:44 +0000240#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000241
drhd2cb50b2009-01-09 21:41:17 +0000242/*
243** Return a human-readable name for a constraint resolution action.
244*/
danba9108b2009-09-22 07:13:42 +0000245#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000246static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000247 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000248 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000249 case OE_SetNull: zName = "SET NULL"; break;
250 case OE_SetDflt: zName = "SET DEFAULT"; break;
251 case OE_Cascade: zName = "CASCADE"; break;
252 case OE_Restrict: zName = "RESTRICT"; break;
253 default: zName = "NO ACTION";
254 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000255 }
drhd2cb50b2009-01-09 21:41:17 +0000256 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000257}
danba9108b2009-09-22 07:13:42 +0000258#endif
danielk197750af3e12008-10-10 17:47:21 +0000259
dane04dc882010-04-20 18:53:15 +0000260
261/*
262** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
263** defined in pager.h. This function returns the associated lowercase
264** journal-mode name.
265*/
266const char *sqlite3JournalModename(int eMode){
267 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000268 "delete", "persist", "off", "truncate", "memory"
269#ifndef SQLITE_OMIT_WAL
270 , "wal"
271#endif
dane04dc882010-04-20 18:53:15 +0000272 };
273 assert( PAGER_JOURNALMODE_DELETE==0 );
274 assert( PAGER_JOURNALMODE_PERSIST==1 );
275 assert( PAGER_JOURNALMODE_OFF==2 );
276 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
277 assert( PAGER_JOURNALMODE_MEMORY==4 );
278 assert( PAGER_JOURNALMODE_WAL==5 );
279 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
280
281 if( eMode==ArraySize(azModeName) ) return 0;
282 return azModeName[eMode];
283}
284
drh87223182004-02-21 14:00:29 +0000285/*
drhc11d4f92003-04-06 21:08:24 +0000286** Process a pragma statement.
287**
288** Pragmas are of this form:
289**
danielk197791cf71b2004-06-26 06:37:06 +0000290** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000291**
292** The identifier might also be a string. The value is a string, and
293** identifier, or a number. If minusFlag is true, then the value is
294** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000295**
296** If the left side is "database.id" then pId1 is the database name
297** and pId2 is the id. If the left side is just "id" then pId1 is the
298** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000299*/
danielk197791cf71b2004-06-26 06:37:06 +0000300void sqlite3Pragma(
301 Parse *pParse,
302 Token *pId1, /* First part of [database.]id field */
303 Token *pId2, /* Second part of [database.]id field, or NULL */
304 Token *pValue, /* Token for <value>, or NULL */
305 int minusFlag /* True if a '-' sign preceded <value> */
306){
307 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
308 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
309 const char *zDb = 0; /* The database name */
310 Token *pId; /* Pointer to <id> token */
311 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000312 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000313 Db *pDb;
drh949f9cd2008-01-12 21:35:57 +0000314 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
drhc11d4f92003-04-06 21:08:24 +0000315 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000316 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000317 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000318
danielk197791cf71b2004-06-26 06:37:06 +0000319 /* Interpret the [database.] part of the pragma statement. iDb is the
320 ** index of the database this pragma is being applied to in db.aDb[]. */
321 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
322 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000323 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000324
danielk1977ddfb2f02006-02-17 12:25:14 +0000325 /* If the temp database has been explicitly named as part of the
326 ** pragma, make sure it is open.
327 */
328 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
329 return;
330 }
331
drh17435752007-08-16 04:30:38 +0000332 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000333 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000334 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000335 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000336 }else{
drh17435752007-08-16 04:30:38 +0000337 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000338 }
danielk197791cf71b2004-06-26 06:37:06 +0000339
drhd2cb50b2009-01-09 21:41:17 +0000340 assert( pId2 );
341 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000342 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000343 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000344 }
345
drh13d70422004-11-13 15:59:14 +0000346#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000347 /*
drh90f5ecb2004-07-22 01:19:35 +0000348 ** PRAGMA [database.]default_cache_size
349 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000350 **
351 ** The first form reports the current persistent setting for the
352 ** page cache size. The value returned is the maximum number of
353 ** pages in the page cache. The second form sets both the current
354 ** page cache size value and the persistent page cache size value
355 ** stored in the database file.
356 **
drh93791ea2010-04-26 17:36:35 +0000357 ** Older versions of SQLite would set the default cache size to a
358 ** negative number to indicate synchronous=OFF. These days, synchronous
359 ** is always on by default regardless of the sign of the default cache
360 ** size. But continue to take the absolute value of the default cache
361 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000362 */
danielk19774adee202004-05-08 08:23:19 +0000363 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000364 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000365 { OP_Transaction, 0, 0, 0}, /* 0 */
366 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
367 { OP_IfPos, 1, 7, 0},
drh3c84ddf2008-01-09 02:15:38 +0000368 { OP_Integer, 0, 2, 0},
369 { OP_Subtract, 1, 2, 1},
danielk1977602b4662009-07-02 07:47:33 +0000370 { OP_IfPos, 1, 7, 0},
371 { OP_Integer, 0, 1, 0}, /* 6 */
drh3c84ddf2008-01-09 02:15:38 +0000372 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000373 };
drh905793e2004-02-21 13:31:09 +0000374 int addr;
danielk19778a414492004-06-29 08:59:35 +0000375 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000376 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000377 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000378 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000379 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000380 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000381 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000382 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000383 sqlite3VdbeChangeP1(v, addr+1, iDb);
384 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000385 }else{
drhc11d4f92003-04-06 21:08:24 +0000386 int size = atoi(zRight);
387 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000388 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000389 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000390 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
danielk197714db2662006-01-09 16:12:04 +0000391 pDb->pSchema->cache_size = size;
392 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000393 }
394 }else
395
396 /*
drh90f5ecb2004-07-22 01:19:35 +0000397 ** PRAGMA [database.]page_size
398 ** PRAGMA [database.]page_size=N
399 **
400 ** The first form reports the current setting for the
401 ** database page size in bytes. The second form sets the
402 ** database page size value. The value can only be set if
403 ** the database has not yet been created.
404 */
405 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
406 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000407 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000408 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000409 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000410 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000411 }else{
danielk1977992772c2007-08-30 10:07:38 +0000412 /* Malloc may fail when setting the page-size, as there is an internal
413 ** buffer that the pager module resizes using sqlite3_realloc().
414 */
danielk1977f653d782008-03-20 11:04:21 +0000415 db->nextPagesize = atoi(zRight);
drhce4869f2009-04-02 20:16:58 +0000416 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
danielk1977992772c2007-08-30 10:07:38 +0000417 db->mallocFailed = 1;
418 }
drh90f5ecb2004-07-22 01:19:35 +0000419 }
420 }else
danielk197741483462007-03-24 16:45:04 +0000421
422 /*
drhf8e632b2007-05-08 14:51:36 +0000423 ** PRAGMA [database.]max_page_count
424 ** PRAGMA [database.]max_page_count=N
425 **
426 ** The first form reports the current setting for the
427 ** maximum number of pages in the database file. The
428 ** second form attempts to change this setting. Both
429 ** forms return the current setting.
430 */
431 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
432 Btree *pBt = pDb->pBt;
433 int newMax = 0;
drhd2cb50b2009-01-09 21:41:17 +0000434 assert( pBt!=0 );
drhf8e632b2007-05-08 14:51:36 +0000435 if( zRight ){
436 newMax = atoi(zRight);
437 }
drhd2cb50b2009-01-09 21:41:17 +0000438 if( ALWAYS(pBt) ){
drhf8e632b2007-05-08 14:51:36 +0000439 newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
440 }
441 returnSingleInt(pParse, "max_page_count", newMax);
442 }else
443
444 /*
drh5b47efa2010-02-12 18:18:39 +0000445 ** PRAGMA [database.]secure_delete
446 ** PRAGMA [database.]secure_delete=ON/OFF
447 **
448 ** The first form reports the current setting for the
449 ** secure_delete flag. The second form changes the secure_delete
450 ** flag setting and reports thenew value.
451 */
452 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
453 Btree *pBt = pDb->pBt;
454 int b = -1;
455 assert( pBt!=0 );
456 if( zRight ){
457 b = getBoolean(zRight);
458 }
drhaf034ed2010-02-12 19:46:26 +0000459 if( pId2->n==0 && b>=0 ){
460 int ii;
461 for(ii=0; ii<db->nDb; ii++){
462 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
463 }
464 }
drh5b47efa2010-02-12 18:18:39 +0000465 b = sqlite3BtreeSecureDelete(pBt, b);
466 returnSingleInt(pParse, "secure_delete", b);
467 }else
468
469 /*
danielk197759a93792008-05-15 17:48:20 +0000470 ** PRAGMA [database.]page_count
471 **
472 ** Return the number of pages in the specified database.
473 */
474 if( sqlite3StrICmp(zLeft,"page_count")==0 ){
danielk197759a93792008-05-15 17:48:20 +0000475 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000476 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197759a93792008-05-15 17:48:20 +0000477 sqlite3CodeVerifySchema(pParse, iDb);
478 iReg = ++pParse->nMem;
479 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
480 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
481 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000482 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC);
danielk197759a93792008-05-15 17:48:20 +0000483 }else
484
485 /*
danielk197741483462007-03-24 16:45:04 +0000486 ** PRAGMA [database.]locking_mode
487 ** PRAGMA [database.]locking_mode = (normal|exclusive)
488 */
489 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
490 const char *zRet = "normal";
491 int eMode = getLockingMode(zRight);
492
493 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
494 /* Simple "PRAGMA locking_mode;" statement. This is a query for
495 ** the current default locking mode (which may be different to
496 ** the locking-mode of the main database).
497 */
498 eMode = db->dfltLockMode;
499 }else{
500 Pager *pPager;
501 if( pId2->n==0 ){
502 /* This indicates that no database name was specified as part
503 ** of the PRAGMA command. In this case the locking-mode must be
504 ** set on all attached databases, as well as the main db file.
505 **
506 ** Also, the sqlite3.dfltLockMode variable is set so that
507 ** any subsequently attached databases also use the specified
508 ** locking mode.
509 */
510 int ii;
511 assert(pDb==&db->aDb[0]);
512 for(ii=2; ii<db->nDb; ii++){
513 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
514 sqlite3PagerLockingMode(pPager, eMode);
515 }
drh4f21c4a2008-12-10 22:15:00 +0000516 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000517 }
518 pPager = sqlite3BtreePager(pDb->pBt);
519 eMode = sqlite3PagerLockingMode(pPager, eMode);
520 }
521
522 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
523 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
524 zRet = "exclusive";
525 }
526 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000527 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000528 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
529 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000530 }else
drh3b020132008-04-17 17:02:01 +0000531
532 /*
533 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000534 ** PRAGMA [database.]journal_mode =
535 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000536 */
537 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
dane04dc882010-04-20 18:53:15 +0000538 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
539
danf6c61472010-07-07 13:54:28 +0000540 if( sqlite3ReadSchema(pParse) ){
541 goto pragma_out;
542 }
543
dane04dc882010-04-20 18:53:15 +0000544 sqlite3VdbeSetNumCols(v, 1);
545 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000546
547 if( zRight==0 ){
548 eMode = PAGER_JOURNALMODE_QUERY;
549 }else{
dane04dc882010-04-20 18:53:15 +0000550 const char *zMode;
drhea678832008-12-10 19:26:22 +0000551 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000552 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000553 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
554 }
555 if( !zMode ){
556 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000557 }
558 }
559 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
danielk1977b53e4962008-06-04 06:45:59 +0000560 /* Simple "PRAGMA journal_mode;" statement. This is a query for
drh3b020132008-04-17 17:02:01 +0000561 ** the current default journal mode (which may be different to
562 ** the journal-mode of the main database).
563 */
564 eMode = db->dfltJournalMode;
dane04dc882010-04-20 18:53:15 +0000565 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
566 sqlite3VdbeChangeP4(v, -1, sqlite3JournalModename(eMode), P4_STATIC);
drh3b020132008-04-17 17:02:01 +0000567 }else{
dane04dc882010-04-20 18:53:15 +0000568 int ii;
569
drh3ebaee92010-05-06 21:37:22 +0000570 if( pId2->n==0 ){
571 /* When there is no database name before the "journal_mode" keyword
572 ** in the PRAGMA, then the journal-mode will be set on
573 ** all attached databases, as well as the main db file.
drh3b020132008-04-17 17:02:01 +0000574 **
575 ** Also, the sqlite3.dfltJournalMode variable is set so that
576 ** any subsequently attached databases also use the specified
drh3ebaee92010-05-06 21:37:22 +0000577 ** journal mode.
drh3b020132008-04-17 17:02:01 +0000578 */
drh4f21c4a2008-12-10 22:15:00 +0000579 db->dfltJournalMode = (u8)eMode;
drh3b020132008-04-17 17:02:01 +0000580 }
dane04dc882010-04-20 18:53:15 +0000581
582 for(ii=db->nDb-1; ii>=0; ii--){
583 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
584 sqlite3VdbeUsesBtree(v, ii);
585 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
586 }
587 }
drh3b020132008-04-17 17:02:01 +0000588 }
dane04dc882010-04-20 18:53:15 +0000589
drh3b020132008-04-17 17:02:01 +0000590 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
591 }else
danielk1977b53e4962008-06-04 06:45:59 +0000592
593 /*
594 ** PRAGMA [database.]journal_size_limit
595 ** PRAGMA [database.]journal_size_limit=N
596 **
drha9e364f2009-01-13 20:14:15 +0000597 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000598 */
599 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
600 Pager *pPager = sqlite3BtreePager(pDb->pBt);
601 i64 iLimit = -2;
602 if( zRight ){
drh3c713642009-04-04 16:02:32 +0000603 sqlite3Atoi64(zRight, &iLimit);
604 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000605 }
606 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000607 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000608 }else
609
drh13d70422004-11-13 15:59:14 +0000610#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000611
612 /*
danielk1977951af802004-11-05 15:45:09 +0000613 ** PRAGMA [database.]auto_vacuum
614 ** PRAGMA [database.]auto_vacuum=N
615 **
drha9e364f2009-01-13 20:14:15 +0000616 ** Get or set the value of the database 'auto-vacuum' parameter.
617 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000618 */
619#ifndef SQLITE_OMIT_AUTOVACUUM
620 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
621 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000622 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000623 if( sqlite3ReadSchema(pParse) ){
624 goto pragma_out;
625 }
danielk1977951af802004-11-05 15:45:09 +0000626 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000627 int auto_vacuum;
628 if( ALWAYS(pBt) ){
629 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
630 }else{
631 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
632 }
danielk1977951af802004-11-05 15:45:09 +0000633 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
634 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000635 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000636 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000637 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000638 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000639 /* Call SetAutoVacuum() to set initialize the internal auto and
640 ** incr-vacuum flags. This is required in case this connection
641 ** creates the database file. It is important that it is created
642 ** as an auto-vacuum capable db.
643 */
644 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
645 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
646 /* When setting the auto_vacuum mode to either "full" or
647 ** "incremental", write the value of meta[6] in the database
648 ** file. Before writing to meta[6], check that meta[3] indicates
649 ** that this really is an auto-vacuum capable database.
650 */
651 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000652 { OP_Transaction, 0, 1, 0}, /* 0 */
653 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
654 { OP_If, 1, 0, 0}, /* 2 */
655 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
656 { OP_Integer, 0, 1, 0}, /* 4 */
657 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000658 };
659 int iAddr;
660 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
661 sqlite3VdbeChangeP1(v, iAddr, iDb);
662 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
663 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
664 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
665 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000666 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000667 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000668 }
danielk1977951af802004-11-05 15:45:09 +0000669 }
670 }else
671#endif
672
drhca5557f2007-05-04 18:30:40 +0000673 /*
674 ** PRAGMA [database.]incremental_vacuum(N)
675 **
676 ** Do N steps of incremental vacuuming on a database.
677 */
678#ifndef SQLITE_OMIT_AUTOVACUUM
679 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
680 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000681 if( sqlite3ReadSchema(pParse) ){
682 goto pragma_out;
683 }
drhca5557f2007-05-04 18:30:40 +0000684 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
685 iLimit = 0x7fffffff;
686 }
687 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000688 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000689 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000690 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000691 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000692 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000693 sqlite3VdbeJumpHere(v, addr);
694 }else
695#endif
696
drh13d70422004-11-13 15:59:14 +0000697#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000698 /*
drh90f5ecb2004-07-22 01:19:35 +0000699 ** PRAGMA [database.]cache_size
700 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000701 **
702 ** The first form reports the current local setting for the
703 ** page cache size. The local setting can be different from
704 ** the persistent cache size value that is stored in the database
705 ** file itself. The value returned is the maximum number of
706 ** pages in the page cache. The second form sets the local
707 ** page cache size value. It does not change the persistent
708 ** cache size stored on the disk so the cache size will revert
709 ** to its default value when the database is closed and reopened.
710 ** N should be a positive integer.
711 */
danielk19774adee202004-05-08 08:23:19 +0000712 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000713 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000714 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000715 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000716 }else{
717 int size = atoi(zRight);
718 if( size<0 ) size = -size;
danielk197714db2662006-01-09 16:12:04 +0000719 pDb->pSchema->cache_size = size;
720 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000721 }
722 }else
723
724 /*
drh90f5ecb2004-07-22 01:19:35 +0000725 ** PRAGMA temp_store
726 ** PRAGMA temp_store = "default"|"memory"|"file"
727 **
728 ** Return or set the local value of the temp_store flag. Changing
729 ** the local value does not make changes to the disk file and the default
730 ** value will be restored the next time the database is opened.
731 **
732 ** Note that it is possible for the library compile-time options to
733 ** override this setting
734 */
735 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
736 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000737 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000738 }else{
739 changeTempStorage(pParse, zRight);
740 }
741 }else
742
743 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000744 ** PRAGMA temp_store_directory
745 ** PRAGMA temp_store_directory = ""|"directory_name"
746 **
747 ** Return or set the local value of the temp_store_directory flag. Changing
748 ** the value sets a specific directory to be used for temporary files.
749 ** Setting to a null string reverts to the default temporary directory search.
750 ** If temporary directory is changed, then invalidateTempStorage.
751 **
752 */
753 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
754 if( !zRight ){
755 if( sqlite3_temp_directory ){
756 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000757 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000758 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000759 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
760 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000761 }
762 }else{
drh78f82d12008-09-02 00:52:52 +0000763#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000764 if( zRight[0] ){
danielk1977fab11272008-09-16 14:38:02 +0000765 int rc;
danielk1977861f7452008-06-05 11:39:11 +0000766 int res;
danielk1977fab11272008-09-16 14:38:02 +0000767 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
768 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000769 sqlite3ErrorMsg(pParse, "not a writable directory");
770 goto pragma_out;
771 }
drh268283b2005-01-08 15:44:25 +0000772 }
danielk1977b06a0b62008-06-26 10:54:12 +0000773 if( SQLITE_TEMP_STORE==0
774 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
775 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000776 ){
777 invalidateTempStorage(pParse);
778 }
drh17435752007-08-16 04:30:38 +0000779 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000780 if( zRight[0] ){
drh633e6d52008-07-28 19:34:53 +0000781 sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000782 }else{
drh268283b2005-01-08 15:44:25 +0000783 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000784 }
drh78f82d12008-09-02 00:52:52 +0000785#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000786 }
787 }else
788
drhd2cb50b2009-01-09 21:41:17 +0000789#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
790# if defined(__APPLE__)
791# define SQLITE_ENABLE_LOCKING_STYLE 1
792# else
793# define SQLITE_ENABLE_LOCKING_STYLE 0
794# endif
795#endif
796#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000797 /*
aswiftaebf4132008-11-21 00:10:35 +0000798 ** PRAGMA [database.]lock_proxy_file
799 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
800 **
801 ** Return or set the value of the lock_proxy_file flag. Changing
802 ** the value sets a specific file to be used for database access locks.
803 **
804 */
805 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
806 if( !zRight ){
807 Pager *pPager = sqlite3BtreePager(pDb->pBt);
808 char *proxy_file_path = NULL;
809 sqlite3_file *pFile = sqlite3PagerFile(pPager);
810 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
811 &proxy_file_path);
812
813 if( proxy_file_path ){
814 sqlite3VdbeSetNumCols(v, 1);
815 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
816 "lock_proxy_file", SQLITE_STATIC);
817 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
818 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
819 }
820 }else{
821 Pager *pPager = sqlite3BtreePager(pDb->pBt);
822 sqlite3_file *pFile = sqlite3PagerFile(pPager);
823 int res;
824 if( zRight[0] ){
825 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
826 zRight);
827 } else {
828 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
829 NULL);
830 }
831 if( res!=SQLITE_OK ){
832 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
833 goto pragma_out;
834 }
835 }
836 }else
drhd2cb50b2009-01-09 21:41:17 +0000837#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000838
839 /*
drh90f5ecb2004-07-22 01:19:35 +0000840 ** PRAGMA [database.]synchronous
841 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000842 **
843 ** Return or set the local value of the synchronous flag. Changing
844 ** the local value does not make changes to the disk file and the
845 ** default value will be restored the next time the database is
846 ** opened.
847 */
danielk19774adee202004-05-08 08:23:19 +0000848 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000849 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000850 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000851 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000852 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000853 if( !db->autoCommit ){
854 sqlite3ErrorMsg(pParse,
855 "Safety level may not be changed inside a transaction");
856 }else{
drh90f5ecb2004-07-22 01:19:35 +0000857 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000858 }
drhc11d4f92003-04-06 21:08:24 +0000859 }
860 }else
drh13d70422004-11-13 15:59:14 +0000861#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000862
drhbf216272005-02-26 18:10:44 +0000863#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000864 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000865 /* The flagPragma() subroutine also generates any necessary code
866 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000867 }else
drhbf216272005-02-26 18:10:44 +0000868#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000869
drh13d70422004-11-13 15:59:14 +0000870#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000871 /*
872 ** PRAGMA table_info(<table>)
873 **
874 ** Return a single row for each column of the named table. The columns of
875 ** the returned data set are:
876 **
877 ** cid: Column id (numbered from left to right, starting at 0)
878 ** name: Column name
879 ** type: Column declaration type.
880 ** notnull: True if 'NOT NULL' is part of column declaration
881 ** dflt_value: The default value for the column, if any.
882 */
drh5260f7e2004-06-26 19:35:29 +0000883 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000884 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000885 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000886 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000887 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000888 int i;
danielk1977034ca142007-06-26 10:38:54 +0000889 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000890 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000891 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000892 pParse->nMem = 6;
danielk197710fb7492008-10-31 10:53:22 +0000893 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
894 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
895 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
896 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
897 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
898 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000899 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000900 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +0000901 if( IsHiddenColumn(pCol) ){
902 nHidden++;
903 continue;
904 }
drh2d401ab2008-01-10 23:50:11 +0000905 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
906 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
907 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000908 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000909 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +0000910 if( pCol->zDflt ){
911 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +0000912 }else{
drh2d401ab2008-01-10 23:50:11 +0000913 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000914 }
drh2d401ab2008-01-10 23:50:11 +0000915 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
916 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000917 }
918 }
919 }else
920
drh5260f7e2004-06-26 19:35:29 +0000921 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000922 Index *pIdx;
923 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000924 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000925 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000926 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000927 int i;
928 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000929 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000930 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000931 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
932 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
933 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000934 for(i=0; i<pIdx->nColumn; i++){
935 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000936 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
937 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000938 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000939 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
940 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000941 }
942 }
943 }else
944
drh5260f7e2004-06-26 19:35:29 +0000945 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000946 Index *pIdx;
947 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000948 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000949 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000950 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000951 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000952 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000953 if( pIdx ){
954 int i = 0;
955 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000956 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000957 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
958 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
959 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000960 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000961 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
962 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
963 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
964 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000965 ++i;
966 pIdx = pIdx->pNext;
967 }
drhc11d4f92003-04-06 21:08:24 +0000968 }
969 }
970 }else
971
drh13d70422004-11-13 15:59:14 +0000972 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
973 int i;
974 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
975 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000976 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000977 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
978 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
979 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +0000980 for(i=0; i<db->nDb; i++){
981 if( db->aDb[i].pBt==0 ) continue;
982 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000983 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
984 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
985 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000986 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000987 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +0000988 }
989 }else
danielk197748af65a2005-02-09 03:20:37 +0000990
991 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
992 int i = 0;
993 HashElem *p;
994 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +0000995 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +0000996 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
997 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000998 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
999 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +00001000 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
1001 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
1002 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +00001003 }
1004 }else
drh13d70422004-11-13 15:59:14 +00001005#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
1006
drhb7f91642004-10-31 02:22:47 +00001007#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +00001008 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +00001009 FKey *pFK;
1010 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +00001011 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +00001012 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +00001013 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +00001014 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +00001015 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +00001016 if( pFK ){
1017 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +00001018 sqlite3VdbeSetNumCols(v, 8);
1019 pParse->nMem = 8;
danielk197710fb7492008-10-31 10:53:22 +00001020 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
1021 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1022 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1023 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1024 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1025 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1026 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1027 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001028 while(pFK){
1029 int j;
1030 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001031 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001032 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1033 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001034 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1035 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1036 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1037 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001038 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001039 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001040 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1041 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1042 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1043 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001044 }
1045 ++i;
1046 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001047 }
drh78100cc2003-08-23 22:40:53 +00001048 }
1049 }
1050 }else
drhb7f91642004-10-31 02:22:47 +00001051#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001052
drhc11d4f92003-04-06 21:08:24 +00001053#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001054 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001055 if( zRight ){
1056 if( getBoolean(zRight) ){
1057 sqlite3ParserTrace(stderr, "parser: ");
1058 }else{
1059 sqlite3ParserTrace(0, 0);
1060 }
drhc11d4f92003-04-06 21:08:24 +00001061 }
1062 }else
1063#endif
1064
drh55ef4d92005-08-14 01:20:37 +00001065 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1066 ** used will be case sensitive or not depending on the RHS.
1067 */
1068 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1069 if( zRight ){
1070 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
1071 }
1072 }else
1073
drh1dcdbc02007-01-27 02:24:54 +00001074#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1075# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1076#endif
1077
drhb7f91642004-10-31 02:22:47 +00001078#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +00001079 /* Pragma "quick_check" is an experimental reduced version of
1080 ** integrity_check designed to detect most database corruption
1081 ** without most of the overhead of a full integrity-check.
1082 */
1083 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1084 || sqlite3StrICmp(zLeft, "quick_check")==0
1085 ){
drh1dcdbc02007-01-27 02:24:54 +00001086 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001087
drhed717fe2003-06-15 23:42:24 +00001088 /* Code that appears at the end of the integrity check. If no error
1089 ** messages have been generated, output OK. Otherwise output the
1090 ** error message
1091 */
drh57196282004-10-06 15:41:16 +00001092 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001093 { OP_AddImm, 1, 0, 0}, /* 0 */
1094 { OP_IfNeg, 1, 0, 0}, /* 1 */
1095 { OP_String8, 0, 3, 0}, /* 2 */
1096 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001097 };
drhed717fe2003-06-15 23:42:24 +00001098
danielk197741c58b72007-12-29 13:39:19 +00001099 int isQuick = (zLeft[0]=='q');
1100
drhed717fe2003-06-15 23:42:24 +00001101 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001102 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001103 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001104 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001105 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001106
1107 /* Set the maximum error count */
1108 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1109 if( zRight ){
1110 mxErr = atoi(zRight);
1111 if( mxErr<=0 ){
1112 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1113 }
1114 }
drh2d401ab2008-01-10 23:50:11 +00001115 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001116
1117 /* Do an integrity check on each database file */
1118 for(i=0; i<db->nDb; i++){
1119 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001120 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001121 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001122
danielk197753c0f742005-03-29 03:10:59 +00001123 if( OMIT_TEMPDB && i==1 ) continue;
1124
drh80242052004-06-09 00:48:12 +00001125 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001126 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001127 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001128 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001129
drhed717fe2003-06-15 23:42:24 +00001130 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001131 **
1132 ** Begin by filling registers 2, 3, ... with the root pages numbers
1133 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001134 */
danielk1977da184232006-01-05 11:34:32 +00001135 pTbls = &db->aDb[i].pSchema->tblHash;
1136 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001137 Table *pTab = sqliteHashData(x);
1138 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001139 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001140 cnt++;
1141 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001142 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001143 cnt++;
1144 }
1145 }
drh2d401ab2008-01-10 23:50:11 +00001146
1147 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001148 if( pParse->nMem < cnt+4 ){
1149 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001150 }
drh2d401ab2008-01-10 23:50:11 +00001151
1152 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001153 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001154 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001155 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1156 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001157 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001158 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001159 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001160 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001161 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001162 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001163
1164 /* Make sure all the indices are constructed correctly.
1165 */
danielk197741c58b72007-12-29 13:39:19 +00001166 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001167 Table *pTab = sqliteHashData(x);
1168 Index *pIdx;
1169 int loopTop;
1170
1171 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001172 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001173 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001174 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001175 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001176 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001177 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001178 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001179 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001180 int jmp2;
drh91fc4a02009-11-12 20:39:03 +00001181 int r1;
drh57196282004-10-06 15:41:16 +00001182 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001183 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001184 { OP_String8, 0, 3, 0}, /* 1 */
1185 { OP_Rowid, 1, 4, 0},
1186 { OP_String8, 0, 5, 0}, /* 3 */
1187 { OP_String8, 0, 6, 0}, /* 4 */
1188 { OP_Concat, 4, 3, 3},
1189 { OP_Concat, 5, 3, 3},
1190 { OP_Concat, 6, 3, 3},
1191 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001192 { OP_IfPos, 1, 0, 0}, /* 9 */
1193 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001194 };
drh91fc4a02009-11-12 20:39:03 +00001195 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
1196 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001197 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001198 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1199 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +00001200 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
drhbb8a2792008-03-19 00:21:30 +00001201 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001202 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001203 }
drh66a51672008-01-03 00:01:23 +00001204 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001205 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001206 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001207 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001208 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001209 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001210 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001211 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001212 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001213 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001214 { OP_String8, 0, 2, 0}, /* 6 */
1215 { OP_String8, 0, 3, 0}, /* 7 */
1216 { OP_Concat, 3, 2, 2},
1217 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001218 };
drh3c84ddf2008-01-09 02:15:38 +00001219 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001220 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001221 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001222 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001223 sqlite3VdbeChangeP1(v, addr+1, j+2);
1224 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1225 sqlite3VdbeChangeP1(v, addr+3, j+2);
1226 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001227 sqlite3VdbeJumpHere(v, addr+4);
1228 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001229 "wrong # of entries in index ", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001230 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
drhed717fe2003-06-15 23:42:24 +00001231 }
1232 }
1233 }
danielk19774adee202004-05-08 08:23:19 +00001234 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001235 sqlite3VdbeChangeP2(v, addr, -mxErr);
1236 sqlite3VdbeJumpHere(v, addr+1);
1237 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001238 }else
drhb7f91642004-10-31 02:22:47 +00001239#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1240
drh13d70422004-11-13 15:59:14 +00001241#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001242 /*
1243 ** PRAGMA encoding
1244 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1245 **
drh85b623f2007-12-13 21:54:09 +00001246 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001247 ** database. If the database is not initialized, it is initialized now.
1248 **
1249 ** The second form of this pragma is a no-op if the main database file
1250 ** has not already been initialized. In this case it sets the default
1251 ** encoding that will be used for the main database file if a new file
1252 ** is created. If an existing main database file is opened, then the
1253 ** default text encoding for the existing database is used.
1254 **
1255 ** In all cases new databases created using the ATTACH command are
1256 ** created to use the same default text encoding as the main database. If
1257 ** the main database has not been initialized and/or created when ATTACH
1258 ** is executed, this is done before the ATTACH operation.
1259 **
1260 ** In the second form this pragma sets the text encoding to be used in
1261 ** new database files created using this database handle. It is only
1262 ** useful if invoked immediately after the main database i
1263 */
1264 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001265 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001266 char *zName;
1267 u8 enc;
1268 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001269 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001270 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1271 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1272 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001273 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001274 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001275 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1276 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001277 { 0, 0 }
1278 };
drh0f7eb612006-08-08 13:51:43 +00001279 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001280 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001281 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001282 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001283 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001284 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001285 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1286 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1287 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1288 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001289 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001290 }else{ /* "PRAGMA encoding = XXX" */
1291 /* Only change the value of sqlite.enc if the database handle is not
1292 ** initialized. If the main database exists, the new sqlite.enc value
1293 ** will be overwritten when the schema is next loaded. If it does not
1294 ** already exists, it will be created to use the new encoding value.
1295 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001296 if(
1297 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1298 DbHasProperty(db, 0, DB_Empty)
1299 ){
danielk19778e227872004-06-07 07:52:17 +00001300 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1301 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001302 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001303 break;
1304 }
1305 }
1306 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001307 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001308 }
1309 }
1310 }
1311 }else
drh13d70422004-11-13 15:59:14 +00001312#endif /* SQLITE_OMIT_UTF16 */
1313
1314#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001315 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001316 ** PRAGMA [database.]schema_version
1317 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001318 **
danielk1977b92b70b2004-11-12 16:11:59 +00001319 ** PRAGMA [database.]user_version
1320 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001321 **
danielk1977b92b70b2004-11-12 16:11:59 +00001322 ** The pragma's schema_version and user_version are used to set or get
1323 ** the value of the schema-version and user-version, respectively. Both
1324 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001325 ** stored in the database header.
1326 **
1327 ** The schema-cookie is usually only manipulated internally by SQLite. It
1328 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001329 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001330 ** SQLite each time a query is executed to ensure that the internal cache
1331 ** of the schema used when compiling the SQL query matches the schema of
1332 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001333 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1334 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001335 ** crashes or database corruption. Use with caution!
1336 **
danielk1977b92b70b2004-11-12 16:11:59 +00001337 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001338 ** applications for any purpose.
1339 */
danielk1977180b56a2007-06-24 08:00:42 +00001340 if( sqlite3StrICmp(zLeft, "schema_version")==0
1341 || sqlite3StrICmp(zLeft, "user_version")==0
1342 || sqlite3StrICmp(zLeft, "freelist_count")==0
1343 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001344 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001345 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001346 switch( zLeft[0] ){
danielk1977180b56a2007-06-24 08:00:42 +00001347 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001348 iCookie = BTREE_FREE_PAGE_COUNT;
1349 break;
1350 case 's': case 'S':
1351 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001352 break;
1353 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001354 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001355 break;
danielk1977dae24952004-11-11 05:10:43 +00001356 }
1357
danielk19770d19f7a2009-06-03 11:25:07 +00001358 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001359 /* Write the specified cookie value */
1360 static const VdbeOpList setCookie[] = {
1361 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001362 { OP_Integer, 0, 1, 0}, /* 1 */
1363 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001364 };
1365 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1366 sqlite3VdbeChangeP1(v, addr, iDb);
1367 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1368 sqlite3VdbeChangeP1(v, addr+2, iDb);
1369 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1370 }else{
1371 /* Read the specified cookie value */
1372 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001373 { OP_Transaction, 0, 0, 0}, /* 0 */
1374 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001375 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001376 };
1377 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1378 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001379 sqlite3VdbeChangeP1(v, addr+1, iDb);
1380 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001381 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001382 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001383 }
drh6e345992007-03-30 11:12:08 +00001384 }else
drh13d70422004-11-13 15:59:14 +00001385#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001386
shanehdc97a8c2010-02-23 20:08:35 +00001387#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1388 /*
1389 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001390 **
drh71caabf2010-02-26 15:39:24 +00001391 ** Return the names of all compile-time options used in this build,
1392 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001393 */
drh264a2d42010-02-25 15:28:41 +00001394 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
shanehdc97a8c2010-02-23 20:08:35 +00001395 int i = 0;
1396 const char *zOpt;
1397 sqlite3VdbeSetNumCols(v, 1);
1398 pParse->nMem = 1;
1399 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1400 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1401 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1402 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1403 }
1404 }else
1405#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1406
dan5cf53532010-05-01 16:40:20 +00001407#ifndef SQLITE_OMIT_WAL
1408 /*
dan5a299f92010-05-03 11:05:08 +00001409 ** PRAGMA [database.]wal_checkpoint
dan5cf53532010-05-01 16:40:20 +00001410 **
1411 ** Checkpoint the database.
1412 */
dan5a299f92010-05-03 11:05:08 +00001413 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
1414 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danaf0cfd32010-05-03 15:58:50 +00001415 sqlite3VdbeAddOp3(v, OP_Checkpoint, pId2->z?iDb:SQLITE_MAX_ATTACHED, 0, 0);
dan7c246102010-04-12 19:00:29 +00001416 }else
dan5a299f92010-05-03 11:05:08 +00001417
1418 /*
1419 ** PRAGMA wal_autocheckpoint
1420 ** PRAGMA wal_autocheckpoint = N
1421 **
1422 ** Configure a database connection to automatically checkpoint a database
1423 ** after accumulating N frames in the log. Or query for the current value
1424 ** of N.
1425 */
1426 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
1427 if( zRight ){
1428 int nAuto = atoi(zRight);
1429 sqlite3_wal_autocheckpoint(db, nAuto);
1430 }
drhb033d8b2010-05-03 13:37:30 +00001431 returnSingleInt(pParse, "wal_autocheckpoint",
1432 db->xWalCallback==sqlite3WalDefaultHook ?
1433 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
dan5a299f92010-05-03 11:05:08 +00001434 }else
dan5cf53532010-05-01 16:40:20 +00001435#endif
dan7c246102010-04-12 19:00:29 +00001436
dougcurrie81c95ef2004-06-18 23:21:47 +00001437#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001438 /*
1439 ** Report the current state of file logs for all databases
1440 */
1441 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001442 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001443 "unlocked", "shared", "reserved", "pending", "exclusive"
1444 };
1445 int i;
drh89ac8c12004-06-09 14:17:20 +00001446 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001447 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001448 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1449 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001450 for(i=0; i<db->nDb; i++){
1451 Btree *pBt;
1452 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001453 const char *zState = "unknown";
1454 int j;
drh89ac8c12004-06-09 14:17:20 +00001455 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001456 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001457 pBt = db->aDb[i].pBt;
1458 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001459 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001460 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001461 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1462 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001463 }
drh2d401ab2008-01-10 23:50:11 +00001464 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1465 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001466 }
danielk1977a4de4532008-09-02 15:44:08 +00001467
drh89ac8c12004-06-09 14:17:20 +00001468 }else
1469#endif
1470
shanehad9f9f62010-02-17 17:48:46 +00001471#ifdef SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001472 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhea678832008-12-10 19:26:22 +00001473 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001474 }else
drhd2cb50b2009-01-09 21:41:17 +00001475 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1476 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1477 }else
1478 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1479 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1480 int i, h1, h2;
1481 char zKey[40];
1482 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1483 h1 += 9*(1&(h1>>6));
1484 h2 += 9*(1&(h2>>6));
1485 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1486 }
1487 if( (zLeft[3] & 0xf)==0xb ){
1488 sqlite3_key(db, zKey, i/2);
1489 }else{
1490 sqlite3_rekey(db, zKey, i/2);
1491 }
1492 }else
drh3c4f2a42005-12-08 18:12:56 +00001493#endif
shanehad9f9f62010-02-17 17:48:46 +00001494#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh21e2cab2006-09-25 18:01:57 +00001495 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
shanehad9f9f62010-02-17 17:48:46 +00001496#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001497 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001498 sqlite3_activate_see(&zRight[4]);
1499 }
1500#endif
1501#ifdef SQLITE_ENABLE_CEROD
1502 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001503 sqlite3_activate_cerod(&zRight[6]);
1504 }
1505#endif
drhd2cb50b2009-01-09 21:41:17 +00001506 }else
drh21e2cab2006-09-25 18:01:57 +00001507#endif
drh3c4f2a42005-12-08 18:12:56 +00001508
drhd2cb50b2009-01-09 21:41:17 +00001509
1510 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001511
drhd2cb50b2009-01-09 21:41:17 +00001512 /*
1513 ** Reset the safety level, in case the fullfsync flag or synchronous
1514 ** setting changed.
1515 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001516#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhd2cb50b2009-01-09 21:41:17 +00001517 if( db->autoCommit ){
1518 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1519 (db->flags&SQLITE_FullFSync)!=0);
danielk1977a21c6b62005-01-24 10:25:59 +00001520 }
drhd2cb50b2009-01-09 21:41:17 +00001521#endif
danielk1977e0048402004-06-15 16:51:01 +00001522pragma_out:
drh633e6d52008-07-28 19:34:53 +00001523 sqlite3DbFree(db, zLeft);
1524 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001525}
drh13d70422004-11-13 15:59:14 +00001526
drh8bfdf722009-06-19 14:06:03 +00001527#endif /* SQLITE_OMIT_PRAGMA */