blob: db55e4bb41fa28af2ce856e3f8ad34ba651a2375 [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) ){
drh60ac3f42010-11-23 18:59:27 +000038 return (u8)sqlite3Atoi(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;
drh60ac3f42010-11-23 18:59:27 +000079 i = sqlite3Atoi(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 },
drhc97d8462010-11-19 18:23:35 +0000175 { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
drh699b3d42009-02-23 16:52:07 +0000176 { "reverse_unordered_selects", SQLITE_ReverseOrder },
drhc6339082010-04-07 16:54:58 +0000177#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
178 { "automatic_index", SQLITE_AutoIndex },
179#endif
drhcf1023c2007-05-08 20:59:49 +0000180#ifdef SQLITE_DEBUG
181 { "sql_trace", SQLITE_SqlTrace },
182 { "vdbe_listing", SQLITE_VdbeListing },
183 { "vdbe_trace", SQLITE_VdbeTrace },
184#endif
drh0cd2d4c2005-11-03 02:15:02 +0000185#ifndef SQLITE_OMIT_CHECK
186 { "ignore_check_constraints", SQLITE_IgnoreChecks },
187#endif
drh722e95a2004-10-25 20:33:44 +0000188 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000189 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
drh7bec5052005-02-06 02:45:41 +0000190 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000191
192 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
193 ** flag if there are any active statements. */
194 { "read_uncommitted", SQLITE_ReadUncommitted },
dan5bde73c2009-09-01 17:11:07 +0000195 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000196
dan75cbd982009-09-21 16:06:03 +0000197 /* This flag may only be set if both foreign-key and trigger support
198 ** are present in the build. */
199#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan1da40a32009-09-19 17:00:31 +0000200 { "foreign_keys", SQLITE_ForeignKeys },
dan75cbd982009-09-21 16:06:03 +0000201#endif
drh87223182004-02-21 14:00:29 +0000202 };
203 int i;
drh722e95a2004-10-25 20:33:44 +0000204 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000205 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000206 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000207 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000208 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000209 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000210 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
211 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000212 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000213 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000214 }else{
dan75cbd982009-09-21 16:06:03 +0000215 int mask = p->mask; /* Mask of bits to set or clear. */
216 if( db->autoCommit==0 ){
217 /* Foreign key support may not be enabled or disabled while not
218 ** in auto-commit mode. */
219 mask &= ~(SQLITE_ForeignKeys);
220 }
221
danielk1977a21c6b62005-01-24 10:25:59 +0000222 if( getBoolean(zRight) ){
dan75cbd982009-09-21 16:06:03 +0000223 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000224 }else{
dan75cbd982009-09-21 16:06:03 +0000225 db->flags &= ~mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000226 }
danielk19778e556522007-11-13 10:30:24 +0000227
228 /* Many of the flag-pragmas modify the code generated by the SQL
229 ** compiler (eg. count_changes). So add an opcode to expire all
230 ** compiled SQL statements after modifying a pragma value.
231 */
drh66a51672008-01-03 00:01:23 +0000232 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000233 }
drh87223182004-02-21 14:00:29 +0000234 }
danielk19778e556522007-11-13 10:30:24 +0000235
drh87223182004-02-21 14:00:29 +0000236 return 1;
237 }
238 }
239 return 0;
240}
drhbf216272005-02-26 18:10:44 +0000241#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000242
drhd2cb50b2009-01-09 21:41:17 +0000243/*
244** Return a human-readable name for a constraint resolution action.
245*/
danba9108b2009-09-22 07:13:42 +0000246#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000247static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000248 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000249 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000250 case OE_SetNull: zName = "SET NULL"; break;
251 case OE_SetDflt: zName = "SET DEFAULT"; break;
252 case OE_Cascade: zName = "CASCADE"; break;
253 case OE_Restrict: zName = "RESTRICT"; break;
254 default: zName = "NO ACTION";
255 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000256 }
drhd2cb50b2009-01-09 21:41:17 +0000257 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000258}
danba9108b2009-09-22 07:13:42 +0000259#endif
danielk197750af3e12008-10-10 17:47:21 +0000260
dane04dc882010-04-20 18:53:15 +0000261
262/*
263** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
264** defined in pager.h. This function returns the associated lowercase
265** journal-mode name.
266*/
267const char *sqlite3JournalModename(int eMode){
268 static char * const azModeName[] = {
dan5cf53532010-05-01 16:40:20 +0000269 "delete", "persist", "off", "truncate", "memory"
270#ifndef SQLITE_OMIT_WAL
271 , "wal"
272#endif
dane04dc882010-04-20 18:53:15 +0000273 };
274 assert( PAGER_JOURNALMODE_DELETE==0 );
275 assert( PAGER_JOURNALMODE_PERSIST==1 );
276 assert( PAGER_JOURNALMODE_OFF==2 );
277 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
278 assert( PAGER_JOURNALMODE_MEMORY==4 );
279 assert( PAGER_JOURNALMODE_WAL==5 );
280 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
281
282 if( eMode==ArraySize(azModeName) ) return 0;
283 return azModeName[eMode];
284}
285
drh87223182004-02-21 14:00:29 +0000286/*
drhc11d4f92003-04-06 21:08:24 +0000287** Process a pragma statement.
288**
289** Pragmas are of this form:
290**
danielk197791cf71b2004-06-26 06:37:06 +0000291** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000292**
293** The identifier might also be a string. The value is a string, and
294** identifier, or a number. If minusFlag is true, then the value is
295** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000296**
297** If the left side is "database.id" then pId1 is the database name
298** and pId2 is the id. If the left side is just "id" then pId1 is the
299** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000300*/
danielk197791cf71b2004-06-26 06:37:06 +0000301void sqlite3Pragma(
302 Parse *pParse,
303 Token *pId1, /* First part of [database.]id field */
304 Token *pId2, /* Second part of [database.]id field, or NULL */
305 Token *pValue, /* Token for <value>, or NULL */
306 int minusFlag /* True if a '-' sign preceded <value> */
307){
308 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
309 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
310 const char *zDb = 0; /* The database name */
311 Token *pId; /* Pointer to <id> token */
312 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000313 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000314 Db *pDb;
drh949f9cd2008-01-12 21:35:57 +0000315 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
drhc11d4f92003-04-06 21:08:24 +0000316 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000317 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000318 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000319
danielk197791cf71b2004-06-26 06:37:06 +0000320 /* Interpret the [database.] part of the pragma statement. iDb is the
321 ** index of the database this pragma is being applied to in db.aDb[]. */
322 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
323 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000324 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000325
danielk1977ddfb2f02006-02-17 12:25:14 +0000326 /* If the temp database has been explicitly named as part of the
327 ** pragma, make sure it is open.
328 */
329 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
330 return;
331 }
332
drh17435752007-08-16 04:30:38 +0000333 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000334 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000335 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000336 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000337 }else{
drh17435752007-08-16 04:30:38 +0000338 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000339 }
danielk197791cf71b2004-06-26 06:37:06 +0000340
drhd2cb50b2009-01-09 21:41:17 +0000341 assert( pId2 );
342 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000343 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000344 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000345 }
346
drh13d70422004-11-13 15:59:14 +0000347#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000348 /*
drh90f5ecb2004-07-22 01:19:35 +0000349 ** PRAGMA [database.]default_cache_size
350 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000351 **
352 ** The first form reports the current persistent setting for the
353 ** page cache size. The value returned is the maximum number of
354 ** pages in the page cache. The second form sets both the current
355 ** page cache size value and the persistent page cache size value
356 ** stored in the database file.
357 **
drh93791ea2010-04-26 17:36:35 +0000358 ** Older versions of SQLite would set the default cache size to a
359 ** negative number to indicate synchronous=OFF. These days, synchronous
360 ** is always on by default regardless of the sign of the default cache
361 ** size. But continue to take the absolute value of the default cache
362 ** size of historical compatibility.
drhc11d4f92003-04-06 21:08:24 +0000363 */
danielk19774adee202004-05-08 08:23:19 +0000364 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000365 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000366 { OP_Transaction, 0, 0, 0}, /* 0 */
367 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
368 { OP_IfPos, 1, 7, 0},
drh3c84ddf2008-01-09 02:15:38 +0000369 { OP_Integer, 0, 2, 0},
370 { OP_Subtract, 1, 2, 1},
danielk1977602b4662009-07-02 07:47:33 +0000371 { OP_IfPos, 1, 7, 0},
372 { OP_Integer, 0, 1, 0}, /* 6 */
drh3c84ddf2008-01-09 02:15:38 +0000373 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000374 };
drh905793e2004-02-21 13:31:09 +0000375 int addr;
danielk19778a414492004-06-29 08:59:35 +0000376 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000377 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000378 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000379 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000380 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000381 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000382 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000383 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000384 sqlite3VdbeChangeP1(v, addr+1, iDb);
385 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000386 }else{
drhd50ffc42011-03-08 02:38:28 +0000387 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
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 */
drh60ac3f42010-11-23 18:59:27 +0000415 db->nextPagesize = sqlite3Atoi(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 /*
drh5b47efa2010-02-12 18:18:39 +0000423 ** PRAGMA [database.]secure_delete
424 ** PRAGMA [database.]secure_delete=ON/OFF
425 **
426 ** The first form reports the current setting for the
427 ** secure_delete flag. The second form changes the secure_delete
428 ** flag setting and reports thenew value.
429 */
430 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
431 Btree *pBt = pDb->pBt;
432 int b = -1;
433 assert( pBt!=0 );
434 if( zRight ){
435 b = getBoolean(zRight);
436 }
drhaf034ed2010-02-12 19:46:26 +0000437 if( pId2->n==0 && b>=0 ){
438 int ii;
439 for(ii=0; ii<db->nDb; ii++){
440 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
441 }
442 }
drh5b47efa2010-02-12 18:18:39 +0000443 b = sqlite3BtreeSecureDelete(pBt, b);
444 returnSingleInt(pParse, "secure_delete", b);
445 }else
446
447 /*
drh60ac3f42010-11-23 18:59:27 +0000448 ** PRAGMA [database.]max_page_count
449 ** PRAGMA [database.]max_page_count=N
450 **
451 ** The first form reports the current setting for the
452 ** maximum number of pages in the database file. The
453 ** second form attempts to change this setting. Both
454 ** forms return the current setting.
455 **
danielk197759a93792008-05-15 17:48:20 +0000456 ** PRAGMA [database.]page_count
457 **
458 ** Return the number of pages in the specified database.
459 */
drh60ac3f42010-11-23 18:59:27 +0000460 if( sqlite3StrICmp(zLeft,"page_count")==0
461 || sqlite3StrICmp(zLeft,"max_page_count")==0
462 ){
danielk197759a93792008-05-15 17:48:20 +0000463 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000464 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197759a93792008-05-15 17:48:20 +0000465 sqlite3CodeVerifySchema(pParse, iDb);
466 iReg = ++pParse->nMem;
drh60ac3f42010-11-23 18:59:27 +0000467 if( zLeft[0]=='p' ){
468 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
469 }else{
470 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
471 }
danielk197759a93792008-05-15 17:48:20 +0000472 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
473 sqlite3VdbeSetNumCols(v, 1);
drh60ac3f42010-11-23 18:59:27 +0000474 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk197759a93792008-05-15 17:48:20 +0000475 }else
476
477 /*
danielk197741483462007-03-24 16:45:04 +0000478 ** PRAGMA [database.]locking_mode
479 ** PRAGMA [database.]locking_mode = (normal|exclusive)
480 */
481 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
482 const char *zRet = "normal";
483 int eMode = getLockingMode(zRight);
484
485 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
486 /* Simple "PRAGMA locking_mode;" statement. This is a query for
487 ** the current default locking mode (which may be different to
488 ** the locking-mode of the main database).
489 */
490 eMode = db->dfltLockMode;
491 }else{
492 Pager *pPager;
493 if( pId2->n==0 ){
494 /* This indicates that no database name was specified as part
495 ** of the PRAGMA command. In this case the locking-mode must be
496 ** set on all attached databases, as well as the main db file.
497 **
498 ** Also, the sqlite3.dfltLockMode variable is set so that
499 ** any subsequently attached databases also use the specified
500 ** locking mode.
501 */
502 int ii;
503 assert(pDb==&db->aDb[0]);
504 for(ii=2; ii<db->nDb; ii++){
505 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
506 sqlite3PagerLockingMode(pPager, eMode);
507 }
drh4f21c4a2008-12-10 22:15:00 +0000508 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000509 }
510 pPager = sqlite3BtreePager(pDb->pBt);
511 eMode = sqlite3PagerLockingMode(pPager, eMode);
512 }
513
514 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
515 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
516 zRet = "exclusive";
517 }
518 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000519 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000520 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
521 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000522 }else
drh3b020132008-04-17 17:02:01 +0000523
524 /*
525 ** PRAGMA [database.]journal_mode
drh3ebaee92010-05-06 21:37:22 +0000526 ** PRAGMA [database.]journal_mode =
527 ** (delete|persist|off|truncate|memory|wal|off)
drh3b020132008-04-17 17:02:01 +0000528 */
529 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000530 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
531 int ii; /* Loop counter */
dane04dc882010-04-20 18:53:15 +0000532
drhc6b2a0f2010-07-08 17:40:37 +0000533 /* Force the schema to be loaded on all databases. This cases all
534 ** database files to be opened and the journal_modes set. */
danf6c61472010-07-07 13:54:28 +0000535 if( sqlite3ReadSchema(pParse) ){
536 goto pragma_out;
537 }
538
dane04dc882010-04-20 18:53:15 +0000539 sqlite3VdbeSetNumCols(v, 1);
540 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000541
542 if( zRight==0 ){
drhc6b2a0f2010-07-08 17:40:37 +0000543 /* If there is no "=MODE" part of the pragma, do a query for the
544 ** current mode */
drh3b020132008-04-17 17:02:01 +0000545 eMode = PAGER_JOURNALMODE_QUERY;
546 }else{
dane04dc882010-04-20 18:53:15 +0000547 const char *zMode;
drhea678832008-12-10 19:26:22 +0000548 int n = sqlite3Strlen30(zRight);
drhf77e2ac2010-07-07 14:33:09 +0000549 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
dane04dc882010-04-20 18:53:15 +0000550 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
551 }
552 if( !zMode ){
drhc6b2a0f2010-07-08 17:40:37 +0000553 /* If the "=MODE" part does not match any known journal mode,
554 ** then do a query */
dane04dc882010-04-20 18:53:15 +0000555 eMode = PAGER_JOURNALMODE_QUERY;
drh3b020132008-04-17 17:02:01 +0000556 }
557 }
drhc6b2a0f2010-07-08 17:40:37 +0000558 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
559 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
560 iDb = 0;
561 pId2->n = 1;
562 }
563 for(ii=db->nDb-1; ii>=0; ii--){
564 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
565 sqlite3VdbeUsesBtree(v, ii);
566 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
dane04dc882010-04-20 18:53:15 +0000567 }
drh3b020132008-04-17 17:02:01 +0000568 }
drh3b020132008-04-17 17:02:01 +0000569 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
570 }else
danielk1977b53e4962008-06-04 06:45:59 +0000571
572 /*
573 ** PRAGMA [database.]journal_size_limit
574 ** PRAGMA [database.]journal_size_limit=N
575 **
drha9e364f2009-01-13 20:14:15 +0000576 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000577 */
578 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
579 Pager *pPager = sqlite3BtreePager(pDb->pBt);
580 i64 iLimit = -2;
581 if( zRight ){
drh9339da12010-09-30 00:50:49 +0000582 sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
drh3c713642009-04-04 16:02:32 +0000583 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000584 }
585 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000586 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000587 }else
588
drh13d70422004-11-13 15:59:14 +0000589#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000590
591 /*
danielk1977951af802004-11-05 15:45:09 +0000592 ** PRAGMA [database.]auto_vacuum
593 ** PRAGMA [database.]auto_vacuum=N
594 **
drha9e364f2009-01-13 20:14:15 +0000595 ** Get or set the value of the database 'auto-vacuum' parameter.
596 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000597 */
598#ifndef SQLITE_OMIT_AUTOVACUUM
599 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
600 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000601 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000602 if( sqlite3ReadSchema(pParse) ){
603 goto pragma_out;
604 }
danielk1977951af802004-11-05 15:45:09 +0000605 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000606 int auto_vacuum;
607 if( ALWAYS(pBt) ){
608 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
609 }else{
610 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
611 }
danielk1977951af802004-11-05 15:45:09 +0000612 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
613 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000614 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000615 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000616 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000617 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000618 /* Call SetAutoVacuum() to set initialize the internal auto and
619 ** incr-vacuum flags. This is required in case this connection
620 ** creates the database file. It is important that it is created
621 ** as an auto-vacuum capable db.
622 */
623 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
624 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
625 /* When setting the auto_vacuum mode to either "full" or
626 ** "incremental", write the value of meta[6] in the database
627 ** file. Before writing to meta[6], check that meta[3] indicates
628 ** that this really is an auto-vacuum capable database.
629 */
630 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000631 { OP_Transaction, 0, 1, 0}, /* 0 */
632 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
633 { OP_If, 1, 0, 0}, /* 2 */
634 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
635 { OP_Integer, 0, 1, 0}, /* 4 */
636 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000637 };
638 int iAddr;
639 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
640 sqlite3VdbeChangeP1(v, iAddr, iDb);
641 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
642 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
643 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
644 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000645 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000646 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000647 }
danielk1977951af802004-11-05 15:45:09 +0000648 }
649 }else
650#endif
651
drhca5557f2007-05-04 18:30:40 +0000652 /*
653 ** PRAGMA [database.]incremental_vacuum(N)
654 **
655 ** Do N steps of incremental vacuuming on a database.
656 */
657#ifndef SQLITE_OMIT_AUTOVACUUM
658 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
659 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000660 if( sqlite3ReadSchema(pParse) ){
661 goto pragma_out;
662 }
drhca5557f2007-05-04 18:30:40 +0000663 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
664 iLimit = 0x7fffffff;
665 }
666 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000667 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000668 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000669 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000670 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000671 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000672 sqlite3VdbeJumpHere(v, addr);
673 }else
674#endif
675
drh13d70422004-11-13 15:59:14 +0000676#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000677 /*
drh90f5ecb2004-07-22 01:19:35 +0000678 ** PRAGMA [database.]cache_size
679 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000680 **
681 ** The first form reports the current local setting for the
682 ** page cache size. The local setting can be different from
683 ** the persistent cache size value that is stored in the database
684 ** file itself. The value returned is the maximum number of
685 ** pages in the page cache. The second form sets the local
686 ** page cache size value. It does not change the persistent
687 ** cache size stored on the disk so the cache size will revert
688 ** to its default value when the database is closed and reopened.
689 ** N should be a positive integer.
690 */
danielk19774adee202004-05-08 08:23:19 +0000691 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000692 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000693 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000694 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000695 }else{
drhd50ffc42011-03-08 02:38:28 +0000696 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
danielk197714db2662006-01-09 16:12:04 +0000697 pDb->pSchema->cache_size = size;
698 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000699 }
700 }else
701
702 /*
drh90f5ecb2004-07-22 01:19:35 +0000703 ** PRAGMA temp_store
704 ** PRAGMA temp_store = "default"|"memory"|"file"
705 **
706 ** Return or set the local value of the temp_store flag. Changing
707 ** the local value does not make changes to the disk file and the default
708 ** value will be restored the next time the database is opened.
709 **
710 ** Note that it is possible for the library compile-time options to
711 ** override this setting
712 */
713 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
714 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000715 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000716 }else{
717 changeTempStorage(pParse, zRight);
718 }
719 }else
720
721 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000722 ** PRAGMA temp_store_directory
723 ** PRAGMA temp_store_directory = ""|"directory_name"
724 **
725 ** Return or set the local value of the temp_store_directory flag. Changing
726 ** the value sets a specific directory to be used for temporary files.
727 ** Setting to a null string reverts to the default temporary directory search.
728 ** If temporary directory is changed, then invalidateTempStorage.
729 **
730 */
731 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
732 if( !zRight ){
733 if( sqlite3_temp_directory ){
734 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000735 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000736 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000737 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
738 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000739 }
740 }else{
drh78f82d12008-09-02 00:52:52 +0000741#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000742 if( zRight[0] ){
danielk1977fab11272008-09-16 14:38:02 +0000743 int rc;
danielk1977861f7452008-06-05 11:39:11 +0000744 int res;
danielk1977fab11272008-09-16 14:38:02 +0000745 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
746 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000747 sqlite3ErrorMsg(pParse, "not a writable directory");
748 goto pragma_out;
749 }
drh268283b2005-01-08 15:44:25 +0000750 }
danielk1977b06a0b62008-06-26 10:54:12 +0000751 if( SQLITE_TEMP_STORE==0
752 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
753 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000754 ){
755 invalidateTempStorage(pParse);
756 }
drh17435752007-08-16 04:30:38 +0000757 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000758 if( zRight[0] ){
drhb9755982010-07-24 16:34:37 +0000759 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000760 }else{
drh268283b2005-01-08 15:44:25 +0000761 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000762 }
drh78f82d12008-09-02 00:52:52 +0000763#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000764 }
765 }else
766
drhd2cb50b2009-01-09 21:41:17 +0000767#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
768# if defined(__APPLE__)
769# define SQLITE_ENABLE_LOCKING_STYLE 1
770# else
771# define SQLITE_ENABLE_LOCKING_STYLE 0
772# endif
773#endif
774#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000775 /*
aswiftaebf4132008-11-21 00:10:35 +0000776 ** PRAGMA [database.]lock_proxy_file
777 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
778 **
779 ** Return or set the value of the lock_proxy_file flag. Changing
780 ** the value sets a specific file to be used for database access locks.
781 **
782 */
783 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
784 if( !zRight ){
785 Pager *pPager = sqlite3BtreePager(pDb->pBt);
786 char *proxy_file_path = NULL;
787 sqlite3_file *pFile = sqlite3PagerFile(pPager);
788 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
789 &proxy_file_path);
790
791 if( proxy_file_path ){
792 sqlite3VdbeSetNumCols(v, 1);
793 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
794 "lock_proxy_file", SQLITE_STATIC);
795 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
796 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
797 }
798 }else{
799 Pager *pPager = sqlite3BtreePager(pDb->pBt);
800 sqlite3_file *pFile = sqlite3PagerFile(pPager);
801 int res;
802 if( zRight[0] ){
803 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
804 zRight);
805 } else {
806 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
807 NULL);
808 }
809 if( res!=SQLITE_OK ){
810 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
811 goto pragma_out;
812 }
813 }
814 }else
drhd2cb50b2009-01-09 21:41:17 +0000815#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000816
817 /*
drh90f5ecb2004-07-22 01:19:35 +0000818 ** PRAGMA [database.]synchronous
819 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000820 **
821 ** Return or set the local value of the synchronous flag. Changing
822 ** the local value does not make changes to the disk file and the
823 ** default value will be restored the next time the database is
824 ** opened.
825 */
danielk19774adee202004-05-08 08:23:19 +0000826 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000827 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000828 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000829 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000830 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000831 if( !db->autoCommit ){
832 sqlite3ErrorMsg(pParse,
833 "Safety level may not be changed inside a transaction");
834 }else{
drh90f5ecb2004-07-22 01:19:35 +0000835 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000836 }
drhc11d4f92003-04-06 21:08:24 +0000837 }
838 }else
drh13d70422004-11-13 15:59:14 +0000839#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000840
drhbf216272005-02-26 18:10:44 +0000841#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000842 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000843 /* The flagPragma() subroutine also generates any necessary code
844 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000845 }else
drhbf216272005-02-26 18:10:44 +0000846#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000847
drh13d70422004-11-13 15:59:14 +0000848#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000849 /*
850 ** PRAGMA table_info(<table>)
851 **
852 ** Return a single row for each column of the named table. The columns of
853 ** the returned data set are:
854 **
855 ** cid: Column id (numbered from left to right, starting at 0)
856 ** name: Column name
857 ** type: Column declaration type.
858 ** notnull: True if 'NOT NULL' is part of column declaration
859 ** dflt_value: The default value for the column, if any.
860 */
drh5260f7e2004-06-26 19:35:29 +0000861 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000862 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000863 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000864 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000865 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000866 int i;
danielk1977034ca142007-06-26 10:38:54 +0000867 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000868 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000869 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000870 pParse->nMem = 6;
danielk197710fb7492008-10-31 10:53:22 +0000871 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
872 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
873 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
874 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
875 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
876 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000877 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000878 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +0000879 if( IsHiddenColumn(pCol) ){
880 nHidden++;
881 continue;
882 }
drh2d401ab2008-01-10 23:50:11 +0000883 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
884 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
885 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000886 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000887 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +0000888 if( pCol->zDflt ){
889 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +0000890 }else{
drh2d401ab2008-01-10 23:50:11 +0000891 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000892 }
drh2d401ab2008-01-10 23:50:11 +0000893 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
894 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000895 }
896 }
897 }else
898
drh5260f7e2004-06-26 19:35:29 +0000899 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000900 Index *pIdx;
901 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000902 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000903 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000904 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000905 int i;
906 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000907 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000908 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000909 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
910 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
911 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000912 for(i=0; i<pIdx->nColumn; i++){
913 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000914 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
915 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000916 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000917 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
918 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000919 }
920 }
921 }else
922
drh5260f7e2004-06-26 19:35:29 +0000923 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000924 Index *pIdx;
925 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000926 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000927 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000928 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000929 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000930 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000931 if( pIdx ){
932 int i = 0;
933 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000934 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000935 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
936 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
937 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000938 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000939 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
940 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
941 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
942 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000943 ++i;
944 pIdx = pIdx->pNext;
945 }
drhc11d4f92003-04-06 21:08:24 +0000946 }
947 }
948 }else
949
drh13d70422004-11-13 15:59:14 +0000950 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
951 int i;
952 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
953 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000954 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000955 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
956 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
957 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +0000958 for(i=0; i<db->nDb; i++){
959 if( db->aDb[i].pBt==0 ) continue;
960 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000961 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
962 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
963 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000964 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000965 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +0000966 }
967 }else
danielk197748af65a2005-02-09 03:20:37 +0000968
969 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
970 int i = 0;
971 HashElem *p;
972 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +0000973 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +0000974 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
975 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000976 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
977 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +0000978 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
979 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
980 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +0000981 }
982 }else
drh13d70422004-11-13 15:59:14 +0000983#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
984
drhb7f91642004-10-31 02:22:47 +0000985#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000986 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000987 FKey *pFK;
988 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000989 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000990 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000991 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000992 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000993 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000994 if( pFK ){
995 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +0000996 sqlite3VdbeSetNumCols(v, 8);
997 pParse->nMem = 8;
danielk197710fb7492008-10-31 10:53:22 +0000998 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
999 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
1000 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
1001 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1002 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1003 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1004 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1005 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001006 while(pFK){
1007 int j;
1008 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001009 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001010 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1011 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001012 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1013 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1014 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1015 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001016 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001017 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001018 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1019 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1020 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1021 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001022 }
1023 ++i;
1024 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001025 }
drh78100cc2003-08-23 22:40:53 +00001026 }
1027 }
1028 }else
drhb7f91642004-10-31 02:22:47 +00001029#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001030
drhc11d4f92003-04-06 21:08:24 +00001031#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001032 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001033 if( zRight ){
1034 if( getBoolean(zRight) ){
1035 sqlite3ParserTrace(stderr, "parser: ");
1036 }else{
1037 sqlite3ParserTrace(0, 0);
1038 }
drhc11d4f92003-04-06 21:08:24 +00001039 }
1040 }else
1041#endif
1042
drh55ef4d92005-08-14 01:20:37 +00001043 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1044 ** used will be case sensitive or not depending on the RHS.
1045 */
1046 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1047 if( zRight ){
1048 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
1049 }
1050 }else
1051
drh1dcdbc02007-01-27 02:24:54 +00001052#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1053# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1054#endif
1055
drhb7f91642004-10-31 02:22:47 +00001056#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +00001057 /* Pragma "quick_check" is an experimental reduced version of
1058 ** integrity_check designed to detect most database corruption
1059 ** without most of the overhead of a full integrity-check.
1060 */
1061 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1062 || sqlite3StrICmp(zLeft, "quick_check")==0
1063 ){
drh1dcdbc02007-01-27 02:24:54 +00001064 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001065
drhed717fe2003-06-15 23:42:24 +00001066 /* Code that appears at the end of the integrity check. If no error
1067 ** messages have been generated, output OK. Otherwise output the
1068 ** error message
1069 */
drh57196282004-10-06 15:41:16 +00001070 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001071 { OP_AddImm, 1, 0, 0}, /* 0 */
1072 { OP_IfNeg, 1, 0, 0}, /* 1 */
1073 { OP_String8, 0, 3, 0}, /* 2 */
1074 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001075 };
drhed717fe2003-06-15 23:42:24 +00001076
danielk197741c58b72007-12-29 13:39:19 +00001077 int isQuick = (zLeft[0]=='q');
1078
drhed717fe2003-06-15 23:42:24 +00001079 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001080 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001081 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001082 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001083 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001084
1085 /* Set the maximum error count */
1086 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1087 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001088 sqlite3GetInt32(zRight, &mxErr);
drh1dcdbc02007-01-27 02:24:54 +00001089 if( mxErr<=0 ){
1090 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1091 }
1092 }
drh2d401ab2008-01-10 23:50:11 +00001093 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001094
1095 /* Do an integrity check on each database file */
1096 for(i=0; i<db->nDb; i++){
1097 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001098 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001099 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001100
danielk197753c0f742005-03-29 03:10:59 +00001101 if( OMIT_TEMPDB && i==1 ) continue;
1102
drh80242052004-06-09 00:48:12 +00001103 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001104 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001105 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001106 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001107
drhed717fe2003-06-15 23:42:24 +00001108 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001109 **
1110 ** Begin by filling registers 2, 3, ... with the root pages numbers
1111 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001112 */
danielk1977da184232006-01-05 11:34:32 +00001113 pTbls = &db->aDb[i].pSchema->tblHash;
1114 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001115 Table *pTab = sqliteHashData(x);
1116 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001117 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001118 cnt++;
1119 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001120 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001121 cnt++;
1122 }
1123 }
drh2d401ab2008-01-10 23:50:11 +00001124
1125 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001126 if( pParse->nMem < cnt+4 ){
1127 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001128 }
drh2d401ab2008-01-10 23:50:11 +00001129
1130 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001131 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001132 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001133 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1134 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001135 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001136 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001137 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001138 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001139 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001140 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001141
1142 /* Make sure all the indices are constructed correctly.
1143 */
danielk197741c58b72007-12-29 13:39:19 +00001144 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001145 Table *pTab = sqliteHashData(x);
1146 Index *pIdx;
1147 int loopTop;
1148
1149 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001150 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001151 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001152 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001153 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001154 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001155 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001156 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001157 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001158 int jmp2;
drh91fc4a02009-11-12 20:39:03 +00001159 int r1;
drh57196282004-10-06 15:41:16 +00001160 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001161 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001162 { OP_String8, 0, 3, 0}, /* 1 */
1163 { OP_Rowid, 1, 4, 0},
1164 { OP_String8, 0, 5, 0}, /* 3 */
1165 { OP_String8, 0, 6, 0}, /* 4 */
1166 { OP_Concat, 4, 3, 3},
1167 { OP_Concat, 5, 3, 3},
1168 { OP_Concat, 6, 3, 3},
1169 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001170 { OP_IfPos, 1, 0, 0}, /* 9 */
1171 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001172 };
drh91fc4a02009-11-12 20:39:03 +00001173 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
1174 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001175 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001176 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1177 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +00001178 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
drhbb8a2792008-03-19 00:21:30 +00001179 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001180 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001181 }
drh66a51672008-01-03 00:01:23 +00001182 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001183 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001184 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001185 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001186 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001187 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001188 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001189 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001190 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001191 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001192 { OP_String8, 0, 2, 0}, /* 6 */
1193 { OP_String8, 0, 3, 0}, /* 7 */
1194 { OP_Concat, 3, 2, 2},
1195 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001196 };
drh3c84ddf2008-01-09 02:15:38 +00001197 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001198 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001199 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001200 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001201 sqlite3VdbeChangeP1(v, addr+1, j+2);
1202 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1203 sqlite3VdbeChangeP1(v, addr+3, j+2);
1204 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001205 sqlite3VdbeJumpHere(v, addr+4);
1206 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001207 "wrong # of entries in index ", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001208 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
drhed717fe2003-06-15 23:42:24 +00001209 }
1210 }
1211 }
danielk19774adee202004-05-08 08:23:19 +00001212 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001213 sqlite3VdbeChangeP2(v, addr, -mxErr);
1214 sqlite3VdbeJumpHere(v, addr+1);
1215 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001216 }else
drhb7f91642004-10-31 02:22:47 +00001217#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1218
drh13d70422004-11-13 15:59:14 +00001219#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001220 /*
1221 ** PRAGMA encoding
1222 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1223 **
drh85b623f2007-12-13 21:54:09 +00001224 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001225 ** database. If the database is not initialized, it is initialized now.
1226 **
1227 ** The second form of this pragma is a no-op if the main database file
1228 ** has not already been initialized. In this case it sets the default
1229 ** encoding that will be used for the main database file if a new file
1230 ** is created. If an existing main database file is opened, then the
1231 ** default text encoding for the existing database is used.
1232 **
1233 ** In all cases new databases created using the ATTACH command are
1234 ** created to use the same default text encoding as the main database. If
1235 ** the main database has not been initialized and/or created when ATTACH
1236 ** is executed, this is done before the ATTACH operation.
1237 **
1238 ** In the second form this pragma sets the text encoding to be used in
1239 ** new database files created using this database handle. It is only
1240 ** useful if invoked immediately after the main database i
1241 */
1242 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001243 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001244 char *zName;
1245 u8 enc;
1246 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001247 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001248 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1249 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1250 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001251 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001252 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001253 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1254 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001255 { 0, 0 }
1256 };
drh0f7eb612006-08-08 13:51:43 +00001257 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001258 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001259 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001260 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001261 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001262 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001263 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1264 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1265 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1266 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001267 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001268 }else{ /* "PRAGMA encoding = XXX" */
1269 /* Only change the value of sqlite.enc if the database handle is not
1270 ** initialized. If the main database exists, the new sqlite.enc value
1271 ** will be overwritten when the schema is next loaded. If it does not
1272 ** already exists, it will be created to use the new encoding value.
1273 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001274 if(
1275 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1276 DbHasProperty(db, 0, DB_Empty)
1277 ){
danielk19778e227872004-06-07 07:52:17 +00001278 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1279 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001280 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001281 break;
1282 }
1283 }
1284 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001285 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001286 }
1287 }
1288 }
1289 }else
drh13d70422004-11-13 15:59:14 +00001290#endif /* SQLITE_OMIT_UTF16 */
1291
1292#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001293 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001294 ** PRAGMA [database.]schema_version
1295 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001296 **
danielk1977b92b70b2004-11-12 16:11:59 +00001297 ** PRAGMA [database.]user_version
1298 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001299 **
danielk1977b92b70b2004-11-12 16:11:59 +00001300 ** The pragma's schema_version and user_version are used to set or get
1301 ** the value of the schema-version and user-version, respectively. Both
1302 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001303 ** stored in the database header.
1304 **
1305 ** The schema-cookie is usually only manipulated internally by SQLite. It
1306 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001307 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001308 ** SQLite each time a query is executed to ensure that the internal cache
1309 ** of the schema used when compiling the SQL query matches the schema of
1310 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001311 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1312 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001313 ** crashes or database corruption. Use with caution!
1314 **
danielk1977b92b70b2004-11-12 16:11:59 +00001315 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001316 ** applications for any purpose.
1317 */
danielk1977180b56a2007-06-24 08:00:42 +00001318 if( sqlite3StrICmp(zLeft, "schema_version")==0
1319 || sqlite3StrICmp(zLeft, "user_version")==0
1320 || sqlite3StrICmp(zLeft, "freelist_count")==0
1321 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001322 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001323 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001324 switch( zLeft[0] ){
danielk1977180b56a2007-06-24 08:00:42 +00001325 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001326 iCookie = BTREE_FREE_PAGE_COUNT;
1327 break;
1328 case 's': case 'S':
1329 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001330 break;
1331 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001332 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001333 break;
danielk1977dae24952004-11-11 05:10:43 +00001334 }
1335
danielk19770d19f7a2009-06-03 11:25:07 +00001336 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001337 /* Write the specified cookie value */
1338 static const VdbeOpList setCookie[] = {
1339 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001340 { OP_Integer, 0, 1, 0}, /* 1 */
1341 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001342 };
1343 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1344 sqlite3VdbeChangeP1(v, addr, iDb);
drh60ac3f42010-11-23 18:59:27 +00001345 sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
danielk1977dae24952004-11-11 05:10:43 +00001346 sqlite3VdbeChangeP1(v, addr+2, iDb);
1347 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1348 }else{
1349 /* Read the specified cookie value */
1350 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001351 { OP_Transaction, 0, 0, 0}, /* 0 */
1352 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001353 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001354 };
1355 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1356 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001357 sqlite3VdbeChangeP1(v, addr+1, iDb);
1358 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001359 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001360 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001361 }
drh6e345992007-03-30 11:12:08 +00001362 }else
drh13d70422004-11-13 15:59:14 +00001363#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001364
shanehdc97a8c2010-02-23 20:08:35 +00001365#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1366 /*
1367 ** PRAGMA compile_options
shanehdc97a8c2010-02-23 20:08:35 +00001368 **
drh71caabf2010-02-26 15:39:24 +00001369 ** Return the names of all compile-time options used in this build,
1370 ** one option per row.
shanehdc97a8c2010-02-23 20:08:35 +00001371 */
drh264a2d42010-02-25 15:28:41 +00001372 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
shanehdc97a8c2010-02-23 20:08:35 +00001373 int i = 0;
1374 const char *zOpt;
1375 sqlite3VdbeSetNumCols(v, 1);
1376 pParse->nMem = 1;
1377 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1378 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1379 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1380 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1381 }
1382 }else
1383#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1384
dan5cf53532010-05-01 16:40:20 +00001385#ifndef SQLITE_OMIT_WAL
1386 /*
dancdc1f042010-11-18 12:11:05 +00001387 ** PRAGMA [database.]wal_checkpoint = passive|full|restart
dan5cf53532010-05-01 16:40:20 +00001388 **
1389 ** Checkpoint the database.
1390 */
dan5a299f92010-05-03 11:05:08 +00001391 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
dana58f26f2010-11-16 18:56:51 +00001392 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
dancdc1f042010-11-18 12:11:05 +00001393 int eMode = SQLITE_CHECKPOINT_PASSIVE;
1394 if( zRight ){
1395 if( sqlite3StrICmp(zRight, "full")==0 ){
1396 eMode = SQLITE_CHECKPOINT_FULL;
1397 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
1398 eMode = SQLITE_CHECKPOINT_RESTART;
1399 }
1400 }
dan5a299f92010-05-03 11:05:08 +00001401 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
dancdc1f042010-11-18 12:11:05 +00001402 sqlite3VdbeSetNumCols(v, 3);
1403 pParse->nMem = 3;
1404 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
1405 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
1406 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
1407
drh30aa3b92011-02-07 23:56:01 +00001408 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
dancdc1f042010-11-18 12:11:05 +00001409 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
dan7c246102010-04-12 19:00:29 +00001410 }else
dan5a299f92010-05-03 11:05:08 +00001411
1412 /*
1413 ** PRAGMA wal_autocheckpoint
1414 ** PRAGMA wal_autocheckpoint = N
1415 **
1416 ** Configure a database connection to automatically checkpoint a database
1417 ** after accumulating N frames in the log. Or query for the current value
1418 ** of N.
1419 */
1420 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
1421 if( zRight ){
drh60ac3f42010-11-23 18:59:27 +00001422 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
dan5a299f92010-05-03 11:05:08 +00001423 }
drhb033d8b2010-05-03 13:37:30 +00001424 returnSingleInt(pParse, "wal_autocheckpoint",
1425 db->xWalCallback==sqlite3WalDefaultHook ?
1426 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
dan5a299f92010-05-03 11:05:08 +00001427 }else
dan5cf53532010-05-01 16:40:20 +00001428#endif
dan7c246102010-04-12 19:00:29 +00001429
dougcurrie81c95ef2004-06-18 23:21:47 +00001430#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001431 /*
1432 ** Report the current state of file logs for all databases
1433 */
1434 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001435 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001436 "unlocked", "shared", "reserved", "pending", "exclusive"
1437 };
1438 int i;
drh89ac8c12004-06-09 14:17:20 +00001439 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001440 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001441 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1442 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001443 for(i=0; i<db->nDb; i++){
1444 Btree *pBt;
1445 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001446 const char *zState = "unknown";
1447 int j;
drh89ac8c12004-06-09 14:17:20 +00001448 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001449 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001450 pBt = db->aDb[i].pBt;
1451 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001452 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001453 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001454 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1455 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001456 }
drh2d401ab2008-01-10 23:50:11 +00001457 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1458 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001459 }
danielk1977a4de4532008-09-02 15:44:08 +00001460
drh89ac8c12004-06-09 14:17:20 +00001461 }else
1462#endif
1463
shanehad9f9f62010-02-17 17:48:46 +00001464#ifdef SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001465 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhea678832008-12-10 19:26:22 +00001466 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001467 }else
drhd2cb50b2009-01-09 21:41:17 +00001468 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1469 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1470 }else
1471 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1472 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1473 int i, h1, h2;
1474 char zKey[40];
1475 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1476 h1 += 9*(1&(h1>>6));
1477 h2 += 9*(1&(h2>>6));
1478 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1479 }
1480 if( (zLeft[3] & 0xf)==0xb ){
1481 sqlite3_key(db, zKey, i/2);
1482 }else{
1483 sqlite3_rekey(db, zKey, i/2);
1484 }
1485 }else
drh3c4f2a42005-12-08 18:12:56 +00001486#endif
shanehad9f9f62010-02-17 17:48:46 +00001487#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh21e2cab2006-09-25 18:01:57 +00001488 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
shanehad9f9f62010-02-17 17:48:46 +00001489#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001490 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001491 sqlite3_activate_see(&zRight[4]);
1492 }
1493#endif
1494#ifdef SQLITE_ENABLE_CEROD
1495 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001496 sqlite3_activate_cerod(&zRight[6]);
1497 }
1498#endif
drhd2cb50b2009-01-09 21:41:17 +00001499 }else
drh21e2cab2006-09-25 18:01:57 +00001500#endif
drh3c4f2a42005-12-08 18:12:56 +00001501
drhd2cb50b2009-01-09 21:41:17 +00001502
1503 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001504
drhd2cb50b2009-01-09 21:41:17 +00001505 /*
1506 ** Reset the safety level, in case the fullfsync flag or synchronous
1507 ** setting changed.
1508 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001509#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhd2cb50b2009-01-09 21:41:17 +00001510 if( db->autoCommit ){
1511 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
drhc97d8462010-11-19 18:23:35 +00001512 (db->flags&SQLITE_FullFSync)!=0,
1513 (db->flags&SQLITE_CkptFullFSync)!=0);
danielk1977a21c6b62005-01-24 10:25:59 +00001514 }
drhd2cb50b2009-01-09 21:41:17 +00001515#endif
danielk1977e0048402004-06-15 16:51:01 +00001516pragma_out:
drh633e6d52008-07-28 19:34:53 +00001517 sqlite3DbFree(db, zLeft);
1518 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001519}
drh13d70422004-11-13 15:59:14 +00001520
drh8bfdf722009-06-19 14:06:03 +00001521#endif /* SQLITE_OMIT_PRAGMA */