os/persistentdata/persistentstorage/sql/SQLite364/pragma.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     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 **
    14 ** $Id: pragma.c,v 1.189 2008/10/10 17:47:21 danielk1977 Exp $
    15 */
    16 #include "sqliteInt.h"
    17 #include <ctype.h>
    18 
    19 /* Ignore this whole file if pragmas are disabled
    20 */
    21 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
    22 
    23 /*
    24 ** Interpret the given string as a safety level.  Return 0 for OFF,
    25 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
    26 ** unrecognized string argument.
    27 **
    28 ** Note that the values returned are one less that the values that
    29 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
    30 ** 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 */
    33 static int getSafetyLevel(const char *z){
    34                              /* 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) ){
    41     return atoi(z);
    42   }
    43   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     }
    48   }
    49   return 1;
    50 }
    51 
    52 /*
    53 ** Interpret the given string as a boolean value.
    54 */
    55 static int getBoolean(const char *z){
    56   return getSafetyLevel(z)&1;
    57 }
    58 
    59 /*
    60 ** Interpret the given string as a locking mode value.
    61 */
    62 static 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 
    70 #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 */
    77 static 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 
    87 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
    88 /*
    89 ** 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 */
    93 static 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 }
   104 #endif /* SQLITE_PAGER_PRAGMAS */
   105 
   106 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   107 /*
   108 ** Invalidate temp storage, either when the temp storage is changed
   109 ** from default, or when 'file' and the temp_store_directory has changed
   110 */
   111 static int invalidateTempStorage(Parse *pParse){
   112   sqlite3 *db = pParse->db;
   113   if( db->aDb[1].pBt!=0 ){
   114     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
   115       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   }
   123   return SQLITE_OK;
   124 }
   125 #endif /* SQLITE_PAGER_PRAGMAS */
   126 
   127 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   128 /*
   129 ** If the TEMP database is open, close it and mark the database schema
   130 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
   131 ** or DEFAULT_TEMP_STORE pragmas.
   132 */
   133 static 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   }
   140   db->temp_store = ts;
   141   return SQLITE_OK;
   142 }
   143 #endif /* SQLITE_PAGER_PRAGMAS */
   144 
   145 /*
   146 ** Generate code to return a single integer value.
   147 */
   148 static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
   149   Vdbe *v = sqlite3GetVdbe(pParse);
   150   int mem = ++pParse->nMem;
   151   sqlite3VdbeAddOp2(v, OP_Integer, value, mem);
   152   if( pParse->explain==0 ){
   153     sqlite3VdbeSetNumCols(v, 1);
   154     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P4_STATIC);
   155   }
   156   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
   157 }
   158 
   159 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
   160 /*
   161 ** 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 */
   165 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
   166   static const struct sPragmaType {
   167     const char *zName;  /* Name of the pragma */
   168     int mask;           /* Mask for the db->flags value */
   169   } aPragma[] = {
   170     { "full_column_names",        SQLITE_FullColNames  },
   171     { "short_column_names",       SQLITE_ShortColNames },
   172     { "count_changes",            SQLITE_CountRows     },
   173     { "empty_result_callbacks",   SQLITE_NullCallback  },
   174     { "legacy_file_format",       SQLITE_LegacyFileFmt },
   175     { "fullfsync",                SQLITE_FullFSync     },
   176 #ifdef SQLITE_DEBUG
   177     { "sql_trace",                SQLITE_SqlTrace      },
   178     { "vdbe_listing",             SQLITE_VdbeListing   },
   179     { "vdbe_trace",               SQLITE_VdbeTrace     },
   180 #endif
   181 #ifndef SQLITE_OMIT_CHECK
   182     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
   183 #endif
   184     /* The following is VERY experimental */
   185     { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
   186     { "omit_readlock",            SQLITE_NoReadlock    },
   187 
   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 },
   191   };
   192   int i;
   193   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 ){
   196       sqlite3 *db = pParse->db;
   197       Vdbe *v;
   198       v = sqlite3GetVdbe(pParse);
   199       if( v ){
   200         if( zRight==0 ){
   201           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
   202         }else{
   203           if( getBoolean(zRight) ){
   204             db->flags |= p->mask;
   205           }else{
   206             db->flags &= ~p->mask;
   207           }
   208 
   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           */
   213           sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
   214         }
   215       }
   216 
   217       return 1;
   218     }
   219   }
   220   return 0;
   221 }
   222 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
   223 
   224 static const char *actionName(u8 action){
   225   switch( action ){
   226     case OE_SetNull:  return "SET NULL";
   227     case OE_SetDflt:  return "SET DEFAULT";
   228     case OE_Restrict: return "RESTRICT";
   229     case OE_Cascade:  return "CASCADE";
   230   }
   231   return "";
   232 }
   233 
   234 /*
   235 ** Process a pragma statement.  
   236 **
   237 ** Pragmas are of this form:
   238 **
   239 **      PRAGMA [database.]id [= value]
   240 **
   241 ** The identifier might also be a string.  The value is a string, and
   242 ** identifier, or a number.  If minusFlag is true, then the value is
   243 ** a number that was preceded by a minus sign.
   244 **
   245 ** If the left side is "database.id" then pId1 is the database name
   246 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
   247 ** id and pId2 is any empty string.
   248 */
   249 void sqlite3Pragma(
   250   Parse *pParse, 
   251   Token *pId1,        /* First part of [database.]id field */
   252   Token *pId2,        /* Second part of [database.]id field, or NULL */
   253   Token *pValue,      /* Token for <value>, or NULL */
   254   int minusFlag       /* True if a '-' sign preceded <value> */
   255 ){
   256   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
   257   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
   258   const char *zDb = 0;   /* The database name */
   259   Token *pId;            /* Pointer to <id> token */
   260   int iDb;               /* Database index for <database> */
   261   sqlite3 *db = pParse->db;
   262   Db *pDb;
   263   Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
   264   if( v==0 ) return;
   265   pParse->nMem = 2;
   266 
   267   /* Interpret the [database.] part of the pragma statement. iDb is the
   268   ** index of the database this pragma is being applied to in db.aDb[]. */
   269   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
   270   if( iDb<0 ) return;
   271   pDb = &db->aDb[iDb];
   272 
   273   /* If the temp database has been explicitly named as part of the 
   274   ** pragma, make sure it is open. 
   275   */
   276   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
   277     return;
   278   }
   279 
   280   zLeft = sqlite3NameFromToken(db, pId);
   281   if( !zLeft ) return;
   282   if( minusFlag ){
   283     zRight = sqlite3MPrintf(db, "-%T", pValue);
   284   }else{
   285     zRight = sqlite3NameFromToken(db, pValue);
   286   }
   287 
   288   zDb = ((pId2 && pId2->n>0)?pDb->zName:0);
   289   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
   290     goto pragma_out;
   291   }
   292  
   293 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   294   /*
   295   **  PRAGMA [database.]default_cache_size
   296   **  PRAGMA [database.]default_cache_size=N
   297   **
   298   ** The first form reports the current persistent setting for the
   299   ** page cache size.  The value returned is the maximum number of
   300   ** pages in the page cache.  The second form sets both the current
   301   ** page cache size value and the persistent page cache size value
   302   ** stored in the database file.
   303   **
   304   ** The default cache size is stored in meta-value 2 of page 1 of the
   305   ** database file.  The cache size is actually the absolute value of
   306   ** this memory location.  The sign of meta-value 2 determines the
   307   ** synchronous setting.  A negative value means synchronous is off
   308   ** and a positive value means synchronous is on.
   309   */
   310   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
   311     static const VdbeOpList getCacheSize[] = {
   312       { OP_ReadCookie,  0, 1,        2},  /* 0 */
   313       { OP_IfPos,       1, 6,        0},
   314       { OP_Integer,     0, 2,        0},
   315       { OP_Subtract,    1, 2,        1},
   316       { OP_IfPos,       1, 6,        0},
   317       { OP_Integer,     0, 1,        0},  /* 5 */
   318       { OP_ResultRow,   1, 1,        0},
   319     };
   320     int addr;
   321     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   322     sqlite3VdbeUsesBtree(v, iDb);
   323     if( !zRight ){
   324       sqlite3VdbeSetNumCols(v, 1);
   325       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P4_STATIC);
   326       pParse->nMem += 2;
   327       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
   328       sqlite3VdbeChangeP1(v, addr, iDb);
   329       sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
   330     }else{
   331       int size = atoi(zRight);
   332       if( size<0 ) size = -size;
   333       sqlite3BeginWriteOperation(pParse, 0, iDb);
   334       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
   335       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2);
   336       addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
   337       sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
   338       sqlite3VdbeJumpHere(v, addr);
   339       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1);
   340       pDb->pSchema->cache_size = size;
   341       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
   342     }
   343   }else
   344 
   345   /*
   346   **  PRAGMA [database.]page_size
   347   **  PRAGMA [database.]page_size=N
   348   **
   349   ** The first form reports the current setting for the
   350   ** database page size in bytes.  The second form sets the
   351   ** database page size value.  The value can only be set if
   352   ** the database has not yet been created.
   353   */
   354   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
   355     Btree *pBt = pDb->pBt;
   356     if( !zRight ){
   357       int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
   358       returnSingleInt(pParse, "page_size", size);
   359     }else{
   360       /* Malloc may fail when setting the page-size, as there is an internal
   361       ** buffer that the pager module resizes using sqlite3_realloc().
   362       */
   363       db->nextPagesize = atoi(zRight);
   364       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1) ){
   365         db->mallocFailed = 1;
   366       }
   367     }
   368   }else
   369 
   370   /*
   371   **  PRAGMA [database.]max_page_count
   372   **  PRAGMA [database.]max_page_count=N
   373   **
   374   ** The first form reports the current setting for the
   375   ** maximum number of pages in the database file.  The 
   376   ** second form attempts to change this setting.  Both
   377   ** forms return the current setting.
   378   */
   379   if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
   380     Btree *pBt = pDb->pBt;
   381     int newMax = 0;
   382     if( zRight ){
   383       newMax = atoi(zRight);
   384     }
   385     if( pBt ){
   386       newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
   387     }
   388     returnSingleInt(pParse, "max_page_count", newMax);
   389   }else
   390 
   391   /*
   392   **  PRAGMA [database.]page_count
   393   **
   394   ** Return the number of pages in the specified database.
   395   */
   396   if( sqlite3StrICmp(zLeft,"page_count")==0 ){
   397     Vdbe *v;
   398     int iReg;
   399     v = sqlite3GetVdbe(pParse);
   400     if( !v || sqlite3ReadSchema(pParse) ) goto pragma_out;
   401     sqlite3CodeVerifySchema(pParse, iDb);
   402     iReg = ++pParse->nMem;
   403     sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
   404     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
   405     sqlite3VdbeSetNumCols(v, 1);
   406     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", P4_STATIC);
   407   }else
   408 
   409   /*
   410   **  PRAGMA [database.]locking_mode
   411   **  PRAGMA [database.]locking_mode = (normal|exclusive)
   412   */
   413   if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
   414     const char *zRet = "normal";
   415     int eMode = getLockingMode(zRight);
   416 
   417     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
   418       /* Simple "PRAGMA locking_mode;" statement. This is a query for
   419       ** the current default locking mode (which may be different to
   420       ** the locking-mode of the main database).
   421       */
   422       eMode = db->dfltLockMode;
   423     }else{
   424       Pager *pPager;
   425       if( pId2->n==0 ){
   426         /* This indicates that no database name was specified as part
   427         ** of the PRAGMA command. In this case the locking-mode must be
   428         ** set on all attached databases, as well as the main db file.
   429         **
   430         ** Also, the sqlite3.dfltLockMode variable is set so that
   431         ** any subsequently attached databases also use the specified
   432         ** locking mode.
   433         */
   434         int ii;
   435         assert(pDb==&db->aDb[0]);
   436         for(ii=2; ii<db->nDb; ii++){
   437           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
   438           sqlite3PagerLockingMode(pPager, eMode);
   439         }
   440         db->dfltLockMode = eMode;
   441       }
   442       pPager = sqlite3BtreePager(pDb->pBt);
   443       eMode = sqlite3PagerLockingMode(pPager, eMode);
   444     }
   445 
   446     assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
   447     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
   448       zRet = "exclusive";
   449     }
   450     sqlite3VdbeSetNumCols(v, 1);
   451     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P4_STATIC);
   452     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
   453     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   454   }else
   455 
   456   /*
   457   **  PRAGMA [database.]journal_mode
   458   **  PRAGMA [database.]journal_mode = (delete|persist|off)
   459   */
   460   if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
   461     int eMode;
   462     static char * const azModeName[] = {"delete", "persist", "off", "truncate"};
   463 
   464     if( zRight==0 ){
   465       eMode = PAGER_JOURNALMODE_QUERY;
   466     }else{
   467       int n = strlen(zRight);
   468       eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
   469       while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
   470         eMode--;
   471       }
   472     }
   473     if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
   474       /* Simple "PRAGMA journal_mode;" statement. This is a query for
   475       ** the current default journal mode (which may be different to
   476       ** the journal-mode of the main database).
   477       */
   478       eMode = db->dfltJournalMode;
   479     }else{
   480       Pager *pPager;
   481       if( pId2->n==0 ){
   482         /* This indicates that no database name was specified as part
   483         ** of the PRAGMA command. In this case the journal-mode must be
   484         ** set on all attached databases, as well as the main db file.
   485         **
   486         ** Also, the sqlite3.dfltJournalMode variable is set so that
   487         ** any subsequently attached databases also use the specified
   488         ** journal mode.
   489         */
   490         int ii;
   491         assert(pDb==&db->aDb[0]);
   492         for(ii=1; ii<db->nDb; ii++){
   493           if( db->aDb[ii].pBt ){
   494             pPager = sqlite3BtreePager(db->aDb[ii].pBt);
   495             sqlite3PagerJournalMode(pPager, eMode);
   496           }
   497         }
   498         db->dfltJournalMode = eMode;
   499       }
   500       pPager = sqlite3BtreePager(pDb->pBt);
   501       eMode = sqlite3PagerJournalMode(pPager, eMode);
   502     }
   503     assert( eMode==PAGER_JOURNALMODE_DELETE
   504               || eMode==PAGER_JOURNALMODE_TRUNCATE
   505               || eMode==PAGER_JOURNALMODE_PERSIST
   506               || eMode==PAGER_JOURNALMODE_OFF );
   507     sqlite3VdbeSetNumCols(v, 1);
   508     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", P4_STATIC);
   509     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, 
   510            azModeName[eMode], P4_STATIC);
   511     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   512   }else
   513 
   514   /*
   515   **  PRAGMA [database.]journal_size_limit
   516   **  PRAGMA [database.]journal_size_limit=N
   517   **
   518   ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
   519   */
   520   if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
   521     Pager *pPager = sqlite3BtreePager(pDb->pBt);
   522     i64 iLimit = -2;
   523     if( zRight ){
   524       int iLimit32 = atoi(zRight);
   525       if( iLimit32<-1 ){
   526         iLimit32 = -1;
   527       }
   528       iLimit = iLimit32;
   529     }
   530     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
   531     returnSingleInt(pParse, "journal_size_limit", (int)iLimit);
   532   }else
   533 
   534 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
   535 
   536   /*
   537   **  PRAGMA [database.]auto_vacuum
   538   **  PRAGMA [database.]auto_vacuum=N
   539   **
   540   ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
   541   */
   542 #ifndef SQLITE_OMIT_AUTOVACUUM
   543   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
   544     Btree *pBt = pDb->pBt;
   545     if( sqlite3ReadSchema(pParse) ){
   546       goto pragma_out;
   547     }
   548     if( !zRight ){
   549       int auto_vacuum = 
   550           pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
   551       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
   552     }else{
   553       int eAuto = getAutoVacuum(zRight);
   554       db->nextAutovac = eAuto;
   555       if( eAuto>=0 ){
   556         /* Call SetAutoVacuum() to set initialize the internal auto and
   557         ** incr-vacuum flags. This is required in case this connection
   558         ** creates the database file. It is important that it is created
   559         ** as an auto-vacuum capable db.
   560         */
   561         int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
   562         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
   563           /* When setting the auto_vacuum mode to either "full" or 
   564           ** "incremental", write the value of meta[6] in the database
   565           ** file. Before writing to meta[6], check that meta[3] indicates
   566           ** that this really is an auto-vacuum capable database.
   567           */
   568           static const VdbeOpList setMeta6[] = {
   569             { OP_Transaction,    0,               1,        0},    /* 0 */
   570             { OP_ReadCookie,     0,               1,        3},    /* 1 */
   571             { OP_If,             1,               0,        0},    /* 2 */
   572             { OP_Halt,           SQLITE_OK,       OE_Abort, 0},    /* 3 */
   573             { OP_Integer,        0,               1,        0},    /* 4 */
   574             { OP_SetCookie,      0,               6,        1},    /* 5 */
   575           };
   576           int iAddr;
   577           iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
   578           sqlite3VdbeChangeP1(v, iAddr, iDb);
   579           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
   580           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
   581           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
   582           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
   583           sqlite3VdbeUsesBtree(v, iDb);
   584         }
   585       }
   586     }
   587   }else
   588 #endif
   589 
   590   /*
   591   **  PRAGMA [database.]incremental_vacuum(N)
   592   **
   593   ** Do N steps of incremental vacuuming on a database.
   594   */
   595 #ifndef SQLITE_OMIT_AUTOVACUUM
   596   if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
   597     int iLimit, addr;
   598     if( sqlite3ReadSchema(pParse) ){
   599       goto pragma_out;
   600     }
   601     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
   602       iLimit = 0x7fffffff;
   603     }
   604     sqlite3BeginWriteOperation(pParse, 0, iDb);
   605     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
   606     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
   607     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
   608     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
   609     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
   610     sqlite3VdbeJumpHere(v, addr);
   611   }else
   612 #endif
   613 
   614 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   615   /*
   616   **  PRAGMA [database.]cache_size
   617   **  PRAGMA [database.]cache_size=N
   618   **
   619   ** The first form reports the current local setting for the
   620   ** page cache size.  The local setting can be different from
   621   ** the persistent cache size value that is stored in the database
   622   ** file itself.  The value returned is the maximum number of
   623   ** pages in the page cache.  The second form sets the local
   624   ** page cache size value.  It does not change the persistent
   625   ** cache size stored on the disk so the cache size will revert
   626   ** to its default value when the database is closed and reopened.
   627   ** N should be a positive integer.
   628   */
   629   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
   630     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   631     if( !zRight ){
   632       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
   633     }else{
   634       int size = atoi(zRight);
   635       if( size<0 ) size = -size;
   636       pDb->pSchema->cache_size = size;
   637       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
   638     }
   639   }else
   640 
   641   /*
   642   **   PRAGMA temp_store
   643   **   PRAGMA temp_store = "default"|"memory"|"file"
   644   **
   645   ** Return or set the local value of the temp_store flag.  Changing
   646   ** the local value does not make changes to the disk file and the default
   647   ** value will be restored the next time the database is opened.
   648   **
   649   ** Note that it is possible for the library compile-time options to
   650   ** override this setting
   651   */
   652   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
   653     if( !zRight ){
   654       returnSingleInt(pParse, "temp_store", db->temp_store);
   655     }else{
   656       changeTempStorage(pParse, zRight);
   657     }
   658   }else
   659 
   660   /*
   661   **   PRAGMA temp_store_directory
   662   **   PRAGMA temp_store_directory = ""|"directory_name"
   663   **
   664   ** Return or set the local value of the temp_store_directory flag.  Changing
   665   ** the value sets a specific directory to be used for temporary files.
   666   ** Setting to a null string reverts to the default temporary directory search.
   667   ** If temporary directory is changed, then invalidateTempStorage.
   668   **
   669   */
   670   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
   671     if( !zRight ){
   672       if( sqlite3_temp_directory ){
   673         sqlite3VdbeSetNumCols(v, 1);
   674         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
   675             "temp_store_directory", P4_STATIC);
   676         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
   677         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   678       }
   679     }else{
   680 #ifndef SQLITE_OMIT_WSD
   681       if( zRight[0] ){
   682         int rc;
   683         int res;
   684         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
   685         if( rc!=SQLITE_OK || res==0 ){
   686           sqlite3ErrorMsg(pParse, "not a writable directory");
   687           goto pragma_out;
   688         }
   689       }
   690       if( SQLITE_TEMP_STORE==0
   691        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
   692        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
   693       ){
   694         invalidateTempStorage(pParse);
   695       }
   696       sqlite3_free(sqlite3_temp_directory);
   697       if( zRight[0] ){
   698         sqlite3_temp_directory = sqlite3DbStrDup(0, zRight);
   699       }else{
   700         sqlite3_temp_directory = 0;
   701       }
   702 #endif /* SQLITE_OMIT_WSD */
   703     }
   704   }else
   705 
   706   /*
   707   **   PRAGMA [database.]synchronous
   708   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
   709   **
   710   ** Return or set the local value of the synchronous flag.  Changing
   711   ** the local value does not make changes to the disk file and the
   712   ** default value will be restored the next time the database is
   713   ** opened.
   714   */
   715   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
   716     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   717     if( !zRight ){
   718       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
   719     }else{
   720       if( !db->autoCommit ){
   721         sqlite3ErrorMsg(pParse, 
   722             "Safety level may not be changed inside a transaction");
   723       }else{
   724         pDb->safety_level = getSafetyLevel(zRight)+1;
   725       }
   726     }
   727   }else
   728 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
   729 
   730 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
   731   if( flagPragma(pParse, zLeft, zRight) ){
   732     /* The flagPragma() subroutine also generates any necessary code
   733     ** there is nothing more to do here */
   734   }else
   735 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
   736 
   737 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
   738   /*
   739   **   PRAGMA table_info(<table>)
   740   **
   741   ** Return a single row for each column of the named table. The columns of
   742   ** the returned data set are:
   743   **
   744   ** cid:        Column id (numbered from left to right, starting at 0)
   745   ** name:       Column name
   746   ** type:       Column declaration type.
   747   ** notnull:    True if 'NOT NULL' is part of column declaration
   748   ** dflt_value: The default value for the column, if any.
   749   */
   750   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
   751     Table *pTab;
   752     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   753     pTab = sqlite3FindTable(db, zRight, zDb);
   754     if( pTab ){
   755       int i;
   756       int nHidden = 0;
   757       Column *pCol;
   758       sqlite3VdbeSetNumCols(v, 6);
   759       pParse->nMem = 6;
   760       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P4_STATIC);
   761       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   762       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P4_STATIC);
   763       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P4_STATIC);
   764       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P4_STATIC);
   765       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P4_STATIC);
   766       sqlite3ViewGetColumnNames(pParse, pTab);
   767       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
   768         const Token *pDflt;
   769         if( IsHiddenColumn(pCol) ){
   770           nHidden++;
   771           continue;
   772         }
   773         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
   774         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
   775         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   776            pCol->zType ? pCol->zType : "", 0);
   777         sqlite3VdbeAddOp2(v, OP_Integer, pCol->notNull, 4);
   778         if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
   779           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n);
   780         }else{
   781           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
   782         }
   783         sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
   784         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
   785       }
   786     }
   787   }else
   788 
   789   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
   790     Index *pIdx;
   791     Table *pTab;
   792     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   793     pIdx = sqlite3FindIndex(db, zRight, zDb);
   794     if( pIdx ){
   795       int i;
   796       pTab = pIdx->pTable;
   797       sqlite3VdbeSetNumCols(v, 3);
   798       pParse->nMem = 3;
   799       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P4_STATIC);
   800       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P4_STATIC);
   801       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P4_STATIC);
   802       for(i=0; i<pIdx->nColumn; i++){
   803         int cnum = pIdx->aiColumn[i];
   804         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   805         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
   806         assert( pTab->nCol>cnum );
   807         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
   808         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   809       }
   810     }
   811   }else
   812 
   813   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
   814     Index *pIdx;
   815     Table *pTab;
   816     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   817     pTab = sqlite3FindTable(db, zRight, zDb);
   818     if( pTab ){
   819       v = sqlite3GetVdbe(pParse);
   820       pIdx = pTab->pIndex;
   821       if( pIdx ){
   822         int i = 0; 
   823         sqlite3VdbeSetNumCols(v, 3);
   824         pParse->nMem = 3;
   825         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
   826         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   827         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P4_STATIC);
   828         while(pIdx){
   829           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   830           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
   831           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
   832           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   833           ++i;
   834           pIdx = pIdx->pNext;
   835         }
   836       }
   837     }
   838   }else
   839 
   840   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
   841     int i;
   842     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   843     sqlite3VdbeSetNumCols(v, 3);
   844     pParse->nMem = 3;
   845     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
   846     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   847     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P4_STATIC);
   848     for(i=0; i<db->nDb; i++){
   849       if( db->aDb[i].pBt==0 ) continue;
   850       assert( db->aDb[i].zName!=0 );
   851       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   852       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
   853       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   854            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
   855       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   856     }
   857   }else
   858 
   859   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
   860     int i = 0;
   861     HashElem *p;
   862     sqlite3VdbeSetNumCols(v, 2);
   863     pParse->nMem = 2;
   864     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC);
   865     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC);
   866     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
   867       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
   868       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
   869       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
   870       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
   871     }
   872   }else
   873 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
   874 
   875 #ifndef SQLITE_OMIT_FOREIGN_KEY
   876   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
   877     FKey *pFK;
   878     Table *pTab;
   879     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   880     pTab = sqlite3FindTable(db, zRight, zDb);
   881     if( pTab ){
   882       v = sqlite3GetVdbe(pParse);
   883       pFK = pTab->pFKey;
   884       if( pFK ){
   885         int i = 0; 
   886         sqlite3VdbeSetNumCols(v, 8);
   887         pParse->nMem = 8;
   888         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P4_STATIC);
   889         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P4_STATIC);
   890         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P4_STATIC);
   891         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P4_STATIC);
   892         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P4_STATIC);
   893         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", P4_STATIC);
   894         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", P4_STATIC);
   895         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", P4_STATIC);
   896         while(pFK){
   897           int j;
   898           for(j=0; j<pFK->nCol; j++){
   899             char *zCol = pFK->aCol[j].zCol;
   900             char *zOnUpdate = (char *)actionName(pFK->updateConf);
   901             char *zOnDelete = (char *)actionName(pFK->deleteConf);
   902             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   903             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
   904             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
   905             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
   906                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
   907             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
   908             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
   909             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
   910             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
   911             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
   912           }
   913           ++i;
   914           pFK = pFK->pNextFrom;
   915         }
   916       }
   917     }
   918   }else
   919 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
   920 
   921 #ifndef NDEBUG
   922   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
   923     if( zRight ){
   924       if( getBoolean(zRight) ){
   925         sqlite3ParserTrace(stderr, "parser: ");
   926       }else{
   927         sqlite3ParserTrace(0, 0);
   928       }
   929     }
   930   }else
   931 #endif
   932 
   933   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
   934   ** used will be case sensitive or not depending on the RHS.
   935   */
   936   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
   937     if( zRight ){
   938       sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
   939     }
   940   }else
   941 
   942 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
   943 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
   944 #endif
   945 
   946 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
   947   /* Pragma "quick_check" is an experimental reduced version of 
   948   ** integrity_check designed to detect most database corruption
   949   ** without most of the overhead of a full integrity-check.
   950   */
   951   if( sqlite3StrICmp(zLeft, "integrity_check")==0
   952    || sqlite3StrICmp(zLeft, "quick_check")==0 
   953   ){
   954     int i, j, addr, mxErr;
   955 
   956     /* Code that appears at the end of the integrity check.  If no error
   957     ** messages have been generated, output OK.  Otherwise output the
   958     ** error message
   959     */
   960     static const VdbeOpList endCode[] = {
   961       { OP_AddImm,      1, 0,        0},    /* 0 */
   962       { OP_IfNeg,       1, 0,        0},    /* 1 */
   963       { OP_String8,     0, 3,        0},    /* 2 */
   964       { OP_ResultRow,   3, 1,        0},
   965     };
   966 
   967     int isQuick = (zLeft[0]=='q');
   968 
   969     /* Initialize the VDBE program */
   970     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   971     pParse->nMem = 6;
   972     sqlite3VdbeSetNumCols(v, 1);
   973     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P4_STATIC);
   974 
   975     /* Set the maximum error count */
   976     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
   977     if( zRight ){
   978       mxErr = atoi(zRight);
   979       if( mxErr<=0 ){
   980         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
   981       }
   982     }
   983     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
   984 
   985     /* Do an integrity check on each database file */
   986     for(i=0; i<db->nDb; i++){
   987       HashElem *x;
   988       Hash *pTbls;
   989       int cnt = 0;
   990 
   991       if( OMIT_TEMPDB && i==1 ) continue;
   992 
   993       sqlite3CodeVerifySchema(pParse, i);
   994       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
   995       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
   996       sqlite3VdbeJumpHere(v, addr);
   997 
   998       /* Do an integrity check of the B-Tree
   999       **
  1000       ** Begin by filling registers 2, 3, ... with the root pages numbers
  1001       ** for all tables and indices in the database.
  1002       */
  1003       pTbls = &db->aDb[i].pSchema->tblHash;
  1004       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1005         Table *pTab = sqliteHashData(x);
  1006         Index *pIdx;
  1007         sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
  1008         cnt++;
  1009         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1010           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
  1011           cnt++;
  1012         }
  1013       }
  1014       if( cnt==0 ) continue;
  1015 
  1016       /* Make sure sufficient number of registers have been allocated */
  1017       if( pParse->nMem < cnt+4 ){
  1018         pParse->nMem = cnt+4;
  1019       }
  1020 
  1021       /* Do the b-tree integrity checks */
  1022       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
  1023       sqlite3VdbeChangeP5(v, i);
  1024       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
  1025       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1026          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
  1027          P4_DYNAMIC);
  1028       sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
  1029       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
  1030       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
  1031       sqlite3VdbeJumpHere(v, addr);
  1032 
  1033       /* Make sure all the indices are constructed correctly.
  1034       */
  1035       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
  1036         Table *pTab = sqliteHashData(x);
  1037         Index *pIdx;
  1038         int loopTop;
  1039 
  1040         if( pTab->pIndex==0 ) continue;
  1041         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
  1042         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1043         sqlite3VdbeJumpHere(v, addr);
  1044         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
  1045         sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
  1046         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
  1047         sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
  1048         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1049           int jmp2;
  1050           static const VdbeOpList idxErr[] = {
  1051             { OP_AddImm,      1, -1,  0},
  1052             { OP_String8,     0,  3,  0},    /* 1 */
  1053             { OP_Rowid,       1,  4,  0},
  1054             { OP_String8,     0,  5,  0},    /* 3 */
  1055             { OP_String8,     0,  6,  0},    /* 4 */
  1056             { OP_Concat,      4,  3,  3},
  1057             { OP_Concat,      5,  3,  3},
  1058             { OP_Concat,      6,  3,  3},
  1059             { OP_ResultRow,   3,  1,  0},
  1060             { OP_IfPos,       1,  0,  0},    /* 9 */
  1061             { OP_Halt,        0,  0,  0},
  1062           };
  1063           sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1);
  1064           jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3);
  1065           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
  1066           sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
  1067           sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
  1068           sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
  1069           sqlite3VdbeJumpHere(v, addr+9);
  1070           sqlite3VdbeJumpHere(v, jmp2);
  1071         }
  1072         sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
  1073         sqlite3VdbeJumpHere(v, loopTop);
  1074         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1075           static const VdbeOpList cntIdx[] = {
  1076              { OP_Integer,      0,  3,  0},
  1077              { OP_Rewind,       0,  0,  0},  /* 1 */
  1078              { OP_AddImm,       3,  1,  0},
  1079              { OP_Next,         0,  0,  0},  /* 3 */
  1080              { OP_Eq,           2,  0,  3},  /* 4 */
  1081              { OP_AddImm,       1, -1,  0},
  1082              { OP_String8,      0,  2,  0},  /* 6 */
  1083              { OP_String8,      0,  3,  0},  /* 7 */
  1084              { OP_Concat,       3,  2,  2},
  1085              { OP_ResultRow,    2,  1,  0},
  1086           };
  1087           if( pIdx->tnum==0 ) continue;
  1088           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
  1089           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1090           sqlite3VdbeJumpHere(v, addr);
  1091           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
  1092           sqlite3VdbeChangeP1(v, addr+1, j+2);
  1093           sqlite3VdbeChangeP2(v, addr+1, addr+4);
  1094           sqlite3VdbeChangeP1(v, addr+3, j+2);
  1095           sqlite3VdbeChangeP2(v, addr+3, addr+2);
  1096           sqlite3VdbeJumpHere(v, addr+4);
  1097           sqlite3VdbeChangeP4(v, addr+6, 
  1098                      "wrong # of entries in index ", P4_STATIC);
  1099           sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
  1100         }
  1101       } 
  1102     }
  1103     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
  1104     sqlite3VdbeChangeP2(v, addr, -mxErr);
  1105     sqlite3VdbeJumpHere(v, addr+1);
  1106     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
  1107   }else
  1108 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  1109 
  1110 #ifndef SQLITE_OMIT_UTF16
  1111   /*
  1112   **   PRAGMA encoding
  1113   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
  1114   **
  1115   ** In its first form, this pragma returns the encoding of the main
  1116   ** database. If the database is not initialized, it is initialized now.
  1117   **
  1118   ** The second form of this pragma is a no-op if the main database file
  1119   ** has not already been initialized. In this case it sets the default
  1120   ** encoding that will be used for the main database file if a new file
  1121   ** is created. If an existing main database file is opened, then the
  1122   ** default text encoding for the existing database is used.
  1123   ** 
  1124   ** In all cases new databases created using the ATTACH command are
  1125   ** created to use the same default text encoding as the main database. If
  1126   ** the main database has not been initialized and/or created when ATTACH
  1127   ** is executed, this is done before the ATTACH operation.
  1128   **
  1129   ** In the second form this pragma sets the text encoding to be used in
  1130   ** new database files created using this database handle. It is only
  1131   ** useful if invoked immediately after the main database i
  1132   */
  1133   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
  1134     static const struct EncName {
  1135       char *zName;
  1136       u8 enc;
  1137     } encnames[] = {
  1138       { "UTF-8",    SQLITE_UTF8        },
  1139       { "UTF8",     SQLITE_UTF8        },
  1140       { "UTF-16le", SQLITE_UTF16LE     },
  1141       { "UTF16le",  SQLITE_UTF16LE     },
  1142       { "UTF-16be", SQLITE_UTF16BE     },
  1143       { "UTF16be",  SQLITE_UTF16BE     },
  1144       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
  1145       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
  1146       { 0, 0 }
  1147     };
  1148     const struct EncName *pEnc;
  1149     if( !zRight ){    /* "PRAGMA encoding" */
  1150       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  1151       sqlite3VdbeSetNumCols(v, 1);
  1152       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P4_STATIC);
  1153       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
  1154       for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
  1155         if( pEnc->enc==ENC(pParse->db) ){
  1156           sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC);
  1157           break;
  1158         }
  1159       }
  1160       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  1161     }else{                        /* "PRAGMA encoding = XXX" */
  1162       /* Only change the value of sqlite.enc if the database handle is not
  1163       ** initialized. If the main database exists, the new sqlite.enc value
  1164       ** will be overwritten when the schema is next loaded. If it does not
  1165       ** already exists, it will be created to use the new encoding value.
  1166       */
  1167       if( 
  1168         !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
  1169         DbHasProperty(db, 0, DB_Empty) 
  1170       ){
  1171         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
  1172           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
  1173             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
  1174             break;
  1175           }
  1176         }
  1177         if( !pEnc->zName ){
  1178           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
  1179         }
  1180       }
  1181     }
  1182   }else
  1183 #endif /* SQLITE_OMIT_UTF16 */
  1184 
  1185 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
  1186   /*
  1187   **   PRAGMA [database.]schema_version
  1188   **   PRAGMA [database.]schema_version = <integer>
  1189   **
  1190   **   PRAGMA [database.]user_version
  1191   **   PRAGMA [database.]user_version = <integer>
  1192   **
  1193   ** The pragma's schema_version and user_version are used to set or get
  1194   ** the value of the schema-version and user-version, respectively. Both
  1195   ** the schema-version and the user-version are 32-bit signed integers
  1196   ** stored in the database header.
  1197   **
  1198   ** The schema-cookie is usually only manipulated internally by SQLite. It
  1199   ** is incremented by SQLite whenever the database schema is modified (by
  1200   ** creating or dropping a table or index). The schema version is used by
  1201   ** SQLite each time a query is executed to ensure that the internal cache
  1202   ** of the schema used when compiling the SQL query matches the schema of
  1203   ** the database against which the compiled query is actually executed.
  1204   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
  1205   ** the schema-version is potentially dangerous and may lead to program
  1206   ** crashes or database corruption. Use with caution!
  1207   **
  1208   ** The user-version is not used internally by SQLite. It may be used by
  1209   ** applications for any purpose.
  1210   */
  1211   if( sqlite3StrICmp(zLeft, "schema_version")==0 
  1212    || sqlite3StrICmp(zLeft, "user_version")==0 
  1213    || sqlite3StrICmp(zLeft, "freelist_count")==0 
  1214   ){
  1215     int iCookie;   /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
  1216     sqlite3VdbeUsesBtree(v, iDb);
  1217     switch( zLeft[0] ){
  1218       case 's': case 'S':
  1219         iCookie = 0;
  1220         break;
  1221       case 'f': case 'F':
  1222         iCookie = 1;
  1223         iDb = (-1*(iDb+1));
  1224         assert(iDb<=0);
  1225         break;
  1226       default:
  1227         iCookie = 5;
  1228         break;
  1229     }
  1230 
  1231     if( zRight && iDb>=0 ){
  1232       /* Write the specified cookie value */
  1233       static const VdbeOpList setCookie[] = {
  1234         { OP_Transaction,    0,  1,  0},    /* 0 */
  1235         { OP_Integer,        0,  1,  0},    /* 1 */
  1236         { OP_SetCookie,      0,  0,  1},    /* 2 */
  1237       };
  1238       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
  1239       sqlite3VdbeChangeP1(v, addr, iDb);
  1240       sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
  1241       sqlite3VdbeChangeP1(v, addr+2, iDb);
  1242       sqlite3VdbeChangeP2(v, addr+2, iCookie);
  1243     }else{
  1244       /* Read the specified cookie value */
  1245       static const VdbeOpList readCookie[] = {
  1246         { OP_ReadCookie,      0,  1,  0},    /* 0 */
  1247         { OP_ResultRow,       1,  1,  0}
  1248       };
  1249       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
  1250       sqlite3VdbeChangeP1(v, addr, iDb);
  1251       sqlite3VdbeChangeP3(v, addr, iCookie);
  1252       sqlite3VdbeSetNumCols(v, 1);
  1253       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P4_TRANSIENT);
  1254     }
  1255   }else
  1256 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
  1257 
  1258 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  1259   /*
  1260   ** Report the current state of file logs for all databases
  1261   */
  1262   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
  1263     static const char *const azLockName[] = {
  1264       "unlocked", "shared", "reserved", "pending", "exclusive"
  1265     };
  1266     int i;
  1267     Vdbe *v = sqlite3GetVdbe(pParse);
  1268     sqlite3VdbeSetNumCols(v, 2);
  1269     pParse->nMem = 2;
  1270     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P4_STATIC);
  1271     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P4_STATIC);
  1272     for(i=0; i<db->nDb; i++){
  1273       Btree *pBt;
  1274       Pager *pPager;
  1275       const char *zState = "unknown";
  1276       int j;
  1277       if( db->aDb[i].zName==0 ) continue;
  1278       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
  1279       pBt = db->aDb[i].pBt;
  1280       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
  1281         zState = "closed";
  1282       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
  1283                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
  1284          zState = azLockName[j];
  1285       }
  1286       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
  1287       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
  1288     }
  1289 
  1290   }else
  1291 #endif
  1292 
  1293 #ifdef SQLITE_SSE
  1294   /*
  1295   ** Check to see if the sqlite_statements table exists.  Create it
  1296   ** if it does not.
  1297   */
  1298   if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
  1299     extern int sqlite3CreateStatementsTable(Parse*);
  1300     sqlite3CreateStatementsTable(pParse);
  1301   }else
  1302 #endif
  1303 
  1304 #if SQLITE_HAS_CODEC
  1305   if( sqlite3StrICmp(zLeft, "key")==0 ){
  1306     sqlite3_key(db, zRight, strlen(zRight));
  1307   }else
  1308 #endif
  1309 #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
  1310   if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
  1311 #if SQLITE_HAS_CODEC
  1312     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
  1313       extern void sqlite3_activate_see(const char*);
  1314       sqlite3_activate_see(&zRight[4]);
  1315     }
  1316 #endif
  1317 #ifdef SQLITE_ENABLE_CEROD
  1318     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
  1319       extern void sqlite3_activate_cerod(const char*);
  1320       sqlite3_activate_cerod(&zRight[6]);
  1321     }
  1322 #endif
  1323   }
  1324 #endif
  1325 
  1326   {}
  1327 
  1328   if( v ){
  1329     /* Code an OP_Expire at the end of each PRAGMA program to cause
  1330     ** the VDBE implementing the pragma to expire. Most (all?) pragmas
  1331     ** are only valid for a single execution.
  1332     */
  1333     sqlite3VdbeAddOp2(v, OP_Expire, 1, 0);
  1334 
  1335     /*
  1336     ** Reset the safety level, in case the fullfsync flag or synchronous
  1337     ** setting changed.
  1338     */
  1339 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  1340     if( db->autoCommit ){
  1341       sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
  1342                  (db->flags&SQLITE_FullFSync)!=0);
  1343     }
  1344 #endif
  1345   }
  1346 pragma_out:
  1347   sqlite3DbFree(db, zLeft);
  1348   sqlite3DbFree(db, zRight);
  1349 }
  1350 
  1351 #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */