blob: 93a933c48bf6128440d7a1410d93c0b5470568b1 [file] [log] [blame]
drhc11d4f92003-04-06 21:08:24 +00001/*
2** 2003 April 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used to implement the PRAGMA command.
drhc11d4f92003-04-06 21:08:24 +000013*/
14#include "sqliteInt.h"
15
drh13d70422004-11-13 15:59:14 +000016/* Ignore this whole file if pragmas are disabled
17*/
drh8bfdf722009-06-19 14:06:03 +000018#if !defined(SQLITE_OMIT_PRAGMA)
drh13d70422004-11-13 15:59:14 +000019
drhc11d4f92003-04-06 21:08:24 +000020/*
drhc11d4f92003-04-06 21:08:24 +000021** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000022** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
23** unrecognized string argument.
drhc11d4f92003-04-06 21:08:24 +000024**
25** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000026** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000027** to support legacy SQL code. The safety level used to be boolean
28** and older scripts may have used numbers 0 for OFF and 1 for ON.
29*/
drh4f21c4a2008-12-10 22:15:00 +000030static u8 getSafetyLevel(const char *z){
drh722e95a2004-10-25 20:33:44 +000031 /* 123456789 123456789 */
32 static const char zText[] = "onoffalseyestruefull";
33 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
34 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
35 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
36 int i, n;
danielk197778ca0e72009-01-20 16:53:39 +000037 if( sqlite3Isdigit(*z) ){
drh3abbd392008-12-10 23:04:13 +000038 return (u8)atoi(z);
drhc11d4f92003-04-06 21:08:24 +000039 }
drhea678832008-12-10 19:26:22 +000040 n = sqlite3Strlen30(z);
danielk197700e13612008-11-17 19:18:54 +000041 for(i=0; i<ArraySize(iLength); i++){
drh722e95a2004-10-25 20:33:44 +000042 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
43 return iValue[i];
44 }
drhc11d4f92003-04-06 21:08:24 +000045 }
46 return 1;
47}
48
49/*
drh722e95a2004-10-25 20:33:44 +000050** Interpret the given string as a boolean value.
51*/
drh4f21c4a2008-12-10 22:15:00 +000052static u8 getBoolean(const char *z){
drh722e95a2004-10-25 20:33:44 +000053 return getSafetyLevel(z)&1;
54}
55
danielk197741483462007-03-24 16:45:04 +000056/*
57** Interpret the given string as a locking mode value.
58*/
59static int getLockingMode(const char *z){
60 if( z ){
61 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
62 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
63 }
64 return PAGER_LOCKINGMODE_QUERY;
65}
66
danielk1977dddbcdc2007-04-26 14:42:34 +000067#ifndef SQLITE_OMIT_AUTOVACUUM
68/*
69** Interpret the given string as an auto-vacuum mode value.
70**
71** The following strings, "none", "full" and "incremental" are
72** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
73*/
74static int getAutoVacuum(const char *z){
75 int i;
76 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
77 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
78 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
79 i = atoi(z);
drh4f21c4a2008-12-10 22:15:00 +000080 return (u8)((i>=0&&i<=2)?i:0);
danielk1977dddbcdc2007-04-26 14:42:34 +000081}
82#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
83
danielk1977b84f96f2005-01-20 11:32:23 +000084#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000085/*
drh90f5ecb2004-07-22 01:19:35 +000086** Interpret the given string as a temp db location. Return 1 for file
87** backed temporary databases, 2 for the Red-Black tree in memory database
88** and 0 to use the compile-time default.
89*/
90static int getTempStore(const char *z){
91 if( z[0]>='0' && z[0]<='2' ){
92 return z[0] - '0';
93 }else if( sqlite3StrICmp(z, "file")==0 ){
94 return 1;
95 }else if( sqlite3StrICmp(z, "memory")==0 ){
96 return 2;
97 }else{
98 return 0;
99 }
100}
drhbf216272005-02-26 18:10:44 +0000101#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000102
drhbf216272005-02-26 18:10:44 +0000103#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000104/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000105** Invalidate temp storage, either when the temp storage is changed
106** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000107*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000108static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000109 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000110 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000111 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000112 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
113 "from within a transaction");
114 return SQLITE_ERROR;
115 }
116 sqlite3BtreeClose(db->aDb[1].pBt);
117 db->aDb[1].pBt = 0;
118 sqlite3ResetInternalSchema(db, 0);
119 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000120 return SQLITE_OK;
121}
drhbf216272005-02-26 18:10:44 +0000122#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000123
drhbf216272005-02-26 18:10:44 +0000124#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000125/*
126** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000127** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000128** or DEFAULT_TEMP_STORE pragmas.
129*/
130static int changeTempStorage(Parse *pParse, const char *zStorageType){
131 int ts = getTempStore(zStorageType);
132 sqlite3 *db = pParse->db;
133 if( db->temp_store==ts ) return SQLITE_OK;
134 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
135 return SQLITE_ERROR;
136 }
drh4f21c4a2008-12-10 22:15:00 +0000137 db->temp_store = (u8)ts;
drh90f5ecb2004-07-22 01:19:35 +0000138 return SQLITE_OK;
139}
drhbf216272005-02-26 18:10:44 +0000140#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000141
142/*
143** Generate code to return a single integer value.
144*/
drh3c713642009-04-04 16:02:32 +0000145static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
drh5bb7ffe2004-09-02 15:14:00 +0000146 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000147 int mem = ++pParse->nMem;
drh3c713642009-04-04 16:02:32 +0000148 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
149 if( pI64 ){
150 memcpy(pI64, &value, sizeof(value));
151 }
152 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
drh10861722009-04-07 00:49:16 +0000153 sqlite3VdbeSetNumCols(v, 1);
154 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
drh66a51672008-01-03 00:01:23 +0000155 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000156}
157
drhbf216272005-02-26 18:10:44 +0000158#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000159/*
drh87223182004-02-21 14:00:29 +0000160** Check to see if zRight and zLeft refer to a pragma that queries
161** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
162** Also, implement the pragma.
163*/
164static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000165 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000166 const char *zName; /* Name of the pragma */
167 int mask; /* Mask for the db->flags value */
168 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000169 { "full_column_names", SQLITE_FullColNames },
170 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000171 { "count_changes", SQLITE_CountRows },
172 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000173 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000174 { "fullfsync", SQLITE_FullFSync },
drh699b3d42009-02-23 16:52:07 +0000175 { "reverse_unordered_selects", SQLITE_ReverseOrder },
drhcf1023c2007-05-08 20:59:49 +0000176#ifdef SQLITE_DEBUG
177 { "sql_trace", SQLITE_SqlTrace },
178 { "vdbe_listing", SQLITE_VdbeListing },
179 { "vdbe_trace", SQLITE_VdbeTrace },
180#endif
drh0cd2d4c2005-11-03 02:15:02 +0000181#ifndef SQLITE_OMIT_CHECK
182 { "ignore_check_constraints", SQLITE_IgnoreChecks },
183#endif
drh722e95a2004-10-25 20:33:44 +0000184 /* The following is VERY experimental */
danielk197734c68fb2007-03-14 15:37:04 +0000185 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
drh7bec5052005-02-06 02:45:41 +0000186 { "omit_readlock", SQLITE_NoReadlock },
danielk1977da184232006-01-05 11:34:32 +0000187
188 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
189 ** flag if there are any active statements. */
190 { "read_uncommitted", SQLITE_ReadUncommitted },
dan5bde73c2009-09-01 17:11:07 +0000191 { "recursive_triggers", SQLITE_RecTriggers },
dan1da40a32009-09-19 17:00:31 +0000192
dan75cbd982009-09-21 16:06:03 +0000193 /* This flag may only be set if both foreign-key and trigger support
194 ** are present in the build. */
195#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan1da40a32009-09-19 17:00:31 +0000196 { "foreign_keys", SQLITE_ForeignKeys },
dan75cbd982009-09-21 16:06:03 +0000197#endif
drh87223182004-02-21 14:00:29 +0000198 };
199 int i;
drh722e95a2004-10-25 20:33:44 +0000200 const struct sPragmaType *p;
danielk197700e13612008-11-17 19:18:54 +0000201 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
drh722e95a2004-10-25 20:33:44 +0000202 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000203 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000204 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000205 v = sqlite3GetVdbe(pParse);
drhd2cb50b2009-01-09 21:41:17 +0000206 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */
207 if( ALWAYS(v) ){
danielk1977a21c6b62005-01-24 10:25:59 +0000208 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000209 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000210 }else{
dan75cbd982009-09-21 16:06:03 +0000211 int mask = p->mask; /* Mask of bits to set or clear. */
212 if( db->autoCommit==0 ){
213 /* Foreign key support may not be enabled or disabled while not
214 ** in auto-commit mode. */
215 mask &= ~(SQLITE_ForeignKeys);
216 }
217
danielk1977a21c6b62005-01-24 10:25:59 +0000218 if( getBoolean(zRight) ){
dan75cbd982009-09-21 16:06:03 +0000219 db->flags |= mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000220 }else{
dan75cbd982009-09-21 16:06:03 +0000221 db->flags &= ~mask;
danielk1977a21c6b62005-01-24 10:25:59 +0000222 }
danielk19778e556522007-11-13 10:30:24 +0000223
224 /* Many of the flag-pragmas modify the code generated by the SQL
225 ** compiler (eg. count_changes). So add an opcode to expire all
226 ** compiled SQL statements after modifying a pragma value.
227 */
drh66a51672008-01-03 00:01:23 +0000228 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000229 }
drh87223182004-02-21 14:00:29 +0000230 }
danielk19778e556522007-11-13 10:30:24 +0000231
drh87223182004-02-21 14:00:29 +0000232 return 1;
233 }
234 }
235 return 0;
236}
drhbf216272005-02-26 18:10:44 +0000237#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000238
drhd2cb50b2009-01-09 21:41:17 +0000239/*
240** Return a human-readable name for a constraint resolution action.
241*/
danba9108b2009-09-22 07:13:42 +0000242#ifndef SQLITE_OMIT_FOREIGN_KEY
danielk197750af3e12008-10-10 17:47:21 +0000243static const char *actionName(u8 action){
drhd2cb50b2009-01-09 21:41:17 +0000244 const char *zName;
danielk197750af3e12008-10-10 17:47:21 +0000245 switch( action ){
dan1da40a32009-09-19 17:00:31 +0000246 case OE_SetNull: zName = "SET NULL"; break;
247 case OE_SetDflt: zName = "SET DEFAULT"; break;
248 case OE_Cascade: zName = "CASCADE"; break;
249 case OE_Restrict: zName = "RESTRICT"; break;
250 default: zName = "NO ACTION";
251 assert( action==OE_None ); break;
danielk197750af3e12008-10-10 17:47:21 +0000252 }
drhd2cb50b2009-01-09 21:41:17 +0000253 return zName;
danielk197750af3e12008-10-10 17:47:21 +0000254}
danba9108b2009-09-22 07:13:42 +0000255#endif
danielk197750af3e12008-10-10 17:47:21 +0000256
drh87223182004-02-21 14:00:29 +0000257/*
drhc11d4f92003-04-06 21:08:24 +0000258** Process a pragma statement.
259**
260** Pragmas are of this form:
261**
danielk197791cf71b2004-06-26 06:37:06 +0000262** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000263**
264** The identifier might also be a string. The value is a string, and
265** identifier, or a number. If minusFlag is true, then the value is
266** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000267**
268** If the left side is "database.id" then pId1 is the database name
269** and pId2 is the id. If the left side is just "id" then pId1 is the
270** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000271*/
danielk197791cf71b2004-06-26 06:37:06 +0000272void sqlite3Pragma(
273 Parse *pParse,
274 Token *pId1, /* First part of [database.]id field */
275 Token *pId2, /* Second part of [database.]id field, or NULL */
276 Token *pValue, /* Token for <value>, or NULL */
277 int minusFlag /* True if a '-' sign preceded <value> */
278){
279 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
280 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
281 const char *zDb = 0; /* The database name */
282 Token *pId; /* Pointer to <id> token */
283 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000284 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000285 Db *pDb;
drh949f9cd2008-01-12 21:35:57 +0000286 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
drhc11d4f92003-04-06 21:08:24 +0000287 if( v==0 ) return;
drh4611d922010-02-25 14:47:01 +0000288 sqlite3VdbeRunOnlyOnce(v);
drh9cbf3422008-01-17 16:22:13 +0000289 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000290
danielk197791cf71b2004-06-26 06:37:06 +0000291 /* Interpret the [database.] part of the pragma statement. iDb is the
292 ** index of the database this pragma is being applied to in db.aDb[]. */
293 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
294 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000295 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000296
danielk1977ddfb2f02006-02-17 12:25:14 +0000297 /* If the temp database has been explicitly named as part of the
298 ** pragma, make sure it is open.
299 */
300 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
301 return;
302 }
303
drh17435752007-08-16 04:30:38 +0000304 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000305 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000306 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000307 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000308 }else{
drh17435752007-08-16 04:30:38 +0000309 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000310 }
danielk197791cf71b2004-06-26 06:37:06 +0000311
drhd2cb50b2009-01-09 21:41:17 +0000312 assert( pId2 );
313 zDb = pId2->n>0 ? pDb->zName : 0;
danielk197791cf71b2004-06-26 06:37:06 +0000314 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000315 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000316 }
317
drh13d70422004-11-13 15:59:14 +0000318#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000319 /*
drh90f5ecb2004-07-22 01:19:35 +0000320 ** PRAGMA [database.]default_cache_size
321 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000322 **
323 ** The first form reports the current persistent setting for the
324 ** page cache size. The value returned is the maximum number of
325 ** pages in the page cache. The second form sets both the current
326 ** page cache size value and the persistent page cache size value
327 ** stored in the database file.
328 **
329 ** The default cache size is stored in meta-value 2 of page 1 of the
330 ** database file. The cache size is actually the absolute value of
331 ** this memory location. The sign of meta-value 2 determines the
332 ** synchronous setting. A negative value means synchronous is off
333 ** and a positive value means synchronous is on.
334 */
danielk19774adee202004-05-08 08:23:19 +0000335 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000336 static const VdbeOpList getCacheSize[] = {
danielk1977602b4662009-07-02 07:47:33 +0000337 { OP_Transaction, 0, 0, 0}, /* 0 */
338 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
339 { OP_IfPos, 1, 7, 0},
drh3c84ddf2008-01-09 02:15:38 +0000340 { OP_Integer, 0, 2, 0},
341 { OP_Subtract, 1, 2, 1},
danielk1977602b4662009-07-02 07:47:33 +0000342 { OP_IfPos, 1, 7, 0},
343 { OP_Integer, 0, 1, 0}, /* 6 */
drh3c84ddf2008-01-09 02:15:38 +0000344 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000345 };
drh905793e2004-02-21 13:31:09 +0000346 int addr;
danielk19778a414492004-06-29 08:59:35 +0000347 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000348 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000349 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000350 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000351 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000352 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000353 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000354 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +0000355 sqlite3VdbeChangeP1(v, addr+1, iDb);
356 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000357 }else{
drhc11d4f92003-04-06 21:08:24 +0000358 int size = atoi(zRight);
359 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000360 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000361 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
danielk19770d19f7a2009-06-03 11:25:07 +0000362 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, BTREE_DEFAULT_CACHE_SIZE);
drh9cbf3422008-01-17 16:22:13 +0000363 addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
364 sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
drh3c84ddf2008-01-09 02:15:38 +0000365 sqlite3VdbeJumpHere(v, addr);
danielk19770d19f7a2009-06-03 11:25:07 +0000366 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
danielk197714db2662006-01-09 16:12:04 +0000367 pDb->pSchema->cache_size = size;
368 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000369 }
370 }else
371
372 /*
drh90f5ecb2004-07-22 01:19:35 +0000373 ** PRAGMA [database.]page_size
374 ** PRAGMA [database.]page_size=N
375 **
376 ** The first form reports the current setting for the
377 ** database page size in bytes. The second form sets the
378 ** database page size value. The value can only be set if
379 ** the database has not yet been created.
380 */
381 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
382 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000383 assert( pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000384 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000385 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000386 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000387 }else{
danielk1977992772c2007-08-30 10:07:38 +0000388 /* Malloc may fail when setting the page-size, as there is an internal
389 ** buffer that the pager module resizes using sqlite3_realloc().
390 */
danielk1977f653d782008-03-20 11:04:21 +0000391 db->nextPagesize = atoi(zRight);
drhce4869f2009-04-02 20:16:58 +0000392 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
danielk1977992772c2007-08-30 10:07:38 +0000393 db->mallocFailed = 1;
394 }
drh90f5ecb2004-07-22 01:19:35 +0000395 }
396 }else
danielk197741483462007-03-24 16:45:04 +0000397
398 /*
drhf8e632b2007-05-08 14:51:36 +0000399 ** PRAGMA [database.]max_page_count
400 ** PRAGMA [database.]max_page_count=N
401 **
402 ** The first form reports the current setting for the
403 ** maximum number of pages in the database file. The
404 ** second form attempts to change this setting. Both
405 ** forms return the current setting.
406 */
407 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
408 Btree *pBt = pDb->pBt;
409 int newMax = 0;
drhd2cb50b2009-01-09 21:41:17 +0000410 assert( pBt!=0 );
drhf8e632b2007-05-08 14:51:36 +0000411 if( zRight ){
412 newMax = atoi(zRight);
413 }
drhd2cb50b2009-01-09 21:41:17 +0000414 if( ALWAYS(pBt) ){
drhf8e632b2007-05-08 14:51:36 +0000415 newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
416 }
417 returnSingleInt(pParse, "max_page_count", newMax);
418 }else
419
420 /*
drh5b47efa2010-02-12 18:18:39 +0000421 ** PRAGMA [database.]secure_delete
422 ** PRAGMA [database.]secure_delete=ON/OFF
423 **
424 ** The first form reports the current setting for the
425 ** secure_delete flag. The second form changes the secure_delete
426 ** flag setting and reports thenew value.
427 */
428 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
429 Btree *pBt = pDb->pBt;
430 int b = -1;
431 assert( pBt!=0 );
432 if( zRight ){
433 b = getBoolean(zRight);
434 }
drhaf034ed2010-02-12 19:46:26 +0000435 if( pId2->n==0 && b>=0 ){
436 int ii;
437 for(ii=0; ii<db->nDb; ii++){
438 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
439 }
440 }
drh5b47efa2010-02-12 18:18:39 +0000441 b = sqlite3BtreeSecureDelete(pBt, b);
442 returnSingleInt(pParse, "secure_delete", b);
443 }else
444
445 /*
danielk197759a93792008-05-15 17:48:20 +0000446 ** PRAGMA [database.]page_count
447 **
448 ** Return the number of pages in the specified database.
449 */
450 if( sqlite3StrICmp(zLeft,"page_count")==0 ){
danielk197759a93792008-05-15 17:48:20 +0000451 int iReg;
drhd2cb50b2009-01-09 21:41:17 +0000452 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197759a93792008-05-15 17:48:20 +0000453 sqlite3CodeVerifySchema(pParse, iDb);
454 iReg = ++pParse->nMem;
455 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
456 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
457 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000458 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC);
danielk197759a93792008-05-15 17:48:20 +0000459 }else
460
461 /*
danielk197741483462007-03-24 16:45:04 +0000462 ** PRAGMA [database.]locking_mode
463 ** PRAGMA [database.]locking_mode = (normal|exclusive)
464 */
465 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
466 const char *zRet = "normal";
467 int eMode = getLockingMode(zRight);
468
469 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
470 /* Simple "PRAGMA locking_mode;" statement. This is a query for
471 ** the current default locking mode (which may be different to
472 ** the locking-mode of the main database).
473 */
474 eMode = db->dfltLockMode;
475 }else{
476 Pager *pPager;
477 if( pId2->n==0 ){
478 /* This indicates that no database name was specified as part
479 ** of the PRAGMA command. In this case the locking-mode must be
480 ** set on all attached databases, as well as the main db file.
481 **
482 ** Also, the sqlite3.dfltLockMode variable is set so that
483 ** any subsequently attached databases also use the specified
484 ** locking mode.
485 */
486 int ii;
487 assert(pDb==&db->aDb[0]);
488 for(ii=2; ii<db->nDb; ii++){
489 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
490 sqlite3PagerLockingMode(pPager, eMode);
491 }
drh4f21c4a2008-12-10 22:15:00 +0000492 db->dfltLockMode = (u8)eMode;
danielk197741483462007-03-24 16:45:04 +0000493 }
494 pPager = sqlite3BtreePager(pDb->pBt);
495 eMode = sqlite3PagerLockingMode(pPager, eMode);
496 }
497
498 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
499 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
500 zRet = "exclusive";
501 }
502 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000503 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000504 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
505 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000506 }else
drh3b020132008-04-17 17:02:01 +0000507
508 /*
509 ** PRAGMA [database.]journal_mode
shanec782f692008-11-10 19:24:38 +0000510 ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
drh3b020132008-04-17 17:02:01 +0000511 */
512 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
513 int eMode;
danielk1977b3175382008-10-17 18:51:52 +0000514 static char * const azModeName[] = {
515 "delete", "persist", "off", "truncate", "memory"
516 };
drh3b020132008-04-17 17:02:01 +0000517
518 if( zRight==0 ){
519 eMode = PAGER_JOURNALMODE_QUERY;
520 }else{
drhea678832008-12-10 19:26:22 +0000521 int n = sqlite3Strlen30(zRight);
drh04335882008-09-26 21:08:08 +0000522 eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
drh3b020132008-04-17 17:02:01 +0000523 while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
524 eMode--;
525 }
526 }
527 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
danielk1977b53e4962008-06-04 06:45:59 +0000528 /* Simple "PRAGMA journal_mode;" statement. This is a query for
drh3b020132008-04-17 17:02:01 +0000529 ** the current default journal mode (which may be different to
530 ** the journal-mode of the main database).
531 */
532 eMode = db->dfltJournalMode;
533 }else{
534 Pager *pPager;
535 if( pId2->n==0 ){
536 /* This indicates that no database name was specified as part
537 ** of the PRAGMA command. In this case the journal-mode must be
538 ** set on all attached databases, as well as the main db file.
539 **
540 ** Also, the sqlite3.dfltJournalMode variable is set so that
541 ** any subsequently attached databases also use the specified
542 ** journal mode.
543 */
544 int ii;
545 assert(pDb==&db->aDb[0]);
drhfdc40e92008-04-17 20:59:37 +0000546 for(ii=1; ii<db->nDb; ii++){
547 if( db->aDb[ii].pBt ){
548 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
549 sqlite3PagerJournalMode(pPager, eMode);
550 }
drh3b020132008-04-17 17:02:01 +0000551 }
drh4f21c4a2008-12-10 22:15:00 +0000552 db->dfltJournalMode = (u8)eMode;
drh3b020132008-04-17 17:02:01 +0000553 }
554 pPager = sqlite3BtreePager(pDb->pBt);
555 eMode = sqlite3PagerJournalMode(pPager, eMode);
556 }
557 assert( eMode==PAGER_JOURNALMODE_DELETE
drh04335882008-09-26 21:08:08 +0000558 || eMode==PAGER_JOURNALMODE_TRUNCATE
drh3b020132008-04-17 17:02:01 +0000559 || eMode==PAGER_JOURNALMODE_PERSIST
danielk1977b3175382008-10-17 18:51:52 +0000560 || eMode==PAGER_JOURNALMODE_OFF
561 || eMode==PAGER_JOURNALMODE_MEMORY );
drh3b020132008-04-17 17:02:01 +0000562 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +0000563 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
drh3b020132008-04-17 17:02:01 +0000564 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0,
565 azModeName[eMode], P4_STATIC);
566 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
567 }else
danielk1977b53e4962008-06-04 06:45:59 +0000568
569 /*
570 ** PRAGMA [database.]journal_size_limit
571 ** PRAGMA [database.]journal_size_limit=N
572 **
drha9e364f2009-01-13 20:14:15 +0000573 ** Get or set the size limit on rollback journal files.
danielk1977b53e4962008-06-04 06:45:59 +0000574 */
575 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
576 Pager *pPager = sqlite3BtreePager(pDb->pBt);
577 i64 iLimit = -2;
578 if( zRight ){
drh3c713642009-04-04 16:02:32 +0000579 sqlite3Atoi64(zRight, &iLimit);
580 if( iLimit<-1 ) iLimit = -1;
danielk1977b53e4962008-06-04 06:45:59 +0000581 }
582 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
drh3c713642009-04-04 16:02:32 +0000583 returnSingleInt(pParse, "journal_size_limit", iLimit);
danielk1977b53e4962008-06-04 06:45:59 +0000584 }else
585
drh13d70422004-11-13 15:59:14 +0000586#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000587
588 /*
danielk1977951af802004-11-05 15:45:09 +0000589 ** PRAGMA [database.]auto_vacuum
590 ** PRAGMA [database.]auto_vacuum=N
591 **
drha9e364f2009-01-13 20:14:15 +0000592 ** Get or set the value of the database 'auto-vacuum' parameter.
593 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
danielk1977951af802004-11-05 15:45:09 +0000594 */
595#ifndef SQLITE_OMIT_AUTOVACUUM
596 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
597 Btree *pBt = pDb->pBt;
drhd2cb50b2009-01-09 21:41:17 +0000598 assert( pBt!=0 );
danielk197727b1f952007-06-25 08:16:58 +0000599 if( sqlite3ReadSchema(pParse) ){
600 goto pragma_out;
601 }
danielk1977951af802004-11-05 15:45:09 +0000602 if( !zRight ){
drhd2cb50b2009-01-09 21:41:17 +0000603 int auto_vacuum;
604 if( ALWAYS(pBt) ){
605 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
606 }else{
607 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
608 }
danielk1977951af802004-11-05 15:45:09 +0000609 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
610 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000611 int eAuto = getAutoVacuum(zRight);
drhd2cb50b2009-01-09 21:41:17 +0000612 assert( eAuto>=0 && eAuto<=2 );
drh4f21c4a2008-12-10 22:15:00 +0000613 db->nextAutovac = (u8)eAuto;
drhd2cb50b2009-01-09 21:41:17 +0000614 if( ALWAYS(eAuto>=0) ){
danielk197727b1f952007-06-25 08:16:58 +0000615 /* Call SetAutoVacuum() to set initialize the internal auto and
616 ** incr-vacuum flags. This is required in case this connection
617 ** creates the database file. It is important that it is created
618 ** as an auto-vacuum capable db.
619 */
620 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
621 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
622 /* When setting the auto_vacuum mode to either "full" or
623 ** "incremental", write the value of meta[6] in the database
624 ** file. Before writing to meta[6], check that meta[3] indicates
625 ** that this really is an auto-vacuum capable database.
626 */
627 static const VdbeOpList setMeta6[] = {
danielk19770d19f7a2009-06-03 11:25:07 +0000628 { OP_Transaction, 0, 1, 0}, /* 0 */
629 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
630 { OP_If, 1, 0, 0}, /* 2 */
631 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
632 { OP_Integer, 0, 1, 0}, /* 4 */
633 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000634 };
635 int iAddr;
636 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
637 sqlite3VdbeChangeP1(v, iAddr, iDb);
638 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
639 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
640 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
641 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000642 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000643 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000644 }
danielk1977951af802004-11-05 15:45:09 +0000645 }
646 }else
647#endif
648
drhca5557f2007-05-04 18:30:40 +0000649 /*
650 ** PRAGMA [database.]incremental_vacuum(N)
651 **
652 ** Do N steps of incremental vacuuming on a database.
653 */
654#ifndef SQLITE_OMIT_AUTOVACUUM
655 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
656 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000657 if( sqlite3ReadSchema(pParse) ){
658 goto pragma_out;
659 }
drhca5557f2007-05-04 18:30:40 +0000660 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
661 iLimit = 0x7fffffff;
662 }
663 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000664 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000665 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000666 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000667 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000668 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000669 sqlite3VdbeJumpHere(v, addr);
670 }else
671#endif
672
drh13d70422004-11-13 15:59:14 +0000673#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000674 /*
drh90f5ecb2004-07-22 01:19:35 +0000675 ** PRAGMA [database.]cache_size
676 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000677 **
678 ** The first form reports the current local setting for the
679 ** page cache size. The local setting can be different from
680 ** the persistent cache size value that is stored in the database
681 ** file itself. The value returned is the maximum number of
682 ** pages in the page cache. The second form sets the local
683 ** page cache size value. It does not change the persistent
684 ** cache size stored on the disk so the cache size will revert
685 ** to its default value when the database is closed and reopened.
686 ** N should be a positive integer.
687 */
danielk19774adee202004-05-08 08:23:19 +0000688 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000689 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000690 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000691 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000692 }else{
693 int size = atoi(zRight);
694 if( size<0 ) size = -size;
danielk197714db2662006-01-09 16:12:04 +0000695 pDb->pSchema->cache_size = size;
696 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000697 }
698 }else
699
700 /*
drh90f5ecb2004-07-22 01:19:35 +0000701 ** PRAGMA temp_store
702 ** PRAGMA temp_store = "default"|"memory"|"file"
703 **
704 ** Return or set the local value of the temp_store flag. Changing
705 ** the local value does not make changes to the disk file and the default
706 ** value will be restored the next time the database is opened.
707 **
708 ** Note that it is possible for the library compile-time options to
709 ** override this setting
710 */
711 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
712 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000713 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000714 }else{
715 changeTempStorage(pParse, zRight);
716 }
717 }else
718
719 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000720 ** PRAGMA temp_store_directory
721 ** PRAGMA temp_store_directory = ""|"directory_name"
722 **
723 ** Return or set the local value of the temp_store_directory flag. Changing
724 ** the value sets a specific directory to be used for temporary files.
725 ** Setting to a null string reverts to the default temporary directory search.
726 ** If temporary directory is changed, then invalidateTempStorage.
727 **
728 */
729 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
730 if( !zRight ){
731 if( sqlite3_temp_directory ){
732 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000733 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
danielk197710fb7492008-10-31 10:53:22 +0000734 "temp_store_directory", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000735 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
736 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000737 }
738 }else{
drh78f82d12008-09-02 00:52:52 +0000739#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000740 if( zRight[0] ){
danielk1977fab11272008-09-16 14:38:02 +0000741 int rc;
danielk1977861f7452008-06-05 11:39:11 +0000742 int res;
danielk1977fab11272008-09-16 14:38:02 +0000743 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
744 if( rc!=SQLITE_OK || res==0 ){
danielk1977861f7452008-06-05 11:39:11 +0000745 sqlite3ErrorMsg(pParse, "not a writable directory");
746 goto pragma_out;
747 }
drh268283b2005-01-08 15:44:25 +0000748 }
danielk1977b06a0b62008-06-26 10:54:12 +0000749 if( SQLITE_TEMP_STORE==0
750 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
751 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000752 ){
753 invalidateTempStorage(pParse);
754 }
drh17435752007-08-16 04:30:38 +0000755 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000756 if( zRight[0] ){
drh633e6d52008-07-28 19:34:53 +0000757 sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000758 }else{
drh268283b2005-01-08 15:44:25 +0000759 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000760 }
drh78f82d12008-09-02 00:52:52 +0000761#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000762 }
763 }else
764
drhd2cb50b2009-01-09 21:41:17 +0000765#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
766# if defined(__APPLE__)
767# define SQLITE_ENABLE_LOCKING_STYLE 1
768# else
769# define SQLITE_ENABLE_LOCKING_STYLE 0
770# endif
771#endif
772#if SQLITE_ENABLE_LOCKING_STYLE
tpoindex9a09a3c2004-12-20 19:01:32 +0000773 /*
aswiftaebf4132008-11-21 00:10:35 +0000774 ** PRAGMA [database.]lock_proxy_file
775 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
776 **
777 ** Return or set the value of the lock_proxy_file flag. Changing
778 ** the value sets a specific file to be used for database access locks.
779 **
780 */
781 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
782 if( !zRight ){
783 Pager *pPager = sqlite3BtreePager(pDb->pBt);
784 char *proxy_file_path = NULL;
785 sqlite3_file *pFile = sqlite3PagerFile(pPager);
786 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
787 &proxy_file_path);
788
789 if( proxy_file_path ){
790 sqlite3VdbeSetNumCols(v, 1);
791 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
792 "lock_proxy_file", SQLITE_STATIC);
793 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
794 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
795 }
796 }else{
797 Pager *pPager = sqlite3BtreePager(pDb->pBt);
798 sqlite3_file *pFile = sqlite3PagerFile(pPager);
799 int res;
800 if( zRight[0] ){
801 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
802 zRight);
803 } else {
804 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
805 NULL);
806 }
807 if( res!=SQLITE_OK ){
808 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
809 goto pragma_out;
810 }
811 }
812 }else
drhd2cb50b2009-01-09 21:41:17 +0000813#endif /* SQLITE_ENABLE_LOCKING_STYLE */
aswiftaebf4132008-11-21 00:10:35 +0000814
815 /*
drh90f5ecb2004-07-22 01:19:35 +0000816 ** PRAGMA [database.]synchronous
817 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000818 **
819 ** Return or set the local value of the synchronous flag. Changing
820 ** the local value does not make changes to the disk file and the
821 ** default value will be restored the next time the database is
822 ** opened.
823 */
danielk19774adee202004-05-08 08:23:19 +0000824 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000825 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000826 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000827 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000828 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000829 if( !db->autoCommit ){
830 sqlite3ErrorMsg(pParse,
831 "Safety level may not be changed inside a transaction");
832 }else{
drh90f5ecb2004-07-22 01:19:35 +0000833 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000834 }
drhc11d4f92003-04-06 21:08:24 +0000835 }
836 }else
drh13d70422004-11-13 15:59:14 +0000837#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000838
drhbf216272005-02-26 18:10:44 +0000839#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000840 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000841 /* The flagPragma() subroutine also generates any necessary code
842 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000843 }else
drhbf216272005-02-26 18:10:44 +0000844#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000845
drh13d70422004-11-13 15:59:14 +0000846#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000847 /*
848 ** PRAGMA table_info(<table>)
849 **
850 ** Return a single row for each column of the named table. The columns of
851 ** the returned data set are:
852 **
853 ** cid: Column id (numbered from left to right, starting at 0)
854 ** name: Column name
855 ** type: Column declaration type.
856 ** notnull: True if 'NOT NULL' is part of column declaration
857 ** dflt_value: The default value for the column, if any.
858 */
drh5260f7e2004-06-26 19:35:29 +0000859 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000860 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000861 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000862 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000863 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000864 int i;
danielk1977034ca142007-06-26 10:38:54 +0000865 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000866 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000867 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000868 pParse->nMem = 6;
danielk197710fb7492008-10-31 10:53:22 +0000869 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
870 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
871 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
872 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
873 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
874 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000875 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000876 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
danielk1977034ca142007-06-26 10:38:54 +0000877 if( IsHiddenColumn(pCol) ){
878 nHidden++;
879 continue;
880 }
drh2d401ab2008-01-10 23:50:11 +0000881 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
882 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
883 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000884 pCol->zType ? pCol->zType : "", 0);
danielk1977f96a3772008-10-23 05:45:07 +0000885 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
drhb7916a72009-05-27 10:31:29 +0000886 if( pCol->zDflt ){
887 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
drh6f835982006-09-25 13:48:30 +0000888 }else{
drh2d401ab2008-01-10 23:50:11 +0000889 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000890 }
drh2d401ab2008-01-10 23:50:11 +0000891 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
892 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000893 }
894 }
895 }else
896
drh5260f7e2004-06-26 19:35:29 +0000897 if( sqlite3StrICmp(zLeft, "index_info")==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 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000902 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000903 int i;
904 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000905 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000906 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000907 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
908 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
909 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000910 for(i=0; i<pIdx->nColumn; i++){
911 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000912 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
913 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000914 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000915 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
916 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000917 }
918 }
919 }else
920
drh5260f7e2004-06-26 19:35:29 +0000921 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000922 Index *pIdx;
923 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000924 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000925 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000926 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000927 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000928 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000929 if( pIdx ){
930 int i = 0;
931 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000932 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000933 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
934 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
935 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000936 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000937 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
938 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
939 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
940 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000941 ++i;
942 pIdx = pIdx->pNext;
943 }
drhc11d4f92003-04-06 21:08:24 +0000944 }
945 }
946 }else
947
drh13d70422004-11-13 15:59:14 +0000948 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
949 int i;
950 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
951 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000952 pParse->nMem = 3;
danielk197710fb7492008-10-31 10:53:22 +0000953 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
954 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
955 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
drh13d70422004-11-13 15:59:14 +0000956 for(i=0; i<db->nDb; i++){
957 if( db->aDb[i].pBt==0 ) continue;
958 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000959 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
960 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
961 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000962 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000963 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +0000964 }
965 }else
danielk197748af65a2005-02-09 03:20:37 +0000966
967 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
968 int i = 0;
969 HashElem *p;
970 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +0000971 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +0000972 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
973 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000974 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
975 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +0000976 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
977 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
978 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +0000979 }
980 }else
drh13d70422004-11-13 15:59:14 +0000981#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
982
drhb7f91642004-10-31 02:22:47 +0000983#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000984 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000985 FKey *pFK;
986 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000987 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000988 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000989 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000990 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000991 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000992 if( pFK ){
993 int i = 0;
danielk197750af3e12008-10-10 17:47:21 +0000994 sqlite3VdbeSetNumCols(v, 8);
995 pParse->nMem = 8;
danielk197710fb7492008-10-31 10:53:22 +0000996 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
997 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
998 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
999 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
1000 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
1001 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
1002 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
1003 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
danielk1977742f9472004-06-16 12:02:43 +00001004 while(pFK){
1005 int j;
1006 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +00001007 char *zCol = pFK->aCol[j].zCol;
dan8099ce62009-09-23 08:43:35 +00001008 char *zOnDelete = (char *)actionName(pFK->aAction[0]);
1009 char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
drh2d401ab2008-01-10 23:50:11 +00001010 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
1011 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
1012 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
1013 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +00001014 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +00001015 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
danielk197750af3e12008-10-10 17:47:21 +00001016 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
1017 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
1018 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
1019 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
danielk1977742f9472004-06-16 12:02:43 +00001020 }
1021 ++i;
1022 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +00001023 }
drh78100cc2003-08-23 22:40:53 +00001024 }
1025 }
1026 }else
drhb7f91642004-10-31 02:22:47 +00001027#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +00001028
drhc11d4f92003-04-06 21:08:24 +00001029#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +00001030 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +00001031 if( zRight ){
1032 if( getBoolean(zRight) ){
1033 sqlite3ParserTrace(stderr, "parser: ");
1034 }else{
1035 sqlite3ParserTrace(0, 0);
1036 }
drhc11d4f92003-04-06 21:08:24 +00001037 }
1038 }else
1039#endif
1040
drh55ef4d92005-08-14 01:20:37 +00001041 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
1042 ** used will be case sensitive or not depending on the RHS.
1043 */
1044 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
1045 if( zRight ){
1046 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
1047 }
1048 }else
1049
drh1dcdbc02007-01-27 02:24:54 +00001050#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
1051# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
1052#endif
1053
drhb7f91642004-10-31 02:22:47 +00001054#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +00001055 /* Pragma "quick_check" is an experimental reduced version of
1056 ** integrity_check designed to detect most database corruption
1057 ** without most of the overhead of a full integrity-check.
1058 */
1059 if( sqlite3StrICmp(zLeft, "integrity_check")==0
1060 || sqlite3StrICmp(zLeft, "quick_check")==0
1061 ){
drh1dcdbc02007-01-27 02:24:54 +00001062 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +00001063
drhed717fe2003-06-15 23:42:24 +00001064 /* Code that appears at the end of the integrity check. If no error
1065 ** messages have been generated, output OK. Otherwise output the
1066 ** error message
1067 */
drh57196282004-10-06 15:41:16 +00001068 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +00001069 { OP_AddImm, 1, 0, 0}, /* 0 */
1070 { OP_IfNeg, 1, 0, 0}, /* 1 */
1071 { OP_String8, 0, 3, 0}, /* 2 */
1072 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +00001073 };
drhed717fe2003-06-15 23:42:24 +00001074
danielk197741c58b72007-12-29 13:39:19 +00001075 int isQuick = (zLeft[0]=='q');
1076
drhed717fe2003-06-15 23:42:24 +00001077 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +00001078 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +00001079 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +00001080 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001081 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
drh1dcdbc02007-01-27 02:24:54 +00001082
1083 /* Set the maximum error count */
1084 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1085 if( zRight ){
1086 mxErr = atoi(zRight);
1087 if( mxErr<=0 ){
1088 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
1089 }
1090 }
drh2d401ab2008-01-10 23:50:11 +00001091 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +00001092
1093 /* Do an integrity check on each database file */
1094 for(i=0; i<db->nDb; i++){
1095 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +00001096 Hash *pTbls;
drh79069752004-05-22 21:30:40 +00001097 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +00001098
danielk197753c0f742005-03-29 03:10:59 +00001099 if( OMIT_TEMPDB && i==1 ) continue;
1100
drh80242052004-06-09 00:48:12 +00001101 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +00001102 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +00001103 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001104 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +00001105
drhed717fe2003-06-15 23:42:24 +00001106 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +00001107 **
1108 ** Begin by filling registers 2, 3, ... with the root pages numbers
1109 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +00001110 */
danielk1977da184232006-01-05 11:34:32 +00001111 pTbls = &db->aDb[i].pSchema->tblHash;
1112 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +00001113 Table *pTab = sqliteHashData(x);
1114 Index *pIdx;
drh98757152008-01-09 23:04:12 +00001115 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001116 cnt++;
1117 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +00001118 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +00001119 cnt++;
1120 }
1121 }
drh2d401ab2008-01-10 23:50:11 +00001122
1123 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +00001124 if( pParse->nMem < cnt+4 ){
1125 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +00001126 }
drh2d401ab2008-01-10 23:50:11 +00001127
1128 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001129 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
drh4f21c4a2008-12-10 22:15:00 +00001130 sqlite3VdbeChangeP5(v, (u8)i);
drh98757152008-01-09 23:04:12 +00001131 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1132 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001133 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001134 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001135 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001136 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001137 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001138 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001139
1140 /* Make sure all the indices are constructed correctly.
1141 */
danielk197741c58b72007-12-29 13:39:19 +00001142 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001143 Table *pTab = sqliteHashData(x);
1144 Index *pIdx;
1145 int loopTop;
1146
1147 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001148 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001149 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001150 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001151 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001152 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001153 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001154 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001155 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001156 int jmp2;
drh91fc4a02009-11-12 20:39:03 +00001157 int r1;
drh57196282004-10-06 15:41:16 +00001158 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001159 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001160 { OP_String8, 0, 3, 0}, /* 1 */
1161 { OP_Rowid, 1, 4, 0},
1162 { OP_String8, 0, 5, 0}, /* 3 */
1163 { OP_String8, 0, 6, 0}, /* 4 */
1164 { OP_Concat, 4, 3, 3},
1165 { OP_Concat, 5, 3, 3},
1166 { OP_Concat, 6, 3, 3},
1167 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001168 { OP_IfPos, 1, 0, 0}, /* 9 */
1169 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001170 };
drh91fc4a02009-11-12 20:39:03 +00001171 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
1172 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
danielk19774adee202004-05-08 08:23:19 +00001173 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001174 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1175 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +00001176 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
drhbb8a2792008-03-19 00:21:30 +00001177 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001178 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001179 }
drh66a51672008-01-03 00:01:23 +00001180 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001181 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001182 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001183 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001184 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001185 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001186 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001187 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001188 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001189 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001190 { OP_String8, 0, 2, 0}, /* 6 */
1191 { OP_String8, 0, 3, 0}, /* 7 */
1192 { OP_Concat, 3, 2, 2},
1193 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001194 };
drh3c84ddf2008-01-09 02:15:38 +00001195 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001196 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001197 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001198 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001199 sqlite3VdbeChangeP1(v, addr+1, j+2);
1200 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1201 sqlite3VdbeChangeP1(v, addr+3, j+2);
1202 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001203 sqlite3VdbeJumpHere(v, addr+4);
1204 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001205 "wrong # of entries in index ", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001206 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
drhed717fe2003-06-15 23:42:24 +00001207 }
1208 }
1209 }
danielk19774adee202004-05-08 08:23:19 +00001210 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001211 sqlite3VdbeChangeP2(v, addr, -mxErr);
1212 sqlite3VdbeJumpHere(v, addr+1);
1213 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001214 }else
drhb7f91642004-10-31 02:22:47 +00001215#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1216
drh13d70422004-11-13 15:59:14 +00001217#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001218 /*
1219 ** PRAGMA encoding
1220 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1221 **
drh85b623f2007-12-13 21:54:09 +00001222 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001223 ** database. If the database is not initialized, it is initialized now.
1224 **
1225 ** The second form of this pragma is a no-op if the main database file
1226 ** has not already been initialized. In this case it sets the default
1227 ** encoding that will be used for the main database file if a new file
1228 ** is created. If an existing main database file is opened, then the
1229 ** default text encoding for the existing database is used.
1230 **
1231 ** In all cases new databases created using the ATTACH command are
1232 ** created to use the same default text encoding as the main database. If
1233 ** the main database has not been initialized and/or created when ATTACH
1234 ** is executed, this is done before the ATTACH operation.
1235 **
1236 ** In the second form this pragma sets the text encoding to be used in
1237 ** new database files created using this database handle. It is only
1238 ** useful if invoked immediately after the main database i
1239 */
1240 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001241 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001242 char *zName;
1243 u8 enc;
1244 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001245 { "UTF8", SQLITE_UTF8 },
drhd2cb50b2009-01-09 21:41:17 +00001246 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
1247 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
1248 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
drh998da3a2004-06-19 15:22:56 +00001249 { "UTF16le", SQLITE_UTF16LE },
drh998da3a2004-06-19 15:22:56 +00001250 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001251 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1252 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001253 { 0, 0 }
1254 };
drh0f7eb612006-08-08 13:51:43 +00001255 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001256 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001257 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001258 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001259 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001260 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
drhd2cb50b2009-01-09 21:41:17 +00001261 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
1262 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
1263 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
1264 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001265 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001266 }else{ /* "PRAGMA encoding = XXX" */
1267 /* Only change the value of sqlite.enc if the database handle is not
1268 ** initialized. If the main database exists, the new sqlite.enc value
1269 ** will be overwritten when the schema is next loaded. If it does not
1270 ** already exists, it will be created to use the new encoding value.
1271 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001272 if(
1273 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1274 DbHasProperty(db, 0, DB_Empty)
1275 ){
danielk19778e227872004-06-07 07:52:17 +00001276 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1277 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001278 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001279 break;
1280 }
1281 }
1282 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001283 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001284 }
1285 }
1286 }
1287 }else
drh13d70422004-11-13 15:59:14 +00001288#endif /* SQLITE_OMIT_UTF16 */
1289
1290#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001291 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001292 ** PRAGMA [database.]schema_version
1293 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001294 **
danielk1977b92b70b2004-11-12 16:11:59 +00001295 ** PRAGMA [database.]user_version
1296 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001297 **
danielk1977b92b70b2004-11-12 16:11:59 +00001298 ** The pragma's schema_version and user_version are used to set or get
1299 ** the value of the schema-version and user-version, respectively. Both
1300 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001301 ** stored in the database header.
1302 **
1303 ** The schema-cookie is usually only manipulated internally by SQLite. It
1304 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001305 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001306 ** SQLite each time a query is executed to ensure that the internal cache
1307 ** of the schema used when compiling the SQL query matches the schema of
1308 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001309 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1310 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001311 ** crashes or database corruption. Use with caution!
1312 **
danielk1977b92b70b2004-11-12 16:11:59 +00001313 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001314 ** applications for any purpose.
1315 */
danielk1977180b56a2007-06-24 08:00:42 +00001316 if( sqlite3StrICmp(zLeft, "schema_version")==0
1317 || sqlite3StrICmp(zLeft, "user_version")==0
1318 || sqlite3StrICmp(zLeft, "freelist_count")==0
1319 ){
danielk19770d19f7a2009-06-03 11:25:07 +00001320 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001321 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001322 switch( zLeft[0] ){
danielk1977180b56a2007-06-24 08:00:42 +00001323 case 'f': case 'F':
danielk19770d19f7a2009-06-03 11:25:07 +00001324 iCookie = BTREE_FREE_PAGE_COUNT;
1325 break;
1326 case 's': case 'S':
1327 iCookie = BTREE_SCHEMA_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001328 break;
1329 default:
danielk19770d19f7a2009-06-03 11:25:07 +00001330 iCookie = BTREE_USER_VERSION;
danielk1977180b56a2007-06-24 08:00:42 +00001331 break;
danielk1977dae24952004-11-11 05:10:43 +00001332 }
1333
danielk19770d19f7a2009-06-03 11:25:07 +00001334 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
danielk1977dae24952004-11-11 05:10:43 +00001335 /* Write the specified cookie value */
1336 static const VdbeOpList setCookie[] = {
1337 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001338 { OP_Integer, 0, 1, 0}, /* 1 */
1339 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001340 };
1341 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1342 sqlite3VdbeChangeP1(v, addr, iDb);
1343 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1344 sqlite3VdbeChangeP1(v, addr+2, iDb);
1345 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1346 }else{
1347 /* Read the specified cookie value */
1348 static const VdbeOpList readCookie[] = {
danielk1977602b4662009-07-02 07:47:33 +00001349 { OP_Transaction, 0, 0, 0}, /* 0 */
1350 { OP_ReadCookie, 0, 1, 0}, /* 1 */
drh2d401ab2008-01-10 23:50:11 +00001351 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001352 };
1353 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1354 sqlite3VdbeChangeP1(v, addr, iDb);
danielk1977602b4662009-07-02 07:47:33 +00001355 sqlite3VdbeChangeP1(v, addr+1, iDb);
1356 sqlite3VdbeChangeP3(v, addr+1, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001357 sqlite3VdbeSetNumCols(v, 1);
danielk197710fb7492008-10-31 10:53:22 +00001358 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001359 }
drh6e345992007-03-30 11:12:08 +00001360 }else
drh13d70422004-11-13 15:59:14 +00001361#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001362
shanehdc97a8c2010-02-23 20:08:35 +00001363#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1364 /*
1365 ** PRAGMA compile_options
1366 ** PRAGMA compile_option(<option>)
1367 **
1368 ** The first form returns a single row for each option that was
1369 ** defined at compile time. The second form returns 0 or 1
1370 ** indicating whether the specified option was defined at
1371 ** compile time.
1372 */
1373 if( sqlite3StrICmp(zLeft, "compile_option")==0 && zRight ){
1374 int used = sqlite3_compileoption_used(zRight);
1375 returnSingleInt(pParse, zRight, used);
1376 }else
1377
1378 if( sqlite3StrICmp(zLeft, "compile_options")==0 && !zRight ){
1379 int i = 0;
1380 const char *zOpt;
1381 sqlite3VdbeSetNumCols(v, 1);
1382 pParse->nMem = 1;
1383 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
1384 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
1385 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
1386 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1387 }
1388 }else
1389#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
1390
dougcurrie81c95ef2004-06-18 23:21:47 +00001391#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001392 /*
1393 ** Report the current state of file logs for all databases
1394 */
1395 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001396 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001397 "unlocked", "shared", "reserved", "pending", "exclusive"
1398 };
1399 int i;
drh89ac8c12004-06-09 14:17:20 +00001400 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001401 pParse->nMem = 2;
danielk197710fb7492008-10-31 10:53:22 +00001402 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
1403 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001404 for(i=0; i<db->nDb; i++){
1405 Btree *pBt;
1406 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001407 const char *zState = "unknown";
1408 int j;
drh89ac8c12004-06-09 14:17:20 +00001409 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001410 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001411 pBt = db->aDb[i].pBt;
1412 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001413 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001414 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001415 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1416 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001417 }
drh2d401ab2008-01-10 23:50:11 +00001418 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1419 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001420 }
danielk1977a4de4532008-09-02 15:44:08 +00001421
drh89ac8c12004-06-09 14:17:20 +00001422 }else
1423#endif
1424
shanehad9f9f62010-02-17 17:48:46 +00001425#ifdef SQLITE_HAS_CODEC
drhd2cb50b2009-01-09 21:41:17 +00001426 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
drhea678832008-12-10 19:26:22 +00001427 sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
drh3c4f2a42005-12-08 18:12:56 +00001428 }else
drhd2cb50b2009-01-09 21:41:17 +00001429 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
1430 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
1431 }else
1432 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
1433 sqlite3StrICmp(zLeft, "hexrekey")==0) ){
1434 int i, h1, h2;
1435 char zKey[40];
1436 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
1437 h1 += 9*(1&(h1>>6));
1438 h2 += 9*(1&(h2>>6));
1439 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
1440 }
1441 if( (zLeft[3] & 0xf)==0xb ){
1442 sqlite3_key(db, zKey, i/2);
1443 }else{
1444 sqlite3_rekey(db, zKey, i/2);
1445 }
1446 }else
drh3c4f2a42005-12-08 18:12:56 +00001447#endif
shanehad9f9f62010-02-17 17:48:46 +00001448#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
drh21e2cab2006-09-25 18:01:57 +00001449 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
shanehad9f9f62010-02-17 17:48:46 +00001450#ifdef SQLITE_HAS_CODEC
drh21e2cab2006-09-25 18:01:57 +00001451 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001452 sqlite3_activate_see(&zRight[4]);
1453 }
1454#endif
1455#ifdef SQLITE_ENABLE_CEROD
1456 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
drh21e2cab2006-09-25 18:01:57 +00001457 sqlite3_activate_cerod(&zRight[6]);
1458 }
1459#endif
drhd2cb50b2009-01-09 21:41:17 +00001460 }else
drh21e2cab2006-09-25 18:01:57 +00001461#endif
drh3c4f2a42005-12-08 18:12:56 +00001462
drhd2cb50b2009-01-09 21:41:17 +00001463
1464 {/* Empty ELSE clause */}
danielk1977a21c6b62005-01-24 10:25:59 +00001465
drhd2cb50b2009-01-09 21:41:17 +00001466 /*
1467 ** Reset the safety level, in case the fullfsync flag or synchronous
1468 ** setting changed.
1469 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001470#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhd2cb50b2009-01-09 21:41:17 +00001471 if( db->autoCommit ){
1472 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1473 (db->flags&SQLITE_FullFSync)!=0);
danielk1977a21c6b62005-01-24 10:25:59 +00001474 }
drhd2cb50b2009-01-09 21:41:17 +00001475#endif
danielk1977e0048402004-06-15 16:51:01 +00001476pragma_out:
drh633e6d52008-07-28 19:34:53 +00001477 sqlite3DbFree(db, zLeft);
1478 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001479}
drh13d70422004-11-13 15:59:14 +00001480
drh8bfdf722009-06-19 14:06:03 +00001481#endif /* SQLITE_OMIT_PRAGMA */