pragma.c

Go to the documentation of this file.
00001 /*
00002 ** 2003 April 6
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains code used to implement the PRAGMA command.
00013 **
00014 ** $Id: pragma.c,v 1.193 2008/11/10 19:24:38 shane Exp $
00015 */
00016 #include "sqliteInt.h"
00017 #include <ctype.h>
00018 
00019 /* Ignore this whole file if pragmas are disabled
00020 */
00021 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
00022 
00023 /*
00024 ** Interpret the given string as a safety level.  Return 0 for OFF,
00025 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
00026 ** unrecognized string argument.
00027 **
00028 ** Note that the values returned are one less that the values that
00029 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
00030 ** to support legacy SQL code.  The safety level used to be boolean
00031 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
00032 */
00033 static int getSafetyLevel(const char *z){
00034                              /* 123456789 123456789 */
00035   static const char zText[] = "onoffalseyestruefull";
00036   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
00037   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
00038   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
00039   int i, n;
00040   if( isdigit(*z) ){
00041     return atoi(z);
00042   }
00043   n = strlen(z);
00044   for(i=0; i<sizeof(iLength); i++){
00045     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
00046       return iValue[i];
00047     }
00048   }
00049   return 1;
00050 }
00051 
00052 /*
00053 ** Interpret the given string as a boolean value.
00054 */
00055 static int getBoolean(const char *z){
00056   return getSafetyLevel(z)&1;
00057 }
00058 
00059 /*
00060 ** Interpret the given string as a locking mode value.
00061 */
00062 static int getLockingMode(const char *z){
00063   if( z ){
00064     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
00065     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
00066   }
00067   return PAGER_LOCKINGMODE_QUERY;
00068 }
00069 
00070 #ifndef SQLITE_OMIT_AUTOVACUUM
00071 /*
00072 ** Interpret the given string as an auto-vacuum mode value.
00073 **
00074 ** The following strings, "none", "full" and "incremental" are 
00075 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
00076 */
00077 static int getAutoVacuum(const char *z){
00078   int i;
00079   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
00080   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
00081   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
00082   i = atoi(z);
00083   return ((i>=0&&i<=2)?i:0);
00084 }
00085 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
00086 
00087 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00088 /*
00089 ** Interpret the given string as a temp db location. Return 1 for file
00090 ** backed temporary databases, 2 for the Red-Black tree in memory database
00091 ** and 0 to use the compile-time default.
00092 */
00093 static int getTempStore(const char *z){
00094   if( z[0]>='0' && z[0]<='2' ){
00095     return z[0] - '0';
00096   }else if( sqlite3StrICmp(z, "file")==0 ){
00097     return 1;
00098   }else if( sqlite3StrICmp(z, "memory")==0 ){
00099     return 2;
00100   }else{
00101     return 0;
00102   }
00103 }
00104 #endif /* SQLITE_PAGER_PRAGMAS */
00105 
00106 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00107 /*
00108 ** Invalidate temp storage, either when the temp storage is changed
00109 ** from default, or when 'file' and the temp_store_directory has changed
00110 */
00111 static int invalidateTempStorage(Parse *pParse){
00112   sqlite3 *db = pParse->db;
00113   if( db->aDb[1].pBt!=0 ){
00114     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
00115       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
00116         "from within a transaction");
00117       return SQLITE_ERROR;
00118     }
00119     sqlite3BtreeClose(db->aDb[1].pBt);
00120     db->aDb[1].pBt = 0;
00121     sqlite3ResetInternalSchema(db, 0);
00122   }
00123   return SQLITE_OK;
00124 }
00125 #endif /* SQLITE_PAGER_PRAGMAS */
00126 
00127 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00128 /*
00129 ** If the TEMP database is open, close it and mark the database schema
00130 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
00131 ** or DEFAULT_TEMP_STORE pragmas.
00132 */
00133 static int changeTempStorage(Parse *pParse, const char *zStorageType){
00134   int ts = getTempStore(zStorageType);
00135   sqlite3 *db = pParse->db;
00136   if( db->temp_store==ts ) return SQLITE_OK;
00137   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
00138     return SQLITE_ERROR;
00139   }
00140   db->temp_store = ts;
00141   return SQLITE_OK;
00142 }
00143 #endif /* SQLITE_PAGER_PRAGMAS */
00144 
00145 /*
00146 ** Generate code to return a single integer value.
00147 */
00148 static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
00149   Vdbe *v = sqlite3GetVdbe(pParse);
00150   int mem = ++pParse->nMem;
00151   sqlite3VdbeAddOp2(v, OP_Integer, value, mem);
00152   if( pParse->explain==0 ){
00153     sqlite3VdbeSetNumCols(v, 1);
00154     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
00155   }
00156   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
00157 }
00158 
00159 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
00160 /*
00161 ** Check to see if zRight and zLeft refer to a pragma that queries
00162 ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
00163 ** Also, implement the pragma.
00164 */
00165 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
00166   static const struct sPragmaType {
00167     const char *zName;  /* Name of the pragma */
00168     int mask;           /* Mask for the db->flags value */
00169   } aPragma[] = {
00170     { "full_column_names",        SQLITE_FullColNames  },
00171     { "short_column_names",       SQLITE_ShortColNames },
00172     { "count_changes",            SQLITE_CountRows     },
00173     { "empty_result_callbacks",   SQLITE_NullCallback  },
00174     { "legacy_file_format",       SQLITE_LegacyFileFmt },
00175     { "fullfsync",                SQLITE_FullFSync     },
00176 #ifdef SQLITE_DEBUG
00177     { "sql_trace",                SQLITE_SqlTrace      },
00178     { "vdbe_listing",             SQLITE_VdbeListing   },
00179     { "vdbe_trace",               SQLITE_VdbeTrace     },
00180 #endif
00181 #ifndef SQLITE_OMIT_CHECK
00182     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
00183 #endif
00184     /* The following is VERY experimental */
00185     { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
00186     { "omit_readlock",            SQLITE_NoReadlock    },
00187 
00188     /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
00189     ** flag if there are any active statements. */
00190     { "read_uncommitted",         SQLITE_ReadUncommitted },
00191   };
00192   int i;
00193   const struct sPragmaType *p;
00194   for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
00195     if( sqlite3StrICmp(zLeft, p->zName)==0 ){
00196       sqlite3 *db = pParse->db;
00197       Vdbe *v;
00198       v = sqlite3GetVdbe(pParse);
00199       if( v ){
00200         if( zRight==0 ){
00201           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
00202         }else{
00203           if( getBoolean(zRight) ){
00204             db->flags |= p->mask;
00205           }else{
00206             db->flags &= ~p->mask;
00207           }
00208 
00209           /* Many of the flag-pragmas modify the code generated by the SQL 
00210           ** compiler (eg. count_changes). So add an opcode to expire all
00211           ** compiled SQL statements after modifying a pragma value.
00212           */
00213           sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
00214         }
00215       }
00216 
00217       return 1;
00218     }
00219   }
00220   return 0;
00221 }
00222 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
00223 
00224 static const char *actionName(u8 action){
00225   switch( action ){
00226     case OE_SetNull:  return "SET NULL";
00227     case OE_SetDflt:  return "SET DEFAULT";
00228     case OE_Restrict: return "RESTRICT";
00229     case OE_Cascade:  return "CASCADE";
00230   }
00231   return "";
00232 }
00233 
00234 /*
00235 ** Process a pragma statement.  
00236 **
00237 ** Pragmas are of this form:
00238 **
00239 **      PRAGMA [database.]id [= value]
00240 **
00241 ** The identifier might also be a string.  The value is a string, and
00242 ** identifier, or a number.  If minusFlag is true, then the value is
00243 ** a number that was preceded by a minus sign.
00244 **
00245 ** If the left side is "database.id" then pId1 is the database name
00246 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
00247 ** id and pId2 is any empty string.
00248 */
00249 void sqlite3Pragma(
00250   Parse *pParse, 
00251   Token *pId1,        /* First part of [database.]id field */
00252   Token *pId2,        /* Second part of [database.]id field, or NULL */
00253   Token *pValue,      /* Token for <value>, or NULL */
00254   int minusFlag       /* True if a '-' sign preceded <value> */
00255 ){
00256   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
00257   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
00258   const char *zDb = 0;   /* The database name */
00259   Token *pId;            /* Pointer to <id> token */
00260   int iDb;               /* Database index for <database> */
00261   sqlite3 *db = pParse->db;
00262   Db *pDb;
00263   Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
00264   if( v==0 ) return;
00265   pParse->nMem = 2;
00266 
00267   /* Interpret the [database.] part of the pragma statement. iDb is the
00268   ** index of the database this pragma is being applied to in db.aDb[]. */
00269   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
00270   if( iDb<0 ) return;
00271   pDb = &db->aDb[iDb];
00272 
00273   /* If the temp database has been explicitly named as part of the 
00274   ** pragma, make sure it is open. 
00275   */
00276   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
00277     return;
00278   }
00279 
00280   zLeft = sqlite3NameFromToken(db, pId);
00281   if( !zLeft ) return;
00282   if( minusFlag ){
00283     zRight = sqlite3MPrintf(db, "-%T", pValue);
00284   }else{
00285     zRight = sqlite3NameFromToken(db, pValue);
00286   }
00287 
00288   zDb = ((pId2 && pId2->n>0)?pDb->zName:0);
00289   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
00290     goto pragma_out;
00291   }
00292  
00293 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00294   /*
00295   **  PRAGMA [database.]default_cache_size
00296   **  PRAGMA [database.]default_cache_size=N
00297   **
00298   ** The first form reports the current persistent setting for the
00299   ** page cache size.  The value returned is the maximum number of
00300   ** pages in the page cache.  The second form sets both the current
00301   ** page cache size value and the persistent page cache size value
00302   ** stored in the database file.
00303   **
00304   ** The default cache size is stored in meta-value 2 of page 1 of the
00305   ** database file.  The cache size is actually the absolute value of
00306   ** this memory location.  The sign of meta-value 2 determines the
00307   ** synchronous setting.  A negative value means synchronous is off
00308   ** and a positive value means synchronous is on.
00309   */
00310   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
00311     static const VdbeOpList getCacheSize[] = {
00312       { OP_ReadCookie,  0, 1,        2},  /* 0 */
00313       { OP_IfPos,       1, 6,        0},
00314       { OP_Integer,     0, 2,        0},
00315       { OP_Subtract,    1, 2,        1},
00316       { OP_IfPos,       1, 6,        0},
00317       { OP_Integer,     0, 1,        0},  /* 5 */
00318       { OP_ResultRow,   1, 1,        0},
00319     };
00320     int addr;
00321     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00322     sqlite3VdbeUsesBtree(v, iDb);
00323     if( !zRight ){
00324       sqlite3VdbeSetNumCols(v, 1);
00325       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
00326       pParse->nMem += 2;
00327       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
00328       sqlite3VdbeChangeP1(v, addr, iDb);
00329       sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
00330     }else{
00331       int size = atoi(zRight);
00332       if( size<0 ) size = -size;
00333       sqlite3BeginWriteOperation(pParse, 0, iDb);
00334       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
00335       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2);
00336       addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
00337       sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
00338       sqlite3VdbeJumpHere(v, addr);
00339       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1);
00340       pDb->pSchema->cache_size = size;
00341       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
00342     }
00343   }else
00344 
00345   /*
00346   **  PRAGMA [database.]page_size
00347   **  PRAGMA [database.]page_size=N
00348   **
00349   ** The first form reports the current setting for the
00350   ** database page size in bytes.  The second form sets the
00351   ** database page size value.  The value can only be set if
00352   ** the database has not yet been created.
00353   */
00354   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
00355     Btree *pBt = pDb->pBt;
00356     if( !zRight ){
00357       int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
00358       returnSingleInt(pParse, "page_size", size);
00359     }else{
00360       /* Malloc may fail when setting the page-size, as there is an internal
00361       ** buffer that the pager module resizes using sqlite3_realloc().
00362       */
00363       db->nextPagesize = atoi(zRight);
00364       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1) ){
00365         db->mallocFailed = 1;
00366       }
00367     }
00368   }else
00369 
00370   /*
00371   **  PRAGMA [database.]max_page_count
00372   **  PRAGMA [database.]max_page_count=N
00373   **
00374   ** The first form reports the current setting for the
00375   ** maximum number of pages in the database file.  The 
00376   ** second form attempts to change this setting.  Both
00377   ** forms return the current setting.
00378   */
00379   if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
00380     Btree *pBt = pDb->pBt;
00381     int newMax = 0;
00382     if( zRight ){
00383       newMax = atoi(zRight);
00384     }
00385     if( pBt ){
00386       newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
00387     }
00388     returnSingleInt(pParse, "max_page_count", newMax);
00389   }else
00390 
00391   /*
00392   **  PRAGMA [database.]page_count
00393   **
00394   ** Return the number of pages in the specified database.
00395   */
00396   if( sqlite3StrICmp(zLeft,"page_count")==0 ){
00397     Vdbe *v;
00398     int iReg;
00399     v = sqlite3GetVdbe(pParse);
00400     if( !v || sqlite3ReadSchema(pParse) ) goto pragma_out;
00401     sqlite3CodeVerifySchema(pParse, iDb);
00402     iReg = ++pParse->nMem;
00403     sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
00404     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
00405     sqlite3VdbeSetNumCols(v, 1);
00406     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC);
00407   }else
00408 
00409   /*
00410   **  PRAGMA [database.]locking_mode
00411   **  PRAGMA [database.]locking_mode = (normal|exclusive)
00412   */
00413   if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
00414     const char *zRet = "normal";
00415     int eMode = getLockingMode(zRight);
00416 
00417     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
00418       /* Simple "PRAGMA locking_mode;" statement. This is a query for
00419       ** the current default locking mode (which may be different to
00420       ** the locking-mode of the main database).
00421       */
00422       eMode = db->dfltLockMode;
00423     }else{
00424       Pager *pPager;
00425       if( pId2->n==0 ){
00426         /* This indicates that no database name was specified as part
00427         ** of the PRAGMA command. In this case the locking-mode must be
00428         ** set on all attached databases, as well as the main db file.
00429         **
00430         ** Also, the sqlite3.dfltLockMode variable is set so that
00431         ** any subsequently attached databases also use the specified
00432         ** locking mode.
00433         */
00434         int ii;
00435         assert(pDb==&db->aDb[0]);
00436         for(ii=2; ii<db->nDb; ii++){
00437           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
00438           sqlite3PagerLockingMode(pPager, eMode);
00439         }
00440         db->dfltLockMode = eMode;
00441       }
00442       pPager = sqlite3BtreePager(pDb->pBt);
00443       eMode = sqlite3PagerLockingMode(pPager, eMode);
00444     }
00445 
00446     assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
00447     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
00448       zRet = "exclusive";
00449     }
00450     sqlite3VdbeSetNumCols(v, 1);
00451     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
00452     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
00453     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
00454   }else
00455 
00456   /*
00457   **  PRAGMA [database.]journal_mode
00458   **  PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
00459   */
00460   if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
00461     int eMode;
00462     static char * const azModeName[] = {
00463       "delete", "persist", "off", "truncate", "memory"
00464     };
00465 
00466     if( zRight==0 ){
00467       eMode = PAGER_JOURNALMODE_QUERY;
00468     }else{
00469       int n = strlen(zRight);
00470       eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
00471       while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
00472         eMode--;
00473       }
00474     }
00475     if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
00476       /* Simple "PRAGMA journal_mode;" statement. This is a query for
00477       ** the current default journal mode (which may be different to
00478       ** the journal-mode of the main database).
00479       */
00480       eMode = db->dfltJournalMode;
00481     }else{
00482       Pager *pPager;
00483       if( pId2->n==0 ){
00484         /* This indicates that no database name was specified as part
00485         ** of the PRAGMA command. In this case the journal-mode must be
00486         ** set on all attached databases, as well as the main db file.
00487         **
00488         ** Also, the sqlite3.dfltJournalMode variable is set so that
00489         ** any subsequently attached databases also use the specified
00490         ** journal mode.
00491         */
00492         int ii;
00493         assert(pDb==&db->aDb[0]);
00494         for(ii=1; ii<db->nDb; ii++){
00495           if( db->aDb[ii].pBt ){
00496             pPager = sqlite3BtreePager(db->aDb[ii].pBt);
00497             sqlite3PagerJournalMode(pPager, eMode);
00498           }
00499         }
00500         db->dfltJournalMode = eMode;
00501       }
00502       pPager = sqlite3BtreePager(pDb->pBt);
00503       eMode = sqlite3PagerJournalMode(pPager, eMode);
00504     }
00505     assert( eMode==PAGER_JOURNALMODE_DELETE
00506               || eMode==PAGER_JOURNALMODE_TRUNCATE
00507               || eMode==PAGER_JOURNALMODE_PERSIST
00508               || eMode==PAGER_JOURNALMODE_OFF
00509               || eMode==PAGER_JOURNALMODE_MEMORY );
00510     sqlite3VdbeSetNumCols(v, 1);
00511     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
00512     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, 
00513            azModeName[eMode], P4_STATIC);
00514     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
00515   }else
00516 
00517   /*
00518   **  PRAGMA [database.]journal_size_limit
00519   **  PRAGMA [database.]journal_size_limit=N
00520   **
00521   ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
00522   */
00523   if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
00524     Pager *pPager = sqlite3BtreePager(pDb->pBt);
00525     i64 iLimit = -2;
00526     if( zRight ){
00527       int iLimit32 = atoi(zRight);
00528       if( iLimit32<-1 ){
00529         iLimit32 = -1;
00530       }
00531       iLimit = iLimit32;
00532     }
00533     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
00534     returnSingleInt(pParse, "journal_size_limit", (int)iLimit);
00535   }else
00536 
00537 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
00538 
00539   /*
00540   **  PRAGMA [database.]auto_vacuum
00541   **  PRAGMA [database.]auto_vacuum=N
00542   **
00543   ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
00544   */
00545 #ifndef SQLITE_OMIT_AUTOVACUUM
00546   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
00547     Btree *pBt = pDb->pBt;
00548     if( sqlite3ReadSchema(pParse) ){
00549       goto pragma_out;
00550     }
00551     if( !zRight ){
00552       int auto_vacuum = 
00553           pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
00554       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
00555     }else{
00556       int eAuto = getAutoVacuum(zRight);
00557       db->nextAutovac = eAuto;
00558       if( eAuto>=0 ){
00559         /* Call SetAutoVacuum() to set initialize the internal auto and
00560         ** incr-vacuum flags. This is required in case this connection
00561         ** creates the database file. It is important that it is created
00562         ** as an auto-vacuum capable db.
00563         */
00564         int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
00565         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
00566           /* When setting the auto_vacuum mode to either "full" or 
00567           ** "incremental", write the value of meta[6] in the database
00568           ** file. Before writing to meta[6], check that meta[3] indicates
00569           ** that this really is an auto-vacuum capable database.
00570           */
00571           static const VdbeOpList setMeta6[] = {
00572             { OP_Transaction,    0,               1,        0},    /* 0 */
00573             { OP_ReadCookie,     0,               1,        3},    /* 1 */
00574             { OP_If,             1,               0,        0},    /* 2 */
00575             { OP_Halt,           SQLITE_OK,       OE_Abort, 0},    /* 3 */
00576             { OP_Integer,        0,               1,        0},    /* 4 */
00577             { OP_SetCookie,      0,               6,        1},    /* 5 */
00578           };
00579           int iAddr;
00580           iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
00581           sqlite3VdbeChangeP1(v, iAddr, iDb);
00582           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
00583           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
00584           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
00585           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
00586           sqlite3VdbeUsesBtree(v, iDb);
00587         }
00588       }
00589     }
00590   }else
00591 #endif
00592 
00593   /*
00594   **  PRAGMA [database.]incremental_vacuum(N)
00595   **
00596   ** Do N steps of incremental vacuuming on a database.
00597   */
00598 #ifndef SQLITE_OMIT_AUTOVACUUM
00599   if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
00600     int iLimit, addr;
00601     if( sqlite3ReadSchema(pParse) ){
00602       goto pragma_out;
00603     }
00604     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
00605       iLimit = 0x7fffffff;
00606     }
00607     sqlite3BeginWriteOperation(pParse, 0, iDb);
00608     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
00609     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
00610     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
00611     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
00612     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
00613     sqlite3VdbeJumpHere(v, addr);
00614   }else
00615 #endif
00616 
00617 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00618   /*
00619   **  PRAGMA [database.]cache_size
00620   **  PRAGMA [database.]cache_size=N
00621   **
00622   ** The first form reports the current local setting for the
00623   ** page cache size.  The local setting can be different from
00624   ** the persistent cache size value that is stored in the database
00625   ** file itself.  The value returned is the maximum number of
00626   ** pages in the page cache.  The second form sets the local
00627   ** page cache size value.  It does not change the persistent
00628   ** cache size stored on the disk so the cache size will revert
00629   ** to its default value when the database is closed and reopened.
00630   ** N should be a positive integer.
00631   */
00632   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
00633     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00634     if( !zRight ){
00635       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
00636     }else{
00637       int size = atoi(zRight);
00638       if( size<0 ) size = -size;
00639       pDb->pSchema->cache_size = size;
00640       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
00641     }
00642   }else
00643 
00644   /*
00645   **   PRAGMA temp_store
00646   **   PRAGMA temp_store = "default"|"memory"|"file"
00647   **
00648   ** Return or set the local value of the temp_store flag.  Changing
00649   ** the local value does not make changes to the disk file and the default
00650   ** value will be restored the next time the database is opened.
00651   **
00652   ** Note that it is possible for the library compile-time options to
00653   ** override this setting
00654   */
00655   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
00656     if( !zRight ){
00657       returnSingleInt(pParse, "temp_store", db->temp_store);
00658     }else{
00659       changeTempStorage(pParse, zRight);
00660     }
00661   }else
00662 
00663   /*
00664   **   PRAGMA temp_store_directory
00665   **   PRAGMA temp_store_directory = ""|"directory_name"
00666   **
00667   ** Return or set the local value of the temp_store_directory flag.  Changing
00668   ** the value sets a specific directory to be used for temporary files.
00669   ** Setting to a null string reverts to the default temporary directory search.
00670   ** If temporary directory is changed, then invalidateTempStorage.
00671   **
00672   */
00673   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
00674     if( !zRight ){
00675       if( sqlite3_temp_directory ){
00676         sqlite3VdbeSetNumCols(v, 1);
00677         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
00678             "temp_store_directory", SQLITE_STATIC);
00679         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
00680         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
00681       }
00682     }else{
00683 #ifndef SQLITE_OMIT_WSD
00684       if( zRight[0] ){
00685         int rc;
00686         int res;
00687         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
00688         if( rc!=SQLITE_OK || res==0 ){
00689           sqlite3ErrorMsg(pParse, "not a writable directory");
00690           goto pragma_out;
00691         }
00692       }
00693       if( SQLITE_TEMP_STORE==0
00694        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
00695        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
00696       ){
00697         invalidateTempStorage(pParse);
00698       }
00699       sqlite3_free(sqlite3_temp_directory);
00700       if( zRight[0] ){
00701         sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
00702       }else{
00703         sqlite3_temp_directory = 0;
00704       }
00705 #endif /* SQLITE_OMIT_WSD */
00706     }
00707   }else
00708 
00709   /*
00710   **   PRAGMA [database.]synchronous
00711   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
00712   **
00713   ** Return or set the local value of the synchronous flag.  Changing
00714   ** the local value does not make changes to the disk file and the
00715   ** default value will be restored the next time the database is
00716   ** opened.
00717   */
00718   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
00719     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00720     if( !zRight ){
00721       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
00722     }else{
00723       if( !db->autoCommit ){
00724         sqlite3ErrorMsg(pParse, 
00725             "Safety level may not be changed inside a transaction");
00726       }else{
00727         pDb->safety_level = getSafetyLevel(zRight)+1;
00728       }
00729     }
00730   }else
00731 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
00732 
00733 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
00734   if( flagPragma(pParse, zLeft, zRight) ){
00735     /* The flagPragma() subroutine also generates any necessary code
00736     ** there is nothing more to do here */
00737   }else
00738 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
00739 
00740 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
00741   /*
00742   **   PRAGMA table_info(<table>)
00743   **
00744   ** Return a single row for each column of the named table. The columns of
00745   ** the returned data set are:
00746   **
00747   ** cid:        Column id (numbered from left to right, starting at 0)
00748   ** name:       Column name
00749   ** type:       Column declaration type.
00750   ** notnull:    True if 'NOT NULL' is part of column declaration
00751   ** dflt_value: The default value for the column, if any.
00752   */
00753   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
00754     Table *pTab;
00755     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00756     pTab = sqlite3FindTable(db, zRight, zDb);
00757     if( pTab ){
00758       int i;
00759       int nHidden = 0;
00760       Column *pCol;
00761       sqlite3VdbeSetNumCols(v, 6);
00762       pParse->nMem = 6;
00763       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
00764       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
00765       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
00766       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
00767       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
00768       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
00769       sqlite3ViewGetColumnNames(pParse, pTab);
00770       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
00771         const Token *pDflt;
00772         if( IsHiddenColumn(pCol) ){
00773           nHidden++;
00774           continue;
00775         }
00776         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
00777         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
00778         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
00779            pCol->zType ? pCol->zType : "", 0);
00780         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
00781         if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
00782           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n);
00783         }else{
00784           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
00785         }
00786         sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
00787         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
00788       }
00789     }
00790   }else
00791 
00792   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
00793     Index *pIdx;
00794     Table *pTab;
00795     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00796     pIdx = sqlite3FindIndex(db, zRight, zDb);
00797     if( pIdx ){
00798       int i;
00799       pTab = pIdx->pTable;
00800       sqlite3VdbeSetNumCols(v, 3);
00801       pParse->nMem = 3;
00802       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
00803       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
00804       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
00805       for(i=0; i<pIdx->nColumn; i++){
00806         int cnum = pIdx->aiColumn[i];
00807         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
00808         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
00809         assert( pTab->nCol>cnum );
00810         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
00811         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
00812       }
00813     }
00814   }else
00815 
00816   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
00817     Index *pIdx;
00818     Table *pTab;
00819     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00820     pTab = sqlite3FindTable(db, zRight, zDb);
00821     if( pTab ){
00822       v = sqlite3GetVdbe(pParse);
00823       pIdx = pTab->pIndex;
00824       if( pIdx ){
00825         int i = 0; 
00826         sqlite3VdbeSetNumCols(v, 3);
00827         pParse->nMem = 3;
00828         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
00829         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
00830         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
00831         while(pIdx){
00832           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
00833           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
00834           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
00835           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
00836           ++i;
00837           pIdx = pIdx->pNext;
00838         }
00839       }
00840     }
00841   }else
00842 
00843   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
00844     int i;
00845     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00846     sqlite3VdbeSetNumCols(v, 3);
00847     pParse->nMem = 3;
00848     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
00849     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
00850     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
00851     for(i=0; i<db->nDb; i++){
00852       if( db->aDb[i].pBt==0 ) continue;
00853       assert( db->aDb[i].zName!=0 );
00854       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
00855       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
00856       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
00857            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
00858       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
00859     }
00860   }else
00861 
00862   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
00863     int i = 0;
00864     HashElem *p;
00865     sqlite3VdbeSetNumCols(v, 2);
00866     pParse->nMem = 2;
00867     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
00868     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
00869     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
00870       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
00871       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
00872       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
00873       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
00874     }
00875   }else
00876 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
00877 
00878 #ifndef SQLITE_OMIT_FOREIGN_KEY
00879   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
00880     FKey *pFK;
00881     Table *pTab;
00882     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00883     pTab = sqlite3FindTable(db, zRight, zDb);
00884     if( pTab ){
00885       v = sqlite3GetVdbe(pParse);
00886       pFK = pTab->pFKey;
00887       if( pFK ){
00888         int i = 0; 
00889         sqlite3VdbeSetNumCols(v, 8);
00890         pParse->nMem = 8;
00891         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
00892         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
00893         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
00894         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
00895         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
00896         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
00897         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
00898         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
00899         while(pFK){
00900           int j;
00901           for(j=0; j<pFK->nCol; j++){
00902             char *zCol = pFK->aCol[j].zCol;
00903             char *zOnUpdate = (char *)actionName(pFK->updateConf);
00904             char *zOnDelete = (char *)actionName(pFK->deleteConf);
00905             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
00906             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
00907             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
00908             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
00909                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
00910             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
00911             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
00912             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
00913             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
00914             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
00915           }
00916           ++i;
00917           pFK = pFK->pNextFrom;
00918         }
00919       }
00920     }
00921   }else
00922 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
00923 
00924 #ifndef NDEBUG
00925   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
00926     if( zRight ){
00927       if( getBoolean(zRight) ){
00928         sqlite3ParserTrace(stderr, "parser: ");
00929       }else{
00930         sqlite3ParserTrace(0, 0);
00931       }
00932     }
00933   }else
00934 #endif
00935 
00936   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
00937   ** used will be case sensitive or not depending on the RHS.
00938   */
00939   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
00940     if( zRight ){
00941       sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
00942     }
00943   }else
00944 
00945 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
00946 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
00947 #endif
00948 
00949 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
00950   /* Pragma "quick_check" is an experimental reduced version of 
00951   ** integrity_check designed to detect most database corruption
00952   ** without most of the overhead of a full integrity-check.
00953   */
00954   if( sqlite3StrICmp(zLeft, "integrity_check")==0
00955    || sqlite3StrICmp(zLeft, "quick_check")==0 
00956   ){
00957     int i, j, addr, mxErr;
00958 
00959     /* Code that appears at the end of the integrity check.  If no error
00960     ** messages have been generated, output OK.  Otherwise output the
00961     ** error message
00962     */
00963     static const VdbeOpList endCode[] = {
00964       { OP_AddImm,      1, 0,        0},    /* 0 */
00965       { OP_IfNeg,       1, 0,        0},    /* 1 */
00966       { OP_String8,     0, 3,        0},    /* 2 */
00967       { OP_ResultRow,   3, 1,        0},
00968     };
00969 
00970     int isQuick = (zLeft[0]=='q');
00971 
00972     /* Initialize the VDBE program */
00973     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00974     pParse->nMem = 6;
00975     sqlite3VdbeSetNumCols(v, 1);
00976     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
00977 
00978     /* Set the maximum error count */
00979     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
00980     if( zRight ){
00981       mxErr = atoi(zRight);
00982       if( mxErr<=0 ){
00983         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
00984       }
00985     }
00986     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
00987 
00988     /* Do an integrity check on each database file */
00989     for(i=0; i<db->nDb; i++){
00990       HashElem *x;
00991       Hash *pTbls;
00992       int cnt = 0;
00993 
00994       if( OMIT_TEMPDB && i==1 ) continue;
00995 
00996       sqlite3CodeVerifySchema(pParse, i);
00997       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
00998       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
00999       sqlite3VdbeJumpHere(v, addr);
01000 
01001       /* Do an integrity check of the B-Tree
01002       **
01003       ** Begin by filling registers 2, 3, ... with the root pages numbers
01004       ** for all tables and indices in the database.
01005       */
01006       pTbls = &db->aDb[i].pSchema->tblHash;
01007       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
01008         Table *pTab = sqliteHashData(x);
01009         Index *pIdx;
01010         sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
01011         cnt++;
01012         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
01013           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
01014           cnt++;
01015         }
01016       }
01017       if( cnt==0 ) continue;
01018 
01019       /* Make sure sufficient number of registers have been allocated */
01020       if( pParse->nMem < cnt+4 ){
01021         pParse->nMem = cnt+4;
01022       }
01023 
01024       /* Do the b-tree integrity checks */
01025       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
01026       sqlite3VdbeChangeP5(v, i);
01027       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
01028       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
01029          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
01030          P4_DYNAMIC);
01031       sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
01032       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
01033       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
01034       sqlite3VdbeJumpHere(v, addr);
01035 
01036       /* Make sure all the indices are constructed correctly.
01037       */
01038       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
01039         Table *pTab = sqliteHashData(x);
01040         Index *pIdx;
01041         int loopTop;
01042 
01043         if( pTab->pIndex==0 ) continue;
01044         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
01045         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
01046         sqlite3VdbeJumpHere(v, addr);
01047         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
01048         sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
01049         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
01050         sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
01051         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
01052           int jmp2;
01053           static const VdbeOpList idxErr[] = {
01054             { OP_AddImm,      1, -1,  0},
01055             { OP_String8,     0,  3,  0},    /* 1 */
01056             { OP_Rowid,       1,  4,  0},
01057             { OP_String8,     0,  5,  0},    /* 3 */
01058             { OP_String8,     0,  6,  0},    /* 4 */
01059             { OP_Concat,      4,  3,  3},
01060             { OP_Concat,      5,  3,  3},
01061             { OP_Concat,      6,  3,  3},
01062             { OP_ResultRow,   3,  1,  0},
01063             { OP_IfPos,       1,  0,  0},    /* 9 */
01064             { OP_Halt,        0,  0,  0},
01065           };
01066           sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
01067           jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
01068           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
01069           sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
01070           sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
01071           sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
01072           sqlite3VdbeJumpHere(v, addr+9);
01073           sqlite3VdbeJumpHere(v, jmp2);
01074         }
01075         sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
01076         sqlite3VdbeJumpHere(v, loopTop);
01077         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
01078           static const VdbeOpList cntIdx[] = {
01079              { OP_Integer,      0,  3,  0},
01080              { OP_Rewind,       0,  0,  0},  /* 1 */
01081              { OP_AddImm,       3,  1,  0},
01082              { OP_Next,         0,  0,  0},  /* 3 */
01083              { OP_Eq,           2,  0,  3},  /* 4 */
01084              { OP_AddImm,       1, -1,  0},
01085              { OP_String8,      0,  2,  0},  /* 6 */
01086              { OP_String8,      0,  3,  0},  /* 7 */
01087              { OP_Concat,       3,  2,  2},
01088              { OP_ResultRow,    2,  1,  0},
01089           };
01090           if( pIdx->tnum==0 ) continue;
01091           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
01092           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
01093           sqlite3VdbeJumpHere(v, addr);
01094           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
01095           sqlite3VdbeChangeP1(v, addr+1, j+2);
01096           sqlite3VdbeChangeP2(v, addr+1, addr+4);
01097           sqlite3VdbeChangeP1(v, addr+3, j+2);
01098           sqlite3VdbeChangeP2(v, addr+3, addr+2);
01099           sqlite3VdbeJumpHere(v, addr+4);
01100           sqlite3VdbeChangeP4(v, addr+6, 
01101                      "wrong # of entries in index ", P4_STATIC);
01102           sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
01103         }
01104       } 
01105     }
01106     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
01107     sqlite3VdbeChangeP2(v, addr, -mxErr);
01108     sqlite3VdbeJumpHere(v, addr+1);
01109     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
01110   }else
01111 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
01112 
01113 #ifndef SQLITE_OMIT_UTF16
01114   /*
01115   **   PRAGMA encoding
01116   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
01117   **
01118   ** In its first form, this pragma returns the encoding of the main
01119   ** database. If the database is not initialized, it is initialized now.
01120   **
01121   ** The second form of this pragma is a no-op if the main database file
01122   ** has not already been initialized. In this case it sets the default
01123   ** encoding that will be used for the main database file if a new file
01124   ** is created. If an existing main database file is opened, then the
01125   ** default text encoding for the existing database is used.
01126   ** 
01127   ** In all cases new databases created using the ATTACH command are
01128   ** created to use the same default text encoding as the main database. If
01129   ** the main database has not been initialized and/or created when ATTACH
01130   ** is executed, this is done before the ATTACH operation.
01131   **
01132   ** In the second form this pragma sets the text encoding to be used in
01133   ** new database files created using this database handle. It is only
01134   ** useful if invoked immediately after the main database i
01135   */
01136   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
01137     static const struct EncName {
01138       char *zName;
01139       u8 enc;
01140     } encnames[] = {
01141       { "UTF-8",    SQLITE_UTF8        },
01142       { "UTF8",     SQLITE_UTF8        },
01143       { "UTF-16le", SQLITE_UTF16LE     },
01144       { "UTF16le",  SQLITE_UTF16LE     },
01145       { "UTF-16be", SQLITE_UTF16BE     },
01146       { "UTF16be",  SQLITE_UTF16BE     },
01147       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
01148       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
01149       { 0, 0 }
01150     };
01151     const struct EncName *pEnc;
01152     if( !zRight ){    /* "PRAGMA encoding" */
01153       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
01154       sqlite3VdbeSetNumCols(v, 1);
01155       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
01156       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
01157       for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
01158         if( pEnc->enc==ENC(pParse->db) ){
01159           sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC);
01160           break;
01161         }
01162       }
01163       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
01164     }else{                        /* "PRAGMA encoding = XXX" */
01165       /* Only change the value of sqlite.enc if the database handle is not
01166       ** initialized. If the main database exists, the new sqlite.enc value
01167       ** will be overwritten when the schema is next loaded. If it does not
01168       ** already exists, it will be created to use the new encoding value.
01169       */
01170       if( 
01171         !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
01172         DbHasProperty(db, 0, DB_Empty) 
01173       ){
01174         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
01175           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
01176             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
01177             break;
01178           }
01179         }
01180         if( !pEnc->zName ){
01181           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
01182         }
01183       }
01184     }
01185   }else
01186 #endif /* SQLITE_OMIT_UTF16 */
01187 
01188 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
01189   /*
01190   **   PRAGMA [database.]schema_version
01191   **   PRAGMA [database.]schema_version = <integer>
01192   **
01193   **   PRAGMA [database.]user_version
01194   **   PRAGMA [database.]user_version = <integer>
01195   **
01196   ** The pragma's schema_version and user_version are used to set or get
01197   ** the value of the schema-version and user-version, respectively. Both
01198   ** the schema-version and the user-version are 32-bit signed integers
01199   ** stored in the database header.
01200   **
01201   ** The schema-cookie is usually only manipulated internally by SQLite. It
01202   ** is incremented by SQLite whenever the database schema is modified (by
01203   ** creating or dropping a table or index). The schema version is used by
01204   ** SQLite each time a query is executed to ensure that the internal cache
01205   ** of the schema used when compiling the SQL query matches the schema of
01206   ** the database against which the compiled query is actually executed.
01207   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
01208   ** the schema-version is potentially dangerous and may lead to program
01209   ** crashes or database corruption. Use with caution!
01210   **
01211   ** The user-version is not used internally by SQLite. It may be used by
01212   ** applications for any purpose.
01213   */
01214   if( sqlite3StrICmp(zLeft, "schema_version")==0 
01215    || sqlite3StrICmp(zLeft, "user_version")==0 
01216    || sqlite3StrICmp(zLeft, "freelist_count")==0 
01217   ){
01218     int iCookie;   /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
01219     sqlite3VdbeUsesBtree(v, iDb);
01220     switch( zLeft[0] ){
01221       case 's': case 'S':
01222         iCookie = 0;
01223         break;
01224       case 'f': case 'F':
01225         iCookie = 1;
01226         iDb = (-1*(iDb+1));
01227         assert(iDb<=0);
01228         break;
01229       default:
01230         iCookie = 5;
01231         break;
01232     }
01233 
01234     if( zRight && iDb>=0 ){
01235       /* Write the specified cookie value */
01236       static const VdbeOpList setCookie[] = {
01237         { OP_Transaction,    0,  1,  0},    /* 0 */
01238         { OP_Integer,        0,  1,  0},    /* 1 */
01239         { OP_SetCookie,      0,  0,  1},    /* 2 */
01240       };
01241       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
01242       sqlite3VdbeChangeP1(v, addr, iDb);
01243       sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
01244       sqlite3VdbeChangeP1(v, addr+2, iDb);
01245       sqlite3VdbeChangeP2(v, addr+2, iCookie);
01246     }else{
01247       /* Read the specified cookie value */
01248       static const VdbeOpList readCookie[] = {
01249         { OP_ReadCookie,      0,  1,  0},    /* 0 */
01250         { OP_ResultRow,       1,  1,  0}
01251       };
01252       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
01253       sqlite3VdbeChangeP1(v, addr, iDb);
01254       sqlite3VdbeChangeP3(v, addr, iCookie);
01255       sqlite3VdbeSetNumCols(v, 1);
01256       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
01257     }
01258   }else
01259 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
01260 
01261 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
01262   /*
01263   ** Report the current state of file logs for all databases
01264   */
01265   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
01266     static const char *const azLockName[] = {
01267       "unlocked", "shared", "reserved", "pending", "exclusive"
01268     };
01269     int i;
01270     Vdbe *v = sqlite3GetVdbe(pParse);
01271     sqlite3VdbeSetNumCols(v, 2);
01272     pParse->nMem = 2;
01273     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
01274     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
01275     for(i=0; i<db->nDb; i++){
01276       Btree *pBt;
01277       Pager *pPager;
01278       const char *zState = "unknown";
01279       int j;
01280       if( db->aDb[i].zName==0 ) continue;
01281       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
01282       pBt = db->aDb[i].pBt;
01283       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
01284         zState = "closed";
01285       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
01286                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
01287          zState = azLockName[j];
01288       }
01289       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
01290       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
01291     }
01292 
01293   }else
01294 #endif
01295 
01296 #ifdef SQLITE_SSE
01297   /*
01298   ** Check to see if the sqlite_statements table exists.  Create it
01299   ** if it does not.
01300   */
01301   if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
01302     extern int sqlite3CreateStatementsTable(Parse*);
01303     sqlite3CreateStatementsTable(pParse);
01304   }else
01305 #endif
01306 
01307 #if SQLITE_HAS_CODEC
01308   if( sqlite3StrICmp(zLeft, "key")==0 ){
01309     sqlite3_key(db, zRight, strlen(zRight));
01310   }else
01311 #endif
01312 #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
01313   if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
01314 #if SQLITE_HAS_CODEC
01315     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
01316       extern void sqlite3_activate_see(const char*);
01317       sqlite3_activate_see(&zRight[4]);
01318     }
01319 #endif
01320 #ifdef SQLITE_ENABLE_CEROD
01321     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
01322       extern void sqlite3_activate_cerod(const char*);
01323       sqlite3_activate_cerod(&zRight[6]);
01324     }
01325 #endif
01326   }
01327 #endif
01328 
01329   {}
01330 
01331   if( v ){
01332     /* Code an OP_Expire at the end of each PRAGMA program to cause
01333     ** the VDBE implementing the pragma to expire. Most (all?) pragmas
01334     ** are only valid for a single execution.
01335     */
01336     sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
01337 
01338     /*
01339     ** Reset the safety level, in case the fullfsync flag or synchronous
01340     ** setting changed.
01341     */
01342 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
01343     if( db->autoCommit ){
01344       sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
01345                  (db->flags&SQLITE_FullFSync)!=0);
01346     }
01347 #endif
01348   }
01349 pragma_out:
01350   sqlite3DbFree(db, zLeft);
01351   sqlite3DbFree(db, zRight);
01352 }
01353 
01354 #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1