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