xref: /sqlite-3.40.0/src/pragma.c (revision 7e475e57)
1c11d4f93Sdrh /*
2c11d4f93Sdrh ** 2003 April 6
3c11d4f93Sdrh **
4c11d4f93Sdrh ** The author disclaims copyright to this source code.  In place of
5c11d4f93Sdrh ** a legal notice, here is a blessing:
6c11d4f93Sdrh **
7c11d4f93Sdrh **    May you do good and not evil.
8c11d4f93Sdrh **    May you find forgiveness for yourself and forgive others.
9c11d4f93Sdrh **    May you share freely, never taking more than you give.
10c11d4f93Sdrh **
11c11d4f93Sdrh *************************************************************************
12c11d4f93Sdrh ** This file contains code used to implement the PRAGMA command.
13c11d4f93Sdrh */
14c11d4f93Sdrh #include "sqliteInt.h"
15c11d4f93Sdrh 
169ccd8659Sdrh #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
179ccd8659Sdrh #  if defined(__APPLE__)
189ccd8659Sdrh #    define SQLITE_ENABLE_LOCKING_STYLE 1
199ccd8659Sdrh #  else
209ccd8659Sdrh #    define SQLITE_ENABLE_LOCKING_STYLE 0
219ccd8659Sdrh #  endif
229ccd8659Sdrh #endif
239ccd8659Sdrh 
249ccd8659Sdrh /***************************************************************************
2567e65e55Sdrh ** The "pragma.h" include file is an automatically generated file that
2667e65e55Sdrh ** that includes the PragType_XXXX macro definitions and the aPragmaName[]
2767e65e55Sdrh ** object.  This ensures that the aPragmaName[] table is arranged in
2867e65e55Sdrh ** lexicographical order to facility a binary search of the pragma name.
2967e65e55Sdrh ** Do not edit pragma.h directly.  Edit and rerun the script in at
3067e65e55Sdrh ** ../tool/mkpragmatab.tcl. */
3167e65e55Sdrh #include "pragma.h"
329ccd8659Sdrh 
33c11d4f93Sdrh /*
34c11d4f93Sdrh ** Interpret the given string as a safety level.  Return 0 for OFF,
356841b1cbSdrh ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or
366841b1cbSdrh ** unrecognized string argument.  The FULL and EXTRA option is disallowed
37908c005cSdrh ** if the omitFull parameter it 1.
38c11d4f93Sdrh **
39c11d4f93Sdrh ** Note that the values returned are one less that the values that
404adee20fSdanielk1977 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
41c11d4f93Sdrh ** to support legacy SQL code.  The safety level used to be boolean
42c11d4f93Sdrh ** and older scripts may have used numbers 0 for OFF and 1 for ON.
43c11d4f93Sdrh */
getSafetyLevel(const char * z,int omitFull,u8 dflt)44eac5bd78Sdrh static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
456841b1cbSdrh                              /* 123456789 123456789 123 */
466841b1cbSdrh   static const char zText[] = "onoffalseyestruextrafull";
476841b1cbSdrh   static const u8 iOffset[] = {0, 1, 2,  4,    9,  12,  15,   20};
486841b1cbSdrh   static const u8 iLength[] = {2, 2, 3,  5,    3,   4,   5,    4};
496841b1cbSdrh   static const u8 iValue[] =  {1, 0, 0,  0,    1,   1,   3,    2};
506841b1cbSdrh                             /* on no off false yes true extra full */
51722e95acSdrh   int i, n;
5278ca0e7eSdanielk1977   if( sqlite3Isdigit(*z) ){
5360ac3f42Sdrh     return (u8)sqlite3Atoi(z);
54c11d4f93Sdrh   }
55ea678832Sdrh   n = sqlite3Strlen30(z);
566841b1cbSdrh   for(i=0; i<ArraySize(iLength); i++){
576841b1cbSdrh     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
586841b1cbSdrh      && (!omitFull || iValue[i]<=1)
596841b1cbSdrh     ){
60722e95acSdrh       return iValue[i];
61722e95acSdrh     }
62c11d4f93Sdrh   }
63908c005cSdrh   return dflt;
64c11d4f93Sdrh }
65c11d4f93Sdrh 
66c11d4f93Sdrh /*
67722e95acSdrh ** Interpret the given string as a boolean value.
68722e95acSdrh */
sqlite3GetBoolean(const char * z,u8 dflt)69eac5bd78Sdrh u8 sqlite3GetBoolean(const char *z, u8 dflt){
7038d9c612Sdrh   return getSafetyLevel(z,1,dflt)!=0;
71722e95acSdrh }
72722e95acSdrh 
73c7dc9bf8Sdrh /* The sqlite3GetBoolean() function is used by other modules but the
74c7dc9bf8Sdrh ** remainder of this file is specific to PRAGMA processing.  So omit
75c7dc9bf8Sdrh ** the rest of the file if PRAGMAs are omitted from the build.
76c7dc9bf8Sdrh */
77c7dc9bf8Sdrh #if !defined(SQLITE_OMIT_PRAGMA)
78c7dc9bf8Sdrh 
7941483468Sdanielk1977 /*
8041483468Sdanielk1977 ** Interpret the given string as a locking mode value.
8141483468Sdanielk1977 */
getLockingMode(const char * z)8241483468Sdanielk1977 static int getLockingMode(const char *z){
8341483468Sdanielk1977   if( z ){
8441483468Sdanielk1977     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
8541483468Sdanielk1977     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
8641483468Sdanielk1977   }
8741483468Sdanielk1977   return PAGER_LOCKINGMODE_QUERY;
8841483468Sdanielk1977 }
8941483468Sdanielk1977 
90dddbcdccSdanielk1977 #ifndef SQLITE_OMIT_AUTOVACUUM
91dddbcdccSdanielk1977 /*
92dddbcdccSdanielk1977 ** Interpret the given string as an auto-vacuum mode value.
93dddbcdccSdanielk1977 **
94dddbcdccSdanielk1977 ** The following strings, "none", "full" and "incremental" are
95dddbcdccSdanielk1977 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
96dddbcdccSdanielk1977 */
getAutoVacuum(const char * z)97dddbcdccSdanielk1977 static int getAutoVacuum(const char *z){
98dddbcdccSdanielk1977   int i;
99dddbcdccSdanielk1977   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
100dddbcdccSdanielk1977   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
101dddbcdccSdanielk1977   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
10260ac3f42Sdrh   i = sqlite3Atoi(z);
1034f21c4afSdrh   return (u8)((i>=0&&i<=2)?i:0);
104dddbcdccSdanielk1977 }
105dddbcdccSdanielk1977 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
106dddbcdccSdanielk1977 
107b84f96f8Sdanielk1977 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
108722e95acSdrh /*
10990f5ecb3Sdrh ** Interpret the given string as a temp db location. Return 1 for file
11090f5ecb3Sdrh ** backed temporary databases, 2 for the Red-Black tree in memory database
11190f5ecb3Sdrh ** and 0 to use the compile-time default.
11290f5ecb3Sdrh */
getTempStore(const char * z)11390f5ecb3Sdrh static int getTempStore(const char *z){
11490f5ecb3Sdrh   if( z[0]>='0' && z[0]<='2' ){
11590f5ecb3Sdrh     return z[0] - '0';
11690f5ecb3Sdrh   }else if( sqlite3StrICmp(z, "file")==0 ){
11790f5ecb3Sdrh     return 1;
11890f5ecb3Sdrh   }else if( sqlite3StrICmp(z, "memory")==0 ){
11990f5ecb3Sdrh     return 2;
12090f5ecb3Sdrh   }else{
12190f5ecb3Sdrh     return 0;
12290f5ecb3Sdrh   }
12390f5ecb3Sdrh }
124bf21627bSdrh #endif /* SQLITE_PAGER_PRAGMAS */
12590f5ecb3Sdrh 
126bf21627bSdrh #ifndef SQLITE_OMIT_PAGER_PRAGMAS
12790f5ecb3Sdrh /*
1289a09a3caStpoindex ** Invalidate temp storage, either when the temp storage is changed
1299a09a3caStpoindex ** from default, or when 'file' and the temp_store_directory has changed
13090f5ecb3Sdrh */
invalidateTempStorage(Parse * pParse)1319a09a3caStpoindex static int invalidateTempStorage(Parse *pParse){
1329bb575fdSdrh   sqlite3 *db = pParse->db;
13390f5ecb3Sdrh   if( db->aDb[1].pBt!=0 ){
13499744fa4Sdrh     if( !db->autoCommit
13599744fa4Sdrh      || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
13699744fa4Sdrh     ){
13790f5ecb3Sdrh       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
13890f5ecb3Sdrh         "from within a transaction");
13990f5ecb3Sdrh       return SQLITE_ERROR;
14090f5ecb3Sdrh     }
14190f5ecb3Sdrh     sqlite3BtreeClose(db->aDb[1].pBt);
14290f5ecb3Sdrh     db->aDb[1].pBt = 0;
14381028a45Sdrh     sqlite3ResetAllSchemasOfConnection(db);
14490f5ecb3Sdrh   }
1459a09a3caStpoindex   return SQLITE_OK;
1469a09a3caStpoindex }
147bf21627bSdrh #endif /* SQLITE_PAGER_PRAGMAS */
1489a09a3caStpoindex 
149bf21627bSdrh #ifndef SQLITE_OMIT_PAGER_PRAGMAS
1509a09a3caStpoindex /*
1519a09a3caStpoindex ** If the TEMP database is open, close it and mark the database schema
152b06a0b67Sdanielk1977 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
1539a09a3caStpoindex ** or DEFAULT_TEMP_STORE pragmas.
1549a09a3caStpoindex */
changeTempStorage(Parse * pParse,const char * zStorageType)1559a09a3caStpoindex static int changeTempStorage(Parse *pParse, const char *zStorageType){
1569a09a3caStpoindex   int ts = getTempStore(zStorageType);
1579a09a3caStpoindex   sqlite3 *db = pParse->db;
1589a09a3caStpoindex   if( db->temp_store==ts ) return SQLITE_OK;
1599a09a3caStpoindex   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
1609a09a3caStpoindex     return SQLITE_ERROR;
1619a09a3caStpoindex   }
1624f21c4afSdrh   db->temp_store = (u8)ts;
16390f5ecb3Sdrh   return SQLITE_OK;
16490f5ecb3Sdrh }
165bf21627bSdrh #endif /* SQLITE_PAGER_PRAGMAS */
16690f5ecb3Sdrh 
16790f5ecb3Sdrh /*
168c232aca1Sdrh ** Set result column names for a pragma.
169b460e52aSdrh */
setPragmaResultColumnNames(Vdbe * v,const PragmaName * pPragma)170c232aca1Sdrh static void setPragmaResultColumnNames(
171b460e52aSdrh   Vdbe *v,                     /* The query under construction */
1722fcc1590Sdrh   const PragmaName *pPragma    /* The pragma */
173b460e52aSdrh ){
174c232aca1Sdrh   u8 n = pPragma->nPragCName;
175c232aca1Sdrh   sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
176c232aca1Sdrh   if( n==0 ){
177c232aca1Sdrh     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
178c232aca1Sdrh   }else{
179c232aca1Sdrh     int i, j;
180c232aca1Sdrh     for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
181c232aca1Sdrh       sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
182b460e52aSdrh     }
183b460e52aSdrh   }
184b460e52aSdrh }
185b460e52aSdrh 
186b460e52aSdrh /*
18790f5ecb3Sdrh ** Generate code to return a single integer value.
18890f5ecb3Sdrh */
returnSingleInt(Vdbe * v,i64 value)189c232aca1Sdrh static void returnSingleInt(Vdbe *v, i64 value){
1907cc023c7Sdrh   sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
1917cc023c7Sdrh   sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1927cc023c7Sdrh }
1937cc023c7Sdrh 
1947cc023c7Sdrh /*
1957cc023c7Sdrh ** Generate code to return a single text value.
1967cc023c7Sdrh */
returnSingleText(Vdbe * v,const char * zValue)1977cc023c7Sdrh static void returnSingleText(
1987cc023c7Sdrh   Vdbe *v,                /* Prepared statement under construction */
1997cc023c7Sdrh   const char *zValue      /* Value to be returned */
2007cc023c7Sdrh ){
2017cc023c7Sdrh   if( zValue ){
202076e85f5Sdrh     sqlite3VdbeLoadString(v, 1, (const char*)zValue);
2037cc023c7Sdrh     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2047cc023c7Sdrh   }
20590f5ecb3Sdrh }
20690f5ecb3Sdrh 
207d3605a4fSdrh 
208d3605a4fSdrh /*
209d3605a4fSdrh ** Set the safety_level and pager flags for pager iDb.  Or if iDb<0
210d3605a4fSdrh ** set these values for all pagers.
211d3605a4fSdrh */
212d3605a4fSdrh #ifndef SQLITE_OMIT_PAGER_PRAGMAS
setAllPagerFlags(sqlite3 * db)213d3605a4fSdrh static void setAllPagerFlags(sqlite3 *db){
214d3605a4fSdrh   if( db->autoCommit ){
215d3605a4fSdrh     Db *pDb = db->aDb;
216d3605a4fSdrh     int n = db->nDb;
217d3605a4fSdrh     assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
218d3605a4fSdrh     assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
219d3605a4fSdrh     assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
220d3605a4fSdrh     assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
221d3605a4fSdrh              ==  PAGER_FLAGS_MASK );
222d3605a4fSdrh     assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
223d3605a4fSdrh     while( (n--) > 0 ){
224d3605a4fSdrh       if( pDb->pBt ){
225d3605a4fSdrh         sqlite3BtreeSetPagerFlags(pDb->pBt,
226d3605a4fSdrh                  pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
227d3605a4fSdrh       }
228d3605a4fSdrh       pDb++;
229d3605a4fSdrh     }
230d3605a4fSdrh   }
231d3605a4fSdrh }
232feb56e0eSdrh #else
233feb56e0eSdrh # define setAllPagerFlags(X)  /* no-op */
234d3605a4fSdrh #endif
235d3605a4fSdrh 
236d3605a4fSdrh 
237d2cb50b7Sdrh /*
238d2cb50b7Sdrh ** Return a human-readable name for a constraint resolution action.
239d2cb50b7Sdrh */
240ba9108b8Sdan #ifndef SQLITE_OMIT_FOREIGN_KEY
actionName(u8 action)24150af3e1dSdanielk1977 static const char *actionName(u8 action){
242d2cb50b7Sdrh   const char *zName;
24350af3e1dSdanielk1977   switch( action ){
244d2cb50b7Sdrh     case OE_SetNull:  zName = "SET NULL";        break;
245d2cb50b7Sdrh     case OE_SetDflt:  zName = "SET DEFAULT";     break;
246d2cb50b7Sdrh     case OE_Cascade:  zName = "CASCADE";         break;
2471da40a38Sdan     case OE_Restrict: zName = "RESTRICT";        break;
2481da40a38Sdan     default:          zName = "NO ACTION";
2491da40a38Sdan                       assert( action==OE_None ); break;
25050af3e1dSdanielk1977   }
251d2cb50b7Sdrh   return zName;
25250af3e1dSdanielk1977 }
253ba9108b8Sdan #endif
25450af3e1dSdanielk1977 
255e04dc88bSdan 
256e04dc88bSdan /*
257e04dc88bSdan ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
258e04dc88bSdan ** defined in pager.h. This function returns the associated lowercase
259e04dc88bSdan ** journal-mode name.
260e04dc88bSdan */
sqlite3JournalModename(int eMode)261e04dc88bSdan const char *sqlite3JournalModename(int eMode){
262e04dc88bSdan   static char * const azModeName[] = {
2635cf53537Sdan     "delete", "persist", "off", "truncate", "memory"
2645cf53537Sdan #ifndef SQLITE_OMIT_WAL
2655cf53537Sdan      , "wal"
2665cf53537Sdan #endif
267e04dc88bSdan   };
268e04dc88bSdan   assert( PAGER_JOURNALMODE_DELETE==0 );
269e04dc88bSdan   assert( PAGER_JOURNALMODE_PERSIST==1 );
270e04dc88bSdan   assert( PAGER_JOURNALMODE_OFF==2 );
271e04dc88bSdan   assert( PAGER_JOURNALMODE_TRUNCATE==3 );
272e04dc88bSdan   assert( PAGER_JOURNALMODE_MEMORY==4 );
273e04dc88bSdan   assert( PAGER_JOURNALMODE_WAL==5 );
274e04dc88bSdan   assert( eMode>=0 && eMode<=ArraySize(azModeName) );
275e04dc88bSdan 
276e04dc88bSdan   if( eMode==ArraySize(azModeName) ) return 0;
277e04dc88bSdan   return azModeName[eMode];
278e04dc88bSdan }
279e04dc88bSdan 
2808722318fSdrh /*
2812fcc1590Sdrh ** Locate a pragma in the aPragmaName[] array.
2822fcc1590Sdrh */
pragmaLocate(const char * zName)2832fcc1590Sdrh static const PragmaName *pragmaLocate(const char *zName){
2842e525322Smistachkin   int upr, lwr, mid = 0, rc;
2852fcc1590Sdrh   lwr = 0;
2862fcc1590Sdrh   upr = ArraySize(aPragmaName)-1;
2872fcc1590Sdrh   while( lwr<=upr ){
2882fcc1590Sdrh     mid = (lwr+upr)/2;
2892fcc1590Sdrh     rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
2902fcc1590Sdrh     if( rc==0 ) break;
2912fcc1590Sdrh     if( rc<0 ){
2922fcc1590Sdrh       upr = mid - 1;
2932fcc1590Sdrh     }else{
2942fcc1590Sdrh       lwr = mid + 1;
2952fcc1590Sdrh     }
2962fcc1590Sdrh   }
2972fcc1590Sdrh   return lwr>upr ? 0 : &aPragmaName[mid];
2982fcc1590Sdrh }
2992fcc1590Sdrh 
3002fcc1590Sdrh /*
30179d5bc80Sdrh ** Create zero or more entries in the output for the SQL functions
30279d5bc80Sdrh ** defined by FuncDef p.
30379d5bc80Sdrh */
pragmaFunclistLine(Vdbe * v,FuncDef * p,int isBuiltin,int showInternFuncs)304337ca519Sdrh static void pragmaFunclistLine(
305337ca519Sdrh   Vdbe *v,               /* The prepared statement being created */
306337ca519Sdrh   FuncDef *p,            /* A particular function definition */
307337ca519Sdrh   int isBuiltin,         /* True if this is a built-in function */
308337ca519Sdrh   int showInternFuncs    /* True if showing internal functions */
309337ca519Sdrh ){
3109606a134Sdrh   u32 mask =
31179d5bc80Sdrh       SQLITE_DETERMINISTIC |
31279d5bc80Sdrh       SQLITE_DIRECTONLY |
31379d5bc80Sdrh       SQLITE_SUBTYPE |
314337ca519Sdrh       SQLITE_INNOCUOUS |
315337ca519Sdrh       SQLITE_FUNC_INTERNAL
31679d5bc80Sdrh   ;
317ceff7615Sdrh   if( showInternFuncs ) mask = 0xffffffff;
3189606a134Sdrh   for(; p; p=p->pNext){
3199606a134Sdrh     const char *zType;
320b84fda37Sdrh     static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" };
321b84fda37Sdrh 
322b84fda37Sdrh     assert( SQLITE_FUNC_ENCMASK==0x3 );
323b84fda37Sdrh     assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 );
324b84fda37Sdrh     assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 );
325b84fda37Sdrh     assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 );
32679d5bc80Sdrh 
32779d5bc80Sdrh     if( p->xSFunc==0 ) continue;
328337ca519Sdrh     if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
329337ca519Sdrh      && showInternFuncs==0
330337ca519Sdrh     ){
331337ca519Sdrh       continue;
332337ca519Sdrh     }
33379d5bc80Sdrh     if( p->xValue!=0 ){
33479d5bc80Sdrh       zType = "w";
33579d5bc80Sdrh     }else if( p->xFinalize!=0 ){
33679d5bc80Sdrh       zType = "a";
33779d5bc80Sdrh     }else{
33879d5bc80Sdrh       zType = "s";
33979d5bc80Sdrh     }
34079d5bc80Sdrh     sqlite3VdbeMultiLoad(v, 1, "sissii",
34179d5bc80Sdrh        p->zName, isBuiltin,
342b84fda37Sdrh        zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
34379d5bc80Sdrh        p->nArg,
34479d5bc80Sdrh        (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
34579d5bc80Sdrh     );
34679d5bc80Sdrh   }
34779d5bc80Sdrh }
34879d5bc80Sdrh 
34979d5bc80Sdrh 
35079d5bc80Sdrh /*
35166accfc5Sdrh ** Helper subroutine for PRAGMA integrity_check:
35266accfc5Sdrh **
3539ecd7086Sdrh ** Generate code to output a single-column result row with a value of the
3549ecd7086Sdrh ** string held in register 3.  Decrement the result count in register 1
3559ecd7086Sdrh ** and halt if the maximum number of result rows have been issued.
35666accfc5Sdrh */
integrityCheckResultRow(Vdbe * v)3579ecd7086Sdrh static int integrityCheckResultRow(Vdbe *v){
35866accfc5Sdrh   int addr;
3599ecd7086Sdrh   sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
36066accfc5Sdrh   addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
36166accfc5Sdrh   VdbeCoverage(v);
3629ecd7086Sdrh   sqlite3VdbeAddOp0(v, OP_Halt);
36366accfc5Sdrh   return addr;
36466accfc5Sdrh }
36566accfc5Sdrh 
36666accfc5Sdrh /*
367c11d4f93Sdrh ** Process a pragma statement.
368c11d4f93Sdrh **
369c11d4f93Sdrh ** Pragmas are of this form:
370c11d4f93Sdrh **
3719b0cf34fSdrh **      PRAGMA [schema.]id [= value]
372c11d4f93Sdrh **
373c11d4f93Sdrh ** The identifier might also be a string.  The value is a string, and
374c11d4f93Sdrh ** identifier, or a number.  If minusFlag is true, then the value is
375c11d4f93Sdrh ** a number that was preceded by a minus sign.
37690f5ecb3Sdrh **
37790f5ecb3Sdrh ** If the left side is "database.id" then pId1 is the database name
37890f5ecb3Sdrh ** and pId2 is the id.  If the left side is just "id" then pId1 is the
37990f5ecb3Sdrh ** id and pId2 is any empty string.
380c11d4f93Sdrh */
sqlite3Pragma(Parse * pParse,Token * pId1,Token * pId2,Token * pValue,int minusFlag)38191cf71b0Sdanielk1977 void sqlite3Pragma(
38291cf71b0Sdanielk1977   Parse *pParse,
3839b0cf34fSdrh   Token *pId1,        /* First part of [schema.]id field */
3849b0cf34fSdrh   Token *pId2,        /* Second part of [schema.]id field, or NULL */
38591cf71b0Sdanielk1977   Token *pValue,      /* Token for <value>, or NULL */
38691cf71b0Sdanielk1977   int minusFlag       /* True if a '-' sign preceded <value> */
38791cf71b0Sdanielk1977 ){
38891cf71b0Sdanielk1977   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
38991cf71b0Sdanielk1977   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
39091cf71b0Sdanielk1977   const char *zDb = 0;   /* The database name */
39191cf71b0Sdanielk1977   Token *pId;            /* Pointer to <id> token */
3923fa97302Sdrh   char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
3939ccd8659Sdrh   int iDb;               /* Database index for <database> */
39406fd5d63Sdrh   int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
39506fd5d63Sdrh   sqlite3 *db = pParse->db;    /* The database connection */
39606fd5d63Sdrh   Db *pDb;                     /* The specific database being pragmaed */
397ef8e986bSdrh   Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
3982fcc1590Sdrh   const PragmaName *pPragma;   /* The pragma */
39906fd5d63Sdrh 
400c11d4f93Sdrh   if( v==0 ) return;
4014611d925Sdrh   sqlite3VdbeRunOnlyOnce(v);
4029cbf3425Sdrh   pParse->nMem = 2;
403c11d4f93Sdrh 
4049b0cf34fSdrh   /* Interpret the [schema.] part of the pragma statement. iDb is the
40591cf71b0Sdanielk1977   ** index of the database this pragma is being applied to in db.aDb[]. */
40691cf71b0Sdanielk1977   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
40791cf71b0Sdanielk1977   if( iDb<0 ) return;
40890f5ecb3Sdrh   pDb = &db->aDb[iDb];
40991cf71b0Sdanielk1977 
410ddfb2f03Sdanielk1977   /* If the temp database has been explicitly named as part of the
411ddfb2f03Sdanielk1977   ** pragma, make sure it is open.
412ddfb2f03Sdanielk1977   */
413ddfb2f03Sdanielk1977   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
414ddfb2f03Sdanielk1977     return;
415ddfb2f03Sdanielk1977   }
416ddfb2f03Sdanielk1977 
41717435752Sdrh   zLeft = sqlite3NameFromToken(db, pId);
41896fb0dd5Sdanielk1977   if( !zLeft ) return;
419c11d4f93Sdrh   if( minusFlag ){
42017435752Sdrh     zRight = sqlite3MPrintf(db, "-%T", pValue);
421c11d4f93Sdrh   }else{
42217435752Sdrh     zRight = sqlite3NameFromToken(db, pValue);
423c11d4f93Sdrh   }
42491cf71b0Sdanielk1977 
425d2cb50b7Sdrh   assert( pId2 );
42669c33826Sdrh   zDb = pId2->n>0 ? pDb->zDbSName : 0;
42791cf71b0Sdanielk1977   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
428e0048400Sdanielk1977     goto pragma_out;
429c11d4f93Sdrh   }
430c11d4f93Sdrh 
43106fd5d63Sdrh   /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
43206fd5d63Sdrh   ** connection.  If it returns SQLITE_OK, then assume that the VFS
43306fd5d63Sdrh   ** handled the pragma and generate a no-op prepared statement.
4348dd7a6a9Sdrh   **
4358dd7a6a9Sdrh   ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
4368dd7a6a9Sdrh   ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
4378dd7a6a9Sdrh   ** object corresponding to the database file to which the pragma
4388dd7a6a9Sdrh   ** statement refers.
4398dd7a6a9Sdrh   **
4408dd7a6a9Sdrh   ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
4418dd7a6a9Sdrh   ** file control is an array of pointers to strings (char**) in which the
4428dd7a6a9Sdrh   ** second element of the array is the name of the pragma and the third
4438dd7a6a9Sdrh   ** element is the argument to the pragma or NULL if the pragma has no
4448dd7a6a9Sdrh   ** argument.
44506fd5d63Sdrh   */
4463fa97302Sdrh   aFcntl[0] = 0;
4473fa97302Sdrh   aFcntl[1] = zLeft;
4483fa97302Sdrh   aFcntl[2] = zRight;
4493fa97302Sdrh   aFcntl[3] = 0;
45080bb6f82Sdan   db->busyHandler.nBusy = 0;
45106fd5d63Sdrh   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
45206fd5d63Sdrh   if( rc==SQLITE_OK ){
453c232aca1Sdrh     sqlite3VdbeSetNumCols(v, 1);
454c232aca1Sdrh     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
455c232aca1Sdrh     returnSingleText(v, aFcntl[0]);
4563fa97302Sdrh     sqlite3_free(aFcntl[0]);
4579ccd8659Sdrh     goto pragma_out;
4589ccd8659Sdrh   }
4599ccd8659Sdrh   if( rc!=SQLITE_NOTFOUND ){
46092c700dbSdrh     if( aFcntl[0] ){
46192c700dbSdrh       sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
46292c700dbSdrh       sqlite3_free(aFcntl[0]);
46392c700dbSdrh     }
46492c700dbSdrh     pParse->nErr++;
46592c700dbSdrh     pParse->rc = rc;
4669ccd8659Sdrh     goto pragma_out;
4679ccd8659Sdrh   }
46806fd5d63Sdrh 
4699ccd8659Sdrh   /* Locate the pragma in the lookup table */
4702fcc1590Sdrh   pPragma = pragmaLocate(zLeft);
471bc98f904Sdrh   if( pPragma==0 ){
472bc98f904Sdrh     /* IMP: R-43042-22504 No error messages are generated if an
473bc98f904Sdrh     ** unknown pragma is issued. */
474bc98f904Sdrh     goto pragma_out;
475bc98f904Sdrh   }
4769ccd8659Sdrh 
477f63936e8Sdrh   /* Make sure the database schema is loaded if the pragma requires that */
478c232aca1Sdrh   if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
479f63936e8Sdrh     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
480f63936e8Sdrh   }
481f63936e8Sdrh 
482c232aca1Sdrh   /* Register the result column names for pragmas that return results */
4839e1ab1a8Sdan   if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
4849e1ab1a8Sdan    && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
4859e1ab1a8Sdan   ){
486c232aca1Sdrh     setPragmaResultColumnNames(v, pPragma);
487c232aca1Sdrh   }
488c232aca1Sdrh 
4899ccd8659Sdrh   /* Jump to the appropriate pragma handler */
490c228be5bSdrh   switch( pPragma->ePragTyp ){
49106fd5d63Sdrh 
492e73c9149Sdrh #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
493c11d4f93Sdrh   /*
4949b0cf34fSdrh   **  PRAGMA [schema.]default_cache_size
4959b0cf34fSdrh   **  PRAGMA [schema.]default_cache_size=N
496c11d4f93Sdrh   **
497c11d4f93Sdrh   ** The first form reports the current persistent setting for the
498c11d4f93Sdrh   ** page cache size.  The value returned is the maximum number of
499c11d4f93Sdrh   ** pages in the page cache.  The second form sets both the current
500c11d4f93Sdrh   ** page cache size value and the persistent page cache size value
501c11d4f93Sdrh   ** stored in the database file.
502c11d4f93Sdrh   **
50393791ea0Sdrh   ** Older versions of SQLite would set the default cache size to a
50493791ea0Sdrh   ** negative number to indicate synchronous=OFF.  These days, synchronous
50593791ea0Sdrh   ** is always on by default regardless of the sign of the default cache
50693791ea0Sdrh   ** size.  But continue to take the absolute value of the default cache
50793791ea0Sdrh   ** size of historical compatibility.
508c11d4f93Sdrh   */
5099ccd8659Sdrh   case PragTyp_DEFAULT_CACHE_SIZE: {
510b06a4ec1Sdrh     static const int iLn = VDBE_OFFSET_LINENO(2);
5115719628aSdrh     static const VdbeOpList getCacheSize[] = {
512602b466eSdanielk1977       { OP_Transaction, 0, 0,        0},                         /* 0 */
513602b466eSdanielk1977       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
514ef8e986bSdrh       { OP_IfPos,       1, 8,        0},
5153c84ddffSdrh       { OP_Integer,     0, 2,        0},
5163c84ddffSdrh       { OP_Subtract,    1, 2,        1},
517ef8e986bSdrh       { OP_IfPos,       1, 8,        0},
518602b466eSdanielk1977       { OP_Integer,     0, 1,        0},                         /* 6 */
519ef8e986bSdrh       { OP_Noop,        0, 0,        0},
5203c84ddffSdrh       { OP_ResultRow,   1, 1,        0},
521c11d4f93Sdrh     };
5222ce1865dSdrh     VdbeOp *aOp;
523fb98264aSdrh     sqlite3VdbeUsesBtree(v, iDb);
52491cf71b0Sdanielk1977     if( !zRight ){
5253c84ddffSdrh       pParse->nMem += 2;
526dad300d8Sdrh       sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
5272ce1865dSdrh       aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
528dad300d8Sdrh       if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
5292ce1865dSdrh       aOp[0].p1 = iDb;
5302ce1865dSdrh       aOp[1].p1 = iDb;
5312ce1865dSdrh       aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
532905793e2Sdrh     }else{
533d50ffc41Sdrh       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
53491cf71b0Sdanielk1977       sqlite3BeginWriteOperation(pParse, 0, iDb);
5351861afcdSdrh       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
5362120608eSdrh       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
53714db2665Sdanielk1977       pDb->pSchema->cache_size = size;
53814db2665Sdanielk1977       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
539c11d4f93Sdrh     }
5409ccd8659Sdrh     break;
5419ccd8659Sdrh   }
542e73c9149Sdrh #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
543c11d4f93Sdrh 
544e73c9149Sdrh #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
545c11d4f93Sdrh   /*
5469b0cf34fSdrh   **  PRAGMA [schema.]page_size
5479b0cf34fSdrh   **  PRAGMA [schema.]page_size=N
54890f5ecb3Sdrh   **
54990f5ecb3Sdrh   ** The first form reports the current setting for the
55090f5ecb3Sdrh   ** database page size in bytes.  The second form sets the
55190f5ecb3Sdrh   ** database page size value.  The value can only be set if
55290f5ecb3Sdrh   ** the database has not yet been created.
55390f5ecb3Sdrh   */
5549ccd8659Sdrh   case PragTyp_PAGE_SIZE: {
55590f5ecb3Sdrh     Btree *pBt = pDb->pBt;
556d2cb50b7Sdrh     assert( pBt!=0 );
55790f5ecb3Sdrh     if( !zRight ){
558d2cb50b7Sdrh       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
559c232aca1Sdrh       returnSingleInt(v, size);
56090f5ecb3Sdrh     }else{
561992772c8Sdanielk1977       /* Malloc may fail when setting the page-size, as there is an internal
562992772c8Sdanielk1977       ** buffer that the pager module resizes using sqlite3_realloc().
563992772c8Sdanielk1977       */
56460ac3f42Sdrh       db->nextPagesize = sqlite3Atoi(zRight);
565e937df81Sdrh       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
5664a642b60Sdrh         sqlite3OomFault(db);
567992772c8Sdanielk1977       }
56890f5ecb3Sdrh     }
5699ccd8659Sdrh     break;
5709ccd8659Sdrh   }
57141483468Sdanielk1977 
57241483468Sdanielk1977   /*
5739b0cf34fSdrh   **  PRAGMA [schema.]secure_delete
574a5907a86Sdrh   **  PRAGMA [schema.]secure_delete=ON/OFF/FAST
5755b47efa6Sdrh   **
5765b47efa6Sdrh   ** The first form reports the current setting for the
5775b47efa6Sdrh   ** secure_delete flag.  The second form changes the secure_delete
5785b47efa6Sdrh   ** flag setting and reports the new value.
5795b47efa6Sdrh   */
5809ccd8659Sdrh   case PragTyp_SECURE_DELETE: {
5815b47efa6Sdrh     Btree *pBt = pDb->pBt;
5825b47efa6Sdrh     int b = -1;
5835b47efa6Sdrh     assert( pBt!=0 );
5845b47efa6Sdrh     if( zRight ){
585a5907a86Sdrh       if( sqlite3_stricmp(zRight, "fast")==0 ){
586a5907a86Sdrh         b = 2;
587a5907a86Sdrh       }else{
58838d9c612Sdrh         b = sqlite3GetBoolean(zRight, 0);
5895b47efa6Sdrh       }
590a5907a86Sdrh     }
591af034ed6Sdrh     if( pId2->n==0 && b>=0 ){
592af034ed6Sdrh       int ii;
593af034ed6Sdrh       for(ii=0; ii<db->nDb; ii++){
594af034ed6Sdrh         sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
595af034ed6Sdrh       }
596af034ed6Sdrh     }
5975b47efa6Sdrh     b = sqlite3BtreeSecureDelete(pBt, b);
598c232aca1Sdrh     returnSingleInt(v, b);
5999ccd8659Sdrh     break;
6009ccd8659Sdrh   }
6015b47efa6Sdrh 
6025b47efa6Sdrh   /*
6039b0cf34fSdrh   **  PRAGMA [schema.]max_page_count
6049b0cf34fSdrh   **  PRAGMA [schema.]max_page_count=N
60560ac3f42Sdrh   **
60660ac3f42Sdrh   ** The first form reports the current setting for the
60760ac3f42Sdrh   ** maximum number of pages in the database file.  The
60860ac3f42Sdrh   ** second form attempts to change this setting.  Both
60960ac3f42Sdrh   ** forms return the current setting.
61060ac3f42Sdrh   **
611e73c9149Sdrh   ** The absolute value of N is used.  This is undocumented and might
612e73c9149Sdrh   ** change.  The only purpose is to provide an easy way to test
613e73c9149Sdrh   ** the sqlite3AbsInt32() function.
614e73c9149Sdrh   **
6159b0cf34fSdrh   **  PRAGMA [schema.]page_count
61659a93791Sdanielk1977   **
61759a93791Sdanielk1977   ** Return the number of pages in the specified database.
61859a93791Sdanielk1977   */
6199ccd8659Sdrh   case PragTyp_PAGE_COUNT: {
62059a93791Sdanielk1977     int iReg;
621e9261dbdSdrh     i64 x = 0;
62259a93791Sdanielk1977     sqlite3CodeVerifySchema(pParse, iDb);
62359a93791Sdanielk1977     iReg = ++pParse->nMem;
624c522731bSdrh     if( sqlite3Tolower(zLeft[0])=='p' ){
62559a93791Sdanielk1977       sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
62660ac3f42Sdrh     }else{
627e9261dbdSdrh       if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){
628e9261dbdSdrh         if( x<0 ) x = 0;
629e9261dbdSdrh         else if( x>0xfffffffe ) x = 0xfffffffe;
630e9261dbdSdrh       }else{
631e9261dbdSdrh         x = 0;
632e9261dbdSdrh       }
633e9261dbdSdrh       sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x);
63460ac3f42Sdrh     }
63559a93791Sdanielk1977     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
6369ccd8659Sdrh     break;
6379ccd8659Sdrh   }
63859a93791Sdanielk1977 
63959a93791Sdanielk1977   /*
6409b0cf34fSdrh   **  PRAGMA [schema.]locking_mode
6419b0cf34fSdrh   **  PRAGMA [schema.]locking_mode = (normal|exclusive)
64241483468Sdanielk1977   */
6439ccd8659Sdrh   case PragTyp_LOCKING_MODE: {
64441483468Sdanielk1977     const char *zRet = "normal";
64541483468Sdanielk1977     int eMode = getLockingMode(zRight);
64641483468Sdanielk1977 
64741483468Sdanielk1977     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
64841483468Sdanielk1977       /* Simple "PRAGMA locking_mode;" statement. This is a query for
64941483468Sdanielk1977       ** the current default locking mode (which may be different to
65041483468Sdanielk1977       ** the locking-mode of the main database).
65141483468Sdanielk1977       */
65241483468Sdanielk1977       eMode = db->dfltLockMode;
65341483468Sdanielk1977     }else{
65441483468Sdanielk1977       Pager *pPager;
65541483468Sdanielk1977       if( pId2->n==0 ){
65641483468Sdanielk1977         /* This indicates that no database name was specified as part
65741483468Sdanielk1977         ** of the PRAGMA command. In this case the locking-mode must be
65841483468Sdanielk1977         ** set on all attached databases, as well as the main db file.
65941483468Sdanielk1977         **
66041483468Sdanielk1977         ** Also, the sqlite3.dfltLockMode variable is set so that
66141483468Sdanielk1977         ** any subsequently attached databases also use the specified
66241483468Sdanielk1977         ** locking mode.
66341483468Sdanielk1977         */
66441483468Sdanielk1977         int ii;
66541483468Sdanielk1977         assert(pDb==&db->aDb[0]);
66641483468Sdanielk1977         for(ii=2; ii<db->nDb; ii++){
66741483468Sdanielk1977           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
66841483468Sdanielk1977           sqlite3PagerLockingMode(pPager, eMode);
66941483468Sdanielk1977         }
6704f21c4afSdrh         db->dfltLockMode = (u8)eMode;
67141483468Sdanielk1977       }
67241483468Sdanielk1977       pPager = sqlite3BtreePager(pDb->pBt);
67341483468Sdanielk1977       eMode = sqlite3PagerLockingMode(pPager, eMode);
67441483468Sdanielk1977     }
67541483468Sdanielk1977 
6769ccd8659Sdrh     assert( eMode==PAGER_LOCKINGMODE_NORMAL
6779ccd8659Sdrh             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
67841483468Sdanielk1977     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
67941483468Sdanielk1977       zRet = "exclusive";
68041483468Sdanielk1977     }
681c232aca1Sdrh     returnSingleText(v, zRet);
6829ccd8659Sdrh     break;
6839ccd8659Sdrh   }
6843b02013eSdrh 
6853b02013eSdrh   /*
6869b0cf34fSdrh   **  PRAGMA [schema.]journal_mode
6879b0cf34fSdrh   **  PRAGMA [schema.]journal_mode =
6883ebaee96Sdrh   **                      (delete|persist|off|truncate|memory|wal|off)
6893b02013eSdrh   */
6909ccd8659Sdrh   case PragTyp_JOURNAL_MODE: {
691e04dc88bSdan     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
692c6b2a0ffSdrh     int ii;           /* Loop counter */
693e04dc88bSdan 
6943b02013eSdrh     if( zRight==0 ){
695c6b2a0ffSdrh       /* If there is no "=MODE" part of the pragma, do a query for the
696c6b2a0ffSdrh       ** current mode */
6973b02013eSdrh       eMode = PAGER_JOURNALMODE_QUERY;
6983b02013eSdrh     }else{
699e04dc88bSdan       const char *zMode;
700ea678832Sdrh       int n = sqlite3Strlen30(zRight);
701f77e2ac2Sdrh       for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
702e04dc88bSdan         if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
703e04dc88bSdan       }
704e04dc88bSdan       if( !zMode ){
705c6b2a0ffSdrh         /* If the "=MODE" part does not match any known journal mode,
706c6b2a0ffSdrh         ** then do a query */
707e04dc88bSdan         eMode = PAGER_JOURNALMODE_QUERY;
7083b02013eSdrh       }
7096c35b306Sdrh       if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
7106c35b306Sdrh         /* Do not allow journal-mode "OFF" in defensive since the database
7116c35b306Sdrh         ** can become corrupted using ordinary SQL when the journal is off */
7126c35b306Sdrh         eMode = PAGER_JOURNALMODE_QUERY;
7136c35b306Sdrh       }
7143b02013eSdrh     }
715c6b2a0ffSdrh     if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
716c6b2a0ffSdrh       /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
717c6b2a0ffSdrh       iDb = 0;
718c6b2a0ffSdrh       pId2->n = 1;
7193b02013eSdrh     }
720e04dc88bSdan     for(ii=db->nDb-1; ii>=0; ii--){
721e04dc88bSdan       if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
722e04dc88bSdan         sqlite3VdbeUsesBtree(v, ii);
723e04dc88bSdan         sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
7243b02013eSdrh       }
725e04dc88bSdan     }
7263b02013eSdrh     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
7279ccd8659Sdrh     break;
7289ccd8659Sdrh   }
729b53e4960Sdanielk1977 
730b53e4960Sdanielk1977   /*
7319b0cf34fSdrh   **  PRAGMA [schema.]journal_size_limit
7329b0cf34fSdrh   **  PRAGMA [schema.]journal_size_limit=N
733b53e4960Sdanielk1977   **
734a9e364f0Sdrh   ** Get or set the size limit on rollback journal files.
735b53e4960Sdanielk1977   */
7369ccd8659Sdrh   case PragTyp_JOURNAL_SIZE_LIMIT: {
737b53e4960Sdanielk1977     Pager *pPager = sqlite3BtreePager(pDb->pBt);
738b53e4960Sdanielk1977     i64 iLimit = -2;
739b53e4960Sdanielk1977     if( zRight ){
7409296c18aSdrh       sqlite3DecOrHexToI64(zRight, &iLimit);
7413c713646Sdrh       if( iLimit<-1 ) iLimit = -1;
742b53e4960Sdanielk1977     }
743b53e4960Sdanielk1977     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
744c232aca1Sdrh     returnSingleInt(v, iLimit);
7459ccd8659Sdrh     break;
7469ccd8659Sdrh   }
747b53e4960Sdanielk1977 
74813d7042aSdrh #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
74990f5ecb3Sdrh 
75090f5ecb3Sdrh   /*
7519b0cf34fSdrh   **  PRAGMA [schema.]auto_vacuum
7529b0cf34fSdrh   **  PRAGMA [schema.]auto_vacuum=N
753951af805Sdanielk1977   **
754a9e364f0Sdrh   ** Get or set the value of the database 'auto-vacuum' parameter.
755a9e364f0Sdrh   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
756951af805Sdanielk1977   */
757951af805Sdanielk1977 #ifndef SQLITE_OMIT_AUTOVACUUM
7589ccd8659Sdrh   case PragTyp_AUTO_VACUUM: {
759951af805Sdanielk1977     Btree *pBt = pDb->pBt;
760d2cb50b7Sdrh     assert( pBt!=0 );
761951af805Sdanielk1977     if( !zRight ){
762c232aca1Sdrh       returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
763951af805Sdanielk1977     }else{
764dddbcdccSdanielk1977       int eAuto = getAutoVacuum(zRight);
765d2cb50b7Sdrh       assert( eAuto>=0 && eAuto<=2 );
7664f21c4afSdrh       db->nextAutovac = (u8)eAuto;
76727b1f95fSdanielk1977       /* Call SetAutoVacuum() to set initialize the internal auto and
76827b1f95fSdanielk1977       ** incr-vacuum flags. This is required in case this connection
76927b1f95fSdanielk1977       ** creates the database file. It is important that it is created
77027b1f95fSdanielk1977       ** as an auto-vacuum capable db.
77127b1f95fSdanielk1977       */
77270708600Sdrh       rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
77327b1f95fSdanielk1977       if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
77427b1f95fSdanielk1977         /* When setting the auto_vacuum mode to either "full" or
77527b1f95fSdanielk1977         ** "incremental", write the value of meta[6] in the database
77627b1f95fSdanielk1977         ** file. Before writing to meta[6], check that meta[3] indicates
77727b1f95fSdanielk1977         ** that this really is an auto-vacuum capable database.
77827b1f95fSdanielk1977         */
779b06a4ec1Sdrh         static const int iLn = VDBE_OFFSET_LINENO(2);
78027b1f95fSdanielk1977         static const VdbeOpList setMeta6[] = {
78127b1f95fSdanielk1977           { OP_Transaction,    0,         1,                 0},    /* 0 */
7820d19f7acSdanielk1977           { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
7831db639ceSdrh           { OP_If,             1,         0,                 0},    /* 2 */
78427b1f95fSdanielk1977           { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
7851861afcdSdrh           { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 0},    /* 4 */
78627b1f95fSdanielk1977         };
7872ce1865dSdrh         VdbeOp *aOp;
7882ce1865dSdrh         int iAddr = sqlite3VdbeCurrentAddr(v);
789dad300d8Sdrh         sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
7902ce1865dSdrh         aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
791dad300d8Sdrh         if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
7922ce1865dSdrh         aOp[0].p1 = iDb;
7932ce1865dSdrh         aOp[1].p1 = iDb;
7942ce1865dSdrh         aOp[2].p2 = iAddr+4;
7951861afcdSdrh         aOp[4].p1 = iDb;
7961861afcdSdrh         aOp[4].p3 = eAuto - 1;
797fb98264aSdrh         sqlite3VdbeUsesBtree(v, iDb);
79827b1f95fSdanielk1977       }
799dddbcdccSdanielk1977     }
8009ccd8659Sdrh     break;
8019ccd8659Sdrh   }
802951af805Sdanielk1977 #endif
803951af805Sdanielk1977 
804ca5557f9Sdrh   /*
8059b0cf34fSdrh   **  PRAGMA [schema.]incremental_vacuum(N)
806ca5557f9Sdrh   **
807ca5557f9Sdrh   ** Do N steps of incremental vacuuming on a database.
808ca5557f9Sdrh   */
809ca5557f9Sdrh #ifndef SQLITE_OMIT_AUTOVACUUM
8109ccd8659Sdrh   case PragTyp_INCREMENTAL_VACUUM: {
81100946d79Sdrh     int iLimit = 0, addr;
812ca5557f9Sdrh     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
813ca5557f9Sdrh       iLimit = 0x7fffffff;
814ca5557f9Sdrh     }
815ca5557f9Sdrh     sqlite3BeginWriteOperation(pParse, 0, iDb);
8164c583128Sdrh     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
817688852abSdrh     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
8182d401ab8Sdrh     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
8198558cde1Sdrh     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
820688852abSdrh     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
821ca5557f9Sdrh     sqlite3VdbeJumpHere(v, addr);
8229ccd8659Sdrh     break;
8239ccd8659Sdrh   }
824ca5557f9Sdrh #endif
825ca5557f9Sdrh 
82613d7042aSdrh #ifndef SQLITE_OMIT_PAGER_PRAGMAS
827951af805Sdanielk1977   /*
8289b0cf34fSdrh   **  PRAGMA [schema.]cache_size
8299b0cf34fSdrh   **  PRAGMA [schema.]cache_size=N
830c11d4f93Sdrh   **
831c11d4f93Sdrh   ** The first form reports the current local setting for the
8323b42abb3Sdrh   ** page cache size. The second form sets the local
8333b42abb3Sdrh   ** page cache size value.  If N is positive then that is the
8343b42abb3Sdrh   ** number of pages in the cache.  If N is negative, then the
8353b42abb3Sdrh   ** number of pages is adjusted so that the cache uses -N kibibytes
8363b42abb3Sdrh   ** of memory.
837c11d4f93Sdrh   */
8389ccd8659Sdrh   case PragTyp_CACHE_SIZE: {
8392120608eSdrh     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
84091cf71b0Sdanielk1977     if( !zRight ){
841c232aca1Sdrh       returnSingleInt(v, pDb->pSchema->cache_size);
842c11d4f93Sdrh     }else{
8433b42abb3Sdrh       int size = sqlite3Atoi(zRight);
84414db2665Sdanielk1977       pDb->pSchema->cache_size = size;
84514db2665Sdanielk1977       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
846c11d4f93Sdrh     }
8479ccd8659Sdrh     break;
8489ccd8659Sdrh   }
849c11d4f93Sdrh 
850c11d4f93Sdrh   /*
8519b0cf34fSdrh   **  PRAGMA [schema.]cache_spill
8529b0cf34fSdrh   **  PRAGMA cache_spill=BOOLEAN
8539b0cf34fSdrh   **  PRAGMA [schema.]cache_spill=N
8549b0cf34fSdrh   **
8559b0cf34fSdrh   ** The first form reports the current local setting for the
8569b0cf34fSdrh   ** page cache spill size. The second form turns cache spill on
8579b0cf34fSdrh   ** or off.  When turnning cache spill on, the size is set to the
8589b0cf34fSdrh   ** current cache_size.  The third form sets a spill size that
8599b0cf34fSdrh   ** may be different form the cache size.
8609b0cf34fSdrh   ** If N is positive then that is the
8619b0cf34fSdrh   ** number of pages in the cache.  If N is negative, then the
8629b0cf34fSdrh   ** number of pages is adjusted so that the cache uses -N kibibytes
8639b0cf34fSdrh   ** of memory.
8649b0cf34fSdrh   **
8659b0cf34fSdrh   ** If the number of cache_spill pages is less then the number of
8669b0cf34fSdrh   ** cache_size pages, no spilling occurs until the page count exceeds
8679b0cf34fSdrh   ** the number of cache_size pages.
8689b0cf34fSdrh   **
8699b0cf34fSdrh   ** The cache_spill=BOOLEAN setting applies to all attached schemas,
8709b0cf34fSdrh   ** not just the schema specified.
8719b0cf34fSdrh   */
8729b0cf34fSdrh   case PragTyp_CACHE_SPILL: {
8739b0cf34fSdrh     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
8749b0cf34fSdrh     if( !zRight ){
875c232aca1Sdrh       returnSingleInt(v,
8769b0cf34fSdrh          (db->flags & SQLITE_CacheSpill)==0 ? 0 :
8779b0cf34fSdrh             sqlite3BtreeSetSpillSize(pDb->pBt,0));
8789b0cf34fSdrh     }else{
8794f9c8ec6Sdrh       int size = 1;
8809b0cf34fSdrh       if( sqlite3GetInt32(zRight, &size) ){
8819b0cf34fSdrh         sqlite3BtreeSetSpillSize(pDb->pBt, size);
8829b0cf34fSdrh       }
8834f9c8ec6Sdrh       if( sqlite3GetBoolean(zRight, size!=0) ){
8849b0cf34fSdrh         db->flags |= SQLITE_CacheSpill;
8859b0cf34fSdrh       }else{
886d5b44d60Sdrh         db->flags &= ~(u64)SQLITE_CacheSpill;
8879b0cf34fSdrh       }
8884f9c8ec6Sdrh       setAllPagerFlags(db);
8899b0cf34fSdrh     }
8909b0cf34fSdrh     break;
8919b0cf34fSdrh   }
8929b0cf34fSdrh 
8939b0cf34fSdrh   /*
8949b0cf34fSdrh   **  PRAGMA [schema.]mmap_size(N)
8955d8a1372Sdan   **
8960d0614bdSdrh   ** Used to set mapping size limit. The mapping size limit is
8975d8a1372Sdan   ** used to limit the aggregate size of all memory mapped regions of the
8985d8a1372Sdan   ** database file. If this parameter is set to zero, then memory mapping
899a1f42c7cSdrh   ** is not used at all.  If N is negative, then the default memory map
9009b4c59faSdrh   ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
901a1f42c7cSdrh   ** The parameter N is measured in bytes.
9025d8a1372Sdan   **
9030d0614bdSdrh   ** This value is advisory.  The underlying VFS is free to memory map
9040d0614bdSdrh   ** as little or as much as it wants.  Except, if N is set to 0 then the
9050d0614bdSdrh   ** upper layers will never invoke the xFetch interfaces to the VFS.
9065d8a1372Sdan   */
9079ccd8659Sdrh   case PragTyp_MMAP_SIZE: {
9089b4c59faSdrh     sqlite3_int64 sz;
909e98844f7Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
9105d8a1372Sdan     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9110d0614bdSdrh     if( zRight ){
912a1f42c7cSdrh       int ii;
9139296c18aSdrh       sqlite3DecOrHexToI64(zRight, &sz);
9149b4c59faSdrh       if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
9159b4c59faSdrh       if( pId2->n==0 ) db->szMmap = sz;
916a1f42c7cSdrh       for(ii=db->nDb-1; ii>=0; ii--){
917a1f42c7cSdrh         if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
9189b4c59faSdrh           sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
919a1f42c7cSdrh         }
920a1f42c7cSdrh       }
9215d8a1372Sdan     }
9229b4c59faSdrh     sz = -1;
9233719f5f6Sdan     rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
924e98844f7Smistachkin #else
9259b4c59faSdrh     sz = 0;
926e98844f7Smistachkin     rc = SQLITE_OK;
927188d4884Sdrh #endif
9283719f5f6Sdan     if( rc==SQLITE_OK ){
929c232aca1Sdrh       returnSingleInt(v, sz);
9303719f5f6Sdan     }else if( rc!=SQLITE_NOTFOUND ){
9313719f5f6Sdan       pParse->nErr++;
9323719f5f6Sdan       pParse->rc = rc;
93334f74903Sdrh     }
9349ccd8659Sdrh     break;
9359ccd8659Sdrh   }
9365d8a1372Sdan 
9375d8a1372Sdan   /*
93890f5ecb3Sdrh   **   PRAGMA temp_store
93990f5ecb3Sdrh   **   PRAGMA temp_store = "default"|"memory"|"file"
94090f5ecb3Sdrh   **
94190f5ecb3Sdrh   ** Return or set the local value of the temp_store flag.  Changing
94290f5ecb3Sdrh   ** the local value does not make changes to the disk file and the default
94390f5ecb3Sdrh   ** value will be restored the next time the database is opened.
94490f5ecb3Sdrh   **
94590f5ecb3Sdrh   ** Note that it is possible for the library compile-time options to
94690f5ecb3Sdrh   ** override this setting
94790f5ecb3Sdrh   */
9489ccd8659Sdrh   case PragTyp_TEMP_STORE: {
94990f5ecb3Sdrh     if( !zRight ){
950c232aca1Sdrh       returnSingleInt(v, db->temp_store);
95190f5ecb3Sdrh     }else{
95290f5ecb3Sdrh       changeTempStorage(pParse, zRight);
95390f5ecb3Sdrh     }
9549ccd8659Sdrh     break;
9559ccd8659Sdrh   }
95690f5ecb3Sdrh 
95790f5ecb3Sdrh   /*
9589a09a3caStpoindex   **   PRAGMA temp_store_directory
9599a09a3caStpoindex   **   PRAGMA temp_store_directory = ""|"directory_name"
9609a09a3caStpoindex   **
9619a09a3caStpoindex   ** Return or set the local value of the temp_store_directory flag.  Changing
9629a09a3caStpoindex   ** the value sets a specific directory to be used for temporary files.
9639a09a3caStpoindex   ** Setting to a null string reverts to the default temporary directory search.
9649a09a3caStpoindex   ** If temporary directory is changed, then invalidateTempStorage.
9659a09a3caStpoindex   **
9669a09a3caStpoindex   */
9679ccd8659Sdrh   case PragTyp_TEMP_STORE_DIRECTORY: {
96818a3a48dSdrh     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
9699a09a3caStpoindex     if( !zRight ){
970c232aca1Sdrh       returnSingleText(v, sqlite3_temp_directory);
9719a09a3caStpoindex     }else{
97278f82d1eSdrh #ifndef SQLITE_OMIT_WSD
973861f7456Sdanielk1977       if( zRight[0] ){
974861f7456Sdanielk1977         int res;
975fab1127bSdanielk1977         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
976fab1127bSdanielk1977         if( rc!=SQLITE_OK || res==0 ){
977268283bcSdrh           sqlite3ErrorMsg(pParse, "not a writable directory");
97818a3a48dSdrh           sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
979268283bcSdrh           goto pragma_out;
980268283bcSdrh         }
981861f7456Sdanielk1977       }
982b06a0b67Sdanielk1977       if( SQLITE_TEMP_STORE==0
983b06a0b67Sdanielk1977        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
984b06a0b67Sdanielk1977        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
985268283bcSdrh       ){
9869a09a3caStpoindex         invalidateTempStorage(pParse);
9879a09a3caStpoindex       }
98817435752Sdrh       sqlite3_free(sqlite3_temp_directory);
989268283bcSdrh       if( zRight[0] ){
990b975598eSdrh         sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
991268283bcSdrh       }else{
9929a09a3caStpoindex         sqlite3_temp_directory = 0;
9939a09a3caStpoindex       }
99478f82d1eSdrh #endif /* SQLITE_OMIT_WSD */
9959a09a3caStpoindex     }
99618a3a48dSdrh     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
9979ccd8659Sdrh     break;
9989ccd8659Sdrh   }
9999a09a3caStpoindex 
1000cc71645bSdrh #if SQLITE_OS_WIN
1001a112d140Smistachkin   /*
1002a112d140Smistachkin   **   PRAGMA data_store_directory
1003a112d140Smistachkin   **   PRAGMA data_store_directory = ""|"directory_name"
1004a112d140Smistachkin   **
1005a112d140Smistachkin   ** Return or set the local value of the data_store_directory flag.  Changing
1006a112d140Smistachkin   ** the value sets a specific directory to be used for database files that
1007a112d140Smistachkin   ** were specified with a relative pathname.  Setting to a null string reverts
1008a112d140Smistachkin   ** to the default database directory, which for database files specified with
1009a112d140Smistachkin   ** a relative path will probably be based on the current directory for the
1010a112d140Smistachkin   ** process.  Database file specified with an absolute path are not impacted
1011a112d140Smistachkin   ** by this setting, regardless of its value.
1012a112d140Smistachkin   **
1013a112d140Smistachkin   */
10149ccd8659Sdrh   case PragTyp_DATA_STORE_DIRECTORY: {
101518a3a48dSdrh     sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
1016a112d140Smistachkin     if( !zRight ){
1017c232aca1Sdrh       returnSingleText(v, sqlite3_data_directory);
1018a112d140Smistachkin     }else{
1019a112d140Smistachkin #ifndef SQLITE_OMIT_WSD
1020a112d140Smistachkin       if( zRight[0] ){
1021a112d140Smistachkin         int res;
1022a112d140Smistachkin         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
1023a112d140Smistachkin         if( rc!=SQLITE_OK || res==0 ){
1024a112d140Smistachkin           sqlite3ErrorMsg(pParse, "not a writable directory");
102518a3a48dSdrh           sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
1026a112d140Smistachkin           goto pragma_out;
1027a112d140Smistachkin         }
1028a112d140Smistachkin       }
1029a112d140Smistachkin       sqlite3_free(sqlite3_data_directory);
1030a112d140Smistachkin       if( zRight[0] ){
1031a112d140Smistachkin         sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
1032a112d140Smistachkin       }else{
1033a112d140Smistachkin         sqlite3_data_directory = 0;
1034a112d140Smistachkin       }
1035a112d140Smistachkin #endif /* SQLITE_OMIT_WSD */
1036a112d140Smistachkin     }
103718a3a48dSdrh     sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
10389ccd8659Sdrh     break;
10399ccd8659Sdrh   }
1040cc71645bSdrh #endif
1041a112d140Smistachkin 
1042d2cb50b7Sdrh #if SQLITE_ENABLE_LOCKING_STYLE
10439a09a3caStpoindex   /*
10449b0cf34fSdrh   **   PRAGMA [schema.]lock_proxy_file
10459b0cf34fSdrh   **   PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
1046aebf413dSaswift   **
1047aebf413dSaswift   ** Return or set the value of the lock_proxy_file flag.  Changing
1048aebf413dSaswift   ** the value sets a specific file to be used for database access locks.
1049aebf413dSaswift   **
1050aebf413dSaswift   */
10519ccd8659Sdrh   case PragTyp_LOCK_PROXY_FILE: {
1052aebf413dSaswift     if( !zRight ){
1053aebf413dSaswift       Pager *pPager = sqlite3BtreePager(pDb->pBt);
1054aebf413dSaswift       char *proxy_file_path = NULL;
1055aebf413dSaswift       sqlite3_file *pFile = sqlite3PagerFile(pPager);
1056c02372ceSdrh       sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
1057aebf413dSaswift                            &proxy_file_path);
1058c232aca1Sdrh       returnSingleText(v, proxy_file_path);
1059aebf413dSaswift     }else{
1060aebf413dSaswift       Pager *pPager = sqlite3BtreePager(pDb->pBt);
1061aebf413dSaswift       sqlite3_file *pFile = sqlite3PagerFile(pPager);
1062aebf413dSaswift       int res;
1063aebf413dSaswift       if( zRight[0] ){
1064aebf413dSaswift         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1065aebf413dSaswift                                      zRight);
1066aebf413dSaswift       } else {
1067aebf413dSaswift         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
1068aebf413dSaswift                                      NULL);
1069aebf413dSaswift       }
1070aebf413dSaswift       if( res!=SQLITE_OK ){
1071aebf413dSaswift         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
1072aebf413dSaswift         goto pragma_out;
1073aebf413dSaswift       }
1074aebf413dSaswift     }
10759ccd8659Sdrh     break;
10769ccd8659Sdrh   }
1077d2cb50b7Sdrh #endif /* SQLITE_ENABLE_LOCKING_STYLE */
1078aebf413dSaswift 
1079aebf413dSaswift   /*
10809b0cf34fSdrh   **   PRAGMA [schema.]synchronous
10816841b1cbSdrh   **   PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
1082c11d4f93Sdrh   **
1083c11d4f93Sdrh   ** Return or set the local value of the synchronous flag.  Changing
1084c11d4f93Sdrh   ** the local value does not make changes to the disk file and the
1085c11d4f93Sdrh   ** default value will be restored the next time the database is
1086c11d4f93Sdrh   ** opened.
1087c11d4f93Sdrh   */
10889ccd8659Sdrh   case PragTyp_SYNCHRONOUS: {
108991cf71b0Sdanielk1977     if( !zRight ){
1090c232aca1Sdrh       returnSingleInt(v, pDb->safety_level-1);
1091c11d4f93Sdrh     }else{
109291cf71b0Sdanielk1977       if( !db->autoCommit ){
109391cf71b0Sdanielk1977         sqlite3ErrorMsg(pParse,
109491cf71b0Sdanielk1977             "Safety level may not be changed inside a transaction");
10957553c828Sdrh       }else if( iDb!=1 ){
1096d99d2836Sdrh         int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
1097d99d2836Sdrh         if( iLevel==0 ) iLevel = 1;
1098d99d2836Sdrh         pDb->safety_level = iLevel;
109950a1a5aaSdrh         pDb->bSyncSet = 1;
1100d3605a4fSdrh         setAllPagerFlags(db);
110191cf71b0Sdanielk1977       }
1102c11d4f93Sdrh     }
11039ccd8659Sdrh     break;
11049ccd8659Sdrh   }
110513d7042aSdrh #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
1106c11d4f93Sdrh 
1107bf21627bSdrh #ifndef SQLITE_OMIT_FLAG_PRAGMAS
11089ccd8659Sdrh   case PragTyp_FLAG: {
11099ccd8659Sdrh     if( zRight==0 ){
1110c232aca1Sdrh       setPragmaResultColumnNames(v, pPragma);
1111c232aca1Sdrh       returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
11129ccd8659Sdrh     }else{
1113fd748c64Sdrh       u64 mask = pPragma->iArg;    /* Mask of bits to set or clear. */
11149ccd8659Sdrh       if( db->autoCommit==0 ){
11159ccd8659Sdrh         /* Foreign key support may not be enabled or disabled while not
11169ccd8659Sdrh         ** in auto-commit mode.  */
11179ccd8659Sdrh         mask &= ~(SQLITE_ForeignKeys);
11189ccd8659Sdrh       }
1119d4530979Sdrh #if SQLITE_USER_AUTHENTICATION
1120b2445d5eSdrh       if( db->auth.authLevel==UAUTH_User ){
1121d4530979Sdrh         /* Do not allow non-admin users to modify the schema arbitrarily */
1122d4530979Sdrh         mask &= ~(SQLITE_WriteSchema);
1123d4530979Sdrh       }
1124d4530979Sdrh #endif
11259ccd8659Sdrh 
11269ccd8659Sdrh       if( sqlite3GetBoolean(zRight, 0) ){
11272eeca204Sdrh         db->flags |= mask;
11289ccd8659Sdrh       }else{
11299ccd8659Sdrh         db->flags &= ~mask;
11309ccd8659Sdrh         if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
1131635e6a92Sdrh         if( (mask & SQLITE_WriteSchema)!=0
1132635e6a92Sdrh          && sqlite3_stricmp(zRight, "reset")==0
1133635e6a92Sdrh         ){
1134bc98f904Sdrh           /* IMP: R-60817-01178 If the argument is "RESET" then schema
1135bc98f904Sdrh           ** writing is disabled (as with "PRAGMA writable_schema=OFF") and,
1136bc98f904Sdrh           ** in addition, the schema is reloaded. */
1137635e6a92Sdrh           sqlite3ResetAllSchemasOfConnection(db);
1138635e6a92Sdrh         }
11399ccd8659Sdrh       }
11409ccd8659Sdrh 
11419ccd8659Sdrh       /* Many of the flag-pragmas modify the code generated by the SQL
11429ccd8659Sdrh       ** compiler (eg. count_changes). So add an opcode to expire all
11439ccd8659Sdrh       ** compiled SQL statements after modifying a pragma value.
11449ccd8659Sdrh       */
114501c736dbSdrh       sqlite3VdbeAddOp0(v, OP_Expire);
1146d3605a4fSdrh       setAllPagerFlags(db);
11479ccd8659Sdrh     }
11489ccd8659Sdrh     break;
11499ccd8659Sdrh   }
1150bf21627bSdrh #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
1151c11d4f93Sdrh 
115213d7042aSdrh #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
115391cf71b0Sdanielk1977   /*
115491cf71b0Sdanielk1977   **   PRAGMA table_info(<table>)
115591cf71b0Sdanielk1977   **
115691cf71b0Sdanielk1977   ** Return a single row for each column of the named table. The columns of
115791cf71b0Sdanielk1977   ** the returned data set are:
115891cf71b0Sdanielk1977   **
115991cf71b0Sdanielk1977   ** cid:        Column id (numbered from left to right, starting at 0)
116091cf71b0Sdanielk1977   ** name:       Column name
116191cf71b0Sdanielk1977   ** type:       Column declaration type.
116291cf71b0Sdanielk1977   ** notnull:    True if 'NOT NULL' is part of column declaration
116391cf71b0Sdanielk1977   ** dflt_value: The default value for the column, if any.
11643e6ac164Sdan   ** pk:         Non-zero for PK fields.
116591cf71b0Sdanielk1977   */
11669ccd8659Sdrh   case PragTyp_TABLE_INFO: if( zRight ){
1167c11d4f93Sdrh     Table *pTab;
1168a78d2c05Sdrh     sqlite3CodeVerifyNamedSchema(pParse, zDb);
11694d249e61Sdrh     pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
1170c11d4f93Sdrh     if( pTab ){
1171384b7fe2Sdrh       int i, k;
1172034ca14fSdanielk1977       int nHidden = 0;
1173f7eece6cSdrh       Column *pCol;
11744415628aSdrh       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
1175d7dc0a36Sdrh       pParse->nMem = 7;
11764adee20fSdanielk1977       sqlite3ViewGetColumnNames(pParse, pTab);
1177f7eece6cSdrh       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
1178ab3c5f26Sdrh         int isHidden = 0;
1179f9751074Sdrh         const Expr *pColExpr;
1180ab3c5f26Sdrh         if( pCol->colFlags & COLFLAG_NOINSERT ){
1181676fa25aSdrh           if( pPragma->iArg==0 ){
1182034ca14fSdanielk1977             nHidden++;
1183034ca14fSdanielk1977             continue;
1184034ca14fSdanielk1977           }
1185ab3c5f26Sdrh           if( pCol->colFlags & COLFLAG_VIRTUAL ){
1186ab3c5f26Sdrh             isHidden = 2;  /* GENERATED ALWAYS AS ... VIRTUAL */
1187c1431144Sdrh           }else if( pCol->colFlags & COLFLAG_STORED ){
1188ab3c5f26Sdrh             isHidden = 3;  /* GENERATED ALWAYS AS ... STORED */
1189c1431144Sdrh           }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
1190ab3c5f26Sdrh             isHidden = 1;  /* HIDDEN */
1191ab3c5f26Sdrh           }
1192034ca14fSdanielk1977         }
1193384b7fe2Sdrh         if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
1194384b7fe2Sdrh           k = 0;
1195384b7fe2Sdrh         }else if( pPk==0 ){
1196384b7fe2Sdrh           k = 1;
1197384b7fe2Sdrh         }else{
11981b678969Sdrh           for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
1199384b7fe2Sdrh         }
1200f9751074Sdrh         pColExpr = sqlite3ColumnExpr(pTab,pCol);
1201f9751074Sdrh         assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
12029d43db5bSdrh         assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue)
12039d43db5bSdrh                   || isHidden>=2 );
1204d7dc0a36Sdrh         sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
1205076e85f5Sdrh                i-nHidden,
1206cf9d36d1Sdrh                pCol->zCnName,
1207d7564865Sdrh                sqlite3ColumnType(pCol,""),
1208076e85f5Sdrh                pCol->notNull ? 1 : 0,
1209f9751074Sdrh                (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
1210d7dc0a36Sdrh                k,
1211d7dc0a36Sdrh                isHidden);
1212c11d4f93Sdrh       }
1213c11d4f93Sdrh     }
12149ccd8659Sdrh   }
12159ccd8659Sdrh   break;
1216c11d4f93Sdrh 
12172e50f670Sdrh   /*
12182e50f670Sdrh   **   PRAGMA table_list
12192e50f670Sdrh   **
12202e50f670Sdrh   ** Return a single row for each table, virtual table, or view in the
12212e50f670Sdrh   ** entire schema.
12222e50f670Sdrh   **
12232e50f670Sdrh   ** schema:     Name of attached database hold this table
12242e50f670Sdrh   ** name:       Name of the table itself
12252e50f670Sdrh   ** type:       "table", "view", "virtual", "shadow"
12262e50f670Sdrh   ** ncol:       Number of columns
12272e50f670Sdrh   ** wr:         True for a WITHOUT ROWID table
12282e50f670Sdrh   ** strict:     True for a STRICT table
12292e50f670Sdrh   */
12302e50f670Sdrh   case PragTyp_TABLE_LIST: {
12312e50f670Sdrh     int ii;
12322e50f670Sdrh     pParse->nMem = 6;
12332e50f670Sdrh     sqlite3CodeVerifyNamedSchema(pParse, zDb);
12342e50f670Sdrh     for(ii=0; ii<db->nDb; ii++){
12352e50f670Sdrh       HashElem *k;
12362e50f670Sdrh       Hash *pHash;
1237dc88b402Sdrh       int initNCol;
12382e50f670Sdrh       if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
1239dc88b402Sdrh 
1240dc88b402Sdrh       /* Ensure that the Table.nCol field is initialized for all views
1241dc88b402Sdrh       ** and virtual tables.  Each time we initialize a Table.nCol value
1242dc88b402Sdrh       ** for a table, that can potentially disrupt the hash table, so restart
1243dc88b402Sdrh       ** the initialization scan.
1244dc88b402Sdrh       */
12452e50f670Sdrh       pHash = &db->aDb[ii].pSchema->tblHash;
1246dc88b402Sdrh       initNCol = sqliteHashCount(pHash);
1247dc88b402Sdrh       while( initNCol-- ){
1248dc88b402Sdrh         for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){
1249dc88b402Sdrh           Table *pTab;
1250dc88b402Sdrh           if( k==0 ){ initNCol = 0; break; }
1251dc88b402Sdrh           pTab = sqliteHashData(k);
1252dc88b402Sdrh           if( pTab->nCol==0 ){
1253dc88b402Sdrh             char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
1254dc88b402Sdrh             if( zSql ){
1255dc88b402Sdrh               sqlite3_stmt *pDummy = 0;
1256dc88b402Sdrh               (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
1257dc88b402Sdrh               (void)sqlite3_finalize(pDummy);
1258dc88b402Sdrh               sqlite3DbFree(db, zSql);
1259dc88b402Sdrh             }
12600c7d3d39Sdrh             if( db->mallocFailed ){
12610c7d3d39Sdrh               sqlite3ErrorMsg(db->pParse, "out of memory");
12620c7d3d39Sdrh               db->pParse->rc = SQLITE_NOMEM_BKPT;
12630c7d3d39Sdrh             }
1264dc88b402Sdrh             pHash = &db->aDb[ii].pSchema->tblHash;
1265dc88b402Sdrh             break;
1266dc88b402Sdrh           }
1267dc88b402Sdrh         }
1268dc88b402Sdrh       }
1269dc88b402Sdrh 
12702e50f670Sdrh       for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){
12712e50f670Sdrh         Table *pTab = sqliteHashData(k);
12722e50f670Sdrh         const char *zType;
12732e50f670Sdrh         if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
12742e50f670Sdrh         if( IsView(pTab) ){
12752e50f670Sdrh           zType = "view";
12762e50f670Sdrh         }else if( IsVirtual(pTab) ){
12772e50f670Sdrh           zType = "virtual";
12782e50f670Sdrh         }else if( pTab->tabFlags & TF_Shadow ){
12792e50f670Sdrh           zType = "shadow";
12802e50f670Sdrh         }else{
12812e50f670Sdrh           zType = "table";
12822e50f670Sdrh         }
12832e50f670Sdrh         sqlite3VdbeMultiLoad(v, 1, "sssiii",
12842e50f670Sdrh            db->aDb[ii].zDbSName,
1285a4a871c2Sdrh            sqlite3PreferredTableName(pTab->zName),
12862e50f670Sdrh            zType,
12872e50f670Sdrh            pTab->nCol,
12882e50f670Sdrh            (pTab->tabFlags & TF_WithoutRowid)!=0,
12892e50f670Sdrh            (pTab->tabFlags & TF_Strict)!=0
12902e50f670Sdrh         );
12912e50f670Sdrh       }
12922e50f670Sdrh     }
12932e50f670Sdrh   }
12942e50f670Sdrh   break;
12952e50f670Sdrh 
129633bec3f5Sdrh #ifdef SQLITE_DEBUG
12973ef26156Sdrh   case PragTyp_STATS: {
12983ef26156Sdrh     Index *pIdx;
12993ef26156Sdrh     HashElem *i;
130033bec3f5Sdrh     pParse->nMem = 5;
13013ef26156Sdrh     sqlite3CodeVerifySchema(pParse, iDb);
13023ef26156Sdrh     for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
13033ef26156Sdrh       Table *pTab = sqliteHashData(i);
130433bec3f5Sdrh       sqlite3VdbeMultiLoad(v, 1, "ssiii",
1305a4a871c2Sdrh            sqlite3PreferredTableName(pTab->zName),
1306076e85f5Sdrh            0,
1307d566c951Sdrh            pTab->szTabRow,
130833bec3f5Sdrh            pTab->nRowLogEst,
130933bec3f5Sdrh            pTab->tabFlags);
13103ef26156Sdrh       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
131140cf27cbSdrh         sqlite3VdbeMultiLoad(v, 2, "siiiX",
1312076e85f5Sdrh            pIdx->zName,
1313d566c951Sdrh            pIdx->szIdxRow,
131433bec3f5Sdrh            pIdx->aiRowLogEst[0],
131533bec3f5Sdrh            pIdx->hasStat1);
131633bec3f5Sdrh         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
13173ef26156Sdrh       }
13183ef26156Sdrh     }
13193ef26156Sdrh   }
13203ef26156Sdrh   break;
132133bec3f5Sdrh #endif
13223ef26156Sdrh 
13239ccd8659Sdrh   case PragTyp_INDEX_INFO: if( zRight ){
1324c11d4f93Sdrh     Index *pIdx;
1325c11d4f93Sdrh     Table *pTab;
13262783e4bcSdrh     pIdx = sqlite3FindIndex(db, zRight, zDb);
13277b1904e3Sdrh     if( pIdx==0 ){
13287b1904e3Sdrh       /* If there is no index named zRight, check to see if there is a
13297b1904e3Sdrh       ** WITHOUT ROWID table named zRight, and if there is, show the
13307b1904e3Sdrh       ** structure of the PRIMARY KEY index for that table. */
13317b1904e3Sdrh       pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
13327b1904e3Sdrh       if( pTab && !HasRowid(pTab) ){
13337b1904e3Sdrh         pIdx = sqlite3PrimaryKeyIndex(pTab);
13347b1904e3Sdrh       }
13357b1904e3Sdrh     }
1336c11d4f93Sdrh     if( pIdx ){
13373c425484Sdan       int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1338c11d4f93Sdrh       int i;
13395e7028c2Sdrh       int mx;
13405e7028c2Sdrh       if( pPragma->iArg ){
13415e7028c2Sdrh         /* PRAGMA index_xinfo (newer version with more rows and columns) */
13425e7028c2Sdrh         mx = pIdx->nColumn;
1343c228be5bSdrh         pParse->nMem = 6;
13445e7028c2Sdrh       }else{
13455e7028c2Sdrh         /* PRAGMA index_info (legacy version) */
13465e7028c2Sdrh         mx = pIdx->nKeyCol;
13475e7028c2Sdrh         pParse->nMem = 3;
13485e7028c2Sdrh       }
13495e7028c2Sdrh       pTab = pIdx->pTable;
13503c425484Sdan       sqlite3CodeVerifySchema(pParse, iIdxDb);
1351c232aca1Sdrh       assert( pParse->nMem<=pPragma->nPragCName );
1352c228be5bSdrh       for(i=0; i<mx; i++){
1353bbbdc83bSdrh         i16 cnum = pIdx->aiColumn[i];
135440cf27cbSdrh         sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
1355cf9d36d1Sdrh                              cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
13565e7028c2Sdrh         if( pPragma->iArg ){
135740cf27cbSdrh           sqlite3VdbeMultiLoad(v, 4, "isiX",
1358076e85f5Sdrh             pIdx->aSortOrder[i],
1359076e85f5Sdrh             pIdx->azColl[i],
1360076e85f5Sdrh             i<pIdx->nKeyCol);
13615e7028c2Sdrh         }
13625e7028c2Sdrh         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
1363c11d4f93Sdrh       }
1364c11d4f93Sdrh     }
13659ccd8659Sdrh   }
13669ccd8659Sdrh   break;
1367c11d4f93Sdrh 
13689ccd8659Sdrh   case PragTyp_INDEX_LIST: if( zRight ){
1369c11d4f93Sdrh     Index *pIdx;
1370c11d4f93Sdrh     Table *pTab;
1371e13e9f54Sdrh     int i;
13722783e4bcSdrh     pTab = sqlite3FindTable(db, zRight, zDb);
1373c11d4f93Sdrh     if( pTab ){
13743c425484Sdan       int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1375c228be5bSdrh       pParse->nMem = 5;
13763c425484Sdan       sqlite3CodeVerifySchema(pParse, iTabDb);
13773ef26156Sdrh       for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
1378c228be5bSdrh         const char *azOrigin[] = { "c", "u", "pk" };
1379076e85f5Sdrh         sqlite3VdbeMultiLoad(v, 1, "isisi",
1380076e85f5Sdrh            i,
1381076e85f5Sdrh            pIdx->zName,
1382076e85f5Sdrh            IsUniqueIndex(pIdx),
1383076e85f5Sdrh            azOrigin[pIdx->idxType],
1384076e85f5Sdrh            pIdx->pPartIdxWhere!=0);
1385c11d4f93Sdrh       }
1386742f947bSdanielk1977     }
13879ccd8659Sdrh   }
13889ccd8659Sdrh   break;
1389c11d4f93Sdrh 
13909ccd8659Sdrh   case PragTyp_DATABASE_LIST: {
139113d7042aSdrh     int i;
13922d401ab8Sdrh     pParse->nMem = 3;
139313d7042aSdrh     for(i=0; i<db->nDb; i++){
139413d7042aSdrh       if( db->aDb[i].pBt==0 ) continue;
139569c33826Sdrh       assert( db->aDb[i].zDbSName!=0 );
1396076e85f5Sdrh       sqlite3VdbeMultiLoad(v, 1, "iss",
1397076e85f5Sdrh          i,
139869c33826Sdrh          db->aDb[i].zDbSName,
1399076e85f5Sdrh          sqlite3BtreeGetFilename(db->aDb[i].pBt));
140013d7042aSdrh     }
14019ccd8659Sdrh   }
14029ccd8659Sdrh   break;
140348af65aeSdanielk1977 
14049ccd8659Sdrh   case PragTyp_COLLATION_LIST: {
140548af65aeSdanielk1977     int i = 0;
140648af65aeSdanielk1977     HashElem *p;
14072d401ab8Sdrh     pParse->nMem = 2;
140848af65aeSdanielk1977     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
140948af65aeSdanielk1977       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
1410076e85f5Sdrh       sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
141148af65aeSdanielk1977     }
14129ccd8659Sdrh   }
14139ccd8659Sdrh   break;
1414ab53bb63Sdrh 
1415cc3f3d1fSdrh #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
1416ab53bb63Sdrh   case PragTyp_FUNCTION_LIST: {
1417ab53bb63Sdrh     int i;
1418ab53bb63Sdrh     HashElem *j;
1419ab53bb63Sdrh     FuncDef *p;
1420337ca519Sdrh     int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
142179d5bc80Sdrh     pParse->nMem = 6;
1422ab53bb63Sdrh     for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
1423ab53bb63Sdrh       for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
1424f9751074Sdrh         assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
1425337ca519Sdrh         pragmaFunclistLine(v, p, 1, showInternFunc);
1426ab53bb63Sdrh       }
1427ab53bb63Sdrh     }
1428ab53bb63Sdrh     for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
1429ab53bb63Sdrh       p = (FuncDef*)sqliteHashData(j);
1430f9751074Sdrh       assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
1431337ca519Sdrh       pragmaFunclistLine(v, p, 0, showInternFunc);
1432ab53bb63Sdrh     }
1433ab53bb63Sdrh   }
1434ab53bb63Sdrh   break;
1435ab53bb63Sdrh 
1436ab53bb63Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
1437ab53bb63Sdrh   case PragTyp_MODULE_LIST: {
1438ab53bb63Sdrh     HashElem *j;
1439ab53bb63Sdrh     pParse->nMem = 1;
1440ab53bb63Sdrh     for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
1441ab53bb63Sdrh       Module *pMod = (Module*)sqliteHashData(j);
1442ab53bb63Sdrh       sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
1443ab53bb63Sdrh     }
1444ab53bb63Sdrh   }
1445ab53bb63Sdrh   break;
1446ab53bb63Sdrh #endif /* SQLITE_OMIT_VIRTUALTABLE */
1447ab53bb63Sdrh 
14488ae11aacSdrh   case PragTyp_PRAGMA_LIST: {
14498ae11aacSdrh     int i;
14508ae11aacSdrh     for(i=0; i<ArraySize(aPragmaName); i++){
14518ae11aacSdrh       sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
14528ae11aacSdrh     }
14538ae11aacSdrh   }
14548ae11aacSdrh   break;
14558ae11aacSdrh #endif /* SQLITE_INTROSPECTION_PRAGMAS */
1456ab53bb63Sdrh 
145713d7042aSdrh #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
145813d7042aSdrh 
1459b7f9164eSdrh #ifndef SQLITE_OMIT_FOREIGN_KEY
14609ccd8659Sdrh   case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
146178100cc9Sdrh     FKey *pFK;
146278100cc9Sdrh     Table *pTab;
14632783e4bcSdrh     pTab = sqlite3FindTable(db, zRight, zDb);
146478b2fa86Sdrh     if( pTab && IsOrdinaryTable(pTab) ){
1465f38524d2Sdrh       pFK = pTab->u.tab.pFKey;
1466742f947bSdanielk1977       if( pFK ){
14673c425484Sdan         int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
146878100cc9Sdrh         int i = 0;
146950af3e1dSdanielk1977         pParse->nMem = 8;
14703c425484Sdan         sqlite3CodeVerifySchema(pParse, iTabDb);
147178100cc9Sdrh         while(pFK){
147278100cc9Sdrh           int j;
147378100cc9Sdrh           for(j=0; j<pFK->nCol; j++){
1474076e85f5Sdrh             sqlite3VdbeMultiLoad(v, 1, "iissssss",
1475076e85f5Sdrh                    i,
1476076e85f5Sdrh                    j,
1477076e85f5Sdrh                    pFK->zTo,
1478cf9d36d1Sdrh                    pTab->aCol[pFK->aCol[j].iFrom].zCnName,
1479076e85f5Sdrh                    pFK->aCol[j].zCol,
1480076e85f5Sdrh                    actionName(pFK->aAction[1]),  /* ON UPDATE */
1481076e85f5Sdrh                    actionName(pFK->aAction[0]),  /* ON DELETE */
1482076e85f5Sdrh                    "NONE");
148378100cc9Sdrh           }
148478100cc9Sdrh           ++i;
148578100cc9Sdrh           pFK = pFK->pNextFrom;
148678100cc9Sdrh         }
148778100cc9Sdrh       }
1488742f947bSdanielk1977     }
14899ccd8659Sdrh   }
14909ccd8659Sdrh   break;
1491b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
149278100cc9Sdrh 
14936c5b915fSdrh #ifndef SQLITE_OMIT_FOREIGN_KEY
149409ff9e16Sdan #ifndef SQLITE_OMIT_TRIGGER
14959ccd8659Sdrh   case PragTyp_FOREIGN_KEY_CHECK: {
1496613028b3Sdrh     FKey *pFK;             /* A foreign key constraint */
1497613028b3Sdrh     Table *pTab;           /* Child table contain "REFERENCES" keyword */
1498613028b3Sdrh     Table *pParent;        /* Parent table that child points to */
1499613028b3Sdrh     Index *pIdx;           /* Index in the parent table */
1500613028b3Sdrh     int i;                 /* Loop counter:  Foreign key number for pTab */
1501613028b3Sdrh     int j;                 /* Loop counter:  Field of the foreign key */
1502613028b3Sdrh     HashElem *k;           /* Loop counter:  Next table in schema */
1503613028b3Sdrh     int x;                 /* result variable */
1504613028b3Sdrh     int regResult;         /* 3 registers to hold a result row */
1505613028b3Sdrh     int regRow;            /* Registers to hold a row from pTab */
1506613028b3Sdrh     int addrTop;           /* Top of a loop checking foreign keys */
1507613028b3Sdrh     int addrOk;            /* Jump here if the key is OK */
15087d22a4d7Sdrh     int *aiCols;           /* child to parent column mapping */
15096c5b915fSdrh 
1510613028b3Sdrh     regResult = pParse->nMem+1;
15114b4b473aSdrh     pParse->nMem += 4;
1512613028b3Sdrh     regRow = ++pParse->nMem;
1513613028b3Sdrh     k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
1514613028b3Sdrh     while( k ){
1515613028b3Sdrh       if( zRight ){
1516613028b3Sdrh         pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
1517613028b3Sdrh         k = 0;
1518613028b3Sdrh       }else{
1519613028b3Sdrh         pTab = (Table*)sqliteHashData(k);
1520613028b3Sdrh         k = sqliteHashNext(k);
1521613028b3Sdrh       }
152278b2fa86Sdrh       if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
1523ec1650a2Sdrh       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1524ec1650a2Sdrh       zDb = db->aDb[iDb].zDbSName;
1525ec1650a2Sdrh       sqlite3CodeVerifySchema(pParse, iDb);
1526ec1650a2Sdrh       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
1527613028b3Sdrh       if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
1528ec1650a2Sdrh       sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
1529076e85f5Sdrh       sqlite3VdbeLoadString(v, regResult, pTab->zName);
153078b2fa86Sdrh       assert( IsOrdinaryTable(pTab) );
1531f38524d2Sdrh       for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
15325e87830fSdan         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
15335e87830fSdan         if( pParent==0 ) continue;
15346c5b915fSdrh         pIdx = 0;
1535ec1650a2Sdrh         sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
1536613028b3Sdrh         x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
15376c5b915fSdrh         if( x==0 ){
15386c5b915fSdrh           if( pIdx==0 ){
1539ec1650a2Sdrh             sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
15406c5b915fSdrh           }else{
1541ec1650a2Sdrh             sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
15422ec2fb22Sdrh             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
15436c5b915fSdrh           }
15446c5b915fSdrh         }else{
1545613028b3Sdrh           k = 0;
15466c5b915fSdrh           break;
15476c5b915fSdrh         }
15486c5b915fSdrh       }
15495e87830fSdan       assert( pParse->nErr>0 || pFK==0 );
1550613028b3Sdrh       if( pFK ) break;
1551613028b3Sdrh       if( pParse->nTab<i ) pParse->nTab = i;
1552688852abSdrh       addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
155378b2fa86Sdrh       assert( IsOrdinaryTable(pTab) );
1554f38524d2Sdrh       for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
15555e87830fSdan         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
15566c5b915fSdrh         pIdx = 0;
15577d22a4d7Sdrh         aiCols = 0;
15585e87830fSdan         if( pParent ){
15597d22a4d7Sdrh           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
1560d96e3821Sdrh           assert( x==0 || db->mallocFailed );
15615e87830fSdan         }
1562ec4ccdbcSdrh         addrOk = sqlite3VdbeMakeLabel(pParse);
15634bee5599Sdan 
15644bee5599Sdan         /* Generate code to read the child key values into registers
15654bee5599Sdan         ** regRow..regRow+n. If any of the child key values are NULL, this
15664bee5599Sdan         ** row cannot cause an FK violation. Jump directly to addrOk in
15674bee5599Sdan         ** this case. */
15684f16ff9dSdrh         if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
15696c5b915fSdrh         for(j=0; j<pFK->nCol; j++){
15704bee5599Sdan           int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
15714bee5599Sdan           sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
1572688852abSdrh           sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
15736c5b915fSdrh         }
15744bee5599Sdan 
15754bee5599Sdan         /* Generate code to query the parent index for a matching parent
15764bee5599Sdan         ** key. If a match is found, jump to addrOk. */
15774bee5599Sdan         if( pIdx ){
157836d2d090Sdrh           sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
1579e9107698Sdrh               sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
158036d2d090Sdrh           sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
1581688852abSdrh           VdbeCoverage(v);
15824bee5599Sdan         }else if( pParent ){
15834bee5599Sdan           int jmp = sqlite3VdbeCurrentAddr(v)+2;
15844bee5599Sdan           sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
15854bee5599Sdan           sqlite3VdbeGoto(v, addrOk);
1586d96e3821Sdrh           assert( pFK->nCol==1 || db->mallocFailed );
15876c5b915fSdrh         }
15884bee5599Sdan 
15894bee5599Sdan         /* Generate code to report an FK violation to the caller. */
1590940464b1Sdan         if( HasRowid(pTab) ){
15916c5b915fSdrh           sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
1592940464b1Sdan         }else{
1593940464b1Sdan           sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
1594940464b1Sdan         }
159540cf27cbSdrh         sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
15964b4b473aSdrh         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
15976c5b915fSdrh         sqlite3VdbeResolveLabel(v, addrOk);
15987d22a4d7Sdrh         sqlite3DbFree(db, aiCols);
15996c5b915fSdrh       }
1600688852abSdrh       sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
16016c5b915fSdrh       sqlite3VdbeJumpHere(v, addrTop);
16026c5b915fSdrh     }
16039ccd8659Sdrh   }
16049ccd8659Sdrh   break;
160509ff9e16Sdan #endif /* !defined(SQLITE_OMIT_TRIGGER) */
16066c5b915fSdrh #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
16076c5b915fSdrh 
160808652b5eSdrh #ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
160955ef4d97Sdrh   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
161055ef4d97Sdrh   ** used will be case sensitive or not depending on the RHS.
161155ef4d97Sdrh   */
16129ccd8659Sdrh   case PragTyp_CASE_SENSITIVE_LIKE: {
161355ef4d97Sdrh     if( zRight ){
161438d9c612Sdrh       sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
161555ef4d97Sdrh     }
16169ccd8659Sdrh   }
16179ccd8659Sdrh   break;
161808652b5eSdrh #endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
161955ef4d97Sdrh 
16201dcdbc06Sdrh #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
16211dcdbc06Sdrh # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
16221dcdbc06Sdrh #endif
16231dcdbc06Sdrh 
1624b7f9164eSdrh #ifndef SQLITE_OMIT_INTEGRITY_CHECK
16258b174f29Sdrh   /*    PRAGMA integrity_check
16268b174f29Sdrh   **    PRAGMA integrity_check(N)
16278b174f29Sdrh   **    PRAGMA quick_check
16288b174f29Sdrh   **    PRAGMA quick_check(N)
16298b174f29Sdrh   **
16308b174f29Sdrh   ** Verify the integrity of the database.
16318b174f29Sdrh   **
16328b174f29Sdrh   ** The "quick_check" is reduced version of
163341c58b78Sdanielk1977   ** integrity_check designed to detect most database corruption
16348b174f29Sdrh   ** without the overhead of cross-checking indexes.  Quick_check
16358b174f29Sdrh   ** is linear time wherease integrity_check is O(NlogN).
163617d2d592Sdrh   **
163717d2d592Sdrh   ** The maximum nubmer of errors is 100 by default.  A different default
163817d2d592Sdrh   ** can be specified using a numeric parameter N.
163917d2d592Sdrh   **
164017d2d592Sdrh   ** Or, the parameter N can be the name of a table.  In that case, only
164117d2d592Sdrh   ** the one table named is verified.  The freelist is only verified if
164217d2d592Sdrh   ** the named table is "sqlite_schema" (or one of its aliases).
164317d2d592Sdrh   **
164417d2d592Sdrh   ** All schemas are checked by default.  To check just a single
164517d2d592Sdrh   ** schema, use the form:
164617d2d592Sdrh   **
164717d2d592Sdrh   **      PRAGMA schema.integrity_check;
164841c58b78Sdanielk1977   */
16499ccd8659Sdrh   case PragTyp_INTEGRITY_CHECK: {
16501dcdbc06Sdrh     int i, j, addr, mxErr;
165117d2d592Sdrh     Table *pObjTab = 0;     /* Check only this one table, if not NULL */
1652ed717fe3Sdrh 
1653c522731bSdrh     int isQuick = (sqlite3Tolower(zLeft[0])=='q');
165441c58b78Sdanielk1977 
16555885e762Sdan     /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
16565885e762Sdan     ** then iDb is set to the index of the database identified by <db>.
16575885e762Sdan     ** In this case, the integrity of database iDb only is verified by
16585885e762Sdan     ** the VDBE created below.
16595885e762Sdan     **
16605885e762Sdan     ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
16615885e762Sdan     ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
16625885e762Sdan     ** to -1 here, to indicate that the VDBE should verify the integrity
16635885e762Sdan     ** of all attached databases.  */
16645885e762Sdan     assert( iDb>=0 );
16655885e762Sdan     assert( iDb==0 || pId2->z );
16665885e762Sdan     if( pId2->z==0 ) iDb = -1;
16675885e762Sdan 
1668ed717fe3Sdrh     /* Initialize the VDBE program */
16692d401ab8Sdrh     pParse->nMem = 6;
16701dcdbc06Sdrh 
16711dcdbc06Sdrh     /* Set the maximum error count */
16721dcdbc06Sdrh     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
16731dcdbc06Sdrh     if( zRight ){
167417d2d592Sdrh       if( sqlite3GetInt32(zRight, &mxErr) ){
16751dcdbc06Sdrh         if( mxErr<=0 ){
16761dcdbc06Sdrh           mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
16771dcdbc06Sdrh         }
167817d2d592Sdrh       }else{
167917d2d592Sdrh         pObjTab = sqlite3LocateTable(pParse, 0, zRight,
168017d2d592Sdrh                       iDb>=0 ? db->aDb[iDb].zDbSName : 0);
168117d2d592Sdrh       }
16821dcdbc06Sdrh     }
168366accfc5Sdrh     sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
1684ed717fe3Sdrh 
1685ed717fe3Sdrh     /* Do an integrity check on each database file */
1686ed717fe3Sdrh     for(i=0; i<db->nDb; i++){
16879ecd7086Sdrh       HashElem *x;     /* For looping over tables in the schema */
16889ecd7086Sdrh       Hash *pTbls;     /* Set of all tables in the schema */
16899ecd7086Sdrh       int *aRoot;      /* Array of root page numbers of all btrees */
16909ecd7086Sdrh       int cnt = 0;     /* Number of entries in aRoot[] */
16919ecd7086Sdrh       int mxIdx = 0;   /* Maximum number of indexes for any table */
1692ed717fe3Sdrh 
169353c0f748Sdanielk1977       if( OMIT_TEMPDB && i==1 ) continue;
16945885e762Sdan       if( iDb>=0 && i!=iDb ) continue;
169553c0f748Sdanielk1977 
169680242055Sdrh       sqlite3CodeVerifySchema(pParse, i);
169780242055Sdrh 
1698ed717fe3Sdrh       /* Do an integrity check of the B-Tree
16992d401ab8Sdrh       **
170098968b22Sdrh       ** Begin by finding the root pages numbers
17012d401ab8Sdrh       ** for all tables and indices in the database.
1702ed717fe3Sdrh       */
17035885e762Sdan       assert( sqlite3SchemaMutexHeld(db, i, 0) );
1704da184236Sdanielk1977       pTbls = &db->aDb[i].pSchema->tblHash;
170598968b22Sdrh       for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
17069ecd7086Sdrh         Table *pTab = sqliteHashData(x);  /* Current table */
17079ecd7086Sdrh         Index *pIdx;                      /* An index on pTab */
17089ecd7086Sdrh         int nIdx;                         /* Number of indexes on pTab */
170917d2d592Sdrh         if( pObjTab && pObjTab!=pTab ) continue;
171098968b22Sdrh         if( HasRowid(pTab) ) cnt++;
1711bb9b5f26Sdrh         for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
1712bb9b5f26Sdrh         if( nIdx>mxIdx ) mxIdx = nIdx;
17136fbe41acSdrh       }
171417d2d592Sdrh       if( cnt==0 ) continue;
171517d2d592Sdrh       if( pObjTab ) cnt++;
171698968b22Sdrh       aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
171798968b22Sdrh       if( aRoot==0 ) break;
171817d2d592Sdrh       cnt = 0;
171917d2d592Sdrh       if( pObjTab ) aRoot[++cnt] = 0;
172017d2d592Sdrh       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
172198968b22Sdrh         Table *pTab = sqliteHashData(x);
172298968b22Sdrh         Index *pIdx;
172317d2d592Sdrh         if( pObjTab && pObjTab!=pTab ) continue;
1724b5c1063aSdrh         if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
17257906975aSdrh         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1726b5c1063aSdrh           aRoot[++cnt] = pIdx->tnum;
17277906975aSdrh         }
17287906975aSdrh       }
1729b5c1063aSdrh       aRoot[0] = cnt;
17302d401ab8Sdrh 
17312d401ab8Sdrh       /* Make sure sufficient number of registers have been allocated */
1732bb9b5f26Sdrh       pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
17333963e584Sdrh       sqlite3ClearTempRegCache(pParse);
17342d401ab8Sdrh 
17352d401ab8Sdrh       /* Do the b-tree integrity checks */
173698968b22Sdrh       sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
17374f21c4afSdrh       sqlite3VdbeChangeP5(v, (u8)i);
1738688852abSdrh       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
173998757157Sdrh       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
174069c33826Sdrh          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
174166a5167bSdrh          P4_DYNAMIC);
17429ecd7086Sdrh       sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
17439ecd7086Sdrh       integrityCheckResultRow(v);
17441dcdbc06Sdrh       sqlite3VdbeJumpHere(v, addr);
1745ed717fe3Sdrh 
1746ed717fe3Sdrh       /* Make sure all the indices are constructed correctly.
1747ed717fe3Sdrh       */
17488b174f29Sdrh       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
1749ed717fe3Sdrh         Table *pTab = sqliteHashData(x);
17506fbe41acSdrh         Index *pIdx, *pPk;
175116b03c01Sdrh         Index *pPrior = 0;      /* Previous index */
1752ed717fe3Sdrh         int loopTop;
175326198bb4Sdrh         int iDataCur, iIdxCur;
17541c2c0b77Sdrh         int r1 = -1;
1755db6940abSdrh         int bStrict;            /* True for a STRICT table */
175616b03c01Sdrh         int r2;                 /* Previous key for WITHOUT ROWID tables */
1757db6940abSdrh         int mxCol;              /* Maximum non-virtual column number */
1758ed717fe3Sdrh 
17599966621dSdrh         if( !IsOrdinaryTable(pTab) ) continue;
176017d2d592Sdrh         if( pObjTab && pObjTab!=pTab ) continue;
176116b03c01Sdrh         if( isQuick || HasRowid(pTab) ){
176216b03c01Sdrh           pPk = 0;
176316b03c01Sdrh           r2 = 0;
176416b03c01Sdrh         }else{
176516b03c01Sdrh           pPk = sqlite3PrimaryKeyIndex(pTab);
176616b03c01Sdrh           r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
176716b03c01Sdrh           sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
176816b03c01Sdrh         }
1769fd261ec6Sdan         sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
17706a53499aSdrh                                    1, 0, &iDataCur, &iIdxCur);
17719ecd7086Sdrh         /* reg[7] counts the number of entries in the table.
17729ecd7086Sdrh         ** reg[8+i] counts the number of entries in the i-th index
17739ecd7086Sdrh         */
17746fbe41acSdrh         sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
17758a9789b6Sdrh         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
17766fbe41acSdrh           sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
17778a9789b6Sdrh         }
1778bb9b5f26Sdrh         assert( pParse->nMem>=8+j );
1779bb9b5f26Sdrh         assert( sqlite3NoTempsInRange(pParse,1,7+j) );
1780688852abSdrh         sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
17816fbe41acSdrh         loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
1782db6940abSdrh 
1783db6940abSdrh         /* Fetch the right-most column from the table.  This will cause
1784db6940abSdrh         ** the entire record header to be parsed and sanity checked.  It
1785c9ef12f6Sdrh         ** will also prepopulate the cursor column cache that is used
1786c9ef12f6Sdrh         ** by the OP_IsType code, so it is a required step.
1787c9ef12f6Sdrh         */
1788db6940abSdrh         mxCol = pTab->nCol-1;
1789db6940abSdrh         while( mxCol>=0
1790db6940abSdrh             && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
1791db6940abSdrh                 || pTab->iPKey==mxCol) ) mxCol--;
1792db6940abSdrh         if( mxCol>=0 ){
1793db6940abSdrh           sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, mxCol, 3);
1794e995d2c2Sdrh           sqlite3VdbeTypeofColumn(v, 3);
1795db6940abSdrh         }
1796c9ef12f6Sdrh 
179749d77ee6Sdrh         if( !isQuick ){
179816b03c01Sdrh           if( pPk ){
179916b03c01Sdrh             /* Verify WITHOUT ROWID keys are in ascending order */
180016b03c01Sdrh             int a1;
180116b03c01Sdrh             char *zErr;
180216b03c01Sdrh             a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
180316b03c01Sdrh             VdbeCoverage(v);
180416b03c01Sdrh             sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v);
180516b03c01Sdrh             zErr = sqlite3MPrintf(db,
180616b03c01Sdrh                    "row not in PRIMARY KEY order for %s",
180716b03c01Sdrh                     pTab->zName);
180816b03c01Sdrh             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
180916b03c01Sdrh             integrityCheckResultRow(v);
181016b03c01Sdrh             sqlite3VdbeJumpHere(v, a1);
181116b03c01Sdrh             sqlite3VdbeJumpHere(v, a1+1);
181216b03c01Sdrh             for(j=0; j<pPk->nKeyCol; j++){
181316b03c01Sdrh               sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j);
181416b03c01Sdrh             }
181516b03c01Sdrh           }
18164011c443Sdrh         }
181749d77ee6Sdrh         /* Verify datatypes for all columns:
181849d77ee6Sdrh         **
181949d77ee6Sdrh         **   (1) NOT NULL columns may not contain a NULL
182049d77ee6Sdrh         **   (2) Datatype must be exact for non-ANY columns in STRICT tables
182149d77ee6Sdrh         **   (3) Datatype for TEXT columns in non-STRICT tables must be
182249d77ee6Sdrh         **       NULL, TEXT, or BLOB.
182349d77ee6Sdrh         **   (4) Datatype for numeric columns in non-STRICT tables must not
182449d77ee6Sdrh         **       be a TEXT value that can be losslessly converted to numeric.
182549d77ee6Sdrh         */
18269e1209d1Sdrh         bStrict = (pTab->tabFlags & TF_Strict)!=0;
1827cefc87fcSdrh         for(j=0; j<pTab->nCol; j++){
1828cefc87fcSdrh           char *zErr;
182949d77ee6Sdrh           Column *pCol = pTab->aCol + j;  /* The column to be checked */
1830c9ef12f6Sdrh           int labelError;               /* Jump here to report an error */
1831c9ef12f6Sdrh           int labelOk;                  /* Jump here if all looks ok */
183249d77ee6Sdrh           int p1, p3, p4;               /* Operands to the OP_IsType opcode */
1833c9ef12f6Sdrh           int doTypeCheck;              /* Check datatypes (besides NOT NULL) */
183449d77ee6Sdrh 
1835cefc87fcSdrh           if( j==pTab->iPKey ) continue;
183649d77ee6Sdrh           if( bStrict ){
183749d77ee6Sdrh             doTypeCheck = pCol->eCType>COLTYPE_ANY;
183849d77ee6Sdrh           }else{
183949d77ee6Sdrh             doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
1840ebd70eedSdrh           }
184149d77ee6Sdrh           if( pCol->notNull==0 && !doTypeCheck ) continue;
1842c9ef12f6Sdrh 
1843c9ef12f6Sdrh           /* Compute the operands that will be needed for OP_IsType */
1844db6940abSdrh           p4 = SQLITE_NULL;
184549d77ee6Sdrh           if( pCol->colFlags & COLFLAG_VIRTUAL ){
184649d77ee6Sdrh             sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
184749d77ee6Sdrh             p1 = -1;
184849d77ee6Sdrh             p3 = 3;
184949d77ee6Sdrh           }else{
185049d77ee6Sdrh             if( pCol->iDflt ){
185149d77ee6Sdrh               sqlite3_value *pDfltValue = 0;
185249d77ee6Sdrh               sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db),
185349d77ee6Sdrh                                    pCol->affinity, &pDfltValue);
185449d77ee6Sdrh               if( pDfltValue ){
185549d77ee6Sdrh                 p4 = sqlite3_value_type(pDfltValue);
185649d77ee6Sdrh                 sqlite3ValueFree(pDfltValue);
185749d77ee6Sdrh               }
185849d77ee6Sdrh             }
185949d77ee6Sdrh             p1 = iDataCur;
186049d77ee6Sdrh             if( !HasRowid(pTab) ){
186149d77ee6Sdrh               testcase( j!=sqlite3TableColumnToStorage(pTab, j) );
186249d77ee6Sdrh               p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j);
186349d77ee6Sdrh             }else{
186449d77ee6Sdrh               p3 = sqlite3TableColumnToStorage(pTab,j);
186549d77ee6Sdrh               testcase( p3!=j);
186649d77ee6Sdrh             }
186749d77ee6Sdrh           }
1868c9ef12f6Sdrh 
1869c9ef12f6Sdrh           labelError = sqlite3VdbeMakeLabel(pParse);
1870c9ef12f6Sdrh           labelOk = sqlite3VdbeMakeLabel(pParse);
18719e1209d1Sdrh           if( pCol->notNull ){
187249d77ee6Sdrh             /* (1) NOT NULL columns may not contain a NULL */
1873c9ef12f6Sdrh             int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
187449d77ee6Sdrh             sqlite3VdbeChangeP5(v, 0x0f);
187549d77ee6Sdrh             VdbeCoverage(v);
1876cefc87fcSdrh             zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
18779e1209d1Sdrh                                 pCol->zCnName);
1878cefc87fcSdrh             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1879c9ef12f6Sdrh             if( doTypeCheck ){
1880c9ef12f6Sdrh               sqlite3VdbeGoto(v, labelError);
1881cefc87fcSdrh               sqlite3VdbeJumpHere(v, jmp2);
1882c9ef12f6Sdrh             }else{
1883c9ef12f6Sdrh               /* VDBE byte code will fall thru */
1884c9ef12f6Sdrh             }
1885cefc87fcSdrh           }
188649d77ee6Sdrh           if( bStrict && doTypeCheck ){
188749d77ee6Sdrh             /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
188849d77ee6Sdrh             static unsigned char aStdTypeMask[] = {
188949d77ee6Sdrh                0x1f,    /* ANY */
189049d77ee6Sdrh                0x18,    /* BLOB */
189149d77ee6Sdrh                0x11,    /* INT */
189249d77ee6Sdrh                0x11,    /* INTEGER */
189349d77ee6Sdrh                0x13,    /* REAL */
189449d77ee6Sdrh                0x14     /* TEXT */
189549d77ee6Sdrh             };
1896c9ef12f6Sdrh             sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
189749d77ee6Sdrh             assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
189849d77ee6Sdrh             sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
18999e1209d1Sdrh             VdbeCoverage(v);
19009e1209d1Sdrh             zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
19019e1209d1Sdrh                                   sqlite3StdType[pCol->eCType-1],
19029e1209d1Sdrh                                   pTab->zName, pTab->aCol[j].zCnName);
19039e1209d1Sdrh             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1904c9ef12f6Sdrh           }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
190549d77ee6Sdrh             /* (3) Datatype for TEXT columns in non-STRICT tables must be
190649d77ee6Sdrh             **     NULL, TEXT, or BLOB. */
1907c9ef12f6Sdrh             sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
190849d77ee6Sdrh             sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
190949d77ee6Sdrh             VdbeCoverage(v);
191049d77ee6Sdrh             zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s",
191149d77ee6Sdrh                                   pTab->zName, pTab->aCol[j].zCnName);
191249d77ee6Sdrh             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
1913c9ef12f6Sdrh           }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
191449d77ee6Sdrh             /* (4) Datatype for numeric columns in non-STRICT tables must not
191549d77ee6Sdrh             **     be a TEXT value that can be converted to numeric. */
1916c9ef12f6Sdrh             sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4);
191749d77ee6Sdrh             sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */
191849d77ee6Sdrh             VdbeCoverage(v);
191949d77ee6Sdrh             if( p1>=0 ){
192049d77ee6Sdrh               sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
192149d77ee6Sdrh             }
192249d77ee6Sdrh             sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC);
1923c9ef12f6Sdrh             sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
192449d77ee6Sdrh             sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */
192549d77ee6Sdrh             VdbeCoverage(v);
192649d77ee6Sdrh             zErr = sqlite3MPrintf(db, "TEXT value in %s.%s",
192749d77ee6Sdrh                                   pTab->zName, pTab->aCol[j].zCnName);
192849d77ee6Sdrh             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
192949d77ee6Sdrh           }
1930c9ef12f6Sdrh           sqlite3VdbeResolveLabel(v, labelError);
1931c9ef12f6Sdrh           integrityCheckResultRow(v);
1932c9ef12f6Sdrh           sqlite3VdbeResolveLabel(v, labelOk);
19339e1209d1Sdrh         }
19348a284dceSdrh         /* Verify CHECK constraints */
19358a284dceSdrh         if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
193675f95588Sdan           ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
193775f95588Sdan           if( db->mallocFailed==0 ){
1938ec4ccdbcSdrh             int addrCkFault = sqlite3VdbeMakeLabel(pParse);
1939ec4ccdbcSdrh             int addrCkOk = sqlite3VdbeMakeLabel(pParse);
19408a284dceSdrh             char *zErr;
19418a284dceSdrh             int k;
19423e34eabcSdrh             pParse->iSelfTab = iDataCur + 1;
19438a284dceSdrh             for(k=pCheck->nExpr-1; k>0; k--){
19448a284dceSdrh               sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
19458a284dceSdrh             }
19468a284dceSdrh             sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
19478a284dceSdrh                 SQLITE_JUMPIFNULL);
19488a284dceSdrh             sqlite3VdbeResolveLabel(v, addrCkFault);
19493e34eabcSdrh             pParse->iSelfTab = 0;
19508a284dceSdrh             zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
19518a284dceSdrh                 pTab->zName);
19528a284dceSdrh             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
19539ecd7086Sdrh             integrityCheckResultRow(v);
19548a284dceSdrh             sqlite3VdbeResolveLabel(v, addrCkOk);
1955cefc87fcSdrh           }
195675f95588Sdan           sqlite3ExprListDelete(db, pCheck);
195775f95588Sdan         }
1958226cef4eSdrh         if( !isQuick ){ /* Omit the remaining tests for quick_check */
1959cefc87fcSdrh           /* Validate index entries for the current row */
1960226cef4eSdrh           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
1961cefc87fcSdrh             int jmp2, jmp3, jmp4, jmp5;
1962ec4ccdbcSdrh             int ckUniq = sqlite3VdbeMakeLabel(pParse);
19636fbe41acSdrh             if( pPk==pIdx ) continue;
19641c2c0b77Sdrh             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
19651c2c0b77Sdrh                                          pPrior, r1);
19661c2c0b77Sdrh             pPrior = pIdx;
19676fbe41acSdrh             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
1968cefc87fcSdrh             /* Verify that an index entry exists for the current table row */
1969cefc87fcSdrh             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
1970688852abSdrh                                         pIdx->nColumn); VdbeCoverage(v);
1971076e85f5Sdrh             sqlite3VdbeLoadString(v, 3, "row ");
19726fbe41acSdrh             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
1973076e85f5Sdrh             sqlite3VdbeLoadString(v, 4, " missing from index ");
19746fbe41acSdrh             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
1975076e85f5Sdrh             jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
19766fbe41acSdrh             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
19779ecd7086Sdrh             jmp4 = integrityCheckResultRow(v);
1978d654be80Sdrh             sqlite3VdbeJumpHere(v, jmp2);
1979cefc87fcSdrh             /* For UNIQUE indexes, verify that only one entry exists with the
1980cefc87fcSdrh             ** current key.  The entry is unique if (1) any column is NULL
1981cefc87fcSdrh             ** or (2) the next entry has a different key */
1982cefc87fcSdrh             if( IsUniqueIndex(pIdx) ){
1983ec4ccdbcSdrh               int uniqOk = sqlite3VdbeMakeLabel(pParse);
1984cefc87fcSdrh               int jmp6;
1985cefc87fcSdrh               int kk;
1986cefc87fcSdrh               for(kk=0; kk<pIdx->nKeyCol; kk++){
1987cefc87fcSdrh                 int iCol = pIdx->aiColumn[kk];
19884b92f98cSdrh                 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
198968391acdSdrh                 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
1990cefc87fcSdrh                 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
1991cefc87fcSdrh                 VdbeCoverage(v);
1992cefc87fcSdrh               }
1993cefc87fcSdrh               jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
1994076e85f5Sdrh               sqlite3VdbeGoto(v, uniqOk);
1995cefc87fcSdrh               sqlite3VdbeJumpHere(v, jmp6);
1996cefc87fcSdrh               sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
1997cefc87fcSdrh                                    pIdx->nKeyCol); VdbeCoverage(v);
1998076e85f5Sdrh               sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
1999076e85f5Sdrh               sqlite3VdbeGoto(v, jmp5);
2000cefc87fcSdrh               sqlite3VdbeResolveLabel(v, uniqOk);
2001cefc87fcSdrh             }
2002cefc87fcSdrh             sqlite3VdbeJumpHere(v, jmp4);
200387744513Sdrh             sqlite3ResolvePartIdxLabel(pParse, jmp3);
2004ed717fe3Sdrh           }
2005226cef4eSdrh         }
2006688852abSdrh         sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
20078a9789b6Sdrh         sqlite3VdbeJumpHere(v, loopTop-1);
20088b174f29Sdrh         if( !isQuick ){
2009076e85f5Sdrh           sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
20108a9789b6Sdrh           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
20116fbe41acSdrh             if( pPk==pIdx ) continue;
201226198bb4Sdrh             sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
201366accfc5Sdrh             addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
20145655c549Sdrh             sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
20159ecd7086Sdrh             sqlite3VdbeLoadString(v, 4, pIdx->zName);
20169ecd7086Sdrh             sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
20179ecd7086Sdrh             integrityCheckResultRow(v);
201866accfc5Sdrh             sqlite3VdbeJumpHere(v, addr);
2019ed717fe3Sdrh           }
202016b03c01Sdrh           if( pPk ){
202116b03c01Sdrh             sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
202216b03c01Sdrh           }
2023ed717fe3Sdrh         }
2024ed717fe3Sdrh       }
2025ed717fe3Sdrh     }
20262ce1865dSdrh     {
20272ce1865dSdrh       static const int iLn = VDBE_OFFSET_LINENO(2);
20282ce1865dSdrh       static const VdbeOpList endCode[] = {
20292ce1865dSdrh         { OP_AddImm,      1, 0,        0},    /* 0 */
203066accfc5Sdrh         { OP_IfNotZero,   1, 4,        0},    /* 1 */
20312ce1865dSdrh         { OP_String8,     0, 3,        0},    /* 2 */
20321b32554bSdrh         { OP_ResultRow,   3, 1,        0},    /* 3 */
203374588cebSdrh         { OP_Halt,        0, 0,        0},    /* 4 */
203474588cebSdrh         { OP_String8,     0, 3,        0},    /* 5 */
203574588cebSdrh         { OP_Goto,        0, 3,        0},    /* 6 */
20362ce1865dSdrh       };
20372ce1865dSdrh       VdbeOp *aOp;
20382ce1865dSdrh 
20392ce1865dSdrh       aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
20402ce1865dSdrh       if( aOp ){
204166accfc5Sdrh         aOp[0].p2 = 1-mxErr;
20422ce1865dSdrh         aOp[2].p4type = P4_STATIC;
20432ce1865dSdrh         aOp[2].p4.z = "ok";
204474588cebSdrh         aOp[5].p4type = P4_STATIC;
204574588cebSdrh         aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
20462ce1865dSdrh       }
204774588cebSdrh       sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
20482ce1865dSdrh     }
20499ccd8659Sdrh   }
20509ccd8659Sdrh   break;
2051b7f9164eSdrh #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
2052b7f9164eSdrh 
205313d7042aSdrh #ifndef SQLITE_OMIT_UTF16
20548e227875Sdanielk1977   /*
20558e227875Sdanielk1977   **   PRAGMA encoding
20568e227875Sdanielk1977   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
20578e227875Sdanielk1977   **
205885b623f2Sdrh   ** In its first form, this pragma returns the encoding of the main
20598e227875Sdanielk1977   ** database. If the database is not initialized, it is initialized now.
20608e227875Sdanielk1977   **
20618e227875Sdanielk1977   ** The second form of this pragma is a no-op if the main database file
20628e227875Sdanielk1977   ** has not already been initialized. In this case it sets the default
20638e227875Sdanielk1977   ** encoding that will be used for the main database file if a new file
20648e227875Sdanielk1977   ** is created. If an existing main database file is opened, then the
20658e227875Sdanielk1977   ** default text encoding for the existing database is used.
20668e227875Sdanielk1977   **
20678e227875Sdanielk1977   ** In all cases new databases created using the ATTACH command are
20688e227875Sdanielk1977   ** created to use the same default text encoding as the main database. If
20698e227875Sdanielk1977   ** the main database has not been initialized and/or created when ATTACH
20708e227875Sdanielk1977   ** is executed, this is done before the ATTACH operation.
20718e227875Sdanielk1977   **
20728e227875Sdanielk1977   ** In the second form this pragma sets the text encoding to be used in
20738e227875Sdanielk1977   ** new database files created using this database handle. It is only
20748e227875Sdanielk1977   ** useful if invoked immediately after the main database i
20758e227875Sdanielk1977   */
20769ccd8659Sdrh   case PragTyp_ENCODING: {
20770f7eb611Sdrh     static const struct EncName {
20788e227875Sdanielk1977       char *zName;
20798e227875Sdanielk1977       u8 enc;
20808e227875Sdanielk1977     } encnames[] = {
2081dc8453fdSdanielk1977       { "UTF8",     SQLITE_UTF8        },
2082d2cb50b7Sdrh       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
2083d2cb50b7Sdrh       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
2084d2cb50b7Sdrh       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
2085dc8453fdSdanielk1977       { "UTF16le",  SQLITE_UTF16LE     },
2086dc8453fdSdanielk1977       { "UTF16be",  SQLITE_UTF16BE     },
20870f7eb611Sdrh       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
20880f7eb611Sdrh       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
20898e227875Sdanielk1977       { 0, 0 }
20908e227875Sdanielk1977     };
20910f7eb611Sdrh     const struct EncName *pEnc;
209291cf71b0Sdanielk1977     if( !zRight ){    /* "PRAGMA encoding" */
20938a41449eSdanielk1977       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
2094d2cb50b7Sdrh       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
2095d2cb50b7Sdrh       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
2096d2cb50b7Sdrh       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
2097c232aca1Sdrh       returnSingleText(v, encnames[ENC(pParse->db)].zName);
20988e227875Sdanielk1977     }else{                        /* "PRAGMA encoding = XXX" */
20998e227875Sdanielk1977       /* Only change the value of sqlite.enc if the database handle is not
21008e227875Sdanielk1977       ** initialized. If the main database exists, the new sqlite.enc value
21018e227875Sdanielk1977       ** will be overwritten when the schema is next loaded. If it does not
21028e227875Sdanielk1977       ** already exists, it will be created to use the new encoding value.
21038e227875Sdanielk1977       */
21040ea2d42aSdan       if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
21058e227875Sdanielk1977         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
21068e227875Sdanielk1977           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
210742a630b1Sdrh             u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
210842a630b1Sdrh             SCHEMA_ENC(db) = enc;
210942a630b1Sdrh             sqlite3SetTextEncoding(db, enc);
21108e227875Sdanielk1977             break;
21118e227875Sdanielk1977           }
21128e227875Sdanielk1977         }
21138e227875Sdanielk1977         if( !pEnc->zName ){
21145260f7e9Sdrh           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
21158e227875Sdanielk1977         }
21168e227875Sdanielk1977       }
21178e227875Sdanielk1977     }
21189ccd8659Sdrh   }
21199ccd8659Sdrh   break;
212013d7042aSdrh #endif /* SQLITE_OMIT_UTF16 */
212113d7042aSdrh 
212213d7042aSdrh #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
2123dae2495bSdanielk1977   /*
21249b0cf34fSdrh   **   PRAGMA [schema.]schema_version
21259b0cf34fSdrh   **   PRAGMA [schema.]schema_version = <integer>
2126dae2495bSdanielk1977   **
21279b0cf34fSdrh   **   PRAGMA [schema.]user_version
21289b0cf34fSdrh   **   PRAGMA [schema.]user_version = <integer>
2129dae2495bSdanielk1977   **
2130e459bd49Sdrh   **   PRAGMA [schema.]freelist_count
2131e459bd49Sdrh   **
2132e459bd49Sdrh   **   PRAGMA [schema.]data_version
21334ee09b4bSdrh   **
21349b0cf34fSdrh   **   PRAGMA [schema.]application_id
21359b0cf34fSdrh   **   PRAGMA [schema.]application_id = <integer>
21364ee09b4bSdrh   **
2137b92b70bbSdanielk1977   ** The pragma's schema_version and user_version are used to set or get
2138b92b70bbSdanielk1977   ** the value of the schema-version and user-version, respectively. Both
2139b92b70bbSdanielk1977   ** the schema-version and the user-version are 32-bit signed integers
2140dae2495bSdanielk1977   ** stored in the database header.
2141dae2495bSdanielk1977   **
2142dae2495bSdanielk1977   ** The schema-cookie is usually only manipulated internally by SQLite. It
2143dae2495bSdanielk1977   ** is incremented by SQLite whenever the database schema is modified (by
2144b92b70bbSdanielk1977   ** creating or dropping a table or index). The schema version is used by
2145dae2495bSdanielk1977   ** SQLite each time a query is executed to ensure that the internal cache
2146dae2495bSdanielk1977   ** of the schema used when compiling the SQL query matches the schema of
2147dae2495bSdanielk1977   ** the database against which the compiled query is actually executed.
2148b92b70bbSdanielk1977   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
2149b92b70bbSdanielk1977   ** the schema-version is potentially dangerous and may lead to program
2150dae2495bSdanielk1977   ** crashes or database corruption. Use with caution!
2151dae2495bSdanielk1977   **
2152b92b70bbSdanielk1977   ** The user-version is not used internally by SQLite. It may be used by
2153dae2495bSdanielk1977   ** applications for any purpose.
2154dae2495bSdanielk1977   */
21559ccd8659Sdrh   case PragTyp_HEADER_VALUE: {
2156c228be5bSdrh     int iCookie = pPragma->iArg;  /* Which cookie to read or write */
2157fb98264aSdrh     sqlite3VdbeUsesBtree(v, iDb);
2158c232aca1Sdrh     if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
2159dae2495bSdanielk1977       /* Write the specified cookie value */
2160dae2495bSdanielk1977       static const VdbeOpList setCookie[] = {
2161dae2495bSdanielk1977         { OP_Transaction,    0,  1,  0},    /* 0 */
21621861afcdSdrh         { OP_SetCookie,      0,  0,  0},    /* 1 */
2163dae2495bSdanielk1977       };
21642ce1865dSdrh       VdbeOp *aOp;
2165dad300d8Sdrh       sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
21662ce1865dSdrh       aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
2167dad300d8Sdrh       if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
21682ce1865dSdrh       aOp[0].p1 = iDb;
21691861afcdSdrh       aOp[1].p1 = iDb;
21701861afcdSdrh       aOp[1].p2 = iCookie;
21711861afcdSdrh       aOp[1].p3 = sqlite3Atoi(zRight);
2172e3863b51Sdrh       aOp[1].p5 = 1;
2173*7e475e57Sdrh       if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
2174*7e475e57Sdrh         /* Do not allow the use of PRAGMA schema_version=VALUE in defensive
2175*7e475e57Sdrh         ** mode.  Change the OP_SetCookie opcode into a no-op.  */
2176*7e475e57Sdrh         aOp[1].opcode = OP_Noop;
2177*7e475e57Sdrh       }
2178dae2495bSdanielk1977     }else{
2179dae2495bSdanielk1977       /* Read the specified cookie value */
2180dae2495bSdanielk1977       static const VdbeOpList readCookie[] = {
2181602b466eSdanielk1977         { OP_Transaction,     0,  0,  0},    /* 0 */
2182602b466eSdanielk1977         { OP_ReadCookie,      0,  1,  0},    /* 1 */
21832d401ab8Sdrh         { OP_ResultRow,       1,  1,  0}
2184dae2495bSdanielk1977       };
21852ce1865dSdrh       VdbeOp *aOp;
2186dad300d8Sdrh       sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
21872ce1865dSdrh       aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
2188dad300d8Sdrh       if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
21892ce1865dSdrh       aOp[0].p1 = iDb;
21902ce1865dSdrh       aOp[1].p1 = iDb;
21912ce1865dSdrh       aOp[1].p3 = iCookie;
2192f71a3664Sdrh       sqlite3VdbeReusable(v);
2193dae2495bSdanielk1977     }
21949ccd8659Sdrh   }
21959ccd8659Sdrh   break;
219613d7042aSdrh #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
2197c11d4f93Sdrh 
2198dc97a8cdSshaneh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
2199dc97a8cdSshaneh   /*
2200dc97a8cdSshaneh   **   PRAGMA compile_options
2201dc97a8cdSshaneh   **
220271caabf0Sdrh   ** Return the names of all compile-time options used in this build,
220371caabf0Sdrh   ** one option per row.
2204dc97a8cdSshaneh   */
22059ccd8659Sdrh   case PragTyp_COMPILE_OPTIONS: {
2206dc97a8cdSshaneh     int i = 0;
2207dc97a8cdSshaneh     const char *zOpt;
2208dc97a8cdSshaneh     pParse->nMem = 1;
2209dc97a8cdSshaneh     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
2210076e85f5Sdrh       sqlite3VdbeLoadString(v, 1, zOpt);
2211dc97a8cdSshaneh       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
2212dc97a8cdSshaneh     }
2213f71a3664Sdrh     sqlite3VdbeReusable(v);
22149ccd8659Sdrh   }
22159ccd8659Sdrh   break;
2216dc97a8cdSshaneh #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
2217dc97a8cdSshaneh 
22185cf53537Sdan #ifndef SQLITE_OMIT_WAL
22195cf53537Sdan   /*
22209b0cf34fSdrh   **   PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
22215cf53537Sdan   **
22225cf53537Sdan   ** Checkpoint the database.
22235cf53537Sdan   */
22249ccd8659Sdrh   case PragTyp_WAL_CHECKPOINT: {
2225099b385dSdrh     int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
2226cdc1f049Sdan     int eMode = SQLITE_CHECKPOINT_PASSIVE;
2227cdc1f049Sdan     if( zRight ){
2228cdc1f049Sdan       if( sqlite3StrICmp(zRight, "full")==0 ){
2229cdc1f049Sdan         eMode = SQLITE_CHECKPOINT_FULL;
2230cdc1f049Sdan       }else if( sqlite3StrICmp(zRight, "restart")==0 ){
2231cdc1f049Sdan         eMode = SQLITE_CHECKPOINT_RESTART;
2232f26a1549Sdan       }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
2233f26a1549Sdan         eMode = SQLITE_CHECKPOINT_TRUNCATE;
2234cdc1f049Sdan       }
2235cdc1f049Sdan     }
2236cdc1f049Sdan     pParse->nMem = 3;
223730aa3b93Sdrh     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
2238cdc1f049Sdan     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
22399ccd8659Sdrh   }
22409ccd8659Sdrh   break;
22415a299f91Sdan 
22425a299f91Sdan   /*
22435a299f91Sdan   **   PRAGMA wal_autocheckpoint
22445a299f91Sdan   **   PRAGMA wal_autocheckpoint = N
22455a299f91Sdan   **
22465a299f91Sdan   ** Configure a database connection to automatically checkpoint a database
22475a299f91Sdan   ** after accumulating N frames in the log. Or query for the current value
22485a299f91Sdan   ** of N.
22495a299f91Sdan   */
22509ccd8659Sdrh   case PragTyp_WAL_AUTOCHECKPOINT: {
22515a299f91Sdan     if( zRight ){
225260ac3f42Sdrh       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
22535a299f91Sdan     }
2254c232aca1Sdrh     returnSingleInt(v,
2255b033d8b9Sdrh        db->xWalCallback==sqlite3WalDefaultHook ?
2256b033d8b9Sdrh            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
22579ccd8659Sdrh   }
22589ccd8659Sdrh   break;
22595cf53537Sdan #endif
22607c24610eSdan 
226109419b4bSdrh   /*
226209419b4bSdrh   **  PRAGMA shrink_memory
226309419b4bSdrh   **
226451a74d4cSdrh   ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
226551a74d4cSdrh   ** connection on which it is invoked to free up as much memory as it
226651a74d4cSdrh   ** can, by calling sqlite3_db_release_memory().
226709419b4bSdrh   */
22689ccd8659Sdrh   case PragTyp_SHRINK_MEMORY: {
226909419b4bSdrh     sqlite3_db_release_memory(db);
22709ccd8659Sdrh     break;
22719ccd8659Sdrh   }
227209419b4bSdrh 
2273f360396cSdrh   /*
22742ead47cbSdrh   **  PRAGMA optimize
22751cfaf8eaSdrh   **  PRAGMA optimize(MASK)
22762ead47cbSdrh   **  PRAGMA schema.optimize
22771cfaf8eaSdrh   **  PRAGMA schema.optimize(MASK)
227899d5b4cdSdrh   **
22792ead47cbSdrh   ** Attempt to optimize the database.  All schemas are optimized in the first
22801cfaf8eaSdrh   ** two forms, and only the specified schema is optimized in the latter two.
22812ead47cbSdrh   **
2282e1f1c080Sdrh   ** The details of optimizations performed by this pragma are expected
22832ead47cbSdrh   ** to change and improve over time.  Applications should anticipate that
22842ead47cbSdrh   ** this pragma will perform new optimizations in future releases.
22852ead47cbSdrh   **
22861cfaf8eaSdrh   ** The optional argument is a bitmask of optimizations to perform:
22872ead47cbSdrh   **
22881cfaf8eaSdrh   **    0x0001    Debugging mode.  Do not actually perform any optimizations
22891cfaf8eaSdrh   **              but instead return one line of text for each optimization
22901cfaf8eaSdrh   **              that would have been done.  Off by default.
229199d5b4cdSdrh   **
22921cfaf8eaSdrh   **    0x0002    Run ANALYZE on tables that might benefit.  On by default.
22931cfaf8eaSdrh   **              See below for additional information.
22941cfaf8eaSdrh   **
22951cfaf8eaSdrh   **    0x0004    (Not yet implemented) Record usage and performance
22961cfaf8eaSdrh   **              information from the current session in the
22971cfaf8eaSdrh   **              database file so that it will be available to "optimize"
22981cfaf8eaSdrh   **              pragmas run by future database connections.
22991cfaf8eaSdrh   **
23001cfaf8eaSdrh   **    0x0008    (Not yet implemented) Create indexes that might have
23011cfaf8eaSdrh   **              been helpful to recent queries
23021cfaf8eaSdrh   **
2303e7c6f97bSdrh   ** The default MASK is and always shall be 0xfffe.  0xfffe means perform all
2304e7c6f97bSdrh   ** of the optimizations listed above except Debug Mode, including new
230559dbe3a5Sdrh   ** optimizations that have not yet been invented.  If new optimizations are
230659dbe3a5Sdrh   ** ever added that should be off by default, those off-by-default
230759dbe3a5Sdrh   ** optimizations will have bitmasks of 0x10000 or larger.
23081cfaf8eaSdrh   **
23091cfaf8eaSdrh   ** DETERMINATION OF WHEN TO RUN ANALYZE
23101cfaf8eaSdrh   **
23111cfaf8eaSdrh   ** In the current implementation, a table is analyzed if only if all of
23122ead47cbSdrh   ** the following are true:
231399d5b4cdSdrh   **
23141cfaf8eaSdrh   ** (1) MASK bit 0x02 is set.
23151cfaf8eaSdrh   **
23161cfaf8eaSdrh   ** (2) The query planner used sqlite_stat1-style statistics for one or
231799d5b4cdSdrh   **     more indexes of the table at some point during the lifetime of
231899d5b4cdSdrh   **     the current connection.
231999d5b4cdSdrh   **
23201cfaf8eaSdrh   ** (3) One or more indexes of the table are currently unanalyzed OR
232199d5b4cdSdrh   **     the number of rows in the table has increased by 25 times or more
232299d5b4cdSdrh   **     since the last time ANALYZE was run.
23232ead47cbSdrh   **
23242ead47cbSdrh   ** The rules for when tables are analyzed are likely to change in
23252ead47cbSdrh   ** future releases.
232672052a73Sdrh   */
23272ead47cbSdrh   case PragTyp_OPTIMIZE: {
232899d5b4cdSdrh     int iDbLast;           /* Loop termination point for the schema loop */
232999d5b4cdSdrh     int iTabCur;           /* Cursor for a table whose size needs checking */
233099d5b4cdSdrh     HashElem *k;           /* Loop over tables of a schema */
233199d5b4cdSdrh     Schema *pSchema;       /* The current schema */
233299d5b4cdSdrh     Table *pTab;           /* A table in the schema */
233399d5b4cdSdrh     Index *pIdx;           /* An index of the table */
233499d5b4cdSdrh     LogEst szThreshold;    /* Size threshold above which reanalysis is needd */
233599d5b4cdSdrh     char *zSubSql;         /* SQL statement for the OP_SqlExec opcode */
23361cfaf8eaSdrh     u32 opMask;            /* Mask of operations to perform */
23374a54bb57Sdrh 
23381cfaf8eaSdrh     if( zRight ){
23391cfaf8eaSdrh       opMask = (u32)sqlite3Atoi(zRight);
23401cfaf8eaSdrh       if( (opMask & 0x02)==0 ) break;
23411cfaf8eaSdrh     }else{
234259dbe3a5Sdrh       opMask = 0xfffe;
23431cfaf8eaSdrh     }
23444a54bb57Sdrh     iTabCur = pParse->nTab++;
23454a54bb57Sdrh     for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
23464a54bb57Sdrh       if( iDb==1 ) continue;
23474a54bb57Sdrh       sqlite3CodeVerifySchema(pParse, iDb);
23484a54bb57Sdrh       pSchema = db->aDb[iDb].pSchema;
23494a54bb57Sdrh       for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
23504a54bb57Sdrh         pTab = (Table*)sqliteHashData(k);
235199d5b4cdSdrh 
235299d5b4cdSdrh         /* If table pTab has not been used in a way that would benefit from
235399d5b4cdSdrh         ** having analysis statistics during the current session, then skip it.
235499d5b4cdSdrh         ** This also has the effect of skipping virtual tables and views */
23554a54bb57Sdrh         if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
235699d5b4cdSdrh 
235799d5b4cdSdrh         /* Reanalyze if the table is 25 times larger than the last analysis */
23584a54bb57Sdrh         szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
23594a54bb57Sdrh         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
23604a54bb57Sdrh           if( !pIdx->hasStat1 ){
236199d5b4cdSdrh             szThreshold = 0; /* Always analyze if any index lacks statistics */
23624a54bb57Sdrh             break;
23634a54bb57Sdrh           }
23644a54bb57Sdrh         }
23654a54bb57Sdrh         if( szThreshold ){
23664a54bb57Sdrh           sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
23674a54bb57Sdrh           sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
23681cfaf8eaSdrh                          sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
23694a54bb57Sdrh           VdbeCoverage(v);
23704a54bb57Sdrh         }
23714a54bb57Sdrh         zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
23724a54bb57Sdrh                                  db->aDb[iDb].zDbSName, pTab->zName);
23731cfaf8eaSdrh         if( opMask & 0x01 ){
23741cfaf8eaSdrh           int r1 = sqlite3GetTempReg(pParse);
23751cfaf8eaSdrh           sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
23761cfaf8eaSdrh           sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
23771cfaf8eaSdrh         }else{
23784a54bb57Sdrh           sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
23794a54bb57Sdrh         }
23804a54bb57Sdrh       }
23811cfaf8eaSdrh     }
2382bce04148Sdrh     sqlite3VdbeAddOp0(v, OP_Expire);
238372052a73Sdrh     break;
238472052a73Sdrh   }
238572052a73Sdrh 
238672052a73Sdrh   /*
2387f360396cSdrh   **   PRAGMA busy_timeout
2388f360396cSdrh   **   PRAGMA busy_timeout = N
2389f360396cSdrh   **
2390f360396cSdrh   ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
2391c0c7b5eeSdrh   ** if one is set.  If no busy handler or a different busy handler is set
2392c0c7b5eeSdrh   ** then 0 is returned.  Setting the busy_timeout to 0 or negative
2393c0c7b5eeSdrh   ** disables the timeout.
2394f360396cSdrh   */
2395d49c358eSdrh   /*case PragTyp_BUSY_TIMEOUT*/ default: {
2396c228be5bSdrh     assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
2397f360396cSdrh     if( zRight ){
2398f360396cSdrh       sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
2399f360396cSdrh     }
2400c232aca1Sdrh     returnSingleInt(v, db->busyTimeout);
24019ccd8659Sdrh     break;
24029ccd8659Sdrh   }
2403f360396cSdrh 
240455e85ca5Sdrh   /*
240555e85ca5Sdrh   **   PRAGMA soft_heap_limit
240655e85ca5Sdrh   **   PRAGMA soft_heap_limit = N
240755e85ca5Sdrh   **
240851a74d4cSdrh   ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
240951a74d4cSdrh   ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
241051a74d4cSdrh   ** specified and is a non-negative integer.
241151a74d4cSdrh   ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
241251a74d4cSdrh   ** returns the same integer that would be returned by the
241351a74d4cSdrh   ** sqlite3_soft_heap_limit64(-1) C-language function.
241455e85ca5Sdrh   */
241555e85ca5Sdrh   case PragTyp_SOFT_HEAP_LIMIT: {
241655e85ca5Sdrh     sqlite3_int64 N;
24179296c18aSdrh     if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
241855e85ca5Sdrh       sqlite3_soft_heap_limit64(N);
241955e85ca5Sdrh     }
2420c232aca1Sdrh     returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
242155e85ca5Sdrh     break;
242255e85ca5Sdrh   }
242355e85ca5Sdrh 
242403459615Sdrh   /*
242510c0e711Sdrh   **   PRAGMA hard_heap_limit
242610c0e711Sdrh   **   PRAGMA hard_heap_limit = N
242710c0e711Sdrh   **
242810c0e711Sdrh   ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap
242910c0e711Sdrh   ** limit.  The hard heap limit can be activated or lowered by this
243010c0e711Sdrh   ** pragma, but not raised or deactivated.  Only the
243110c0e711Sdrh   ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
243210c0e711Sdrh   ** the hard heap limit.  This allows an application to set a heap limit
243310c0e711Sdrh   ** constraint that cannot be relaxed by an untrusted SQL script.
243410c0e711Sdrh   */
243510c0e711Sdrh   case PragTyp_HARD_HEAP_LIMIT: {
243610c0e711Sdrh     sqlite3_int64 N;
243710c0e711Sdrh     if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
243810c0e711Sdrh       sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
243910c0e711Sdrh       if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N);
244010c0e711Sdrh     }
244131999c5cSdrh     returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
244210c0e711Sdrh     break;
244310c0e711Sdrh   }
244410c0e711Sdrh 
244510c0e711Sdrh   /*
244603459615Sdrh   **   PRAGMA threads
244703459615Sdrh   **   PRAGMA threads = N
244803459615Sdrh   **
244903459615Sdrh   ** Configure the maximum number of worker threads.  Return the new
245003459615Sdrh   ** maximum, which might be less than requested.
245103459615Sdrh   */
245203459615Sdrh   case PragTyp_THREADS: {
245303459615Sdrh     sqlite3_int64 N;
2454111544cbSdrh     if( zRight
245503459615Sdrh      && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
245603459615Sdrh      && N>=0
245703459615Sdrh     ){
2458111544cbSdrh       sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
245903459615Sdrh     }
2460c232aca1Sdrh     returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
246103459615Sdrh     break;
246203459615Sdrh   }
246303459615Sdrh 
246449a76a8fSdrh   /*
246549a76a8fSdrh   **   PRAGMA analysis_limit
246649a76a8fSdrh   **   PRAGMA analysis_limit = N
246749a76a8fSdrh   **
246849a76a8fSdrh   ** Configure the maximum number of rows that ANALYZE will examine
246949a76a8fSdrh   ** in each index that it looks at.  Return the new limit.
247049a76a8fSdrh   */
247149a76a8fSdrh   case PragTyp_ANALYSIS_LIMIT: {
247249a76a8fSdrh     sqlite3_int64 N;
247349a76a8fSdrh     if( zRight
2474bc98f904Sdrh      && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
247549a76a8fSdrh      && N>=0
247649a76a8fSdrh     ){
247749a76a8fSdrh       db->nAnalysisLimit = (int)(N&0x7fffffff);
247849a76a8fSdrh     }
2479bc98f904Sdrh     returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
248049a76a8fSdrh     break;
248149a76a8fSdrh   }
248249a76a8fSdrh 
248381c95efaSdougcurrie #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
248489ac8c1aSdrh   /*
248589ac8c1aSdrh   ** Report the current state of file logs for all databases
248689ac8c1aSdrh   */
24879ccd8659Sdrh   case PragTyp_LOCK_STATUS: {
24885719628aSdrh     static const char *const azLockName[] = {
248989ac8c1aSdrh       "unlocked", "shared", "reserved", "pending", "exclusive"
249089ac8c1aSdrh     };
249189ac8c1aSdrh     int i;
24922d401ab8Sdrh     pParse->nMem = 2;
249389ac8c1aSdrh     for(i=0; i<db->nDb; i++){
249489ac8c1aSdrh       Btree *pBt;
24959e33c2c1Sdrh       const char *zState = "unknown";
24969e33c2c1Sdrh       int j;
249769c33826Sdrh       if( db->aDb[i].zDbSName==0 ) continue;
249889ac8c1aSdrh       pBt = db->aDb[i].pBt;
24995a05be1bSdrh       if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
25009e33c2c1Sdrh         zState = "closed";
250169c33826Sdrh       }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
25029e33c2c1Sdrh                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
25039e33c2c1Sdrh          zState = azLockName[j];
250489ac8c1aSdrh       }
250569c33826Sdrh       sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
250689ac8c1aSdrh     }
25079ccd8659Sdrh     break;
25089ccd8659Sdrh   }
250989ac8c1aSdrh #endif
251089ac8c1aSdrh 
2511b48c0d59Sdrh #if defined(SQLITE_ENABLE_CEROD)
25129ccd8659Sdrh   case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
251321e2cab9Sdrh     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
251421e2cab9Sdrh       sqlite3_activate_cerod(&zRight[6]);
251521e2cab9Sdrh     }
25169ccd8659Sdrh   }
25179ccd8659Sdrh   break;
251821e2cab9Sdrh #endif
25193c4f2a42Sdrh 
25209ccd8659Sdrh   } /* End of the PRAGMA switch */
2521d2cb50b7Sdrh 
25229e1ab1a8Sdan   /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
25239e1ab1a8Sdan   ** purpose is to execute assert() statements to verify that if the
25249e1ab1a8Sdan   ** PragFlg_NoColumns1 flag is set and the caller specified an argument
25259e1ab1a8Sdan   ** to the PRAGMA, the implementation has not added any OP_ResultRow
25269e1ab1a8Sdan   ** instructions to the VM.  */
25279e1ab1a8Sdan   if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
25289e1ab1a8Sdan     sqlite3VdbeVerifyNoResultRow(v);
25299e1ab1a8Sdan   }
25309e1ab1a8Sdan 
2531e0048400Sdanielk1977 pragma_out:
2532633e6d57Sdrh   sqlite3DbFree(db, zLeft);
2533633e6d57Sdrh   sqlite3DbFree(db, zRight);
2534c11d4f93Sdrh }
25352fcc1590Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
25362fcc1590Sdrh /*****************************************************************************
25372fcc1590Sdrh ** Implementation of an eponymous virtual table that runs a pragma.
25382fcc1590Sdrh **
25392fcc1590Sdrh */
25402fcc1590Sdrh typedef struct PragmaVtab PragmaVtab;
25412fcc1590Sdrh typedef struct PragmaVtabCursor PragmaVtabCursor;
25422fcc1590Sdrh struct PragmaVtab {
25432fcc1590Sdrh   sqlite3_vtab base;        /* Base class.  Must be first */
25442fcc1590Sdrh   sqlite3 *db;              /* The database connection to which it belongs */
25452fcc1590Sdrh   const PragmaName *pName;  /* Name of the pragma */
25462fcc1590Sdrh   u8 nHidden;               /* Number of hidden columns */
25472fcc1590Sdrh   u8 iHidden;               /* Index of the first hidden column */
25482fcc1590Sdrh };
25492fcc1590Sdrh struct PragmaVtabCursor {
25502fcc1590Sdrh   sqlite3_vtab_cursor base; /* Base class.  Must be first */
25512fcc1590Sdrh   sqlite3_stmt *pPragma;    /* The pragma statement to run */
25522fcc1590Sdrh   sqlite_int64 iRowid;      /* Current rowid */
25532fcc1590Sdrh   char *azArg[2];           /* Value of the argument and schema */
25542fcc1590Sdrh };
25552fcc1590Sdrh 
25562fcc1590Sdrh /*
25572fcc1590Sdrh ** Pragma virtual table module xConnect method.
25582fcc1590Sdrh */
pragmaVtabConnect(sqlite3 * db,void * pAux,int argc,const char * const * argv,sqlite3_vtab ** ppVtab,char ** pzErr)25592fcc1590Sdrh static int pragmaVtabConnect(
25602fcc1590Sdrh   sqlite3 *db,
25612fcc1590Sdrh   void *pAux,
25622fcc1590Sdrh   int argc, const char *const*argv,
25632fcc1590Sdrh   sqlite3_vtab **ppVtab,
25642fcc1590Sdrh   char **pzErr
25652fcc1590Sdrh ){
25662fcc1590Sdrh   const PragmaName *pPragma = (const PragmaName*)pAux;
25672fcc1590Sdrh   PragmaVtab *pTab = 0;
25682fcc1590Sdrh   int rc;
25692fcc1590Sdrh   int i, j;
25702fcc1590Sdrh   char cSep = '(';
25712fcc1590Sdrh   StrAccum acc;
25722fcc1590Sdrh   char zBuf[200];
25732fcc1590Sdrh 
2574344a1bf1Sdrh   UNUSED_PARAMETER(argc);
2575344a1bf1Sdrh   UNUSED_PARAMETER(argv);
25762fcc1590Sdrh   sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
25770cdbe1aeSdrh   sqlite3_str_appendall(&acc, "CREATE TABLE x");
25782fcc1590Sdrh   for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
25790cdbe1aeSdrh     sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
25802fcc1590Sdrh     cSep = ',';
25812fcc1590Sdrh   }
25829a63f092Sdrh   if( i==0 ){
25830cdbe1aeSdrh     sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
25849a63f092Sdrh     i++;
25859a63f092Sdrh   }
25862fcc1590Sdrh   j = 0;
25872fcc1590Sdrh   if( pPragma->mPragFlg & PragFlg_Result1 ){
25880cdbe1aeSdrh     sqlite3_str_appendall(&acc, ",arg HIDDEN");
25892fcc1590Sdrh     j++;
25902fcc1590Sdrh   }
25912fcc1590Sdrh   if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
25920cdbe1aeSdrh     sqlite3_str_appendall(&acc, ",schema HIDDEN");
25932fcc1590Sdrh     j++;
25942fcc1590Sdrh   }
25950cdbe1aeSdrh   sqlite3_str_append(&acc, ")", 1);
25962fcc1590Sdrh   sqlite3StrAccumFinish(&acc);
25972fcc1590Sdrh   assert( strlen(zBuf) < sizeof(zBuf)-1 );
25982fcc1590Sdrh   rc = sqlite3_declare_vtab(db, zBuf);
25992fcc1590Sdrh   if( rc==SQLITE_OK ){
26002fcc1590Sdrh     pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
26012fcc1590Sdrh     if( pTab==0 ){
26022fcc1590Sdrh       rc = SQLITE_NOMEM;
26032fcc1590Sdrh     }else{
26042fcc1590Sdrh       memset(pTab, 0, sizeof(PragmaVtab));
26052fcc1590Sdrh       pTab->pName = pPragma;
26062fcc1590Sdrh       pTab->db = db;
26072fcc1590Sdrh       pTab->iHidden = i;
26082fcc1590Sdrh       pTab->nHidden = j;
26092fcc1590Sdrh     }
26102fcc1590Sdrh   }else{
26112fcc1590Sdrh     *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
26122fcc1590Sdrh   }
26132fcc1590Sdrh 
26142fcc1590Sdrh   *ppVtab = (sqlite3_vtab*)pTab;
26152fcc1590Sdrh   return rc;
26162fcc1590Sdrh }
26172fcc1590Sdrh 
26182fcc1590Sdrh /*
26192fcc1590Sdrh ** Pragma virtual table module xDisconnect method.
26202fcc1590Sdrh */
pragmaVtabDisconnect(sqlite3_vtab * pVtab)26212fcc1590Sdrh static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
26222fcc1590Sdrh   PragmaVtab *pTab = (PragmaVtab*)pVtab;
26232fcc1590Sdrh   sqlite3_free(pTab);
26242fcc1590Sdrh   return SQLITE_OK;
26252fcc1590Sdrh }
26262fcc1590Sdrh 
26272fcc1590Sdrh /* Figure out the best index to use to search a pragma virtual table.
26282fcc1590Sdrh **
26292fcc1590Sdrh ** There are not really any index choices.  But we want to encourage the
26302fcc1590Sdrh ** query planner to give == constraints on as many hidden parameters as
26312fcc1590Sdrh ** possible, and especially on the first hidden parameter.  So return a
26322fcc1590Sdrh ** high cost if hidden parameters are unconstrained.
26332fcc1590Sdrh */
pragmaVtabBestIndex(sqlite3_vtab * tab,sqlite3_index_info * pIdxInfo)26342fcc1590Sdrh static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
26352fcc1590Sdrh   PragmaVtab *pTab = (PragmaVtab*)tab;
26362fcc1590Sdrh   const struct sqlite3_index_constraint *pConstraint;
26372fcc1590Sdrh   int i, j;
26382fcc1590Sdrh   int seen[2];
26392fcc1590Sdrh 
2640ae7045cdSdrh   pIdxInfo->estimatedCost = (double)1;
26412fcc1590Sdrh   if( pTab->nHidden==0 ){ return SQLITE_OK; }
26422fcc1590Sdrh   pConstraint = pIdxInfo->aConstraint;
26432fcc1590Sdrh   seen[0] = 0;
26442fcc1590Sdrh   seen[1] = 0;
26452fcc1590Sdrh   for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
26462fcc1590Sdrh     if( pConstraint->usable==0 ) continue;
26472fcc1590Sdrh     if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
26482fcc1590Sdrh     if( pConstraint->iColumn < pTab->iHidden ) continue;
26492fcc1590Sdrh     j = pConstraint->iColumn - pTab->iHidden;
26502fcc1590Sdrh     assert( j < 2 );
2651d7175ebeSdrh     seen[j] = i+1;
26522fcc1590Sdrh   }
26532fcc1590Sdrh   if( seen[0]==0 ){
26542fcc1590Sdrh     pIdxInfo->estimatedCost = (double)2147483647;
26552fcc1590Sdrh     pIdxInfo->estimatedRows = 2147483647;
26562fcc1590Sdrh     return SQLITE_OK;
26572fcc1590Sdrh   }
2658d7175ebeSdrh   j = seen[0]-1;
26592fcc1590Sdrh   pIdxInfo->aConstraintUsage[j].argvIndex = 1;
26602fcc1590Sdrh   pIdxInfo->aConstraintUsage[j].omit = 1;
26612fcc1590Sdrh   if( seen[1]==0 ) return SQLITE_OK;
26622fcc1590Sdrh   pIdxInfo->estimatedCost = (double)20;
26632fcc1590Sdrh   pIdxInfo->estimatedRows = 20;
2664d7175ebeSdrh   j = seen[1]-1;
26652fcc1590Sdrh   pIdxInfo->aConstraintUsage[j].argvIndex = 2;
26662fcc1590Sdrh   pIdxInfo->aConstraintUsage[j].omit = 1;
26672fcc1590Sdrh   return SQLITE_OK;
26682fcc1590Sdrh }
26692fcc1590Sdrh 
26702fcc1590Sdrh /* Create a new cursor for the pragma virtual table */
pragmaVtabOpen(sqlite3_vtab * pVtab,sqlite3_vtab_cursor ** ppCursor)26712fcc1590Sdrh static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
26722fcc1590Sdrh   PragmaVtabCursor *pCsr;
26732fcc1590Sdrh   pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
26742fcc1590Sdrh   if( pCsr==0 ) return SQLITE_NOMEM;
26752fcc1590Sdrh   memset(pCsr, 0, sizeof(PragmaVtabCursor));
26762fcc1590Sdrh   pCsr->base.pVtab = pVtab;
26772fcc1590Sdrh   *ppCursor = &pCsr->base;
26782fcc1590Sdrh   return SQLITE_OK;
26792fcc1590Sdrh }
26802fcc1590Sdrh 
26812fcc1590Sdrh /* Clear all content from pragma virtual table cursor. */
pragmaVtabCursorClear(PragmaVtabCursor * pCsr)26822fcc1590Sdrh static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
26832fcc1590Sdrh   int i;
26842fcc1590Sdrh   sqlite3_finalize(pCsr->pPragma);
26852fcc1590Sdrh   pCsr->pPragma = 0;
26862fcc1590Sdrh   for(i=0; i<ArraySize(pCsr->azArg); i++){
26872fcc1590Sdrh     sqlite3_free(pCsr->azArg[i]);
26882fcc1590Sdrh     pCsr->azArg[i] = 0;
26892fcc1590Sdrh   }
26902fcc1590Sdrh }
26912fcc1590Sdrh 
26922fcc1590Sdrh /* Close a pragma virtual table cursor */
pragmaVtabClose(sqlite3_vtab_cursor * cur)26932fcc1590Sdrh static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
26942fcc1590Sdrh   PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
26952fcc1590Sdrh   pragmaVtabCursorClear(pCsr);
26969a63f092Sdrh   sqlite3_free(pCsr);
26972fcc1590Sdrh   return SQLITE_OK;
26982fcc1590Sdrh }
26992fcc1590Sdrh 
27002fcc1590Sdrh /* Advance the pragma virtual table cursor to the next row */
pragmaVtabNext(sqlite3_vtab_cursor * pVtabCursor)27012fcc1590Sdrh static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
27022fcc1590Sdrh   PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
27032fcc1590Sdrh   int rc = SQLITE_OK;
27042fcc1590Sdrh 
27052fcc1590Sdrh   /* Increment the xRowid value */
27062fcc1590Sdrh   pCsr->iRowid++;
27079a63f092Sdrh   assert( pCsr->pPragma );
27082fcc1590Sdrh   if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
27092fcc1590Sdrh     rc = sqlite3_finalize(pCsr->pPragma);
27102fcc1590Sdrh     pCsr->pPragma = 0;
27112fcc1590Sdrh     pragmaVtabCursorClear(pCsr);
27122fcc1590Sdrh   }
27132fcc1590Sdrh   return rc;
27142fcc1590Sdrh }
27152fcc1590Sdrh 
27162fcc1590Sdrh /*
27172fcc1590Sdrh ** Pragma virtual table module xFilter method.
27182fcc1590Sdrh */
pragmaVtabFilter(sqlite3_vtab_cursor * pVtabCursor,int idxNum,const char * idxStr,int argc,sqlite3_value ** argv)27192fcc1590Sdrh static int pragmaVtabFilter(
27202fcc1590Sdrh   sqlite3_vtab_cursor *pVtabCursor,
27212fcc1590Sdrh   int idxNum, const char *idxStr,
27222fcc1590Sdrh   int argc, sqlite3_value **argv
27232fcc1590Sdrh ){
27242fcc1590Sdrh   PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
27252fcc1590Sdrh   PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
27262fcc1590Sdrh   int rc;
2727d8b7200bSdrh   int i, j;
27282fcc1590Sdrh   StrAccum acc;
27292fcc1590Sdrh   char *zSql;
27302fcc1590Sdrh 
2731344a1bf1Sdrh   UNUSED_PARAMETER(idxNum);
2732344a1bf1Sdrh   UNUSED_PARAMETER(idxStr);
27332fcc1590Sdrh   pragmaVtabCursorClear(pCsr);
2734d8b7200bSdrh   j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
2735d8b7200bSdrh   for(i=0; i<argc; i++, j++){
2736d8ecefa5Sdan     const char *zText = (const char*)sqlite3_value_text(argv[i]);
2737d8b7200bSdrh     assert( j<ArraySize(pCsr->azArg) );
2738d8ecefa5Sdan     assert( pCsr->azArg[j]==0 );
2739d8ecefa5Sdan     if( zText ){
2740d8ecefa5Sdan       pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
2741d8b7200bSdrh       if( pCsr->azArg[j]==0 ){
27422fcc1590Sdrh         return SQLITE_NOMEM;
27432fcc1590Sdrh       }
27442fcc1590Sdrh     }
2745d8ecefa5Sdan   }
2746d7175ebeSdrh   sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
27470cdbe1aeSdrh   sqlite3_str_appendall(&acc, "PRAGMA ");
27482fcc1590Sdrh   if( pCsr->azArg[1] ){
27490cdbe1aeSdrh     sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
27502fcc1590Sdrh   }
27510cdbe1aeSdrh   sqlite3_str_appendall(&acc, pTab->pName->zName);
27522fcc1590Sdrh   if( pCsr->azArg[0] ){
27530cdbe1aeSdrh     sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
27542fcc1590Sdrh   }
27552fcc1590Sdrh   zSql = sqlite3StrAccumFinish(&acc);
27562fcc1590Sdrh   if( zSql==0 ) return SQLITE_NOMEM;
27572fcc1590Sdrh   rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
27582fcc1590Sdrh   sqlite3_free(zSql);
27592fcc1590Sdrh   if( rc!=SQLITE_OK ){
27602fcc1590Sdrh     pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
27612fcc1590Sdrh     return rc;
27622fcc1590Sdrh   }
27632fcc1590Sdrh   return pragmaVtabNext(pVtabCursor);
27642fcc1590Sdrh }
27652fcc1590Sdrh 
27662fcc1590Sdrh /*
27672fcc1590Sdrh ** Pragma virtual table module xEof method.
27682fcc1590Sdrh */
pragmaVtabEof(sqlite3_vtab_cursor * pVtabCursor)27692fcc1590Sdrh static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
27702fcc1590Sdrh   PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
27712fcc1590Sdrh   return (pCsr->pPragma==0);
27722fcc1590Sdrh }
27732fcc1590Sdrh 
27742fcc1590Sdrh /* The xColumn method simply returns the corresponding column from
27752fcc1590Sdrh ** the PRAGMA.
27762fcc1590Sdrh */
pragmaVtabColumn(sqlite3_vtab_cursor * pVtabCursor,sqlite3_context * ctx,int i)27772fcc1590Sdrh static int pragmaVtabColumn(
27782fcc1590Sdrh   sqlite3_vtab_cursor *pVtabCursor,
27792fcc1590Sdrh   sqlite3_context *ctx,
27802fcc1590Sdrh   int i
27812fcc1590Sdrh ){
27822fcc1590Sdrh   PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
27832fcc1590Sdrh   PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
27842fcc1590Sdrh   if( i<pTab->iHidden ){
27852fcc1590Sdrh     sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
27862fcc1590Sdrh   }else{
27872fcc1590Sdrh     sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
27882fcc1590Sdrh   }
27892fcc1590Sdrh   return SQLITE_OK;
27902fcc1590Sdrh }
27912fcc1590Sdrh 
27922fcc1590Sdrh /*
27932fcc1590Sdrh ** Pragma virtual table module xRowid method.
27942fcc1590Sdrh */
pragmaVtabRowid(sqlite3_vtab_cursor * pVtabCursor,sqlite_int64 * p)27952fcc1590Sdrh static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
27962fcc1590Sdrh   PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
27972fcc1590Sdrh   *p = pCsr->iRowid;
27982fcc1590Sdrh   return SQLITE_OK;
27992fcc1590Sdrh }
28002fcc1590Sdrh 
28012fcc1590Sdrh /* The pragma virtual table object */
28022fcc1590Sdrh static const sqlite3_module pragmaVtabModule = {
28032fcc1590Sdrh   0,                           /* iVersion */
28042fcc1590Sdrh   0,                           /* xCreate - create a table */
28052fcc1590Sdrh   pragmaVtabConnect,           /* xConnect - connect to an existing table */
28062fcc1590Sdrh   pragmaVtabBestIndex,         /* xBestIndex - Determine search strategy */
28072fcc1590Sdrh   pragmaVtabDisconnect,        /* xDisconnect - Disconnect from a table */
28082fcc1590Sdrh   0,                           /* xDestroy - Drop a table */
28092fcc1590Sdrh   pragmaVtabOpen,              /* xOpen - open a cursor */
28102fcc1590Sdrh   pragmaVtabClose,             /* xClose - close a cursor */
28112fcc1590Sdrh   pragmaVtabFilter,            /* xFilter - configure scan constraints */
28122fcc1590Sdrh   pragmaVtabNext,              /* xNext - advance a cursor */
28132fcc1590Sdrh   pragmaVtabEof,               /* xEof */
28142fcc1590Sdrh   pragmaVtabColumn,            /* xColumn - read data */
28152fcc1590Sdrh   pragmaVtabRowid,             /* xRowid - read data */
28162fcc1590Sdrh   0,                           /* xUpdate - write data */
28172fcc1590Sdrh   0,                           /* xBegin - begin transaction */
28182fcc1590Sdrh   0,                           /* xSync - sync transaction */
28192fcc1590Sdrh   0,                           /* xCommit - commit transaction */
28202fcc1590Sdrh   0,                           /* xRollback - rollback transaction */
28212fcc1590Sdrh   0,                           /* xFindFunction - function overloading */
28222fcc1590Sdrh   0,                           /* xRename - rename the table */
28232fcc1590Sdrh   0,                           /* xSavepoint */
28242fcc1590Sdrh   0,                           /* xRelease */
282584c501baSdrh   0,                           /* xRollbackTo */
282684c501baSdrh   0                            /* xShadowName */
28272fcc1590Sdrh };
28282fcc1590Sdrh 
28292fcc1590Sdrh /*
28302fcc1590Sdrh ** Check to see if zTabName is really the name of a pragma.  If it is,
28312fcc1590Sdrh ** then register an eponymous virtual table for that pragma and return
28322fcc1590Sdrh ** a pointer to the Module object for the new virtual table.
28332fcc1590Sdrh */
sqlite3PragmaVtabRegister(sqlite3 * db,const char * zName)28342fcc1590Sdrh Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
28352fcc1590Sdrh   const PragmaName *pName;
28362fcc1590Sdrh   assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
28372fcc1590Sdrh   pName = pragmaLocate(zName+7);
28382fcc1590Sdrh   if( pName==0 ) return 0;
28392fcc1590Sdrh   if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
28402fcc1590Sdrh   assert( sqlite3HashFind(&db->aModule, zName)==0 );
2841d7175ebeSdrh   return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
28422fcc1590Sdrh }
28432fcc1590Sdrh 
28442fcc1590Sdrh #endif /* SQLITE_OMIT_VIRTUALTABLE */
284513d7042aSdrh 
28468bfdf721Sdrh #endif /* SQLITE_OMIT_PRAGMA */
2847