blob: 30252da0ce7d8b0cdba5d7025770b04b7020e97d [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**
danielk1977a4de4532008-09-02 15:44:08 +000014** $Id: pragma.c,v 1.186 2008/09/02 15:44:09 danielk1977 Exp $
drhc11d4f92003-04-06 21:08:24 +000015*/
16#include "sqliteInt.h"
drh13bff812003-04-15 01:19:47 +000017#include <ctype.h>
drhc11d4f92003-04-06 21:08:24 +000018
drh13d70422004-11-13 15:59:14 +000019/* Ignore this whole file if pragmas are disabled
20*/
drh0ccebe72005-06-07 22:22:50 +000021#if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
drh13d70422004-11-13 15:59:14 +000022
drhc11d4f92003-04-06 21:08:24 +000023/*
drhc11d4f92003-04-06 21:08:24 +000024** Interpret the given string as a safety level. Return 0 for OFF,
jplyonb1639ff2004-01-19 04:52:29 +000025** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
26** unrecognized string argument.
drhc11d4f92003-04-06 21:08:24 +000027**
28** Note that the values returned are one less that the values that
danielk19774adee202004-05-08 08:23:19 +000029** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
drhc11d4f92003-04-06 21:08:24 +000030** to support legacy SQL code. The safety level used to be boolean
31** and older scripts may have used numbers 0 for OFF and 1 for ON.
32*/
drh2646da72005-12-09 20:02:05 +000033static int getSafetyLevel(const char *z){
drh722e95a2004-10-25 20:33:44 +000034 /* 123456789 123456789 */
35 static const char zText[] = "onoffalseyestruefull";
36 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
37 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
38 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
39 int i, n;
40 if( isdigit(*z) ){
drhc11d4f92003-04-06 21:08:24 +000041 return atoi(z);
42 }
drh722e95a2004-10-25 20:33:44 +000043 n = strlen(z);
44 for(i=0; i<sizeof(iLength); i++){
45 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
46 return iValue[i];
47 }
drhc11d4f92003-04-06 21:08:24 +000048 }
49 return 1;
50}
51
52/*
drh722e95a2004-10-25 20:33:44 +000053** Interpret the given string as a boolean value.
54*/
drh2646da72005-12-09 20:02:05 +000055static int getBoolean(const char *z){
drh722e95a2004-10-25 20:33:44 +000056 return getSafetyLevel(z)&1;
57}
58
danielk197741483462007-03-24 16:45:04 +000059/*
60** Interpret the given string as a locking mode value.
61*/
62static int getLockingMode(const char *z){
63 if( z ){
64 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
65 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
66 }
67 return PAGER_LOCKINGMODE_QUERY;
68}
69
danielk1977dddbcdc2007-04-26 14:42:34 +000070#ifndef SQLITE_OMIT_AUTOVACUUM
71/*
72** Interpret the given string as an auto-vacuum mode value.
73**
74** The following strings, "none", "full" and "incremental" are
75** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
76*/
77static int getAutoVacuum(const char *z){
78 int i;
79 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
80 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
81 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
82 i = atoi(z);
83 return ((i>=0&&i<=2)?i:0);
84}
85#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
86
danielk1977b84f96f2005-01-20 11:32:23 +000087#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh722e95a2004-10-25 20:33:44 +000088/*
drh90f5ecb2004-07-22 01:19:35 +000089** Interpret the given string as a temp db location. Return 1 for file
90** backed temporary databases, 2 for the Red-Black tree in memory database
91** and 0 to use the compile-time default.
92*/
93static int getTempStore(const char *z){
94 if( z[0]>='0' && z[0]<='2' ){
95 return z[0] - '0';
96 }else if( sqlite3StrICmp(z, "file")==0 ){
97 return 1;
98 }else if( sqlite3StrICmp(z, "memory")==0 ){
99 return 2;
100 }else{
101 return 0;
102 }
103}
drhbf216272005-02-26 18:10:44 +0000104#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000105
drhbf216272005-02-26 18:10:44 +0000106#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000107/*
tpoindex9a09a3c2004-12-20 19:01:32 +0000108** Invalidate temp storage, either when the temp storage is changed
109** from default, or when 'file' and the temp_store_directory has changed
drh90f5ecb2004-07-22 01:19:35 +0000110*/
tpoindex9a09a3c2004-12-20 19:01:32 +0000111static int invalidateTempStorage(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +0000112 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000113 if( db->aDb[1].pBt!=0 ){
danielk1977983e2302008-07-08 07:35:51 +0000114 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
drh90f5ecb2004-07-22 01:19:35 +0000115 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
116 "from within a transaction");
117 return SQLITE_ERROR;
118 }
119 sqlite3BtreeClose(db->aDb[1].pBt);
120 db->aDb[1].pBt = 0;
121 sqlite3ResetInternalSchema(db, 0);
122 }
tpoindex9a09a3c2004-12-20 19:01:32 +0000123 return SQLITE_OK;
124}
drhbf216272005-02-26 18:10:44 +0000125#endif /* SQLITE_PAGER_PRAGMAS */
tpoindex9a09a3c2004-12-20 19:01:32 +0000126
drhbf216272005-02-26 18:10:44 +0000127#ifndef SQLITE_OMIT_PAGER_PRAGMAS
tpoindex9a09a3c2004-12-20 19:01:32 +0000128/*
129** If the TEMP database is open, close it and mark the database schema
danielk1977b06a0b62008-06-26 10:54:12 +0000130** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
tpoindex9a09a3c2004-12-20 19:01:32 +0000131** or DEFAULT_TEMP_STORE pragmas.
132*/
133static int changeTempStorage(Parse *pParse, const char *zStorageType){
134 int ts = getTempStore(zStorageType);
135 sqlite3 *db = pParse->db;
136 if( db->temp_store==ts ) return SQLITE_OK;
137 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
138 return SQLITE_ERROR;
139 }
drh90f5ecb2004-07-22 01:19:35 +0000140 db->temp_store = ts;
141 return SQLITE_OK;
142}
drhbf216272005-02-26 18:10:44 +0000143#endif /* SQLITE_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000144
145/*
146** Generate code to return a single integer value.
147*/
drh5bb7ffe2004-09-02 15:14:00 +0000148static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
149 Vdbe *v = sqlite3GetVdbe(pParse);
drh0a07c102008-01-03 18:03:08 +0000150 int mem = ++pParse->nMem;
drh4c583122008-01-04 22:01:03 +0000151 sqlite3VdbeAddOp2(v, OP_Integer, value, mem);
drh5bb7ffe2004-09-02 15:14:00 +0000152 if( pParse->explain==0 ){
153 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +0000154 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P4_STATIC);
drh5bb7ffe2004-09-02 15:14:00 +0000155 }
drh66a51672008-01-03 00:01:23 +0000156 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
drh90f5ecb2004-07-22 01:19:35 +0000157}
158
drhbf216272005-02-26 18:10:44 +0000159#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh90f5ecb2004-07-22 01:19:35 +0000160/*
drh87223182004-02-21 14:00:29 +0000161** Check to see if zRight and zLeft refer to a pragma that queries
162** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
163** Also, implement the pragma.
164*/
165static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
drh722e95a2004-10-25 20:33:44 +0000166 static const struct sPragmaType {
drh87223182004-02-21 14:00:29 +0000167 const char *zName; /* Name of the pragma */
168 int mask; /* Mask for the db->flags value */
169 } aPragma[] = {
drh87223182004-02-21 14:00:29 +0000170 { "full_column_names", SQLITE_FullColNames },
171 { "short_column_names", SQLITE_ShortColNames },
drh87223182004-02-21 14:00:29 +0000172 { "count_changes", SQLITE_CountRows },
173 { "empty_result_callbacks", SQLITE_NullCallback },
drhe321c292006-01-12 01:56:43 +0000174 { "legacy_file_format", SQLITE_LegacyFileFmt },
drhac530b12006-02-11 01:25:50 +0000175 { "fullfsync", SQLITE_FullFSync },
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 },
drh87223182004-02-21 14:00:29 +0000191 };
192 int i;
drh722e95a2004-10-25 20:33:44 +0000193 const struct sPragmaType *p;
194 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
195 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
drh9bb575f2004-09-06 17:24:11 +0000196 sqlite3 *db = pParse->db;
drh87223182004-02-21 14:00:29 +0000197 Vdbe *v;
danielk1977a21c6b62005-01-24 10:25:59 +0000198 v = sqlite3GetVdbe(pParse);
199 if( v ){
200 if( zRight==0 ){
drh722e95a2004-10-25 20:33:44 +0000201 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
drhd89bd002005-01-22 03:03:54 +0000202 }else{
danielk1977a21c6b62005-01-24 10:25:59 +0000203 if( getBoolean(zRight) ){
204 db->flags |= p->mask;
205 }else{
206 db->flags &= ~p->mask;
207 }
danielk19778e556522007-11-13 10:30:24 +0000208
209 /* Many of the flag-pragmas modify the code generated by the SQL
210 ** compiler (eg. count_changes). So add an opcode to expire all
211 ** compiled SQL statements after modifying a pragma value.
212 */
drh66a51672008-01-03 00:01:23 +0000213 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
drhd89bd002005-01-22 03:03:54 +0000214 }
drh87223182004-02-21 14:00:29 +0000215 }
danielk19778e556522007-11-13 10:30:24 +0000216
drh87223182004-02-21 14:00:29 +0000217 return 1;
218 }
219 }
220 return 0;
221}
drhbf216272005-02-26 18:10:44 +0000222#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drh87223182004-02-21 14:00:29 +0000223
224/*
drhc11d4f92003-04-06 21:08:24 +0000225** Process a pragma statement.
226**
227** Pragmas are of this form:
228**
danielk197791cf71b2004-06-26 06:37:06 +0000229** PRAGMA [database.]id [= value]
drhc11d4f92003-04-06 21:08:24 +0000230**
231** The identifier might also be a string. The value is a string, and
232** identifier, or a number. If minusFlag is true, then the value is
233** a number that was preceded by a minus sign.
drh90f5ecb2004-07-22 01:19:35 +0000234**
235** If the left side is "database.id" then pId1 is the database name
236** and pId2 is the id. If the left side is just "id" then pId1 is the
237** id and pId2 is any empty string.
drhc11d4f92003-04-06 21:08:24 +0000238*/
danielk197791cf71b2004-06-26 06:37:06 +0000239void sqlite3Pragma(
240 Parse *pParse,
241 Token *pId1, /* First part of [database.]id field */
242 Token *pId2, /* Second part of [database.]id field, or NULL */
243 Token *pValue, /* Token for <value>, or NULL */
244 int minusFlag /* True if a '-' sign preceded <value> */
245){
246 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
247 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
248 const char *zDb = 0; /* The database name */
249 Token *pId; /* Pointer to <id> token */
250 int iDb; /* Database index for <database> */
drh9bb575f2004-09-06 17:24:11 +0000251 sqlite3 *db = pParse->db;
drh90f5ecb2004-07-22 01:19:35 +0000252 Db *pDb;
drh949f9cd2008-01-12 21:35:57 +0000253 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
drhc11d4f92003-04-06 21:08:24 +0000254 if( v==0 ) return;
drh9cbf3422008-01-17 16:22:13 +0000255 pParse->nMem = 2;
drhc11d4f92003-04-06 21:08:24 +0000256
danielk197791cf71b2004-06-26 06:37:06 +0000257 /* Interpret the [database.] part of the pragma statement. iDb is the
258 ** index of the database this pragma is being applied to in db.aDb[]. */
259 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
260 if( iDb<0 ) return;
drh90f5ecb2004-07-22 01:19:35 +0000261 pDb = &db->aDb[iDb];
danielk197791cf71b2004-06-26 06:37:06 +0000262
danielk1977ddfb2f02006-02-17 12:25:14 +0000263 /* If the temp database has been explicitly named as part of the
264 ** pragma, make sure it is open.
265 */
266 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
267 return;
268 }
269
drh17435752007-08-16 04:30:38 +0000270 zLeft = sqlite3NameFromToken(db, pId);
danielk197796fb0dd2004-06-30 09:49:22 +0000271 if( !zLeft ) return;
drhc11d4f92003-04-06 21:08:24 +0000272 if( minusFlag ){
drh17435752007-08-16 04:30:38 +0000273 zRight = sqlite3MPrintf(db, "-%T", pValue);
drhc11d4f92003-04-06 21:08:24 +0000274 }else{
drh17435752007-08-16 04:30:38 +0000275 zRight = sqlite3NameFromToken(db, pValue);
drhc11d4f92003-04-06 21:08:24 +0000276 }
danielk197791cf71b2004-06-26 06:37:06 +0000277
danielk1977260d8a62008-08-20 16:34:24 +0000278 zDb = ((pId2 && pId2->n>0)?pDb->zName:0);
danielk197791cf71b2004-06-26 06:37:06 +0000279 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
danielk1977e0048402004-06-15 16:51:01 +0000280 goto pragma_out;
drhc11d4f92003-04-06 21:08:24 +0000281 }
282
drh13d70422004-11-13 15:59:14 +0000283#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhc11d4f92003-04-06 21:08:24 +0000284 /*
drh90f5ecb2004-07-22 01:19:35 +0000285 ** PRAGMA [database.]default_cache_size
286 ** PRAGMA [database.]default_cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000287 **
288 ** The first form reports the current persistent setting for the
289 ** page cache size. The value returned is the maximum number of
290 ** pages in the page cache. The second form sets both the current
291 ** page cache size value and the persistent page cache size value
292 ** stored in the database file.
293 **
294 ** The default cache size is stored in meta-value 2 of page 1 of the
295 ** database file. The cache size is actually the absolute value of
296 ** this memory location. The sign of meta-value 2 determines the
297 ** synchronous setting. A negative value means synchronous is off
298 ** and a positive value means synchronous is on.
299 */
danielk19774adee202004-05-08 08:23:19 +0000300 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
drh57196282004-10-06 15:41:16 +0000301 static const VdbeOpList getCacheSize[] = {
drh3c84ddf2008-01-09 02:15:38 +0000302 { OP_ReadCookie, 0, 1, 2}, /* 0 */
303 { OP_IfPos, 1, 6, 0},
304 { OP_Integer, 0, 2, 0},
305 { OP_Subtract, 1, 2, 1},
306 { OP_IfPos, 1, 6, 0},
307 { OP_Integer, 0, 1, 0}, /* 5 */
308 { OP_ResultRow, 1, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000309 };
drh905793e2004-02-21 13:31:09 +0000310 int addr;
danielk19778a414492004-06-29 08:59:35 +0000311 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drhfb982642007-08-30 01:19:59 +0000312 sqlite3VdbeUsesBtree(v, iDb);
danielk197791cf71b2004-06-26 06:37:06 +0000313 if( !zRight ){
danielk197722322fd2004-05-25 23:35:17 +0000314 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +0000315 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P4_STATIC);
drh3c84ddf2008-01-09 02:15:38 +0000316 pParse->nMem += 2;
danielk19774adee202004-05-08 08:23:19 +0000317 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
danielk197791cf71b2004-06-26 06:37:06 +0000318 sqlite3VdbeChangeP1(v, addr, iDb);
drhc797d4d2007-05-08 01:08:49 +0000319 sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
drhc11d4f92003-04-06 21:08:24 +0000320 }else{
drhc11d4f92003-04-06 21:08:24 +0000321 int size = atoi(zRight);
322 if( size<0 ) size = -size;
danielk197791cf71b2004-06-26 06:37:06 +0000323 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000324 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
325 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2);
326 addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
327 sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
drh3c84ddf2008-01-09 02:15:38 +0000328 sqlite3VdbeJumpHere(v, addr);
drh9cbf3422008-01-17 16:22:13 +0000329 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1);
danielk197714db2662006-01-09 16:12:04 +0000330 pDb->pSchema->cache_size = size;
331 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000332 }
333 }else
334
335 /*
drh90f5ecb2004-07-22 01:19:35 +0000336 ** PRAGMA [database.]page_size
337 ** PRAGMA [database.]page_size=N
338 **
339 ** The first form reports the current setting for the
340 ** database page size in bytes. The second form sets the
341 ** database page size value. The value can only be set if
342 ** the database has not yet been created.
343 */
344 if( sqlite3StrICmp(zLeft,"page_size")==0 ){
345 Btree *pBt = pDb->pBt;
346 if( !zRight ){
347 int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
drh5bb7ffe2004-09-02 15:14:00 +0000348 returnSingleInt(pParse, "page_size", size);
drh90f5ecb2004-07-22 01:19:35 +0000349 }else{
danielk1977992772c2007-08-30 10:07:38 +0000350 /* Malloc may fail when setting the page-size, as there is an internal
351 ** buffer that the pager module resizes using sqlite3_realloc().
352 */
danielk1977f653d782008-03-20 11:04:21 +0000353 db->nextPagesize = atoi(zRight);
354 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1) ){
danielk1977992772c2007-08-30 10:07:38 +0000355 db->mallocFailed = 1;
356 }
drh90f5ecb2004-07-22 01:19:35 +0000357 }
358 }else
danielk197741483462007-03-24 16:45:04 +0000359
360 /*
drhf8e632b2007-05-08 14:51:36 +0000361 ** PRAGMA [database.]max_page_count
362 ** PRAGMA [database.]max_page_count=N
363 **
364 ** The first form reports the current setting for the
365 ** maximum number of pages in the database file. The
366 ** second form attempts to change this setting. Both
367 ** forms return the current setting.
368 */
369 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
370 Btree *pBt = pDb->pBt;
371 int newMax = 0;
372 if( zRight ){
373 newMax = atoi(zRight);
374 }
375 if( pBt ){
376 newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
377 }
378 returnSingleInt(pParse, "max_page_count", newMax);
379 }else
380
381 /*
danielk197759a93792008-05-15 17:48:20 +0000382 ** PRAGMA [database.]page_count
383 **
384 ** Return the number of pages in the specified database.
385 */
386 if( sqlite3StrICmp(zLeft,"page_count")==0 ){
387 Vdbe *v;
388 int iReg;
389 v = sqlite3GetVdbe(pParse);
390 if( !v || sqlite3ReadSchema(pParse) ) goto pragma_out;
391 sqlite3CodeVerifySchema(pParse, iDb);
392 iReg = ++pParse->nMem;
393 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
394 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
395 sqlite3VdbeSetNumCols(v, 1);
396 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", P4_STATIC);
397 }else
398
399 /*
danielk197741483462007-03-24 16:45:04 +0000400 ** PRAGMA [database.]locking_mode
401 ** PRAGMA [database.]locking_mode = (normal|exclusive)
402 */
403 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
404 const char *zRet = "normal";
405 int eMode = getLockingMode(zRight);
406
407 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
408 /* Simple "PRAGMA locking_mode;" statement. This is a query for
409 ** the current default locking mode (which may be different to
410 ** the locking-mode of the main database).
411 */
412 eMode = db->dfltLockMode;
413 }else{
414 Pager *pPager;
415 if( pId2->n==0 ){
416 /* This indicates that no database name was specified as part
417 ** of the PRAGMA command. In this case the locking-mode must be
418 ** set on all attached databases, as well as the main db file.
419 **
420 ** Also, the sqlite3.dfltLockMode variable is set so that
421 ** any subsequently attached databases also use the specified
422 ** locking mode.
423 */
424 int ii;
425 assert(pDb==&db->aDb[0]);
426 for(ii=2; ii<db->nDb; ii++){
427 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
428 sqlite3PagerLockingMode(pPager, eMode);
429 }
430 db->dfltLockMode = eMode;
431 }
432 pPager = sqlite3BtreePager(pDb->pBt);
433 eMode = sqlite3PagerLockingMode(pPager, eMode);
434 }
435
436 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
437 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
438 zRet = "exclusive";
439 }
440 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +0000441 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000442 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
443 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk197741483462007-03-24 16:45:04 +0000444 }else
drh3b020132008-04-17 17:02:01 +0000445
446 /*
447 ** PRAGMA [database.]journal_mode
448 ** PRAGMA [database.]journal_mode = (delete|persist|off)
449 */
450 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
451 int eMode;
danielk1977a4de4532008-09-02 15:44:08 +0000452 static char * const azModeName[] = {"delete", "persist", "off"};
drh3b020132008-04-17 17:02:01 +0000453
454 if( zRight==0 ){
455 eMode = PAGER_JOURNALMODE_QUERY;
456 }else{
457 int n = strlen(zRight);
458 eMode = 2;
459 while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
460 eMode--;
461 }
462 }
463 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
danielk1977b53e4962008-06-04 06:45:59 +0000464 /* Simple "PRAGMA journal_mode;" statement. This is a query for
drh3b020132008-04-17 17:02:01 +0000465 ** the current default journal mode (which may be different to
466 ** the journal-mode of the main database).
467 */
468 eMode = db->dfltJournalMode;
469 }else{
470 Pager *pPager;
471 if( pId2->n==0 ){
472 /* This indicates that no database name was specified as part
473 ** of the PRAGMA command. In this case the journal-mode must be
474 ** set on all attached databases, as well as the main db file.
475 **
476 ** Also, the sqlite3.dfltJournalMode variable is set so that
477 ** any subsequently attached databases also use the specified
478 ** journal mode.
479 */
480 int ii;
481 assert(pDb==&db->aDb[0]);
drhfdc40e92008-04-17 20:59:37 +0000482 for(ii=1; ii<db->nDb; ii++){
483 if( db->aDb[ii].pBt ){
484 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
485 sqlite3PagerJournalMode(pPager, eMode);
486 }
drh3b020132008-04-17 17:02:01 +0000487 }
488 db->dfltJournalMode = eMode;
489 }
490 pPager = sqlite3BtreePager(pDb->pBt);
491 eMode = sqlite3PagerJournalMode(pPager, eMode);
492 }
493 assert( eMode==PAGER_JOURNALMODE_DELETE
494 || eMode==PAGER_JOURNALMODE_PERSIST
495 || eMode==PAGER_JOURNALMODE_OFF );
496 sqlite3VdbeSetNumCols(v, 1);
497 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", P4_STATIC);
498 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0,
499 azModeName[eMode], P4_STATIC);
500 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
501 }else
danielk1977b53e4962008-06-04 06:45:59 +0000502
503 /*
504 ** PRAGMA [database.]journal_size_limit
505 ** PRAGMA [database.]journal_size_limit=N
506 **
507 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
508 */
509 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
510 Pager *pPager = sqlite3BtreePager(pDb->pBt);
511 i64 iLimit = -2;
512 if( zRight ){
513 int iLimit32 = atoi(zRight);
514 if( iLimit32<-1 ){
515 iLimit32 = -1;
516 }
517 iLimit = iLimit32;
518 }
519 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
520 returnSingleInt(pParse, "journal_size_limit", (int)iLimit);
521 }else
522
drh13d70422004-11-13 15:59:14 +0000523#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drh90f5ecb2004-07-22 01:19:35 +0000524
525 /*
danielk1977951af802004-11-05 15:45:09 +0000526 ** PRAGMA [database.]auto_vacuum
527 ** PRAGMA [database.]auto_vacuum=N
528 **
529 ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
530 */
531#ifndef SQLITE_OMIT_AUTOVACUUM
532 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
533 Btree *pBt = pDb->pBt;
danielk197727b1f952007-06-25 08:16:58 +0000534 if( sqlite3ReadSchema(pParse) ){
535 goto pragma_out;
536 }
danielk1977951af802004-11-05 15:45:09 +0000537 if( !zRight ){
538 int auto_vacuum =
539 pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
540 returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
541 }else{
danielk1977dddbcdc2007-04-26 14:42:34 +0000542 int eAuto = getAutoVacuum(zRight);
drhddac25c2007-12-05 01:38:23 +0000543 db->nextAutovac = eAuto;
danielk1977dddbcdc2007-04-26 14:42:34 +0000544 if( eAuto>=0 ){
danielk197727b1f952007-06-25 08:16:58 +0000545 /* Call SetAutoVacuum() to set initialize the internal auto and
546 ** incr-vacuum flags. This is required in case this connection
547 ** creates the database file. It is important that it is created
548 ** as an auto-vacuum capable db.
549 */
550 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
551 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
552 /* When setting the auto_vacuum mode to either "full" or
553 ** "incremental", write the value of meta[6] in the database
554 ** file. Before writing to meta[6], check that meta[3] indicates
555 ** that this really is an auto-vacuum capable database.
556 */
557 static const VdbeOpList setMeta6[] = {
558 { OP_Transaction, 0, 1, 0}, /* 0 */
drh1db639c2008-01-17 02:36:28 +0000559 { OP_ReadCookie, 0, 1, 3}, /* 1 */
560 { OP_If, 1, 0, 0}, /* 2 */
danielk197727b1f952007-06-25 08:16:58 +0000561 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
drh1db639c2008-01-17 02:36:28 +0000562 { OP_Integer, 0, 1, 0}, /* 4 */
563 { OP_SetCookie, 0, 6, 1}, /* 5 */
danielk197727b1f952007-06-25 08:16:58 +0000564 };
565 int iAddr;
566 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
567 sqlite3VdbeChangeP1(v, iAddr, iDb);
568 sqlite3VdbeChangeP1(v, iAddr+1, iDb);
569 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
570 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
571 sqlite3VdbeChangeP1(v, iAddr+5, iDb);
drhfb982642007-08-30 01:19:59 +0000572 sqlite3VdbeUsesBtree(v, iDb);
danielk197727b1f952007-06-25 08:16:58 +0000573 }
danielk1977dddbcdc2007-04-26 14:42:34 +0000574 }
danielk1977951af802004-11-05 15:45:09 +0000575 }
576 }else
577#endif
578
drhca5557f2007-05-04 18:30:40 +0000579 /*
580 ** PRAGMA [database.]incremental_vacuum(N)
581 **
582 ** Do N steps of incremental vacuuming on a database.
583 */
584#ifndef SQLITE_OMIT_AUTOVACUUM
585 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
586 int iLimit, addr;
danielk197717a240a2007-05-23 13:50:23 +0000587 if( sqlite3ReadSchema(pParse) ){
588 goto pragma_out;
589 }
drhca5557f2007-05-04 18:30:40 +0000590 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
591 iLimit = 0x7fffffff;
592 }
593 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh4c583122008-01-04 22:01:03 +0000594 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
drh3c84ddf2008-01-09 02:15:38 +0000595 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
drh2d401ab2008-01-10 23:50:11 +0000596 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
drh8558cde2008-01-05 05:20:10 +0000597 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
drh3c84ddf2008-01-09 02:15:38 +0000598 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
drhca5557f2007-05-04 18:30:40 +0000599 sqlite3VdbeJumpHere(v, addr);
600 }else
601#endif
602
drh13d70422004-11-13 15:59:14 +0000603#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk1977951af802004-11-05 15:45:09 +0000604 /*
drh90f5ecb2004-07-22 01:19:35 +0000605 ** PRAGMA [database.]cache_size
606 ** PRAGMA [database.]cache_size=N
drhc11d4f92003-04-06 21:08:24 +0000607 **
608 ** The first form reports the current local setting for the
609 ** page cache size. The local setting can be different from
610 ** the persistent cache size value that is stored in the database
611 ** file itself. The value returned is the maximum number of
612 ** pages in the page cache. The second form sets the local
613 ** page cache size value. It does not change the persistent
614 ** cache size stored on the disk so the cache size will revert
615 ** to its default value when the database is closed and reopened.
616 ** N should be a positive integer.
617 */
danielk19774adee202004-05-08 08:23:19 +0000618 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000619 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000620 if( !zRight ){
danielk197714db2662006-01-09 16:12:04 +0000621 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000622 }else{
623 int size = atoi(zRight);
624 if( size<0 ) size = -size;
danielk197714db2662006-01-09 16:12:04 +0000625 pDb->pSchema->cache_size = size;
626 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
drhc11d4f92003-04-06 21:08:24 +0000627 }
628 }else
629
630 /*
drh90f5ecb2004-07-22 01:19:35 +0000631 ** PRAGMA temp_store
632 ** PRAGMA temp_store = "default"|"memory"|"file"
633 **
634 ** Return or set the local value of the temp_store flag. Changing
635 ** the local value does not make changes to the disk file and the default
636 ** value will be restored the next time the database is opened.
637 **
638 ** Note that it is possible for the library compile-time options to
639 ** override this setting
640 */
641 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
642 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000643 returnSingleInt(pParse, "temp_store", db->temp_store);
drh90f5ecb2004-07-22 01:19:35 +0000644 }else{
645 changeTempStorage(pParse, zRight);
646 }
647 }else
648
649 /*
tpoindex9a09a3c2004-12-20 19:01:32 +0000650 ** PRAGMA temp_store_directory
651 ** PRAGMA temp_store_directory = ""|"directory_name"
652 **
653 ** Return or set the local value of the temp_store_directory flag. Changing
654 ** the value sets a specific directory to be used for temporary files.
655 ** Setting to a null string reverts to the default temporary directory search.
656 ** If temporary directory is changed, then invalidateTempStorage.
657 **
658 */
659 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
660 if( !zRight ){
661 if( sqlite3_temp_directory ){
662 sqlite3VdbeSetNumCols(v, 1);
danielk1977955de522006-02-10 02:27:42 +0000663 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
drh66a51672008-01-03 00:01:23 +0000664 "temp_store_directory", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +0000665 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
666 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
tpoindex9a09a3c2004-12-20 19:01:32 +0000667 }
668 }else{
drh78f82d12008-09-02 00:52:52 +0000669#ifndef SQLITE_OMIT_WSD
danielk1977861f7452008-06-05 11:39:11 +0000670 if( zRight[0] ){
671 int res;
672 sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
673 if( res==0 ){
674 sqlite3ErrorMsg(pParse, "not a writable directory");
675 goto pragma_out;
676 }
drh268283b2005-01-08 15:44:25 +0000677 }
danielk1977b06a0b62008-06-26 10:54:12 +0000678 if( SQLITE_TEMP_STORE==0
679 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
680 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
drh268283b2005-01-08 15:44:25 +0000681 ){
682 invalidateTempStorage(pParse);
683 }
drh17435752007-08-16 04:30:38 +0000684 sqlite3_free(sqlite3_temp_directory);
drh268283b2005-01-08 15:44:25 +0000685 if( zRight[0] ){
drh633e6d52008-07-28 19:34:53 +0000686 sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
tpoindex9a09a3c2004-12-20 19:01:32 +0000687 }else{
drh268283b2005-01-08 15:44:25 +0000688 sqlite3_temp_directory = 0;
tpoindex9a09a3c2004-12-20 19:01:32 +0000689 }
drh78f82d12008-09-02 00:52:52 +0000690#endif /* SQLITE_OMIT_WSD */
tpoindex9a09a3c2004-12-20 19:01:32 +0000691 }
692 }else
693
694 /*
drh90f5ecb2004-07-22 01:19:35 +0000695 ** PRAGMA [database.]synchronous
696 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
drhc11d4f92003-04-06 21:08:24 +0000697 **
698 ** Return or set the local value of the synchronous flag. Changing
699 ** the local value does not make changes to the disk file and the
700 ** default value will be restored the next time the database is
701 ** opened.
702 */
danielk19774adee202004-05-08 08:23:19 +0000703 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
danielk19778a414492004-06-29 08:59:35 +0000704 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk197791cf71b2004-06-26 06:37:06 +0000705 if( !zRight ){
drh5bb7ffe2004-09-02 15:14:00 +0000706 returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
drhc11d4f92003-04-06 21:08:24 +0000707 }else{
danielk197791cf71b2004-06-26 06:37:06 +0000708 if( !db->autoCommit ){
709 sqlite3ErrorMsg(pParse,
710 "Safety level may not be changed inside a transaction");
711 }else{
drh90f5ecb2004-07-22 01:19:35 +0000712 pDb->safety_level = getSafetyLevel(zRight)+1;
danielk197791cf71b2004-06-26 06:37:06 +0000713 }
drhc11d4f92003-04-06 21:08:24 +0000714 }
715 }else
drh13d70422004-11-13 15:59:14 +0000716#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000717
drhbf216272005-02-26 18:10:44 +0000718#ifndef SQLITE_OMIT_FLAG_PRAGMAS
drh87223182004-02-21 14:00:29 +0000719 if( flagPragma(pParse, zLeft, zRight) ){
drh90f5ecb2004-07-22 01:19:35 +0000720 /* The flagPragma() subroutine also generates any necessary code
721 ** there is nothing more to do here */
drhc11d4f92003-04-06 21:08:24 +0000722 }else
drhbf216272005-02-26 18:10:44 +0000723#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +0000724
drh13d70422004-11-13 15:59:14 +0000725#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
danielk197791cf71b2004-06-26 06:37:06 +0000726 /*
727 ** PRAGMA table_info(<table>)
728 **
729 ** Return a single row for each column of the named table. The columns of
730 ** the returned data set are:
731 **
732 ** cid: Column id (numbered from left to right, starting at 0)
733 ** name: Column name
734 ** type: Column declaration type.
735 ** notnull: True if 'NOT NULL' is part of column declaration
736 ** dflt_value: The default value for the column, if any.
737 */
drh5260f7e2004-06-26 19:35:29 +0000738 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000739 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000740 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000741 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000742 if( pTab ){
drhc11d4f92003-04-06 21:08:24 +0000743 int i;
danielk1977034ca142007-06-26 10:38:54 +0000744 int nHidden = 0;
drhf7eece62006-02-06 21:34:27 +0000745 Column *pCol;
drh9f6696a2006-02-09 16:52:23 +0000746 sqlite3VdbeSetNumCols(v, 6);
drh2d401ab2008-01-10 23:50:11 +0000747 pParse->nMem = 6;
drh66a51672008-01-03 00:01:23 +0000748 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P4_STATIC);
749 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
750 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P4_STATIC);
751 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P4_STATIC);
752 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P4_STATIC);
753 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P4_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000754 sqlite3ViewGetColumnNames(pParse, pTab);
drhf7eece62006-02-06 21:34:27 +0000755 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
drh417ec632006-08-14 14:23:41 +0000756 const Token *pDflt;
danielk1977034ca142007-06-26 10:38:54 +0000757 if( IsHiddenColumn(pCol) ){
758 nHidden++;
759 continue;
760 }
drh2d401ab2008-01-10 23:50:11 +0000761 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
762 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
763 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drhbfa8b102006-03-03 21:20:16 +0000764 pCol->zType ? pCol->zType : "", 0);
drh2d401ab2008-01-10 23:50:11 +0000765 sqlite3VdbeAddOp2(v, OP_Integer, pCol->notNull, 4);
drh736c7d42006-11-30 13:06:37 +0000766 if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
drh2d401ab2008-01-10 23:50:11 +0000767 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n);
drh6f835982006-09-25 13:48:30 +0000768 }else{
drh2d401ab2008-01-10 23:50:11 +0000769 sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
drh6f835982006-09-25 13:48:30 +0000770 }
drh2d401ab2008-01-10 23:50:11 +0000771 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
772 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
drhc11d4f92003-04-06 21:08:24 +0000773 }
774 }
775 }else
776
drh5260f7e2004-06-26 19:35:29 +0000777 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000778 Index *pIdx;
779 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000780 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000781 pIdx = sqlite3FindIndex(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000782 if( pIdx ){
drhc11d4f92003-04-06 21:08:24 +0000783 int i;
784 pTab = pIdx->pTable;
danielk197722322fd2004-05-25 23:35:17 +0000785 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000786 pParse->nMem = 3;
drh66a51672008-01-03 00:01:23 +0000787 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P4_STATIC);
788 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P4_STATIC);
789 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +0000790 for(i=0; i<pIdx->nColumn; i++){
791 int cnum = pIdx->aiColumn[i];
drh2d401ab2008-01-10 23:50:11 +0000792 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
793 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
drhc11d4f92003-04-06 21:08:24 +0000794 assert( pTab->nCol>cnum );
drh2d401ab2008-01-10 23:50:11 +0000795 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
796 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drhc11d4f92003-04-06 21:08:24 +0000797 }
798 }
799 }else
800
drh5260f7e2004-06-26 19:35:29 +0000801 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
drhc11d4f92003-04-06 21:08:24 +0000802 Index *pIdx;
803 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000804 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000805 pTab = sqlite3FindTable(db, zRight, zDb);
drhc11d4f92003-04-06 21:08:24 +0000806 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000807 v = sqlite3GetVdbe(pParse);
drhc11d4f92003-04-06 21:08:24 +0000808 pIdx = pTab->pIndex;
danielk1977742f9472004-06-16 12:02:43 +0000809 if( pIdx ){
810 int i = 0;
811 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000812 pParse->nMem = 3;
drh66a51672008-01-03 00:01:23 +0000813 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
814 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
815 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P4_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000816 while(pIdx){
drh2d401ab2008-01-10 23:50:11 +0000817 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
818 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
819 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
820 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
danielk1977742f9472004-06-16 12:02:43 +0000821 ++i;
822 pIdx = pIdx->pNext;
823 }
drhc11d4f92003-04-06 21:08:24 +0000824 }
825 }
826 }else
827
drh13d70422004-11-13 15:59:14 +0000828 if( sqlite3StrICmp(zLeft, "database_list")==0 ){
829 int i;
830 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
831 sqlite3VdbeSetNumCols(v, 3);
drh2d401ab2008-01-10 23:50:11 +0000832 pParse->nMem = 3;
drh66a51672008-01-03 00:01:23 +0000833 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
834 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
835 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P4_STATIC);
drh13d70422004-11-13 15:59:14 +0000836 for(i=0; i<db->nDb; i++){
837 if( db->aDb[i].pBt==0 ) continue;
838 assert( db->aDb[i].zName!=0 );
drh2d401ab2008-01-10 23:50:11 +0000839 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
840 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
841 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
drh13d70422004-11-13 15:59:14 +0000842 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
drh2d401ab2008-01-10 23:50:11 +0000843 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
drh13d70422004-11-13 15:59:14 +0000844 }
845 }else
danielk197748af65a2005-02-09 03:20:37 +0000846
847 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
848 int i = 0;
849 HashElem *p;
850 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +0000851 pParse->nMem = 2;
drh66a51672008-01-03 00:01:23 +0000852 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
853 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
danielk197748af65a2005-02-09 03:20:37 +0000854 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
855 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
drh2d401ab2008-01-10 23:50:11 +0000856 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
857 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
858 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
danielk197748af65a2005-02-09 03:20:37 +0000859 }
860 }else
drh13d70422004-11-13 15:59:14 +0000861#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
862
drhb7f91642004-10-31 02:22:47 +0000863#ifndef SQLITE_OMIT_FOREIGN_KEY
drh5260f7e2004-06-26 19:35:29 +0000864 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
drh78100cc2003-08-23 22:40:53 +0000865 FKey *pFK;
866 Table *pTab;
danielk19778a414492004-06-29 08:59:35 +0000867 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2783e4b2004-10-05 15:42:53 +0000868 pTab = sqlite3FindTable(db, zRight, zDb);
drh78100cc2003-08-23 22:40:53 +0000869 if( pTab ){
danielk19774adee202004-05-08 08:23:19 +0000870 v = sqlite3GetVdbe(pParse);
drh78100cc2003-08-23 22:40:53 +0000871 pFK = pTab->pFKey;
danielk1977742f9472004-06-16 12:02:43 +0000872 if( pFK ){
873 int i = 0;
874 sqlite3VdbeSetNumCols(v, 5);
drh2d401ab2008-01-10 23:50:11 +0000875 pParse->nMem = 5;
drh66a51672008-01-03 00:01:23 +0000876 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P4_STATIC);
877 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P4_STATIC);
878 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P4_STATIC);
879 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P4_STATIC);
880 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P4_STATIC);
danielk1977742f9472004-06-16 12:02:43 +0000881 while(pFK){
882 int j;
883 for(j=0; j<pFK->nCol; j++){
drh2f471492005-06-23 03:15:07 +0000884 char *zCol = pFK->aCol[j].zCol;
drh2d401ab2008-01-10 23:50:11 +0000885 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
886 sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
887 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
888 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
drh66a51672008-01-03 00:01:23 +0000889 pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
drh2d401ab2008-01-10 23:50:11 +0000890 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
891 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
danielk1977742f9472004-06-16 12:02:43 +0000892 }
893 ++i;
894 pFK = pFK->pNextFrom;
drh78100cc2003-08-23 22:40:53 +0000895 }
drh78100cc2003-08-23 22:40:53 +0000896 }
897 }
898 }else
drhb7f91642004-10-31 02:22:47 +0000899#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh78100cc2003-08-23 22:40:53 +0000900
drhc11d4f92003-04-06 21:08:24 +0000901#ifndef NDEBUG
danielk19774adee202004-05-08 08:23:19 +0000902 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
drh55ef4d92005-08-14 01:20:37 +0000903 if( zRight ){
904 if( getBoolean(zRight) ){
905 sqlite3ParserTrace(stderr, "parser: ");
906 }else{
907 sqlite3ParserTrace(0, 0);
908 }
drhc11d4f92003-04-06 21:08:24 +0000909 }
910 }else
911#endif
912
drh55ef4d92005-08-14 01:20:37 +0000913 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
914 ** used will be case sensitive or not depending on the RHS.
915 */
916 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
917 if( zRight ){
918 sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
919 }
920 }else
921
drh1dcdbc02007-01-27 02:24:54 +0000922#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
923# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
924#endif
925
drhb7f91642004-10-31 02:22:47 +0000926#ifndef SQLITE_OMIT_INTEGRITY_CHECK
danielk197741c58b72007-12-29 13:39:19 +0000927 /* Pragma "quick_check" is an experimental reduced version of
928 ** integrity_check designed to detect most database corruption
929 ** without most of the overhead of a full integrity-check.
930 */
931 if( sqlite3StrICmp(zLeft, "integrity_check")==0
932 || sqlite3StrICmp(zLeft, "quick_check")==0
933 ){
drh1dcdbc02007-01-27 02:24:54 +0000934 int i, j, addr, mxErr;
drhed717fe2003-06-15 23:42:24 +0000935
drhed717fe2003-06-15 23:42:24 +0000936 /* Code that appears at the end of the integrity check. If no error
937 ** messages have been generated, output OK. Otherwise output the
938 ** error message
939 */
drh57196282004-10-06 15:41:16 +0000940 static const VdbeOpList endCode[] = {
drh2d401ab2008-01-10 23:50:11 +0000941 { OP_AddImm, 1, 0, 0}, /* 0 */
942 { OP_IfNeg, 1, 0, 0}, /* 1 */
943 { OP_String8, 0, 3, 0}, /* 2 */
944 { OP_ResultRow, 3, 1, 0},
drhc11d4f92003-04-06 21:08:24 +0000945 };
drhed717fe2003-06-15 23:42:24 +0000946
danielk197741c58b72007-12-29 13:39:19 +0000947 int isQuick = (zLeft[0]=='q');
948
drhed717fe2003-06-15 23:42:24 +0000949 /* Initialize the VDBE program */
danielk19778a414492004-06-29 08:59:35 +0000950 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
drh2d401ab2008-01-10 23:50:11 +0000951 pParse->nMem = 6;
danielk197722322fd2004-05-25 23:35:17 +0000952 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +0000953 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P4_STATIC);
drh1dcdbc02007-01-27 02:24:54 +0000954
955 /* Set the maximum error count */
956 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
957 if( zRight ){
958 mxErr = atoi(zRight);
959 if( mxErr<=0 ){
960 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
961 }
962 }
drh2d401ab2008-01-10 23:50:11 +0000963 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
drhed717fe2003-06-15 23:42:24 +0000964
965 /* Do an integrity check on each database file */
966 for(i=0; i<db->nDb; i++){
967 HashElem *x;
danielk1977da184232006-01-05 11:34:32 +0000968 Hash *pTbls;
drh79069752004-05-22 21:30:40 +0000969 int cnt = 0;
drhed717fe2003-06-15 23:42:24 +0000970
danielk197753c0f742005-03-29 03:10:59 +0000971 if( OMIT_TEMPDB && i==1 ) continue;
972
drh80242052004-06-09 00:48:12 +0000973 sqlite3CodeVerifySchema(pParse, i);
drh2d401ab2008-01-10 23:50:11 +0000974 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
drh66a51672008-01-03 00:01:23 +0000975 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +0000976 sqlite3VdbeJumpHere(v, addr);
drh80242052004-06-09 00:48:12 +0000977
drhed717fe2003-06-15 23:42:24 +0000978 /* Do an integrity check of the B-Tree
drh2d401ab2008-01-10 23:50:11 +0000979 **
980 ** Begin by filling registers 2, 3, ... with the root pages numbers
981 ** for all tables and indices in the database.
drhed717fe2003-06-15 23:42:24 +0000982 */
danielk1977da184232006-01-05 11:34:32 +0000983 pTbls = &db->aDb[i].pSchema->tblHash;
984 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
drh79069752004-05-22 21:30:40 +0000985 Table *pTab = sqliteHashData(x);
986 Index *pIdx;
drh98757152008-01-09 23:04:12 +0000987 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +0000988 cnt++;
989 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh98757152008-01-09 23:04:12 +0000990 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
drh79069752004-05-22 21:30:40 +0000991 cnt++;
992 }
993 }
drh1dcdbc02007-01-27 02:24:54 +0000994 if( cnt==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +0000995
996 /* Make sure sufficient number of registers have been allocated */
danielk1977a7a8e142008-02-13 18:25:27 +0000997 if( pParse->nMem < cnt+4 ){
998 pParse->nMem = cnt+4;
drh98757152008-01-09 23:04:12 +0000999 }
drh2d401ab2008-01-10 23:50:11 +00001000
1001 /* Do the b-tree integrity checks */
drh98757152008-01-09 23:04:12 +00001002 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
1003 sqlite3VdbeChangeP5(v, i);
1004 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
1005 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
danielk19771e536952007-08-16 10:09:01 +00001006 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
drh66a51672008-01-03 00:01:23 +00001007 P4_DYNAMIC);
drhb21e7c72008-06-22 12:37:57 +00001008 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
danielk1977a7a8e142008-02-13 18:25:27 +00001009 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
drh98757152008-01-09 23:04:12 +00001010 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
drh1dcdbc02007-01-27 02:24:54 +00001011 sqlite3VdbeJumpHere(v, addr);
drhed717fe2003-06-15 23:42:24 +00001012
1013 /* Make sure all the indices are constructed correctly.
1014 */
danielk197741c58b72007-12-29 13:39:19 +00001015 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
drhed717fe2003-06-15 23:42:24 +00001016 Table *pTab = sqliteHashData(x);
1017 Index *pIdx;
1018 int loopTop;
1019
1020 if( pTab->pIndex==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001021 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
drh66a51672008-01-03 00:01:23 +00001022 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001023 sqlite3VdbeJumpHere(v, addr);
drh290c1942004-08-21 17:54:45 +00001024 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
drh2d401ab2008-01-10 23:50:11 +00001025 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */
drh66a51672008-01-03 00:01:23 +00001026 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
drh2d401ab2008-01-10 23:50:11 +00001027 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */
drhed717fe2003-06-15 23:42:24 +00001028 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
danielk197713adf8a2004-06-03 16:08:41 +00001029 int jmp2;
drh57196282004-10-06 15:41:16 +00001030 static const VdbeOpList idxErr[] = {
drh8558cde2008-01-05 05:20:10 +00001031 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001032 { OP_String8, 0, 3, 0}, /* 1 */
1033 { OP_Rowid, 1, 4, 0},
1034 { OP_String8, 0, 5, 0}, /* 3 */
1035 { OP_String8, 0, 6, 0}, /* 4 */
1036 { OP_Concat, 4, 3, 3},
1037 { OP_Concat, 5, 3, 3},
1038 { OP_Concat, 6, 3, 3},
1039 { OP_ResultRow, 3, 1, 0},
drhbb8a2792008-03-19 00:21:30 +00001040 { OP_IfPos, 1, 0, 0}, /* 9 */
1041 { OP_Halt, 0, 0, 0},
drhed717fe2003-06-15 23:42:24 +00001042 };
drhe14006d2008-03-25 17:23:32 +00001043 sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
drh2d401ab2008-01-10 23:50:11 +00001044 jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
danielk19774adee202004-05-08 08:23:19 +00001045 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
drh24003452008-01-03 01:28:59 +00001046 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
1047 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
drh66a51672008-01-03 00:01:23 +00001048 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
drhbb8a2792008-03-19 00:21:30 +00001049 sqlite3VdbeJumpHere(v, addr+9);
drhd654be82005-09-20 17:42:23 +00001050 sqlite3VdbeJumpHere(v, jmp2);
drhed717fe2003-06-15 23:42:24 +00001051 }
drh66a51672008-01-03 00:01:23 +00001052 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
drhd654be82005-09-20 17:42:23 +00001053 sqlite3VdbeJumpHere(v, loopTop);
drhed717fe2003-06-15 23:42:24 +00001054 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
drh57196282004-10-06 15:41:16 +00001055 static const VdbeOpList cntIdx[] = {
drh4c583122008-01-04 22:01:03 +00001056 { OP_Integer, 0, 3, 0},
drhd654be82005-09-20 17:42:23 +00001057 { OP_Rewind, 0, 0, 0}, /* 1 */
drh8558cde2008-01-05 05:20:10 +00001058 { OP_AddImm, 3, 1, 0},
drhd654be82005-09-20 17:42:23 +00001059 { OP_Next, 0, 0, 0}, /* 3 */
drh2d401ab2008-01-10 23:50:11 +00001060 { OP_Eq, 2, 0, 3}, /* 4 */
drh8558cde2008-01-05 05:20:10 +00001061 { OP_AddImm, 1, -1, 0},
drh2d401ab2008-01-10 23:50:11 +00001062 { OP_String8, 0, 2, 0}, /* 6 */
1063 { OP_String8, 0, 3, 0}, /* 7 */
1064 { OP_Concat, 3, 2, 2},
1065 { OP_ResultRow, 2, 1, 0},
drhed717fe2003-06-15 23:42:24 +00001066 };
1067 if( pIdx->tnum==0 ) continue;
drh3c84ddf2008-01-09 02:15:38 +00001068 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
drh66a51672008-01-03 00:01:23 +00001069 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
drh1dcdbc02007-01-27 02:24:54 +00001070 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001071 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
drhd654be82005-09-20 17:42:23 +00001072 sqlite3VdbeChangeP1(v, addr+1, j+2);
1073 sqlite3VdbeChangeP2(v, addr+1, addr+4);
1074 sqlite3VdbeChangeP1(v, addr+3, j+2);
1075 sqlite3VdbeChangeP2(v, addr+3, addr+2);
drh2d401ab2008-01-10 23:50:11 +00001076 sqlite3VdbeJumpHere(v, addr+4);
1077 sqlite3VdbeChangeP4(v, addr+6,
drh24003452008-01-03 01:28:59 +00001078 "wrong # of entries in index ", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001079 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
drhed717fe2003-06-15 23:42:24 +00001080 }
1081 }
1082 }
danielk19774adee202004-05-08 08:23:19 +00001083 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
drh2d401ab2008-01-10 23:50:11 +00001084 sqlite3VdbeChangeP2(v, addr, -mxErr);
1085 sqlite3VdbeJumpHere(v, addr+1);
1086 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
drhc11d4f92003-04-06 21:08:24 +00001087 }else
drhb7f91642004-10-31 02:22:47 +00001088#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1089
drh13d70422004-11-13 15:59:14 +00001090#ifndef SQLITE_OMIT_UTF16
danielk19778e227872004-06-07 07:52:17 +00001091 /*
1092 ** PRAGMA encoding
1093 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
1094 **
drh85b623f2007-12-13 21:54:09 +00001095 ** In its first form, this pragma returns the encoding of the main
danielk19778e227872004-06-07 07:52:17 +00001096 ** database. If the database is not initialized, it is initialized now.
1097 **
1098 ** The second form of this pragma is a no-op if the main database file
1099 ** has not already been initialized. In this case it sets the default
1100 ** encoding that will be used for the main database file if a new file
1101 ** is created. If an existing main database file is opened, then the
1102 ** default text encoding for the existing database is used.
1103 **
1104 ** In all cases new databases created using the ATTACH command are
1105 ** created to use the same default text encoding as the main database. If
1106 ** the main database has not been initialized and/or created when ATTACH
1107 ** is executed, this is done before the ATTACH operation.
1108 **
1109 ** In the second form this pragma sets the text encoding to be used in
1110 ** new database files created using this database handle. It is only
1111 ** useful if invoked immediately after the main database i
1112 */
1113 if( sqlite3StrICmp(zLeft, "encoding")==0 ){
drh0f7eb612006-08-08 13:51:43 +00001114 static const struct EncName {
danielk19778e227872004-06-07 07:52:17 +00001115 char *zName;
1116 u8 enc;
1117 } encnames[] = {
drh998da3a2004-06-19 15:22:56 +00001118 { "UTF-8", SQLITE_UTF8 },
1119 { "UTF8", SQLITE_UTF8 },
1120 { "UTF-16le", SQLITE_UTF16LE },
1121 { "UTF16le", SQLITE_UTF16LE },
1122 { "UTF-16be", SQLITE_UTF16BE },
1123 { "UTF16be", SQLITE_UTF16BE },
drh0f7eb612006-08-08 13:51:43 +00001124 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
1125 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
danielk19778e227872004-06-07 07:52:17 +00001126 { 0, 0 }
1127 };
drh0f7eb612006-08-08 13:51:43 +00001128 const struct EncName *pEnc;
danielk197791cf71b2004-06-26 06:37:06 +00001129 if( !zRight ){ /* "PRAGMA encoding" */
danielk19778a414492004-06-29 08:59:35 +00001130 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
danielk19778e227872004-06-07 07:52:17 +00001131 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +00001132 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P4_STATIC);
drh2d401ab2008-01-10 23:50:11 +00001133 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
danielk19778e227872004-06-07 07:52:17 +00001134 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
danielk197714db2662006-01-09 16:12:04 +00001135 if( pEnc->enc==ENC(pParse->db) ){
drh66a51672008-01-03 00:01:23 +00001136 sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC);
danielk19778e227872004-06-07 07:52:17 +00001137 break;
1138 }
1139 }
drh2d401ab2008-01-10 23:50:11 +00001140 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
danielk19778e227872004-06-07 07:52:17 +00001141 }else{ /* "PRAGMA encoding = XXX" */
1142 /* Only change the value of sqlite.enc if the database handle is not
1143 ** initialized. If the main database exists, the new sqlite.enc value
1144 ** will be overwritten when the schema is next loaded. If it does not
1145 ** already exists, it will be created to use the new encoding value.
1146 */
danielk1977b82e7ed2006-01-11 14:09:31 +00001147 if(
1148 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
1149 DbHasProperty(db, 0, DB_Empty)
1150 ){
danielk19778e227872004-06-07 07:52:17 +00001151 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
1152 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
drh0f7eb612006-08-08 13:51:43 +00001153 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
danielk19778e227872004-06-07 07:52:17 +00001154 break;
1155 }
1156 }
1157 if( !pEnc->zName ){
drh5260f7e2004-06-26 19:35:29 +00001158 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
danielk19778e227872004-06-07 07:52:17 +00001159 }
1160 }
1161 }
1162 }else
drh13d70422004-11-13 15:59:14 +00001163#endif /* SQLITE_OMIT_UTF16 */
1164
1165#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
danielk1977dae24952004-11-11 05:10:43 +00001166 /*
danielk1977b92b70b2004-11-12 16:11:59 +00001167 ** PRAGMA [database.]schema_version
1168 ** PRAGMA [database.]schema_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001169 **
danielk1977b92b70b2004-11-12 16:11:59 +00001170 ** PRAGMA [database.]user_version
1171 ** PRAGMA [database.]user_version = <integer>
danielk1977dae24952004-11-11 05:10:43 +00001172 **
danielk1977b92b70b2004-11-12 16:11:59 +00001173 ** The pragma's schema_version and user_version are used to set or get
1174 ** the value of the schema-version and user-version, respectively. Both
1175 ** the schema-version and the user-version are 32-bit signed integers
danielk1977dae24952004-11-11 05:10:43 +00001176 ** stored in the database header.
1177 **
1178 ** The schema-cookie is usually only manipulated internally by SQLite. It
1179 ** is incremented by SQLite whenever the database schema is modified (by
danielk1977b92b70b2004-11-12 16:11:59 +00001180 ** creating or dropping a table or index). The schema version is used by
danielk1977dae24952004-11-11 05:10:43 +00001181 ** SQLite each time a query is executed to ensure that the internal cache
1182 ** of the schema used when compiling the SQL query matches the schema of
1183 ** the database against which the compiled query is actually executed.
danielk1977b92b70b2004-11-12 16:11:59 +00001184 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
1185 ** the schema-version is potentially dangerous and may lead to program
danielk1977dae24952004-11-11 05:10:43 +00001186 ** crashes or database corruption. Use with caution!
1187 **
danielk1977b92b70b2004-11-12 16:11:59 +00001188 ** The user-version is not used internally by SQLite. It may be used by
danielk1977dae24952004-11-11 05:10:43 +00001189 ** applications for any purpose.
1190 */
danielk1977180b56a2007-06-24 08:00:42 +00001191 if( sqlite3StrICmp(zLeft, "schema_version")==0
1192 || sqlite3StrICmp(zLeft, "user_version")==0
1193 || sqlite3StrICmp(zLeft, "freelist_count")==0
1194 ){
danielk1977dae24952004-11-11 05:10:43 +00001195 int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
drhfb982642007-08-30 01:19:59 +00001196 sqlite3VdbeUsesBtree(v, iDb);
danielk1977180b56a2007-06-24 08:00:42 +00001197 switch( zLeft[0] ){
1198 case 's': case 'S':
1199 iCookie = 0;
1200 break;
1201 case 'f': case 'F':
1202 iCookie = 1;
1203 iDb = (-1*(iDb+1));
1204 assert(iDb<=0);
1205 break;
1206 default:
1207 iCookie = 5;
1208 break;
danielk1977dae24952004-11-11 05:10:43 +00001209 }
1210
danielk1977180b56a2007-06-24 08:00:42 +00001211 if( zRight && iDb>=0 ){
danielk1977dae24952004-11-11 05:10:43 +00001212 /* Write the specified cookie value */
1213 static const VdbeOpList setCookie[] = {
1214 { OP_Transaction, 0, 1, 0}, /* 0 */
drh9cbf3422008-01-17 16:22:13 +00001215 { OP_Integer, 0, 1, 0}, /* 1 */
1216 { OP_SetCookie, 0, 0, 1}, /* 2 */
danielk1977dae24952004-11-11 05:10:43 +00001217 };
1218 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
1219 sqlite3VdbeChangeP1(v, addr, iDb);
1220 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
1221 sqlite3VdbeChangeP1(v, addr+2, iDb);
1222 sqlite3VdbeChangeP2(v, addr+2, iCookie);
1223 }else{
1224 /* Read the specified cookie value */
1225 static const VdbeOpList readCookie[] = {
drh2d401ab2008-01-10 23:50:11 +00001226 { OP_ReadCookie, 0, 1, 0}, /* 0 */
1227 { OP_ResultRow, 1, 1, 0}
danielk1977dae24952004-11-11 05:10:43 +00001228 };
1229 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
1230 sqlite3VdbeChangeP1(v, addr, iDb);
drh4c583122008-01-04 22:01:03 +00001231 sqlite3VdbeChangeP3(v, addr, iCookie);
danielk1977dae24952004-11-11 05:10:43 +00001232 sqlite3VdbeSetNumCols(v, 1);
drh66a51672008-01-03 00:01:23 +00001233 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P4_TRANSIENT);
danielk1977dae24952004-11-11 05:10:43 +00001234 }
drh6e345992007-03-30 11:12:08 +00001235 }else
drh13d70422004-11-13 15:59:14 +00001236#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
drhc11d4f92003-04-06 21:08:24 +00001237
dougcurrie81c95ef2004-06-18 23:21:47 +00001238#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00001239 /*
1240 ** Report the current state of file logs for all databases
1241 */
1242 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
drh57196282004-10-06 15:41:16 +00001243 static const char *const azLockName[] = {
drh89ac8c12004-06-09 14:17:20 +00001244 "unlocked", "shared", "reserved", "pending", "exclusive"
1245 };
1246 int i;
1247 Vdbe *v = sqlite3GetVdbe(pParse);
1248 sqlite3VdbeSetNumCols(v, 2);
drh2d401ab2008-01-10 23:50:11 +00001249 pParse->nMem = 2;
drh66a51672008-01-03 00:01:23 +00001250 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P4_STATIC);
1251 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001252 for(i=0; i<db->nDb; i++){
1253 Btree *pBt;
1254 Pager *pPager;
drh9e33c2c2007-08-31 18:34:59 +00001255 const char *zState = "unknown";
1256 int j;
drh89ac8c12004-06-09 14:17:20 +00001257 if( db->aDb[i].zName==0 ) continue;
drh2d401ab2008-01-10 23:50:11 +00001258 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
drh89ac8c12004-06-09 14:17:20 +00001259 pBt = db->aDb[i].pBt;
1260 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
drh9e33c2c2007-08-31 18:34:59 +00001261 zState = "closed";
drhc4dd3fd2008-01-22 01:48:05 +00001262 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
drh9e33c2c2007-08-31 18:34:59 +00001263 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
1264 zState = azLockName[j];
drh89ac8c12004-06-09 14:17:20 +00001265 }
drh2d401ab2008-01-10 23:50:11 +00001266 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
1267 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
drh89ac8c12004-06-09 14:17:20 +00001268 }
danielk1977a4de4532008-09-02 15:44:08 +00001269
drh89ac8c12004-06-09 14:17:20 +00001270 }else
1271#endif
1272
drh89dec812005-04-28 19:03:37 +00001273#ifdef SQLITE_SSE
1274 /*
1275 ** Check to see if the sqlite_statements table exists. Create it
1276 ** if it does not.
1277 */
1278 if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
danielk19773a3f38e2005-05-22 06:49:56 +00001279 extern int sqlite3CreateStatementsTable(Parse*);
1280 sqlite3CreateStatementsTable(pParse);
drh89dec812005-04-28 19:03:37 +00001281 }else
1282#endif
1283
drh3c4f2a42005-12-08 18:12:56 +00001284#if SQLITE_HAS_CODEC
1285 if( sqlite3StrICmp(zLeft, "key")==0 ){
1286 sqlite3_key(db, zRight, strlen(zRight));
1287 }else
1288#endif
drh21e2cab2006-09-25 18:01:57 +00001289#if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
1290 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
1291#if SQLITE_HAS_CODEC
1292 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
1293 extern void sqlite3_activate_see(const char*);
1294 sqlite3_activate_see(&zRight[4]);
1295 }
1296#endif
1297#ifdef SQLITE_ENABLE_CEROD
1298 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
1299 extern void sqlite3_activate_cerod(const char*);
1300 sqlite3_activate_cerod(&zRight[6]);
1301 }
1302#endif
1303 }
1304#endif
drh3c4f2a42005-12-08 18:12:56 +00001305
drhc11d4f92003-04-06 21:08:24 +00001306 {}
danielk1977a21c6b62005-01-24 10:25:59 +00001307
1308 if( v ){
1309 /* Code an OP_Expire at the end of each PRAGMA program to cause
1310 ** the VDBE implementing the pragma to expire. Most (all?) pragmas
1311 ** are only valid for a single execution.
1312 */
drh66a51672008-01-03 00:01:23 +00001313 sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
drhac530b12006-02-11 01:25:50 +00001314
1315 /*
1316 ** Reset the safety level, in case the fullfsync flag or synchronous
1317 ** setting changed.
1318 */
danielk1977ddfb2f02006-02-17 12:25:14 +00001319#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +00001320 if( db->autoCommit ){
1321 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
1322 (db->flags&SQLITE_FullFSync)!=0);
1323 }
danielk1977ddfb2f02006-02-17 12:25:14 +00001324#endif
danielk1977a21c6b62005-01-24 10:25:59 +00001325 }
danielk1977e0048402004-06-15 16:51:01 +00001326pragma_out:
drh633e6d52008-07-28 19:34:53 +00001327 sqlite3DbFree(db, zLeft);
1328 sqlite3DbFree(db, zRight);
drhc11d4f92003-04-06 21:08:24 +00001329}
drh13d70422004-11-13 15:59:14 +00001330
drh0ccebe72005-06-07 22:22:50 +00001331#endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */