blob: 3d779a75fa984ee2b7b5dbfc6eec0ba40576384e [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.
13**
danielk1977602b4662009-07-02 07:47:33 +000014** $Id: pragma.c,v 1.214 2009/07/02 07:47:33 danielk1977 Exp $
drhc11d4f92003-04-06 21:08:24 +000015*/
16#include "sqliteInt.h"
17
drh13d70422004-11-13 15:59:14 +000018/* Ignore this whole file if pragmas are disabled
19*/
drh8bfdf722009-06-19 14:06:03 +000020#if !defined(SQLITE_OMIT_PRAGMA)
drh13d70422004-11-13 15:59:14 +000021
drhc11d4f92003-04-06 21:08:24 +000022/*
drhc11d4f92003-04-06 21:08:24 +000023** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000024** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
25** unrecognized string argument.
drhc11d4f92003-04-06 21:08:24 +000026**
27** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000028** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000029** to support legacy SQL code. The safety level used to be boolean
30** and older scripts may have used numbers 0 for OFF and 1 for ON.
31*/
drh4f21c4a2008-12-10 22:15:00 +000032static u8 getSafetyLevel(const char *z){
drh722e95a2004-10-25 20:33:44 +000033 /* 123456789 123456789 */
34 static const char zText[] = "onoffalseyestruefull";
35 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
36 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
37 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
38 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000039 if( sqlite3Isdigit(*z) ){
drh3abbd392008-12-10 23:04:13 +000040 return (u8)atoi(z);
drhc11d4f92003-04-06 21:08:24 +000041 }
drhea678832008-12-10 19:26:22 +000042 n = sqlite3Strlen30(z);
danielk197700e13612008-11-17 19:18:54 +000043 for(i=0; i<ArraySize(iLength); i++){
drh722e95a2004-10-25 20:33:44 +000044 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
45 return iValue[i];
46 }
drhc11d4f92003-04-06 21:08:24 +000047 }
48 return 1;
49}
50
51/*
drh722e95a2004-10-25 20:33:44 +000052** Interpret the given string as a boolean value.
53*/
drh4f21c4a2008-12-10 22:15:00 +000054static u8 getBoolean(const char *z){
drh722e95a2004-10-25 20:33:44 +000055 return getSafetyLevel(z)&1;
56}
57
danielk197741483462007-03-24 16:45:04 +000058/*
59** Interpret the given string as a locking mode value.
60*/
61static int getLockingMode(const char *z){
62 if( z ){
63 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
64 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
65 }
66 return PAGER_LOCKINGMODE_QUERY;
67}
68
danielk1977dddbcdc2007-04-26 14:42:34 +000069#ifndef SQLITE_OMIT_AUTOVACUUM
70/*
71** Interpret the given string as an auto-vacuum mode value.
72**
73** The following strings, "none", "full" and "incremental" are
74** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
75*/
76static int getAutoVacuum(const char *z){
77 int i;
78 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
79 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
80 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
81 i = atoi(z);
drh4f21c4a2008-12-10 22:15:00 +000082 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +000083}
84#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
85
danielk1977b84f96f2005-01-20 11:32:23 +000086#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000087/*
drh90f5ecb2004-07-22 01:19:35 +000088** Interpret the given string as a temp db location. Return 1 for file
89** backed temporary databases, 2 for the Red-Black tree in memory database
90** and 0 to use the compile-time default.
91*/
92static int getTempStore(const char *z){
93 if( z[0]>='0' && z[0]<='2' ){
94 return z[0] - '0';
95 }else if( sqlite3StrICmp(z, "file")==0 ){
96 return 1;
97 }else if( sqlite3StrICmp(z, "memory")==0 ){
98 return 2;
99 }else{
100 return 0;
101 }
102}
drhbf216272005-02-26 18:10:44 +0000103#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000104
drhbf216272005-02-26 18:10:44 +0000105#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000106/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000107** Invalidate temp storage, either when the temp storage is changed
108** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000109*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000110static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000111 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000112 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000113 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000114 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
115 "from within a transaction");
116 return SQLITE_ERROR;
117 }
118 sqlite3BtreeClose(db->aDb[1].pBt);
119 db->aDb[1].pBt = 0;
120 sqlite3ResetInternalSchema(db, 0);
121 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000122 return SQLITE_OK;
123}
drhbf216272005-02-26 18:10:44 +0000124#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000125
drhbf216272005-02-26 18:10:44 +0000126#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000127/*
128** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000129** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000130** or DEFAULT_TEMP_STORE pragmas.
131*/
132static int changeTempStorage(Parse *pParse, const char *zStorageType){
133 int ts = getTempStore(zStorageType);
134 sqlite3 *db = pParse->db;
135 if( db->temp_store==ts ) return SQLITE_OK;
136 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
137 return SQLITE_ERROR;
138 }
drh4f21c4a2008-12-10 22:15:00 +0000139 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000140 return SQLITE_OK;
141}
drhbf216272005-02-26 18:10:44 +0000142#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000143
144/*
145** Generate code to return a single integer value.
146*/
drh3c713642009-04-04 16:02:32 +0000147static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000148 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000149 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000150 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
151 if( pI64 ){
152 memcpy(pI64, &value, sizeof(value));
153 }
154 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000155 sqlite3VdbeSetNumCols(v, 1);
156 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000157 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000158}
159
drhbf216272005-02-26 18:10:44 +0000160#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000161/*
drh87223182004-02-21 14:00:29 +0000162** Check to see if zRight and zLeft refer to a pragma that queries
163** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
164** Also, implement the pragma.
165*/
166static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000167 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000168 const char *zName; /* Name of the pragma */
169 int mask; /* Mask for the db->flags value */
170 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000171 { "full_column_names", SQLITE_FullColNames },
172 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000173 { "count_changes", SQLITE_CountRows },
174 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000175 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000176 { "fullfsync", SQLITE_FullFSync },
drh699b3d42009-02-23 16:52:07 +0000177 { "reverse_unordered_selects", SQLITE_ReverseOrder },
drhcf1023c2007-05-08 20:59:49 +0000178#ifdef SQLITE_DEBUG
179 { "sql_trace", SQLITE_SqlTrace },
180 { "vdbe_listing", SQLITE_VdbeListing },
181 { "vdbe_trace", SQLITE_VdbeTrace },
182#endif
drh0cd2d4c2005-11-03 02:15:02 +0000183#ifndef SQLITE_OMIT_CHECK
184 { "ignore_check_constraints", SQLITE_IgnoreChecks },
185#endif
drh722e95a2004-10-25 20:33:44 +0000186 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000187 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
drh7bec5052005-02-06 02:45:41 +0000188 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000189
190 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
191 ** flag if there are any active statements. */
192 { "read_uncommitted", SQLITE_ReadUncommitted },
dan5bde73c2009-09-01 17:11:07 +0000193 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000194
dan75cbd982009-09-21 16:06:03 +0000195 /* This flag may only be set if both foreign-key and trigger support
196 ** are present in the build. */
197#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan1da40a32009-09-19 17:00:31 +0000198 { "foreign_keys", SQLITE_ForeignKeys },
dan75cbd982009-09-21 16:06:03 +0000199#endif
drh87223182004-02-21 14:00:29 +0000200 };
201 int i;
drh722e95a2004-10-25 20:33:44 +0000202 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000203 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000204 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000205 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000206 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000207 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000208 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
209 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000210 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000211 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000212 }else{
dan75cbd982009-09-21 16:06:03 +0000213 int mask = p->mask; /* Mask of bits to set or clear. */
214 if( db->autoCommit==0 ){
215 /* Foreign key support may not be enabled or disabled while not
216 ** in auto-commit mode. */
217 mask &= ~(SQLITE_ForeignKeys);
218 }
219
danielk1977a21c6b62005-01-24 10:25:59 +0000220 if( getBoolean(zRight) ){
dan75cbd982009-09-21 16:06:03 +0000221 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000222 }else{
dan75cbd982009-09-21 16:06:03 +0000223 db->flags &= ~mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000224 }
danielk19778e556522007-11-13 10:30:24 +0000225
226 /* Many of the flag-pragmas modify the code generated by the SQL
227 ** compiler (eg. count_changes). So add an opcode to expire all
228 ** compiled SQL statements after modifying a pragma value.
229 */
drh66a51672008-01-03 00:01:23 +0000230 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000231 }
drh87223182004-02-21 14:00:29 +0000232 }
danielk19778e556522007-11-13 10:30:24 +0000233
drh87223182004-02-21 14:00:29 +0000234 return 1;
235 }
236 }
237 return 0;
238}
drhbf216272005-02-26 18:10:44 +0000239#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000240
drhd2cb50b2009-01-09 21:41:17 +0000241/*
242** Return a human-readable name for a constraint resolution action.
243*/
danba9108b2009-09-22 07:13:42 +0000244#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000245static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000246 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000247 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000248 case OE_SetNull: zName = "SET NULL"; break;
249 case OE_SetDflt: zName = "SET DEFAULT"; break;
250 case OE_Cascade: zName = "CASCADE"; break;
251 case OE_Restrict: zName = "RESTRICT"; break;
252 default: zName = "NO ACTION";
253 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000254 }
drhd2cb50b2009-01-09 21:41:17 +0000255 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000256}
danba9108b2009-09-22 07:13:42 +0000257#endif
danielk197750af3e12008-10-10 17:47:21 +0000258
drh87223182004-02-21 14:00:29 +0000259/*
drhc11d4f92003-04-06 21:08:24 +0000260** Process a pragma statement.
261**
262** Pragmas are of this form:
263**
danielk197791cf71b2004-06-26 06:37:06 +0000264** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000265**
266** The identifier might also be a string. The value is a string, and
267** identifier, or a number. If minusFlag is true, then the value is
268** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000269**
270** If the left side is "database.id" then pId1 is the database name
271** and pId2 is the id. If the left side is just "id" then pId1 is the
272** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000273*/
danielk197791cf71b2004-06-26 06:37:06 +0000274void sqlite3Pragma(
275 Parse *pParse,
276 Token *pId1, /* First part of [database.]id field */
277 Token *pId2, /* Second part of [database.]id field, or NULL */
278 Token *pValue, /* Token for <value>, or NULL */
279 int minusFlag /* True if a '-' sign preceded <value> */
280){
281 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
282 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
283 const char *zDb = 0; /* The database name */
284 Token *pId; /* Pointer to <id> token */
285 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000286 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000287 Db *pDb;
drh949f9cd2008-01-12 21:35:57 +0000288 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
drhc11d4f92003-04-06 21:08:24 +0000289 if( v==0 ) return;
drh9cbf3422008-01-17 16:22:13 +0000290 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000291
danielk197791cf71b2004-06-26 06:37:06 +0000292 /* Interpret the [database.] part of the pragma statement. iDb is the
293 ** index of the database this pragma is being applied to in db.aDb[]. */
294 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
295 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000296 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000297
danielk1977ddfb2f02006-02-17 12:25:14 +0000298 /* If the temp database has been explicitly named as part of the
299 ** pragma, make sure it is open.
300 */
301 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
302 return;
303 }
304
drh17435752007-08-16 04:30:38 +0000305 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000306 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000307 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000308 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000309 }else{
drh17435752007-08-16 04:30:38 +0000310 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000311 }
danielk197791cf71b2004-06-26 06:37:06 +0000312
drhd2cb50b2009-01-09 21:41:17 +0000313 assert( pId2 );
314 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000315 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000316 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000317 }
318
drh13d70422004-11-13 15:59:14 +0000319#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000320 /*
drh90f5ecb2004-07-22 01:19:35 +0000321 ** PRAGMA [database.]default_cache_size
322 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000323 **
324 ** The first form reports the current persistent setting for the
325 ** page cache size. The value returned is the maximum number of
326 ** pages in the page cache. The second form sets both the current
327 ** page cache size value and the persistent page cache size value
328 ** stored in the database file.
329 **
330 ** The default cache size is stored in meta-value 2 of page 1 of the
331 ** database file. The cache size is actually the absolute value of
332 ** this memory location. The sign of meta-value 2 determines the
333 ** synchronous setting. A negative value means synchronous is off
334 ** and a positive value means synchronous is on.
335 */
danielk19774adee202004-05-08 08:23:19 +0000336 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000337 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000338 { OP_Transaction, 0, 0, 0}, /* 0 */
339 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
340 { OP_IfPos, 1, 7, 0},
drh3c84ddf2008-01-09 02:15:38 +0000341 { OP_Integer, 0, 2, 0},
342 { OP_Subtract, 1, 2, 1},
danielk1977602b4662009-07-02 07:47:33 +0000343 { OP_IfPos, 1, 7, 0},
344 { OP_Integer, 0, 1, 0}, /* 6 */
drh3c84ddf2008-01-09 02:15:38 +0000345 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000346 };
drh905793e2004-02-21 13:31:09 +0000347 int addr;
danielk19778a414492004-06-29 08:59:35 +0000348 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000349 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000350 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000351 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000352 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000353 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000354 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000355 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000356 sqlite3VdbeChangeP1(v, addr+1, iDb);
357 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000358 }else{
drhc11d4f92003-04-06 21:08:24 +0000359 int size = atoi(zRight);
360 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000361 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000362 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000363 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, BTREE_DEFAULT_CACHE_SIZE);
drh9cbf3422008-01-17 16:22:13 +0000364 addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
365 sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
drh3c84ddf2008-01-09 02:15:38 +0000366 sqlite3VdbeJumpHere(v, addr);
danielk19770d19f7a2009-06-03 11:25:07 +0000367 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
danielk197714db2662006-01-09 16:12:04 +0000368 pDb->pSchema->cache_size = size;
369 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000370 }
371 }else
372
373 /*
drh90f5ecb2004-07-22 01:19:35 +0000374 ** PRAGMA [database.]page_size
375 ** PRAGMA [database.]page_size=N
376 **
377 ** The first form reports the current setting for the
378 ** database page size in bytes. The second form sets the
379 ** database page size value. The value can only be set if
380 ** the database has not yet been created.
381 */
382 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
383 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000384 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000385 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000386 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000387 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000388 }else{
danielk1977992772c2007-08-30 10:07:38 +0000389 /* Malloc may fail when setting the page-size, as there is an internal
390 ** buffer that the pager module resizes using sqlite3_realloc().
391 */
danielk1977f653d782008-03-20 11:04:21 +0000392 db->nextPagesize = atoi(zRight);
drhce4869f2009-04-02 20:16:58 +0000393 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
danielk1977992772c2007-08-30 10:07:38 +0000394 db->mallocFailed = 1;
395 }
drh90f5ecb2004-07-22 01:19:35 +0000396 }
397 }else
danielk197741483462007-03-24 16:45:04 +0000398
399 /*
drhf8e632b2007-05-08 14:51:36 +0000400 ** PRAGMA [database.]max_page_count
401 ** PRAGMA [database.]max_page_count=N
402 **
403 ** The first form reports the current setting for the
404 ** maximum number of pages in the database file. The
405 ** second form attempts to change this setting. Both
406 ** forms return the current setting.
407 */
408 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
409 Btree *pBt = pDb->pBt;
410 int newMax = 0;
drhd2cb50b2009-01-09 21:41:17 +0000411 assert( pBt!=0 );
drhf8e632b2007-05-08 14:51:36 +0000412 if( zRight ){
413 newMax = atoi(zRight);
414 }
drhd2cb50b2009-01-09 21:41:17 +0000415 if( ALWAYS(pBt) ){
drhf8e632b2007-05-08 14:51:36 +0000416 newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
417 }
418 returnSingleInt(pParse, "max_page_count", newMax);
419 }else
420
421 /*
danielk197759a93792008-05-15 17:48:20 +0000422 ** PRAGMA [database.]page_count
423 **
424 ** Return the number of pages in the specified database.
425 */
426 if( sqlite3StrICmp(zLeft,"page_count")==0 ){
danielk197759a93792008-05-15 17:48:20 +0000427 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000428 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197759a93792008-05-15 17:48:20 +0000429 sqlite3CodeVerifySchema(pParse, iDb);
430 iReg = ++pParse->nMem;
431 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
432 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
433 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000434 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC);
danielk197759a93792008-05-15 17:48:20 +0000435 }else
436
437 /*
danielk197741483462007-03-24 16:45:04 +0000438 ** PRAGMA [database.]locking_mode
439 ** PRAGMA [database.]locking_mode = (normal|exclusive)
440 */
441 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
442 const char *zRet = "normal";
443 int eMode = getLockingMode(zRight);
444
445 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
446 /* Simple "PRAGMA locking_mode;" statement. This is a query for
447 ** the current default locking mode (which may be different to
448 ** the locking-mode of the main database).
449 */
450 eMode = db->dfltLockMode;
451 }else{
452 Pager *pPager;
453 if( pId2->n==0 ){
454 /* This indicates that no database name was specified as part
455 ** of the PRAGMA command. In this case the locking-mode must be
456 ** set on all attached databases, as well as the main db file.
457 **
458 ** Also, the sqlite3.dfltLockMode variable is set so that
459 ** any subsequently attached databases also use the specified
460 ** locking mode.
461 */
462 int ii;
463 assert(pDb==&db->aDb[0]);
464 for(ii=2; ii<db->nDb; ii++){
465 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
466 sqlite3PagerLockingMode(pPager, eMode);
467 }
drh4f21c4a2008-12-10 22:15:00 +0000468 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000469 }
470 pPager = sqlite3BtreePager(pDb->pBt);
471 eMode = sqlite3PagerLockingMode(pPager, eMode);
472 }
473
474 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
475 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
476 zRet = "exclusive";
477 }
478 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000479 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000480 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
481 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000482 }else
drh3b020132008-04-17 17:02:01 +0000483
484 /*
485 ** PRAGMA [database.]journal_mode
shanec782f692008-11-10 19:24:38 +0000486 ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
drh3b020132008-04-17 17:02:01 +0000487 */
488 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
489 int eMode;
danielk1977b3175382008-10-17 18:51:52 +0000490 static char * const azModeName[] = {
491 "delete", "persist", "off", "truncate", "memory"
492 };
drh3b020132008-04-17 17:02:01 +0000493
494 if( zRight==0 ){
495 eMode = PAGER_JOURNALMODE_QUERY;
496 }else{
drhea678832008-12-10 19:26:22 +0000497 int n = sqlite3Strlen30(zRight);
drh04335882008-09-26 21:08:08 +0000498 eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
drh3b020132008-04-17 17:02:01 +0000499 while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
500 eMode--;
501 }
502 }
503 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
danielk1977b53e4962008-06-04 06:45:59 +0000504 /* Simple "PRAGMA journal_mode;" statement. This is a query for
drh3b020132008-04-17 17:02:01 +0000505 ** the current default journal mode (which may be different to
506 ** the journal-mode of the main database).
507 */
508 eMode = db->dfltJournalMode;
509 }else{
510 Pager *pPager;
511 if( pId2->n==0 ){
512 /* This indicates that no database name was specified as part
513 ** of the PRAGMA command. In this case the journal-mode must be
514 ** set on all attached databases, as well as the main db file.
515 **
516 ** Also, the sqlite3.dfltJournalMode variable is set so that
517 ** any subsequently attached databases also use the specified
518 ** journal mode.
519 */
520 int ii;
521 assert(pDb==&db->aDb[0]);
drhfdc40e92008-04-17 20:59:37 +0000522 for(ii=1; ii<db->nDb; ii++){
523 if( db->aDb[ii].pBt ){
524 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
525 sqlite3PagerJournalMode(pPager, eMode);
526 }
drh3b020132008-04-17 17:02:01 +0000527 }
drh4f21c4a2008-12-10 22:15:00 +0000528 db->dfltJournalMode = (u8)eMode;
drh3b020132008-04-17 17:02:01 +0000529 }
530 pPager = sqlite3BtreePager(pDb->pBt);
531 eMode = sqlite3PagerJournalMode(pPager, eMode);
532 }
533 assert( eMode==PAGER_JOURNALMODE_DELETE
drh04335882008-09-26 21:08:08 +0000534 || eMode==PAGER_JOURNALMODE_TRUNCATE
drh3b020132008-04-17 17:02:01 +0000535 || eMode==PAGER_JOURNALMODE_PERSIST
danielk1977b3175382008-10-17 18:51:52 +0000536 || eMode==PAGER_JOURNALMODE_OFF
537 || eMode==PAGER_JOURNALMODE_MEMORY );
drh3b020132008-04-17 17:02:01 +0000538 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000539 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000540 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0,
541 azModeName[eMode], P4_STATIC);
542 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
543 }else
danielk1977b53e4962008-06-04 06:45:59 +0000544
545 /*
546 ** PRAGMA [database.]journal_size_limit
547 ** PRAGMA [database.]journal_size_limit=N
548 **
drha9e364f2009-01-13 20:14:15 +0000549 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000550 */
551 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
552 Pager *pPager = sqlite3BtreePager(pDb->pBt);
553 i64 iLimit = -2;
554 if( zRight ){
drh3c713642009-04-04 16:02:32 +0000555 sqlite3Atoi64(zRight, &iLimit);
556 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000557 }
558 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000559 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000560 }else
561
drh13d70422004-11-13 15:59:14 +0000562#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000563
564 /*
danielk1977951af802004-11-05 15:45:09 +0000565 ** PRAGMA [database.]auto_vacuum
566 ** PRAGMA [database.]auto_vacuum=N
567 **
drha9e364f2009-01-13 20:14:15 +0000568 ** Get or set the value of the database 'auto-vacuum' parameter.
569 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000570 */
571#ifndef SQLITE_OMIT_AUTOVACUUM
572 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
573 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000574 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000575 if( sqlite3ReadSchema(pParse) ){
576 goto pragma_out;
577 }
danielk1977951af802004-11-05 15:45:09 +0000578 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000579 int auto_vacuum;
580 if( ALWAYS(pBt) ){
581 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
582 }else{
583 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
584 }
danielk1977951af802004-11-05 15:45:09 +0000585 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
586 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000587 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000588 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000589 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000590 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000591 /* Call SetAutoVacuum() to set initialize the internal auto and
592 ** incr-vacuum flags. This is required in case this connection
593 ** creates the database file. It is important that it is created
594 ** as an auto-vacuum capable db.
595 */
596 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
597 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
598 /* When setting the auto_vacuum mode to either "full" or
599 ** "incremental", write the value of meta[6] in the database
600 ** file. Before writing to meta[6], check that meta[3] indicates
601 ** that this really is an auto-vacuum capable database.
602 */
603 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000604 { OP_Transaction, 0, 1, 0}, /* 0 */
605 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
606 { OP_If, 1, 0, 0}, /* 2 */
607 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
608 { OP_Integer, 0, 1, 0}, /* 4 */
609 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000610 };
611 int iAddr;
612 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
613 sqlite3VdbeChangeP1(v, iAddr, iDb);
614 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
615 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
616 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
617 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000618 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000619 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000620 }
danielk1977951af802004-11-05 15:45:09 +0000621 }
622 }else
623#endif
624
drhca5557f2007-05-04 18:30:40 +0000625 /*
626 ** PRAGMA [database.]incremental_vacuum(N)
627 **
628 ** Do N steps of incremental vacuuming on a database.
629 */
630#ifndef SQLITE_OMIT_AUTOVACUUM
631 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
632 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000633 if( sqlite3ReadSchema(pParse) ){
634 goto pragma_out;
635 }
drhca5557f2007-05-04 18:30:40 +0000636 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
637 iLimit = 0x7fffffff;
638 }
639 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000640 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000641 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000642 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000643 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000644 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000645 sqlite3VdbeJumpHere(v, addr);
646 }else
647#endif
648
drh13d70422004-11-13 15:59:14 +0000649#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000650 /*
drh90f5ecb2004-07-22 01:19:35 +0000651 ** PRAGMA [database.]cache_size
652 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000653 **
654 ** The first form reports the current local setting for the
655 ** page cache size. The local setting can be different from
656 ** the persistent cache size value that is stored in the database
657 ** file itself. The value returned is the maximum number of
658 ** pages in the page cache. The second form sets the local
659 ** page cache size value. It does not change the persistent
660 ** cache size stored on the disk so the cache size will revert
661 ** to its default value when the database is closed and reopened.
662 ** N should be a positive integer.
663 */
danielk19774adee202004-05-08 08:23:19 +0000664 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000665 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000666 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000667 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000668 }else{
669 int size = atoi(zRight);
670 if( size<0 ) size = -size;
danielk197714db2662006-01-09 16:12:04 +0000671 pDb->pSchema->cache_size = size;
672 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000673 }
674 }else
675
676 /*
drh90f5ecb2004-07-22 01:19:35 +0000677 ** PRAGMA temp_store
678 ** PRAGMA temp_store = "default"|"memory"|"file"
679 **
680 ** Return or set the local value of the temp_store flag. Changing
681 ** the local value does not make changes to the disk file and the default
682 ** value will be restored the next time the database is opened.
683 **
684 ** Note that it is possible for the library compile-time options to
685 ** override this setting
686 */
687 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
688 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000689 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000690 }else{
691 changeTempStorage(pParse, zRight);
692 }
693 }else
694
695 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000696 ** PRAGMA temp_store_directory
697 ** PRAGMA temp_store_directory = ""|"directory_name"
698 **
699 ** Return or set the local value of the temp_store_directory flag. Changing
700 ** the value sets a specific directory to be used for temporary files.
701 ** Setting to a null string reverts to the default temporary directory search.
702 ** If temporary directory is changed, then invalidateTempStorage.
703 **
704 */
705 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
706 if( !zRight ){
707 if( sqlite3_temp_directory ){
708 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000709 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000710 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000711 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
712 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000713 }
714 }else{
drh78f82d12008-09-02 00:52:52 +0000715#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000716 if( zRight[0] ){
danielk1977fab11272008-09-16 14:38:02 +0000717 int rc;
danielk1977861f7452008-06-05 11:39:11 +0000718 int res;
danielk1977fab11272008-09-16 14:38:02 +0000719 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
720 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000721 sqlite3ErrorMsg(pParse, "not a writable directory");
722 goto pragma_out;
723 }
drh268283b2005-01-08 15:44:25 +0000724 }
danielk1977b06a0b62008-06-26 10:54:12 +0000725 if( SQLITE_TEMP_STORE==0
726 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
727 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000728 ){
729 invalidateTempStorage(pParse);
730 }
drh17435752007-08-16 04:30:38 +0000731 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000732 if( zRight[0] ){
drh633e6d52008-07-28 19:34:53 +0000733 sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000734 }else{
drh268283b2005-01-08 15:44:25 +0000735 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000736 }
drh78f82d12008-09-02 00:52:52 +0000737#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000738 }
739 }else
740
drhd2cb50b2009-01-09 21:41:17 +0000741#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
742# if defined(__APPLE__)
743# define SQLITE_ENABLE_LOCKING_STYLE 1
744# else
745# define SQLITE_ENABLE_LOCKING_STYLE 0
746# endif
747#endif
748#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000749 /*
aswiftaebf4132008-11-21 00:10:35 +0000750 ** PRAGMA [database.]lock_proxy_file
751 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
752 **
753 ** Return or set the value of the lock_proxy_file flag. Changing
754 ** the value sets a specific file to be used for database access locks.
755 **
756 */
757 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
758 if( !zRight ){
759 Pager *pPager = sqlite3BtreePager(pDb->pBt);
760 char *proxy_file_path = NULL;
761 sqlite3_file *pFile = sqlite3PagerFile(pPager);
762 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
763 &proxy_file_path);
764
765 if( proxy_file_path ){
766 sqlite3VdbeSetNumCols(v, 1);
767 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
768 "lock_proxy_file", SQLITE_STATIC);
769 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
770 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
771 }
772 }else{
773 Pager *pPager = sqlite3BtreePager(pDb->pBt);
774 sqlite3_file *pFile = sqlite3PagerFile(pPager);
775 int res;
776 if( zRight[0] ){
777 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
778 zRight);
779 } else {
780 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
781 NULL);
782 }
783 if( res!=SQLITE_OK ){
784 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
785 goto pragma_out;
786 }
787 }
788 }else
drhd2cb50b2009-01-09 21:41:17 +0000789#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000790
791 /*
drh90f5ecb2004-07-22 01:19:35 +0000792 ** PRAGMA [database.]synchronous
793 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000794 **
795 ** Return or set the local value of the synchronous flag. Changing
796 ** the local value does not make changes to the disk file and the
797 ** default value will be restored the next time the database is
798 ** opened.
799 */
danielk19774adee202004-05-08 08:23:19 +0000800 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000801 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000802 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000803 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000804 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000805 if( !db->autoCommit ){
806 sqlite3ErrorMsg(pParse,
807 "Safety level may not be changed inside a transaction");
808 }else{
drh90f5ecb2004-07-22 01:19:35 +0000809 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000810 }
drhc11d4f92003-04-06 21:08:24 +0000811 }
812 }else
drh13d70422004-11-13 15:59:14 +0000813#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000814
drhbf216272005-02-26 18:10:44 +0000815#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000816 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000817 /* The flagPragma() subroutine also generates any necessary code
818 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000819 }else
drhbf216272005-02-26 18:10:44 +0000820#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000821
drh13d70422004-11-13 15:59:14 +0000822#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000823 /*
824 ** PRAGMA table_info(<table>)
825 **
826 ** Return a single row for each column of the named table. The columns of
827 ** the returned data set are:
828 **
829 ** cid: Column id (numbered from left to right, starting at 0)
830 ** name: Column name
831 ** type: Column declaration type.
832 ** notnull: True if 'NOT NULL' is part of column declaration
833 ** dflt_value: The default value for the column, if any.
834 */
drh5260f7e2004-06-26 19:35:29 +0000835 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000836 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000837 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000838 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000839 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000840 int i;
danielk1977034ca142007-06-26 10:38:54 +0000841 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000842 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000843 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000844 pParse->nMem = 6;
danielk197710fb7492008-10-31 10:53:22 +0000845 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
846 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
847 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
848 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
849 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
850 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000852 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +0000853 if( IsHiddenColumn(pCol) ){
854 nHidden++;
855 continue;
856 }
drh2d401ab2008-01-10 23:50:11 +0000857 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
858 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
859 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000860 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000861 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +0000862 if( pCol->zDflt ){
863 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +0000864 }else{
drh2d401ab2008-01-10 23:50:11 +0000865 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000866 }
drh2d401ab2008-01-10 23:50:11 +0000867 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
868 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000869 }
870 }
871 }else
872
drh5260f7e2004-06-26 19:35:29 +0000873 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000874 Index *pIdx;
875 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000876 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000877 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000878 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000879 int i;
880 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000881 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000882 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000883 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
884 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
885 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000886 for(i=0; i<pIdx->nColumn; i++){
887 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000888 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
889 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000890 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000891 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
892 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000893 }
894 }
895 }else
896
drh5260f7e2004-06-26 19:35:29 +0000897 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000898 Index *pIdx;
899 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000900 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000901 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000902 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000903 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000904 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000905 if( pIdx ){
906 int i = 0;
907 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, "seq", SQLITE_STATIC);
910 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
911 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000912 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000913 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
914 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
915 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
916 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000917 ++i;
918 pIdx = pIdx->pNext;
919 }
drhc11d4f92003-04-06 21:08:24 +0000920 }
921 }
922 }else
923
drh13d70422004-11-13 15:59:14 +0000924 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
925 int i;
926 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
927 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000928 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000929 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
930 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
931 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +0000932 for(i=0; i<db->nDb; i++){
933 if( db->aDb[i].pBt==0 ) continue;
934 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000935 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
936 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
937 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000938 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000939 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +0000940 }
941 }else
danielk197748af65a2005-02-09 03:20:37 +0000942
943 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
944 int i = 0;
945 HashElem *p;
946 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +0000947 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +0000948 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
949 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000950 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
951 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +0000952 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
953 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
954 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +0000955 }
956 }else
drh13d70422004-11-13 15:59:14 +0000957#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
958
drhb7f91642004-10-31 02:22:47 +0000959#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000960 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000961 FKey *pFK;
962 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000963 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000964 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000965 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000966 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000967 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000968 if( pFK ){
969 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +0000970 sqlite3VdbeSetNumCols(v, 8);
971 pParse->nMem = 8;
danielk197710fb7492008-10-31 10:53:22 +0000972 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
973 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
974 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
975 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
976 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
977 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
978 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
979 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000980 while(pFK){
981 int j;
982 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +0000983 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +0000984 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
985 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +0000986 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
987 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
988 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
989 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +0000990 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +0000991 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +0000992 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
993 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
994 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
995 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +0000996 }
997 ++i;
998 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +0000999 }
drh78100cc2003-08-23 22:40:53 +00001000 }
1001 }
1002 }else
drhb7f91642004-10-31 02:22:47 +00001003#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001004
drhc11d4f92003-04-06 21:08:24 +00001005#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001006 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001007 if( zRight ){
1008 if( getBoolean(zRight) ){
1009 sqlite3ParserTrace(stderr, "parser: ");
1010 }else{
1011 sqlite3ParserTrace(0, 0);
1012 }
drhc11d4f92003-04-06 21:08:24 +00001013 }
1014 }else
1015#endif
1016
drh55ef4d92005-08-14 01:20:37 +00001017 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1018 ** used will be case sensitive or not depending on the RHS.
1019 */
1020 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1021 if( zRight ){
1022 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
1023 }
1024 }else
1025
drh1dcdbc02007-01-27 02:24:54 +00001026#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1027# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1028#endif
1029
drhb7f91642004-10-31 02:22:47 +00001030#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +00001031 /* Pragma "quick_check" is an experimental reduced version of
1032 ** integrity_check designed to detect most database corruption
1033 ** without most of the overhead of a full integrity-check.
1034 */
1035 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1036 || sqlite3StrICmp(zLeft, "quick_check")==0
1037 ){
drh1dcdbc02007-01-27 02:24:54 +00001038 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001039
drhed717fe2003-06-15 23:42:24 +00001040 /* Code that appears at the end of the integrity check. If no error
1041 ** messages have been generated, output OK. Otherwise output the
1042 ** error message
1043 */
drh57196282004-10-06 15:41:16 +00001044 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001045 { OP_AddImm, 1, 0, 0}, /* 0 */
1046 { OP_IfNeg, 1, 0, 0}, /* 1 */
1047 { OP_String8, 0, 3, 0}, /* 2 */
1048 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001049 };
drhed717fe2003-06-15 23:42:24 +00001050
danielk197741c58b72007-12-29 13:39:19 +00001051 int isQuick = (zLeft[0]=='q');
1052
drhed717fe2003-06-15 23:42:24 +00001053 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001054 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001055 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001056 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001057 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001058
1059 /* Set the maximum error count */
1060 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1061 if( zRight ){
1062 mxErr = atoi(zRight);
1063 if( mxErr<=0 ){
1064 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1065 }
1066 }
drh2d401ab2008-01-10 23:50:11 +00001067 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001068
1069 /* Do an integrity check on each database file */
1070 for(i=0; i<db->nDb; i++){
1071 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001072 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001073 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001074
danielk197753c0f742005-03-29 03:10:59 +00001075 if( OMIT_TEMPDB && i==1 ) continue;
1076
drh80242052004-06-09 00:48:12 +00001077 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001078 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001079 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001080 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001081
drhed717fe2003-06-15 23:42:24 +00001082 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001083 **
1084 ** Begin by filling registers 2, 3, ... with the root pages numbers
1085 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001086 */
danielk1977da184232006-01-05 11:34:32 +00001087 pTbls = &db->aDb[i].pSchema->tblHash;
1088 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001089 Table *pTab = sqliteHashData(x);
1090 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001091 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001092 cnt++;
1093 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001094 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001095 cnt++;
1096 }
1097 }
drh2d401ab2008-01-10 23:50:11 +00001098
1099 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001100 if( pParse->nMem < cnt+4 ){
1101 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001102 }
drh2d401ab2008-01-10 23:50:11 +00001103
1104 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001105 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001106 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001107 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1108 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001109 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001110 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001111 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001112 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001113 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001114 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001115
1116 /* Make sure all the indices are constructed correctly.
1117 */
danielk197741c58b72007-12-29 13:39:19 +00001118 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001119 Table *pTab = sqliteHashData(x);
1120 Index *pIdx;
1121 int loopTop;
1122
1123 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001124 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001125 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001126 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001127 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001128 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001129 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001130 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001131 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001132 int jmp2;
drh57196282004-10-06 15:41:16 +00001133 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001134 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001135 { OP_String8, 0, 3, 0}, /* 1 */
1136 { OP_Rowid, 1, 4, 0},
1137 { OP_String8, 0, 5, 0}, /* 3 */
1138 { OP_String8, 0, 6, 0}, /* 4 */
1139 { OP_Concat, 4, 3, 3},
1140 { OP_Concat, 5, 3, 3},
1141 { OP_Concat, 6, 3, 3},
1142 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001143 { OP_IfPos, 1, 0, 0}, /* 9 */
1144 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001145 };
drhe14006d2008-03-25 17:23:32 +00001146 sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
drh2d401ab2008-01-10 23:50:11 +00001147 jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
danielk19774adee202004-05-08 08:23:19 +00001148 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001149 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1150 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +00001151 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
drhbb8a2792008-03-19 00:21:30 +00001152 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001153 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001154 }
drh66a51672008-01-03 00:01:23 +00001155 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001156 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001157 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001158 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001159 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001160 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001161 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001162 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001163 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001164 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001165 { OP_String8, 0, 2, 0}, /* 6 */
1166 { OP_String8, 0, 3, 0}, /* 7 */
1167 { OP_Concat, 3, 2, 2},
1168 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001169 };
drh3c84ddf2008-01-09 02:15:38 +00001170 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001171 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001172 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001173 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001174 sqlite3VdbeChangeP1(v, addr+1, j+2);
1175 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1176 sqlite3VdbeChangeP1(v, addr+3, j+2);
1177 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001178 sqlite3VdbeJumpHere(v, addr+4);
1179 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001180 "wrong # of entries in index ", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001181 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
drhed717fe2003-06-15 23:42:24 +00001182 }
1183 }
1184 }
danielk19774adee202004-05-08 08:23:19 +00001185 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001186 sqlite3VdbeChangeP2(v, addr, -mxErr);
1187 sqlite3VdbeJumpHere(v, addr+1);
1188 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001189 }else
drhb7f91642004-10-31 02:22:47 +00001190#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1191
drh13d70422004-11-13 15:59:14 +00001192#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001193 /*
1194 ** PRAGMA encoding
1195 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1196 **
drh85b623f2007-12-13 21:54:09 +00001197 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001198 ** database. If the database is not initialized, it is initialized now.
1199 **
1200 ** The second form of this pragma is a no-op if the main database file
1201 ** has not already been initialized. In this case it sets the default
1202 ** encoding that will be used for the main database file if a new file
1203 ** is created. If an existing main database file is opened, then the
1204 ** default text encoding for the existing database is used.
1205 **
1206 ** In all cases new databases created using the ATTACH command are
1207 ** created to use the same default text encoding as the main database. If
1208 ** the main database has not been initialized and/or created when ATTACH
1209 ** is executed, this is done before the ATTACH operation.
1210 **
1211 ** In the second form this pragma sets the text encoding to be used in
1212 ** new database files created using this database handle. It is only
1213 ** useful if invoked immediately after the main database i
1214 */
1215 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001216 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001217 char *zName;
1218 u8 enc;
1219 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001220 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001221 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1222 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1223 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001224 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001225 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001226 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1227 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001228 { 0, 0 }
1229 };
drh0f7eb612006-08-08 13:51:43 +00001230 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001231 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001232 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001233 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001234 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001235 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001236 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1237 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1238 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1239 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001240 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001241 }else{ /* "PRAGMA encoding = XXX" */
1242 /* Only change the value of sqlite.enc if the database handle is not
1243 ** initialized. If the main database exists, the new sqlite.enc value
1244 ** will be overwritten when the schema is next loaded. If it does not
1245 ** already exists, it will be created to use the new encoding value.
1246 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001247 if(
1248 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1249 DbHasProperty(db, 0, DB_Empty)
1250 ){
danielk19778e227872004-06-07 07:52:17 +00001251 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1252 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001253 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001254 break;
1255 }
1256 }
1257 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001258 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001259 }
1260 }
1261 }
1262 }else
drh13d70422004-11-13 15:59:14 +00001263#endif /* SQLITE_OMIT_UTF16 */
1264
1265#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001266 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001267 ** PRAGMA [database.]schema_version
1268 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001269 **
danielk1977b92b70b2004-11-12 16:11:59 +00001270 ** PRAGMA [database.]user_version
1271 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001272 **
danielk1977b92b70b2004-11-12 16:11:59 +00001273 ** The pragma's schema_version and user_version are used to set or get
1274 ** the value of the schema-version and user-version, respectively. Both
1275 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001276 ** stored in the database header.
1277 **
1278 ** The schema-cookie is usually only manipulated internally by SQLite. It
1279 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001280 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001281 ** SQLite each time a query is executed to ensure that the internal cache
1282 ** of the schema used when compiling the SQL query matches the schema of
1283 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001284 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1285 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001286 ** crashes or database corruption. Use with caution!
1287 **
danielk1977b92b70b2004-11-12 16:11:59 +00001288 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001289 ** applications for any purpose.
1290 */
danielk1977180b56a2007-06-24 08:00:42 +00001291 if( sqlite3StrICmp(zLeft, "schema_version")==0
1292 || sqlite3StrICmp(zLeft, "user_version")==0
1293 || sqlite3StrICmp(zLeft, "freelist_count")==0
1294 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001295 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001296 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001297 switch( zLeft[0] ){
danielk1977180b56a2007-06-24 08:00:42 +00001298 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001299 iCookie = BTREE_FREE_PAGE_COUNT;
1300 break;
1301 case 's': case 'S':
1302 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001303 break;
1304 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001305 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001306 break;
danielk1977dae24952004-11-11 05:10:43 +00001307 }
1308
danielk19770d19f7a2009-06-03 11:25:07 +00001309 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001310 /* Write the specified cookie value */
1311 static const VdbeOpList setCookie[] = {
1312 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001313 { OP_Integer, 0, 1, 0}, /* 1 */
1314 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001315 };
1316 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1317 sqlite3VdbeChangeP1(v, addr, iDb);
1318 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1319 sqlite3VdbeChangeP1(v, addr+2, iDb);
1320 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1321 }else{
1322 /* Read the specified cookie value */
1323 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001324 { OP_Transaction, 0, 0, 0}, /* 0 */
1325 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001326 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001327 };
1328 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1329 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001330 sqlite3VdbeChangeP1(v, addr+1, iDb);
1331 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001332 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001333 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001334 }
drh6e345992007-03-30 11:12:08 +00001335 }else
drh13d70422004-11-13 15:59:14 +00001336#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001337
dougcurrie81c95ef2004-06-18 23:21:47 +00001338#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001339 /*
1340 ** Report the current state of file logs for all databases
1341 */
1342 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001343 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001344 "unlocked", "shared", "reserved", "pending", "exclusive"
1345 };
1346 int i;
drh89ac8c12004-06-09 14:17:20 +00001347 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001348 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001349 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1350 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001351 for(i=0; i<db->nDb; i++){
1352 Btree *pBt;
1353 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001354 const char *zState = "unknown";
1355 int j;
drh89ac8c12004-06-09 14:17:20 +00001356 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001357 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001358 pBt = db->aDb[i].pBt;
1359 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001360 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001361 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001362 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1363 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001364 }
drh2d401ab2008-01-10 23:50:11 +00001365 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1366 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001367 }
danielk1977a4de4532008-09-02 15:44:08 +00001368
drh89ac8c12004-06-09 14:17:20 +00001369 }else
1370#endif
1371
drh3c4f2a42005-12-08 18:12:56 +00001372#if SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001373 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhea678832008-12-10 19:26:22 +00001374 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001375 }else
drhd2cb50b2009-01-09 21:41:17 +00001376 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1377 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1378 }else
1379 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1380 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1381 int i, h1, h2;
1382 char zKey[40];
1383 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1384 h1 += 9*(1&(h1>>6));
1385 h2 += 9*(1&(h2>>6));
1386 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1387 }
1388 if( (zLeft[3] & 0xf)==0xb ){
1389 sqlite3_key(db, zKey, i/2);
1390 }else{
1391 sqlite3_rekey(db, zKey, i/2);
1392 }
1393 }else
drh3c4f2a42005-12-08 18:12:56 +00001394#endif
drh21e2cab2006-09-25 18:01:57 +00001395#if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
1396 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
1397#if SQLITE_HAS_CODEC
1398 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1399 extern void sqlite3_activate_see(const char*);
1400 sqlite3_activate_see(&zRight[4]);
1401 }
1402#endif
1403#ifdef SQLITE_ENABLE_CEROD
1404 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1405 extern void sqlite3_activate_cerod(const char*);
1406 sqlite3_activate_cerod(&zRight[6]);
1407 }
1408#endif
drhd2cb50b2009-01-09 21:41:17 +00001409 }else
drh21e2cab2006-09-25 18:01:57 +00001410#endif
drh3c4f2a42005-12-08 18:12:56 +00001411
drhd2cb50b2009-01-09 21:41:17 +00001412
1413 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001414
drhd2cb50b2009-01-09 21:41:17 +00001415 /* Code an OP_Expire at the end of each PRAGMA program to cause
1416 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
1417 ** are only valid for a single execution.
1418 */
1419 sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
drhac530b12006-02-11 01:25:50 +00001420
drhd2cb50b2009-01-09 21:41:17 +00001421 /*
1422 ** Reset the safety level, in case the fullfsync flag or synchronous
1423 ** setting changed.
1424 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001425#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhd2cb50b2009-01-09 21:41:17 +00001426 if( db->autoCommit ){
1427 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1428 (db->flags&SQLITE_FullFSync)!=0);
danielk1977a21c6b62005-01-24 10:25:59 +00001429 }
drhd2cb50b2009-01-09 21:41:17 +00001430#endif
danielk1977e0048402004-06-15 16:51:01 +00001431pragma_out:
drh633e6d52008-07-28 19:34:53 +00001432 sqlite3DbFree(db, zLeft);
1433 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001434}
drh13d70422004-11-13 15:59:14 +00001435
drh8bfdf722009-06-19 14:06:03 +00001436#endif /* SQLITE_OMIT_PRAGMA */