Back to index

lightning-sunbird  0.9+nobinonly
pragma.c
Go to the documentation of this file.
00001 /*
00002 ** 2003 April 6
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains code used to implement the PRAGMA command.
00013 **
00014 ** $Id: pragma.c,v 1.120 2006/03/03 21:20:17 drh Exp $
00015 */
00016 #include "sqliteInt.h"
00017 #include "os.h"
00018 #include <ctype.h>
00019 
00020 /* Ignore this whole file if pragmas are disabled
00021 */
00022 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
00023 
00024 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
00025 # include "pager.h"
00026 # include "btree.h"
00027 #endif
00028 
00029 /*
00030 ** Interpret the given string as a safety level.  Return 0 for OFF,
00031 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
00032 ** unrecognized string argument.
00033 **
00034 ** Note that the values returned are one less that the values that
00035 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
00036 ** to support legacy SQL code.  The safety level used to be boolean
00037 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
00038 */
00039 static int getSafetyLevel(const char *z){
00040                              /* 123456789 123456789 */
00041   static const char zText[] = "onoffalseyestruefull";
00042   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
00043   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
00044   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
00045   int i, n;
00046   if( isdigit(*z) ){
00047     return atoi(z);
00048   }
00049   n = strlen(z);
00050   for(i=0; i<sizeof(iLength); i++){
00051     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
00052       return iValue[i];
00053     }
00054   }
00055   return 1;
00056 }
00057 
00058 /*
00059 ** Interpret the given string as a boolean value.
00060 */
00061 static int getBoolean(const char *z){
00062   return getSafetyLevel(z)&1;
00063 }
00064 
00065 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00066 /*
00067 ** Interpret the given string as a temp db location. Return 1 for file
00068 ** backed temporary databases, 2 for the Red-Black tree in memory database
00069 ** and 0 to use the compile-time default.
00070 */
00071 static int getTempStore(const char *z){
00072   if( z[0]>='0' && z[0]<='2' ){
00073     return z[0] - '0';
00074   }else if( sqlite3StrICmp(z, "file")==0 ){
00075     return 1;
00076   }else if( sqlite3StrICmp(z, "memory")==0 ){
00077     return 2;
00078   }else{
00079     return 0;
00080   }
00081 }
00082 #endif /* SQLITE_PAGER_PRAGMAS */
00083 
00084 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00085 /*
00086 ** Invalidate temp storage, either when the temp storage is changed
00087 ** from default, or when 'file' and the temp_store_directory has changed
00088 */
00089 static int invalidateTempStorage(Parse *pParse){
00090   sqlite3 *db = pParse->db;
00091   if( db->aDb[1].pBt!=0 ){
00092     if( db->flags & SQLITE_InTrans ){
00093       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
00094         "from within a transaction");
00095       return SQLITE_ERROR;
00096     }
00097     sqlite3BtreeClose(db->aDb[1].pBt);
00098     db->aDb[1].pBt = 0;
00099     sqlite3ResetInternalSchema(db, 0);
00100   }
00101   return SQLITE_OK;
00102 }
00103 #endif /* SQLITE_PAGER_PRAGMAS */
00104 
00105 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00106 /*
00107 ** If the TEMP database is open, close it and mark the database schema
00108 ** as needing reloading.  This must be done when using the TEMP_STORE
00109 ** or DEFAULT_TEMP_STORE pragmas.
00110 */
00111 static int changeTempStorage(Parse *pParse, const char *zStorageType){
00112   int ts = getTempStore(zStorageType);
00113   sqlite3 *db = pParse->db;
00114   if( db->temp_store==ts ) return SQLITE_OK;
00115   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
00116     return SQLITE_ERROR;
00117   }
00118   db->temp_store = ts;
00119   return SQLITE_OK;
00120 }
00121 #endif /* SQLITE_PAGER_PRAGMAS */
00122 
00123 /*
00124 ** Generate code to return a single integer value.
00125 */
00126 static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
00127   Vdbe *v = sqlite3GetVdbe(pParse);
00128   sqlite3VdbeAddOp(v, OP_Integer, value, 0);
00129   if( pParse->explain==0 ){
00130     sqlite3VdbeSetNumCols(v, 1);
00131     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P3_STATIC);
00132   }
00133   sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
00134 }
00135 
00136 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
00137 /*
00138 ** Check to see if zRight and zLeft refer to a pragma that queries
00139 ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
00140 ** Also, implement the pragma.
00141 */
00142 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
00143   static const struct sPragmaType {
00144     const char *zName;  /* Name of the pragma */
00145     int mask;           /* Mask for the db->flags value */
00146   } aPragma[] = {
00147     { "vdbe_trace",               SQLITE_VdbeTrace     },
00148     { "sql_trace",                SQLITE_SqlTrace      },
00149     { "vdbe_listing",             SQLITE_VdbeListing   },
00150     { "full_column_names",        SQLITE_FullColNames  },
00151     { "short_column_names",       SQLITE_ShortColNames },
00152     { "count_changes",            SQLITE_CountRows     },
00153     { "empty_result_callbacks",   SQLITE_NullCallback  },
00154     { "legacy_file_format",       SQLITE_LegacyFileFmt },
00155     { "fullfsync",                SQLITE_FullFSync     },
00156 #ifndef SQLITE_OMIT_CHECK
00157     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
00158 #endif
00159     /* The following is VERY experimental */
00160     { "writable_schema",          SQLITE_WriteSchema   },
00161     { "omit_readlock",            SQLITE_NoReadlock    },
00162 
00163     /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
00164     ** flag if there are any active statements. */
00165     { "read_uncommitted",         SQLITE_ReadUncommitted },
00166   };
00167   int i;
00168   const struct sPragmaType *p;
00169   for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
00170     if( sqlite3StrICmp(zLeft, p->zName)==0 ){
00171       sqlite3 *db = pParse->db;
00172       Vdbe *v;
00173       v = sqlite3GetVdbe(pParse);
00174       if( v ){
00175         if( zRight==0 ){
00176           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
00177         }else{
00178           if( getBoolean(zRight) ){
00179             db->flags |= p->mask;
00180           }else{
00181             db->flags &= ~p->mask;
00182           }
00183         }
00184       }
00185       return 1;
00186     }
00187   }
00188   return 0;
00189 }
00190 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
00191 
00192 /*
00193 ** Process a pragma statement.  
00194 **
00195 ** Pragmas are of this form:
00196 **
00197 **      PRAGMA [database.]id [= value]
00198 **
00199 ** The identifier might also be a string.  The value is a string, and
00200 ** identifier, or a number.  If minusFlag is true, then the value is
00201 ** a number that was preceded by a minus sign.
00202 **
00203 ** If the left side is "database.id" then pId1 is the database name
00204 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
00205 ** id and pId2 is any empty string.
00206 */
00207 void sqlite3Pragma(
00208   Parse *pParse, 
00209   Token *pId1,        /* First part of [database.]id field */
00210   Token *pId2,        /* Second part of [database.]id field, or NULL */
00211   Token *pValue,      /* Token for <value>, or NULL */
00212   int minusFlag       /* True if a '-' sign preceded <value> */
00213 ){
00214   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
00215   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
00216   const char *zDb = 0;   /* The database name */
00217   Token *pId;            /* Pointer to <id> token */
00218   int iDb;               /* Database index for <database> */
00219   sqlite3 *db = pParse->db;
00220   Db *pDb;
00221   Vdbe *v = sqlite3GetVdbe(pParse);
00222   if( v==0 ) return;
00223 
00224   /* Interpret the [database.] part of the pragma statement. iDb is the
00225   ** index of the database this pragma is being applied to in db.aDb[]. */
00226   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
00227   if( iDb<0 ) return;
00228   pDb = &db->aDb[iDb];
00229 
00230   /* If the temp database has been explicitly named as part of the 
00231   ** pragma, make sure it is open. 
00232   */
00233   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
00234     return;
00235   }
00236 
00237   zLeft = sqlite3NameFromToken(pId);
00238   if( !zLeft ) return;
00239   if( minusFlag ){
00240     zRight = sqlite3MPrintf("-%T", pValue);
00241   }else{
00242     zRight = sqlite3NameFromToken(pValue);
00243   }
00244 
00245   zDb = ((iDb>0)?pDb->zName:0);
00246   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
00247     goto pragma_out;
00248   }
00249  
00250 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00251   /*
00252   **  PRAGMA [database.]default_cache_size
00253   **  PRAGMA [database.]default_cache_size=N
00254   **
00255   ** The first form reports the current persistent setting for the
00256   ** page cache size.  The value returned is the maximum number of
00257   ** pages in the page cache.  The second form sets both the current
00258   ** page cache size value and the persistent page cache size value
00259   ** stored in the database file.
00260   **
00261   ** The default cache size is stored in meta-value 2 of page 1 of the
00262   ** database file.  The cache size is actually the absolute value of
00263   ** this memory location.  The sign of meta-value 2 determines the
00264   ** synchronous setting.  A negative value means synchronous is off
00265   ** and a positive value means synchronous is on.
00266   */
00267   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
00268     static const VdbeOpList getCacheSize[] = {
00269       { OP_ReadCookie,  0, 2,        0},  /* 0 */
00270       { OP_AbsValue,    0, 0,        0},
00271       { OP_Dup,         0, 0,        0},
00272       { OP_Integer,     0, 0,        0},
00273       { OP_Ne,          0, 6,        0},
00274       { OP_Integer,     0, 0,        0},  /* 5 */
00275       { OP_Callback,    1, 0,        0},
00276     };
00277     int addr;
00278     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00279     if( !zRight ){
00280       sqlite3VdbeSetNumCols(v, 1);
00281       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC);
00282       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
00283       sqlite3VdbeChangeP1(v, addr, iDb);
00284       sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES);
00285     }else{
00286       int size = atoi(zRight);
00287       if( size<0 ) size = -size;
00288       sqlite3BeginWriteOperation(pParse, 0, iDb);
00289       sqlite3VdbeAddOp(v, OP_Integer, size, 0);
00290       sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2);
00291       addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
00292       sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3);
00293       sqlite3VdbeAddOp(v, OP_Negative, 0, 0);
00294       sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2);
00295       pDb->pSchema->cache_size = size;
00296       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
00297     }
00298   }else
00299 
00300   /*
00301   **  PRAGMA [database.]page_size
00302   **  PRAGMA [database.]page_size=N
00303   **
00304   ** The first form reports the current setting for the
00305   ** database page size in bytes.  The second form sets the
00306   ** database page size value.  The value can only be set if
00307   ** the database has not yet been created.
00308   */
00309   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
00310     Btree *pBt = pDb->pBt;
00311     if( !zRight ){
00312       int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
00313       returnSingleInt(pParse, "page_size", size);
00314     }else{
00315       sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1);
00316     }
00317   }else
00318 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
00319 
00320   /*
00321   **  PRAGMA [database.]auto_vacuum
00322   **  PRAGMA [database.]auto_vacuum=N
00323   **
00324   ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
00325   */
00326 #ifndef SQLITE_OMIT_AUTOVACUUM
00327   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
00328     Btree *pBt = pDb->pBt;
00329     if( !zRight ){
00330       int auto_vacuum = 
00331           pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
00332       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
00333     }else{
00334       sqlite3BtreeSetAutoVacuum(pBt, getBoolean(zRight));
00335     }
00336   }else
00337 #endif
00338 
00339 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00340   /*
00341   **  PRAGMA [database.]cache_size
00342   **  PRAGMA [database.]cache_size=N
00343   **
00344   ** The first form reports the current local setting for the
00345   ** page cache size.  The local setting can be different from
00346   ** the persistent cache size value that is stored in the database
00347   ** file itself.  The value returned is the maximum number of
00348   ** pages in the page cache.  The second form sets the local
00349   ** page cache size value.  It does not change the persistent
00350   ** cache size stored on the disk so the cache size will revert
00351   ** to its default value when the database is closed and reopened.
00352   ** N should be a positive integer.
00353   */
00354   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
00355     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00356     if( !zRight ){
00357       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
00358     }else{
00359       int size = atoi(zRight);
00360       if( size<0 ) size = -size;
00361       pDb->pSchema->cache_size = size;
00362       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
00363     }
00364   }else
00365 
00366   /*
00367   **   PRAGMA temp_store
00368   **   PRAGMA temp_store = "default"|"memory"|"file"
00369   **
00370   ** Return or set the local value of the temp_store flag.  Changing
00371   ** the local value does not make changes to the disk file and the default
00372   ** value will be restored the next time the database is opened.
00373   **
00374   ** Note that it is possible for the library compile-time options to
00375   ** override this setting
00376   */
00377   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
00378     if( !zRight ){
00379       returnSingleInt(pParse, "temp_store", db->temp_store);
00380     }else{
00381       changeTempStorage(pParse, zRight);
00382     }
00383   }else
00384 
00385   /*
00386   **   PRAGMA temp_store_directory
00387   **   PRAGMA temp_store_directory = ""|"directory_name"
00388   **
00389   ** Return or set the local value of the temp_store_directory flag.  Changing
00390   ** the value sets a specific directory to be used for temporary files.
00391   ** Setting to a null string reverts to the default temporary directory search.
00392   ** If temporary directory is changed, then invalidateTempStorage.
00393   **
00394   */
00395   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
00396     if( !zRight ){
00397       if( sqlite3_temp_directory ){
00398         sqlite3VdbeSetNumCols(v, 1);
00399         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
00400             "temp_store_directory", P3_STATIC);
00401         sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
00402         sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
00403       }
00404     }else{
00405       if( zRight[0] && !sqlite3OsIsDirWritable(zRight) ){
00406         sqlite3ErrorMsg(pParse, "not a writable directory");
00407         goto pragma_out;
00408       }
00409       if( TEMP_STORE==0
00410        || (TEMP_STORE==1 && db->temp_store<=1)
00411        || (TEMP_STORE==2 && db->temp_store==1)
00412       ){
00413         invalidateTempStorage(pParse);
00414       }
00415       sqliteFree(sqlite3_temp_directory);
00416       if( zRight[0] ){
00417         sqlite3_temp_directory = zRight;
00418         zRight = 0;
00419       }else{
00420         sqlite3_temp_directory = 0;
00421       }
00422     }
00423   }else
00424 
00425   /*
00426   **   PRAGMA [database.]synchronous
00427   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
00428   **
00429   ** Return or set the local value of the synchronous flag.  Changing
00430   ** the local value does not make changes to the disk file and the
00431   ** default value will be restored the next time the database is
00432   ** opened.
00433   */
00434   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
00435     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00436     if( !zRight ){
00437       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
00438     }else{
00439       if( !db->autoCommit ){
00440         sqlite3ErrorMsg(pParse, 
00441             "Safety level may not be changed inside a transaction");
00442       }else{
00443         pDb->safety_level = getSafetyLevel(zRight)+1;
00444       }
00445     }
00446   }else
00447 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
00448 
00449 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
00450   if( flagPragma(pParse, zLeft, zRight) ){
00451     /* The flagPragma() subroutine also generates any necessary code
00452     ** there is nothing more to do here */
00453   }else
00454 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
00455 
00456 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
00457   /*
00458   **   PRAGMA table_info(<table>)
00459   **
00460   ** Return a single row for each column of the named table. The columns of
00461   ** the returned data set are:
00462   **
00463   ** cid:        Column id (numbered from left to right, starting at 0)
00464   ** name:       Column name
00465   ** type:       Column declaration type.
00466   ** notnull:    True if 'NOT NULL' is part of column declaration
00467   ** dflt_value: The default value for the column, if any.
00468   */
00469   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
00470     Table *pTab;
00471     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00472     pTab = sqlite3FindTable(db, zRight, zDb);
00473     if( pTab ){
00474       int i;
00475       Column *pCol;
00476       sqlite3VdbeSetNumCols(v, 6);
00477       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P3_STATIC);
00478       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
00479       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P3_STATIC);
00480       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P3_STATIC);
00481       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P3_STATIC);
00482       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P3_STATIC);
00483       sqlite3ViewGetColumnNames(pParse, pTab);
00484       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
00485         sqlite3VdbeAddOp(v, OP_Integer, i, 0);
00486         sqlite3VdbeOp3(v, OP_String8, 0, 0, pCol->zName, 0);
00487         sqlite3VdbeOp3(v, OP_String8, 0, 0,
00488            pCol->zType ? pCol->zType : "", 0);
00489         sqlite3VdbeAddOp(v, OP_Integer, pCol->notNull, 0);
00490         sqlite3ExprCode(pParse, pCol->pDflt);
00491         sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey, 0);
00492         sqlite3VdbeAddOp(v, OP_Callback, 6, 0);
00493       }
00494     }
00495   }else
00496 
00497   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
00498     Index *pIdx;
00499     Table *pTab;
00500     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00501     pIdx = sqlite3FindIndex(db, zRight, zDb);
00502     if( pIdx ){
00503       int i;
00504       pTab = pIdx->pTable;
00505       sqlite3VdbeSetNumCols(v, 3);
00506       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P3_STATIC);
00507       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P3_STATIC);
00508       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P3_STATIC);
00509       for(i=0; i<pIdx->nColumn; i++){
00510         int cnum = pIdx->aiColumn[i];
00511         sqlite3VdbeAddOp(v, OP_Integer, i, 0);
00512         sqlite3VdbeAddOp(v, OP_Integer, cnum, 0);
00513         assert( pTab->nCol>cnum );
00514         sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0);
00515         sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
00516       }
00517     }
00518   }else
00519 
00520   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
00521     Index *pIdx;
00522     Table *pTab;
00523     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00524     pTab = sqlite3FindTable(db, zRight, zDb);
00525     if( pTab ){
00526       v = sqlite3GetVdbe(pParse);
00527       pIdx = pTab->pIndex;
00528       if( pIdx ){
00529         int i = 0; 
00530         sqlite3VdbeSetNumCols(v, 3);
00531         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
00532         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
00533         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P3_STATIC);
00534         while(pIdx){
00535           sqlite3VdbeAddOp(v, OP_Integer, i, 0);
00536           sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
00537           sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
00538           sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
00539           ++i;
00540           pIdx = pIdx->pNext;
00541         }
00542       }
00543     }
00544   }else
00545 
00546   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
00547     int i;
00548     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00549     sqlite3VdbeSetNumCols(v, 3);
00550     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
00551     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
00552     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P3_STATIC);
00553     for(i=0; i<db->nDb; i++){
00554       if( db->aDb[i].pBt==0 ) continue;
00555       assert( db->aDb[i].zName!=0 );
00556       sqlite3VdbeAddOp(v, OP_Integer, i, 0);
00557       sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
00558       sqlite3VdbeOp3(v, OP_String8, 0, 0,
00559            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
00560       sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
00561     }
00562   }else
00563 
00564   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
00565     int i = 0;
00566     HashElem *p;
00567     sqlite3VdbeSetNumCols(v, 2);
00568     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
00569     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
00570     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
00571       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
00572       sqlite3VdbeAddOp(v, OP_Integer, i++, 0);
00573       sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0);
00574       sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
00575     }
00576   }else
00577 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
00578 
00579 #ifndef SQLITE_OMIT_FOREIGN_KEY
00580   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
00581     FKey *pFK;
00582     Table *pTab;
00583     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00584     pTab = sqlite3FindTable(db, zRight, zDb);
00585     if( pTab ){
00586       v = sqlite3GetVdbe(pParse);
00587       pFK = pTab->pFKey;
00588       if( pFK ){
00589         int i = 0; 
00590         sqlite3VdbeSetNumCols(v, 5);
00591         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P3_STATIC);
00592         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P3_STATIC);
00593         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P3_STATIC);
00594         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P3_STATIC);
00595         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P3_STATIC);
00596         while(pFK){
00597           int j;
00598           for(j=0; j<pFK->nCol; j++){
00599             char *zCol = pFK->aCol[j].zCol;
00600             sqlite3VdbeAddOp(v, OP_Integer, i, 0);
00601             sqlite3VdbeAddOp(v, OP_Integer, j, 0);
00602             sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0);
00603             sqlite3VdbeOp3(v, OP_String8, 0, 0,
00604                              pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
00605             sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0);
00606             sqlite3VdbeAddOp(v, OP_Callback, 5, 0);
00607           }
00608           ++i;
00609           pFK = pFK->pNextFrom;
00610         }
00611       }
00612     }
00613   }else
00614 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
00615 
00616 #ifndef NDEBUG
00617   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
00618     extern void sqlite3ParserTrace(FILE*, char *);
00619     if( zRight ){
00620       if( getBoolean(zRight) ){
00621         sqlite3ParserTrace(stderr, "parser: ");
00622       }else{
00623         sqlite3ParserTrace(0, 0);
00624       }
00625     }
00626   }else
00627 #endif
00628 
00629   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
00630   ** used will be case sensitive or not depending on the RHS.
00631   */
00632   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
00633     if( zRight ){
00634       sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
00635     }
00636   }else
00637 
00638 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
00639   if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){
00640     int i, j, addr;
00641 
00642     /* Code that appears at the end of the integrity check.  If no error
00643     ** messages have been generated, output OK.  Otherwise output the
00644     ** error message
00645     */
00646     static const VdbeOpList endCode[] = {
00647       { OP_MemLoad,     0, 0,        0},
00648       { OP_Integer,     0, 0,        0},
00649       { OP_Ne,          0, 0,        0},    /* 2 */
00650       { OP_String8,     0, 0,        "ok"},
00651       { OP_Callback,    1, 0,        0},
00652     };
00653 
00654     /* Initialize the VDBE program */
00655     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00656     sqlite3VdbeSetNumCols(v, 1);
00657     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P3_STATIC);
00658     sqlite3VdbeAddOp(v, OP_MemInt, 0, 0);  /* Initialize error count to 0 */
00659 
00660     /* Do an integrity check on each database file */
00661     for(i=0; i<db->nDb; i++){
00662       HashElem *x;
00663       Hash *pTbls;
00664       int cnt = 0;
00665 
00666       if( OMIT_TEMPDB && i==1 ) continue;
00667 
00668       sqlite3CodeVerifySchema(pParse, i);
00669 
00670       /* Do an integrity check of the B-Tree
00671       */
00672       pTbls = &db->aDb[i].pSchema->tblHash;
00673       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
00674         Table *pTab = sqliteHashData(x);
00675         Index *pIdx;
00676         sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0);
00677         cnt++;
00678         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
00679           sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0);
00680           cnt++;
00681         }
00682       }
00683       assert( cnt>0 );
00684       sqlite3VdbeAddOp(v, OP_IntegrityCk, cnt, i);
00685       sqlite3VdbeAddOp(v, OP_Dup, 0, 1);
00686       addr = sqlite3VdbeOp3(v, OP_String8, 0, 0, "ok", P3_STATIC);
00687       sqlite3VdbeAddOp(v, OP_Eq, 0, addr+7);
00688       sqlite3VdbeOp3(v, OP_String8, 0, 0,
00689          sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName),
00690          P3_DYNAMIC);
00691       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
00692       sqlite3VdbeAddOp(v, OP_Concat, 0, 1);
00693       sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
00694       sqlite3VdbeAddOp(v, OP_MemIncr, 1, 0);
00695 
00696       /* Make sure all the indices are constructed correctly.
00697       */
00698       sqlite3CodeVerifySchema(pParse, i);
00699       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
00700         Table *pTab = sqliteHashData(x);
00701         Index *pIdx;
00702         int loopTop;
00703 
00704         if( pTab->pIndex==0 ) continue;
00705         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
00706         sqlite3VdbeAddOp(v, OP_MemInt, 0, 1);
00707         loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0);
00708         sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1);
00709         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
00710           int jmp2;
00711           static const VdbeOpList idxErr[] = {
00712             { OP_MemIncr,     1,  0,  0},
00713             { OP_String8,     0,  0,  "rowid "},
00714             { OP_Rowid,       1,  0,  0},
00715             { OP_String8,     0,  0,  " missing from index "},
00716             { OP_String8,     0,  0,  0},    /* 4 */
00717             { OP_Concat,      2,  0,  0},
00718             { OP_Callback,    1,  0,  0},
00719           };
00720           sqlite3GenerateIndexKey(v, pIdx, 1);
00721           jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0);
00722           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
00723           sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
00724           sqlite3VdbeJumpHere(v, jmp2);
00725         }
00726         sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1);
00727         sqlite3VdbeJumpHere(v, loopTop);
00728         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
00729           static const VdbeOpList cntIdx[] = {
00730              { OP_MemInt,       0,  2,  0},
00731              { OP_Rewind,       0,  0,  0},  /* 1 */
00732              { OP_MemIncr,      1,  2,  0},
00733              { OP_Next,         0,  0,  0},  /* 3 */
00734              { OP_MemLoad,      1,  0,  0},
00735              { OP_MemLoad,      2,  0,  0},
00736              { OP_Eq,           0,  0,  0},  /* 6 */
00737              { OP_MemIncr,      1,  0,  0},
00738              { OP_String8,      0,  0,  "wrong # of entries in index "},
00739              { OP_String8,      0,  0,  0},  /* 9 */
00740              { OP_Concat,       0,  0,  0},
00741              { OP_Callback,     1,  0,  0},
00742           };
00743           if( pIdx->tnum==0 ) continue;
00744           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
00745           sqlite3VdbeChangeP1(v, addr+1, j+2);
00746           sqlite3VdbeChangeP2(v, addr+1, addr+4);
00747           sqlite3VdbeChangeP1(v, addr+3, j+2);
00748           sqlite3VdbeChangeP2(v, addr+3, addr+2);
00749           sqlite3VdbeJumpHere(v, addr+6);
00750           sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC);
00751         }
00752       } 
00753     }
00754     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
00755     sqlite3VdbeJumpHere(v, addr+2);
00756   }else
00757 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
00758 
00759 #ifndef SQLITE_OMIT_UTF16
00760   /*
00761   **   PRAGMA encoding
00762   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
00763   **
00764   ** In it's first form, this pragma returns the encoding of the main
00765   ** database. If the database is not initialized, it is initialized now.
00766   **
00767   ** The second form of this pragma is a no-op if the main database file
00768   ** has not already been initialized. In this case it sets the default
00769   ** encoding that will be used for the main database file if a new file
00770   ** is created. If an existing main database file is opened, then the
00771   ** default text encoding for the existing database is used.
00772   ** 
00773   ** In all cases new databases created using the ATTACH command are
00774   ** created to use the same default text encoding as the main database. If
00775   ** the main database has not been initialized and/or created when ATTACH
00776   ** is executed, this is done before the ATTACH operation.
00777   **
00778   ** In the second form this pragma sets the text encoding to be used in
00779   ** new database files created using this database handle. It is only
00780   ** useful if invoked immediately after the main database i
00781   */
00782   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
00783     static struct EncName {
00784       char *zName;
00785       u8 enc;
00786     } encnames[] = {
00787       { "UTF-8",    SQLITE_UTF8        },
00788       { "UTF8",     SQLITE_UTF8        },
00789       { "UTF-16le", SQLITE_UTF16LE     },
00790       { "UTF16le",  SQLITE_UTF16LE     },
00791       { "UTF-16be", SQLITE_UTF16BE     },
00792       { "UTF16be",  SQLITE_UTF16BE     },
00793       { "UTF-16",   0 /* Filled in at run-time */ },
00794       { "UTF16",    0 /* Filled in at run-time */ },
00795       { 0, 0 }
00796     };
00797     struct EncName *pEnc;
00798     encnames[6].enc = encnames[7].enc = SQLITE_UTF16NATIVE;
00799     if( !zRight ){    /* "PRAGMA encoding" */
00800       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
00801       sqlite3VdbeSetNumCols(v, 1);
00802       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P3_STATIC);
00803       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
00804       for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
00805         if( pEnc->enc==ENC(pParse->db) ){
00806           sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
00807           break;
00808         }
00809       }
00810       sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
00811     }else{                        /* "PRAGMA encoding = XXX" */
00812       /* Only change the value of sqlite.enc if the database handle is not
00813       ** initialized. If the main database exists, the new sqlite.enc value
00814       ** will be overwritten when the schema is next loaded. If it does not
00815       ** already exists, it will be created to use the new encoding value.
00816       */
00817       if( 
00818         !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
00819         DbHasProperty(db, 0, DB_Empty) 
00820       ){
00821         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
00822           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
00823             ENC(pParse->db) = pEnc->enc;
00824             break;
00825           }
00826         }
00827         if( !pEnc->zName ){
00828           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
00829         }
00830       }
00831     }
00832   }else
00833 #endif /* SQLITE_OMIT_UTF16 */
00834 
00835 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
00836   /*
00837   **   PRAGMA [database.]schema_version
00838   **   PRAGMA [database.]schema_version = <integer>
00839   **
00840   **   PRAGMA [database.]user_version
00841   **   PRAGMA [database.]user_version = <integer>
00842   **
00843   ** The pragma's schema_version and user_version are used to set or get
00844   ** the value of the schema-version and user-version, respectively. Both
00845   ** the schema-version and the user-version are 32-bit signed integers
00846   ** stored in the database header.
00847   **
00848   ** The schema-cookie is usually only manipulated internally by SQLite. It
00849   ** is incremented by SQLite whenever the database schema is modified (by
00850   ** creating or dropping a table or index). The schema version is used by
00851   ** SQLite each time a query is executed to ensure that the internal cache
00852   ** of the schema used when compiling the SQL query matches the schema of
00853   ** the database against which the compiled query is actually executed.
00854   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
00855   ** the schema-version is potentially dangerous and may lead to program
00856   ** crashes or database corruption. Use with caution!
00857   **
00858   ** The user-version is not used internally by SQLite. It may be used by
00859   ** applications for any purpose.
00860   */
00861   if( sqlite3StrICmp(zLeft, "schema_version")==0 ||
00862       sqlite3StrICmp(zLeft, "user_version")==0 ){
00863 
00864     int iCookie;   /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
00865     if( zLeft[0]=='s' || zLeft[0]=='S' ){
00866       iCookie = 0;
00867     }else{
00868       iCookie = 5;
00869     }
00870 
00871     if( zRight ){
00872       /* Write the specified cookie value */
00873       static const VdbeOpList setCookie[] = {
00874         { OP_Transaction,    0,  1,  0},    /* 0 */
00875         { OP_Integer,        0,  0,  0},    /* 1 */
00876         { OP_SetCookie,      0,  0,  0},    /* 2 */
00877       };
00878       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
00879       sqlite3VdbeChangeP1(v, addr, iDb);
00880       sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
00881       sqlite3VdbeChangeP1(v, addr+2, iDb);
00882       sqlite3VdbeChangeP2(v, addr+2, iCookie);
00883     }else{
00884       /* Read the specified cookie value */
00885       static const VdbeOpList readCookie[] = {
00886         { OP_ReadCookie,      0,  0,  0},    /* 0 */
00887         { OP_Callback,        1,  0,  0}
00888       };
00889       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
00890       sqlite3VdbeChangeP1(v, addr, iDb);
00891       sqlite3VdbeChangeP2(v, addr, iCookie);
00892       sqlite3VdbeSetNumCols(v, 1);
00893     }
00894   }
00895 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
00896 
00897 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
00898   /*
00899   ** Report the current state of file logs for all databases
00900   */
00901   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
00902     static const char *const azLockName[] = {
00903       "unlocked", "shared", "reserved", "pending", "exclusive"
00904     };
00905     int i;
00906     Vdbe *v = sqlite3GetVdbe(pParse);
00907     sqlite3VdbeSetNumCols(v, 2);
00908     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P3_STATIC);
00909     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P3_STATIC);
00910     for(i=0; i<db->nDb; i++){
00911       Btree *pBt;
00912       Pager *pPager;
00913       if( db->aDb[i].zName==0 ) continue;
00914       sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC);
00915       pBt = db->aDb[i].pBt;
00916       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
00917         sqlite3VdbeOp3(v, OP_String8, 0, 0, "closed", P3_STATIC);
00918       }else{
00919         int j = sqlite3pager_lockstate(pPager);
00920         sqlite3VdbeOp3(v, OP_String8, 0, 0, 
00921             (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC);
00922       }
00923       sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
00924     }
00925   }else
00926 #endif
00927 
00928 #ifdef SQLITE_SSE
00929   /*
00930   ** Check to see if the sqlite_statements table exists.  Create it
00931   ** if it does not.
00932   */
00933   if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
00934     extern int sqlite3CreateStatementsTable(Parse*);
00935     sqlite3CreateStatementsTable(pParse);
00936   }else
00937 #endif
00938 
00939 #if SQLITE_HAS_CODEC
00940   if( sqlite3StrICmp(zLeft, "key")==0 ){
00941     sqlite3_key(db, zRight, strlen(zRight));
00942   }else
00943 #endif
00944 
00945   {}
00946 
00947   if( v ){
00948     /* Code an OP_Expire at the end of each PRAGMA program to cause
00949     ** the VDBE implementing the pragma to expire. Most (all?) pragmas
00950     ** are only valid for a single execution.
00951     */
00952     sqlite3VdbeAddOp(v, OP_Expire, 1, 0);
00953 
00954     /*
00955     ** Reset the safety level, in case the fullfsync flag or synchronous
00956     ** setting changed.
00957     */
00958 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
00959     if( db->autoCommit ){
00960       sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
00961                  (db->flags&SQLITE_FullFSync)!=0);
00962     }
00963 #endif
00964   }
00965 pragma_out:
00966   sqliteFree(zLeft);
00967   sqliteFree(zRight);
00968 }
00969 
00970 #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */