xref: /illumos-gate/usr/src/lib/libsqlite/src/pragma.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** 2003 April 6
37c478bd9Sstevel@tonic-gate **
47c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code.  In place of
57c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
67c478bd9Sstevel@tonic-gate **
77c478bd9Sstevel@tonic-gate **    May you do good and not evil.
87c478bd9Sstevel@tonic-gate **    May you find forgiveness for yourself and forgive others.
97c478bd9Sstevel@tonic-gate **    May you share freely, never taking more than you give.
107c478bd9Sstevel@tonic-gate **
117c478bd9Sstevel@tonic-gate *************************************************************************
127c478bd9Sstevel@tonic-gate ** This file contains code used to implement the PRAGMA command.
137c478bd9Sstevel@tonic-gate **
147c478bd9Sstevel@tonic-gate ** $Id: pragma.c,v 1.19 2004/04/23 17:04:45 drh Exp $
157c478bd9Sstevel@tonic-gate */
167c478bd9Sstevel@tonic-gate #include "sqliteInt.h"
177c478bd9Sstevel@tonic-gate #include <ctype.h>
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate /*
207c478bd9Sstevel@tonic-gate ** Interpret the given string as a boolean value.
217c478bd9Sstevel@tonic-gate */
getBoolean(const char * z)227c478bd9Sstevel@tonic-gate static int getBoolean(const char *z){
237c478bd9Sstevel@tonic-gate   static char *azTrue[] = { "yes", "on", "true" };
247c478bd9Sstevel@tonic-gate   int i;
257c478bd9Sstevel@tonic-gate   if( z[0]==0 ) return 0;
267c478bd9Sstevel@tonic-gate   if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
277c478bd9Sstevel@tonic-gate     return atoi(z);
287c478bd9Sstevel@tonic-gate   }
297c478bd9Sstevel@tonic-gate   for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
307c478bd9Sstevel@tonic-gate     if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
317c478bd9Sstevel@tonic-gate   }
327c478bd9Sstevel@tonic-gate   return 0;
337c478bd9Sstevel@tonic-gate }
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate ** Interpret the given string as a safety level.  Return 0 for OFF,
37*1da57d55SToomas Soome ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
387c478bd9Sstevel@tonic-gate ** unrecognized string argument.
397c478bd9Sstevel@tonic-gate **
407c478bd9Sstevel@tonic-gate ** Note that the values returned are one less that the values that
417c478bd9Sstevel@tonic-gate ** should be passed into sqliteBtreeSetSafetyLevel().  The is done
427c478bd9Sstevel@tonic-gate ** to support legacy SQL code.  The safety level used to be boolean
437c478bd9Sstevel@tonic-gate ** and older scripts may have used numbers 0 for OFF and 1 for ON.
447c478bd9Sstevel@tonic-gate */
getSafetyLevel(char * z)457c478bd9Sstevel@tonic-gate static int getSafetyLevel(char *z){
467c478bd9Sstevel@tonic-gate   static const struct {
477c478bd9Sstevel@tonic-gate     const char *zWord;
487c478bd9Sstevel@tonic-gate     int val;
497c478bd9Sstevel@tonic-gate   } aKey[] = {
507c478bd9Sstevel@tonic-gate     { "no",    0 },
517c478bd9Sstevel@tonic-gate     { "off",   0 },
527c478bd9Sstevel@tonic-gate     { "false", 0 },
537c478bd9Sstevel@tonic-gate     { "yes",   1 },
547c478bd9Sstevel@tonic-gate     { "on",    1 },
557c478bd9Sstevel@tonic-gate     { "true",  1 },
567c478bd9Sstevel@tonic-gate     { "full",  2 },
577c478bd9Sstevel@tonic-gate   };
587c478bd9Sstevel@tonic-gate   int i;
597c478bd9Sstevel@tonic-gate   if( z[0]==0 ) return 1;
607c478bd9Sstevel@tonic-gate   if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
617c478bd9Sstevel@tonic-gate     return atoi(z);
627c478bd9Sstevel@tonic-gate   }
637c478bd9Sstevel@tonic-gate   for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){
647c478bd9Sstevel@tonic-gate     if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val;
657c478bd9Sstevel@tonic-gate   }
667c478bd9Sstevel@tonic-gate   return 1;
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate ** Interpret the given string as a temp db location. Return 1 for file
717c478bd9Sstevel@tonic-gate ** backed temporary databases, 2 for the Red-Black tree in memory database
727c478bd9Sstevel@tonic-gate ** and 0 to use the compile-time default.
737c478bd9Sstevel@tonic-gate */
getTempStore(const char * z)747c478bd9Sstevel@tonic-gate static int getTempStore(const char *z){
757c478bd9Sstevel@tonic-gate   if( z[0]>='0' && z[0]<='2' ){
767c478bd9Sstevel@tonic-gate     return z[0] - '0';
777c478bd9Sstevel@tonic-gate   }else if( sqliteStrICmp(z, "file")==0 ){
787c478bd9Sstevel@tonic-gate     return 1;
797c478bd9Sstevel@tonic-gate   }else if( sqliteStrICmp(z, "memory")==0 ){
807c478bd9Sstevel@tonic-gate     return 2;
817c478bd9Sstevel@tonic-gate   }else{
827c478bd9Sstevel@tonic-gate     return 0;
837c478bd9Sstevel@tonic-gate   }
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate ** If the TEMP database is open, close it and mark the database schema
887c478bd9Sstevel@tonic-gate ** as needing reloading.  This must be done when using the TEMP_STORE
897c478bd9Sstevel@tonic-gate ** or DEFAULT_TEMP_STORE pragmas.
907c478bd9Sstevel@tonic-gate */
changeTempStorage(Parse * pParse,const char * zStorageType)917c478bd9Sstevel@tonic-gate static int changeTempStorage(Parse *pParse, const char *zStorageType){
927c478bd9Sstevel@tonic-gate   int ts = getTempStore(zStorageType);
937c478bd9Sstevel@tonic-gate   sqlite *db = pParse->db;
947c478bd9Sstevel@tonic-gate   if( db->temp_store==ts ) return SQLITE_OK;
957c478bd9Sstevel@tonic-gate   if( db->aDb[1].pBt!=0 ){
967c478bd9Sstevel@tonic-gate     if( db->flags & SQLITE_InTrans ){
977c478bd9Sstevel@tonic-gate       sqliteErrorMsg(pParse, "temporary storage cannot be changed "
987c478bd9Sstevel@tonic-gate         "from within a transaction");
997c478bd9Sstevel@tonic-gate       return SQLITE_ERROR;
1007c478bd9Sstevel@tonic-gate     }
1017c478bd9Sstevel@tonic-gate     sqliteBtreeClose(db->aDb[1].pBt);
1027c478bd9Sstevel@tonic-gate     db->aDb[1].pBt = 0;
1037c478bd9Sstevel@tonic-gate     sqliteResetInternalSchema(db, 0);
1047c478bd9Sstevel@tonic-gate   }
1057c478bd9Sstevel@tonic-gate   db->temp_store = ts;
1067c478bd9Sstevel@tonic-gate   return SQLITE_OK;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate ** Check to see if zRight and zLeft refer to a pragma that queries
1117c478bd9Sstevel@tonic-gate ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
1127c478bd9Sstevel@tonic-gate ** Also, implement the pragma.
1137c478bd9Sstevel@tonic-gate */
flagPragma(Parse * pParse,const char * zLeft,const char * zRight)1147c478bd9Sstevel@tonic-gate static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
1157c478bd9Sstevel@tonic-gate   static const struct {
1167c478bd9Sstevel@tonic-gate     const char *zName;  /* Name of the pragma */
1177c478bd9Sstevel@tonic-gate     int mask;           /* Mask for the db->flags value */
1187c478bd9Sstevel@tonic-gate   } aPragma[] = {
1197c478bd9Sstevel@tonic-gate     { "vdbe_trace",               SQLITE_VdbeTrace     },
1207c478bd9Sstevel@tonic-gate     { "full_column_names",        SQLITE_FullColNames  },
1217c478bd9Sstevel@tonic-gate     { "short_column_names",       SQLITE_ShortColNames },
1227c478bd9Sstevel@tonic-gate     { "show_datatypes",           SQLITE_ReportTypes   },
1237c478bd9Sstevel@tonic-gate     { "count_changes",            SQLITE_CountRows     },
1247c478bd9Sstevel@tonic-gate     { "empty_result_callbacks",   SQLITE_NullCallback  },
1257c478bd9Sstevel@tonic-gate   };
1267c478bd9Sstevel@tonic-gate   int i;
1277c478bd9Sstevel@tonic-gate   for(i=0; i<sizeof(aPragma)/sizeof(aPragma[0]); i++){
1287c478bd9Sstevel@tonic-gate     if( sqliteStrICmp(zLeft, aPragma[i].zName)==0 ){
1297c478bd9Sstevel@tonic-gate       sqlite *db = pParse->db;
1307c478bd9Sstevel@tonic-gate       Vdbe *v;
1317c478bd9Sstevel@tonic-gate       if( strcmp(zLeft,zRight)==0 && (v = sqliteGetVdbe(pParse))!=0 ){
1327c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_ColumnName, 0, 1, aPragma[i].zName, P3_STATIC);
1337c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_ColumnName, 1, 0, "boolean", P3_STATIC);
1347c478bd9Sstevel@tonic-gate         sqliteVdbeCode(v, OP_Integer, (db->flags & aPragma[i].mask)!=0, 0,
1357c478bd9Sstevel@tonic-gate                           OP_Callback, 1, 0,
1367c478bd9Sstevel@tonic-gate                           0);
1377c478bd9Sstevel@tonic-gate       }else if( getBoolean(zRight) ){
1387c478bd9Sstevel@tonic-gate         db->flags |= aPragma[i].mask;
1397c478bd9Sstevel@tonic-gate       }else{
1407c478bd9Sstevel@tonic-gate         db->flags &= ~aPragma[i].mask;
1417c478bd9Sstevel@tonic-gate       }
1427c478bd9Sstevel@tonic-gate       return 1;
1437c478bd9Sstevel@tonic-gate     }
1447c478bd9Sstevel@tonic-gate   }
1457c478bd9Sstevel@tonic-gate   return 0;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
149*1da57d55SToomas Soome ** Process a pragma statement.
1507c478bd9Sstevel@tonic-gate **
1517c478bd9Sstevel@tonic-gate ** Pragmas are of this form:
1527c478bd9Sstevel@tonic-gate **
1537c478bd9Sstevel@tonic-gate **      PRAGMA id = value
1547c478bd9Sstevel@tonic-gate **
1557c478bd9Sstevel@tonic-gate ** The identifier might also be a string.  The value is a string, and
1567c478bd9Sstevel@tonic-gate ** identifier, or a number.  If minusFlag is true, then the value is
1577c478bd9Sstevel@tonic-gate ** a number that was preceded by a minus sign.
1587c478bd9Sstevel@tonic-gate */
sqlitePragma(Parse * pParse,Token * pLeft,Token * pRight,int minusFlag)1597c478bd9Sstevel@tonic-gate void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1607c478bd9Sstevel@tonic-gate   char *zLeft = 0;
1617c478bd9Sstevel@tonic-gate   char *zRight = 0;
1627c478bd9Sstevel@tonic-gate   sqlite *db = pParse->db;
1637c478bd9Sstevel@tonic-gate   Vdbe *v = sqliteGetVdbe(pParse);
1647c478bd9Sstevel@tonic-gate   if( v==0 ) return;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate   zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1677c478bd9Sstevel@tonic-gate   sqliteDequote(zLeft);
1687c478bd9Sstevel@tonic-gate   if( minusFlag ){
1697c478bd9Sstevel@tonic-gate     zRight = 0;
1707c478bd9Sstevel@tonic-gate     sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1717c478bd9Sstevel@tonic-gate   }else{
1727c478bd9Sstevel@tonic-gate     zRight = sqliteStrNDup(pRight->z, pRight->n);
1737c478bd9Sstevel@tonic-gate     sqliteDequote(zRight);
1747c478bd9Sstevel@tonic-gate   }
1757c478bd9Sstevel@tonic-gate   if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, 0) ){
1767c478bd9Sstevel@tonic-gate     sqliteFree(zLeft);
1777c478bd9Sstevel@tonic-gate     sqliteFree(zRight);
1787c478bd9Sstevel@tonic-gate     return;
1797c478bd9Sstevel@tonic-gate   }
180*1da57d55SToomas Soome 
1817c478bd9Sstevel@tonic-gate   /*
1827c478bd9Sstevel@tonic-gate   **  PRAGMA default_cache_size
1837c478bd9Sstevel@tonic-gate   **  PRAGMA default_cache_size=N
1847c478bd9Sstevel@tonic-gate   **
1857c478bd9Sstevel@tonic-gate   ** The first form reports the current persistent setting for the
1867c478bd9Sstevel@tonic-gate   ** page cache size.  The value returned is the maximum number of
1877c478bd9Sstevel@tonic-gate   ** pages in the page cache.  The second form sets both the current
1887c478bd9Sstevel@tonic-gate   ** page cache size value and the persistent page cache size value
1897c478bd9Sstevel@tonic-gate   ** stored in the database file.
1907c478bd9Sstevel@tonic-gate   **
1917c478bd9Sstevel@tonic-gate   ** The default cache size is stored in meta-value 2 of page 1 of the
1927c478bd9Sstevel@tonic-gate   ** database file.  The cache size is actually the absolute value of
1937c478bd9Sstevel@tonic-gate   ** this memory location.  The sign of meta-value 2 determines the
1947c478bd9Sstevel@tonic-gate   ** synchronous setting.  A negative value means synchronous is off
1957c478bd9Sstevel@tonic-gate   ** and a positive value means synchronous is on.
1967c478bd9Sstevel@tonic-gate   */
1977c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
1987c478bd9Sstevel@tonic-gate     static VdbeOpList getCacheSize[] = {
1997c478bd9Sstevel@tonic-gate       { OP_ReadCookie,  0, 2,        0},
2007c478bd9Sstevel@tonic-gate       { OP_AbsValue,    0, 0,        0},
2017c478bd9Sstevel@tonic-gate       { OP_Dup,         0, 0,        0},
2027c478bd9Sstevel@tonic-gate       { OP_Integer,     0, 0,        0},
2037c478bd9Sstevel@tonic-gate       { OP_Ne,          0, 6,        0},
2047c478bd9Sstevel@tonic-gate       { OP_Integer,     0, 0,        0},  /* 5 */
2057c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 1,        "cache_size"},
2067c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0},
2077c478bd9Sstevel@tonic-gate     };
2087c478bd9Sstevel@tonic-gate     int addr;
2097c478bd9Sstevel@tonic-gate     if( pRight->z==pLeft->z ){
2107c478bd9Sstevel@tonic-gate       addr = sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2117c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP1(v, addr+5, MAX_PAGES);
2127c478bd9Sstevel@tonic-gate     }else{
2137c478bd9Sstevel@tonic-gate       int size = atoi(zRight);
2147c478bd9Sstevel@tonic-gate       if( size<0 ) size = -size;
2157c478bd9Sstevel@tonic-gate       sqliteBeginWriteOperation(pParse, 0, 0);
2167c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, size, 0);
2177c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2187c478bd9Sstevel@tonic-gate       addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2197c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2207c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2217c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2227c478bd9Sstevel@tonic-gate       sqliteEndWriteOperation(pParse);
2237c478bd9Sstevel@tonic-gate       db->cache_size = db->cache_size<0 ? -size : size;
2247c478bd9Sstevel@tonic-gate       sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2257c478bd9Sstevel@tonic-gate     }
2267c478bd9Sstevel@tonic-gate   }else
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate   /*
2297c478bd9Sstevel@tonic-gate   **  PRAGMA cache_size
2307c478bd9Sstevel@tonic-gate   **  PRAGMA cache_size=N
2317c478bd9Sstevel@tonic-gate   **
2327c478bd9Sstevel@tonic-gate   ** The first form reports the current local setting for the
2337c478bd9Sstevel@tonic-gate   ** page cache size.  The local setting can be different from
2347c478bd9Sstevel@tonic-gate   ** the persistent cache size value that is stored in the database
2357c478bd9Sstevel@tonic-gate   ** file itself.  The value returned is the maximum number of
2367c478bd9Sstevel@tonic-gate   ** pages in the page cache.  The second form sets the local
2377c478bd9Sstevel@tonic-gate   ** page cache size value.  It does not change the persistent
2387c478bd9Sstevel@tonic-gate   ** cache size stored on the disk so the cache size will revert
2397c478bd9Sstevel@tonic-gate   ** to its default value when the database is closed and reopened.
2407c478bd9Sstevel@tonic-gate   ** N should be a positive integer.
2417c478bd9Sstevel@tonic-gate   */
2427c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2437c478bd9Sstevel@tonic-gate     static VdbeOpList getCacheSize[] = {
2447c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 1,        "cache_size"},
2457c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0},
2467c478bd9Sstevel@tonic-gate     };
2477c478bd9Sstevel@tonic-gate     if( pRight->z==pLeft->z ){
2487c478bd9Sstevel@tonic-gate       int size = db->cache_size;;
2497c478bd9Sstevel@tonic-gate       if( size<0 ) size = -size;
2507c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, size, 0);
2517c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2527c478bd9Sstevel@tonic-gate     }else{
2537c478bd9Sstevel@tonic-gate       int size = atoi(zRight);
2547c478bd9Sstevel@tonic-gate       if( size<0 ) size = -size;
2557c478bd9Sstevel@tonic-gate       if( db->cache_size<0 ) size = -size;
2567c478bd9Sstevel@tonic-gate       db->cache_size = size;
2577c478bd9Sstevel@tonic-gate       sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2587c478bd9Sstevel@tonic-gate     }
2597c478bd9Sstevel@tonic-gate   }else
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate   /*
2627c478bd9Sstevel@tonic-gate   **  PRAGMA default_synchronous
2637c478bd9Sstevel@tonic-gate   **  PRAGMA default_synchronous=ON|OFF|NORMAL|FULL
2647c478bd9Sstevel@tonic-gate   **
2657c478bd9Sstevel@tonic-gate   ** The first form returns the persistent value of the "synchronous" setting
2667c478bd9Sstevel@tonic-gate   ** that is stored in the database.  This is the synchronous setting that
2677c478bd9Sstevel@tonic-gate   ** is used whenever the database is opened unless overridden by a separate
2687c478bd9Sstevel@tonic-gate   ** "synchronous" pragma.  The second form changes the persistent and the
2697c478bd9Sstevel@tonic-gate   ** local synchronous setting to the value given.
2707c478bd9Sstevel@tonic-gate   **
2717c478bd9Sstevel@tonic-gate   ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls
2727c478bd9Sstevel@tonic-gate   ** to make sure data is committed to disk.  Write operations are very fast,
2737c478bd9Sstevel@tonic-gate   ** but a power failure can leave the database in an inconsistent state.
2747c478bd9Sstevel@tonic-gate   ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to
2757c478bd9Sstevel@tonic-gate   ** make sure data is being written to disk.  The risk of corruption due to
2767c478bd9Sstevel@tonic-gate   ** a power loss in this mode is negligible but non-zero.  If synchronous
2777c478bd9Sstevel@tonic-gate   ** is FULL, extra fsync()s occur to reduce the risk of corruption to near
2787c478bd9Sstevel@tonic-gate   ** zero, but with a write performance penalty.  The default mode is NORMAL.
2797c478bd9Sstevel@tonic-gate   */
2807c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
2817c478bd9Sstevel@tonic-gate     static VdbeOpList getSync[] = {
2827c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 1,        "synchronous"},
2837c478bd9Sstevel@tonic-gate       { OP_ReadCookie,  0, 3,        0},
2847c478bd9Sstevel@tonic-gate       { OP_Dup,         0, 0,        0},
2857c478bd9Sstevel@tonic-gate       { OP_If,          0, 0,        0},  /* 3 */
2867c478bd9Sstevel@tonic-gate       { OP_ReadCookie,  0, 2,        0},
2877c478bd9Sstevel@tonic-gate       { OP_Integer,     0, 0,        0},
2887c478bd9Sstevel@tonic-gate       { OP_Lt,          0, 5,        0},
2897c478bd9Sstevel@tonic-gate       { OP_AddImm,      1, 0,        0},
2907c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0},
2917c478bd9Sstevel@tonic-gate       { OP_Halt,        0, 0,        0},
2927c478bd9Sstevel@tonic-gate       { OP_AddImm,     -1, 0,        0},  /* 10 */
2937c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0}
2947c478bd9Sstevel@tonic-gate     };
2957c478bd9Sstevel@tonic-gate     if( pRight->z==pLeft->z ){
2967c478bd9Sstevel@tonic-gate       int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2977c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, addr+3, addr+10);
2987c478bd9Sstevel@tonic-gate     }else{
2997c478bd9Sstevel@tonic-gate       int addr;
3007c478bd9Sstevel@tonic-gate       int size = db->cache_size;
3017c478bd9Sstevel@tonic-gate       if( size<0 ) size = -size;
3027c478bd9Sstevel@tonic-gate       sqliteBeginWriteOperation(pParse, 0, 0);
3037c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
3047c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Dup, 0, 0);
3057c478bd9Sstevel@tonic-gate       addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
3067c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
3077c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
3087c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
3097c478bd9Sstevel@tonic-gate       db->safety_level = getSafetyLevel(zRight)+1;
3107c478bd9Sstevel@tonic-gate       if( db->safety_level==1 ){
3117c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Negative, 0, 0);
3127c478bd9Sstevel@tonic-gate         size = -size;
3137c478bd9Sstevel@tonic-gate       }
3147c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
3157c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0);
3167c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_SetCookie, 0, 3);
3177c478bd9Sstevel@tonic-gate       sqliteEndWriteOperation(pParse);
3187c478bd9Sstevel@tonic-gate       db->cache_size = size;
3197c478bd9Sstevel@tonic-gate       sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
3207c478bd9Sstevel@tonic-gate       sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
3217c478bd9Sstevel@tonic-gate     }
3227c478bd9Sstevel@tonic-gate   }else
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate   /*
3257c478bd9Sstevel@tonic-gate   **   PRAGMA synchronous
3267c478bd9Sstevel@tonic-gate   **   PRAGMA synchronous=OFF|ON|NORMAL|FULL
3277c478bd9Sstevel@tonic-gate   **
3287c478bd9Sstevel@tonic-gate   ** Return or set the local value of the synchronous flag.  Changing
3297c478bd9Sstevel@tonic-gate   ** the local value does not make changes to the disk file and the
3307c478bd9Sstevel@tonic-gate   ** default value will be restored the next time the database is
3317c478bd9Sstevel@tonic-gate   ** opened.
3327c478bd9Sstevel@tonic-gate   */
3337c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft,"synchronous")==0 ){
3347c478bd9Sstevel@tonic-gate     static VdbeOpList getSync[] = {
3357c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 1,        "synchronous"},
3367c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0},
3377c478bd9Sstevel@tonic-gate     };
3387c478bd9Sstevel@tonic-gate     if( pRight->z==pLeft->z ){
3397c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
3407c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
3417c478bd9Sstevel@tonic-gate     }else{
3427c478bd9Sstevel@tonic-gate       int size = db->cache_size;
3437c478bd9Sstevel@tonic-gate       if( size<0 ) size = -size;
3447c478bd9Sstevel@tonic-gate       db->safety_level = getSafetyLevel(zRight)+1;
3457c478bd9Sstevel@tonic-gate       if( db->safety_level==1 ) size = -size;
3467c478bd9Sstevel@tonic-gate       db->cache_size = size;
3477c478bd9Sstevel@tonic-gate       sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
3487c478bd9Sstevel@tonic-gate       sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
3497c478bd9Sstevel@tonic-gate     }
3507c478bd9Sstevel@tonic-gate   }else
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate #ifndef NDEBUG
3537c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
3547c478bd9Sstevel@tonic-gate     if( getBoolean(zRight) ){
3557c478bd9Sstevel@tonic-gate       always_code_trigger_setup = 1;
3567c478bd9Sstevel@tonic-gate     }else{
3577c478bd9Sstevel@tonic-gate       always_code_trigger_setup = 0;
3587c478bd9Sstevel@tonic-gate     }
3597c478bd9Sstevel@tonic-gate   }else
3607c478bd9Sstevel@tonic-gate #endif
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate   if( flagPragma(pParse, zLeft, zRight) ){
3637c478bd9Sstevel@tonic-gate     /* The flagPragma() call also generates any necessary code */
3647c478bd9Sstevel@tonic-gate   }else
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "table_info")==0 ){
3677c478bd9Sstevel@tonic-gate     Table *pTab;
3687c478bd9Sstevel@tonic-gate     pTab = sqliteFindTable(db, zRight, 0);
3697c478bd9Sstevel@tonic-gate     if( pTab ){
3707c478bd9Sstevel@tonic-gate       static VdbeOpList tableInfoPreface[] = {
3717c478bd9Sstevel@tonic-gate         { OP_ColumnName,  0, 0,       "cid"},
3727c478bd9Sstevel@tonic-gate         { OP_ColumnName,  1, 0,       "name"},
3737c478bd9Sstevel@tonic-gate         { OP_ColumnName,  2, 0,       "type"},
3747c478bd9Sstevel@tonic-gate         { OP_ColumnName,  3, 0,       "notnull"},
3757c478bd9Sstevel@tonic-gate         { OP_ColumnName,  4, 0,       "dflt_value"},
3767c478bd9Sstevel@tonic-gate         { OP_ColumnName,  5, 1,       "pk"},
3777c478bd9Sstevel@tonic-gate       };
3787c478bd9Sstevel@tonic-gate       int i;
3797c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
3807c478bd9Sstevel@tonic-gate       sqliteViewGetColumnNames(pParse, pTab);
3817c478bd9Sstevel@tonic-gate       for(i=0; i<pTab->nCol; i++){
3827c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, i, 0);
3837c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[i].zName, 0);
3847c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0,
3857c478bd9Sstevel@tonic-gate            pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", 0);
3867c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
3877c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0,
3887c478bd9Sstevel@tonic-gate            pTab->aCol[i].zDflt, P3_STATIC);
3897c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].isPrimKey, 0);
3907c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Callback, 6, 0);
3917c478bd9Sstevel@tonic-gate       }
3927c478bd9Sstevel@tonic-gate     }
3937c478bd9Sstevel@tonic-gate   }else
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "index_info")==0 ){
3967c478bd9Sstevel@tonic-gate     Index *pIdx;
3977c478bd9Sstevel@tonic-gate     Table *pTab;
3987c478bd9Sstevel@tonic-gate     pIdx = sqliteFindIndex(db, zRight, 0);
3997c478bd9Sstevel@tonic-gate     if( pIdx ){
4007c478bd9Sstevel@tonic-gate       static VdbeOpList tableInfoPreface[] = {
4017c478bd9Sstevel@tonic-gate         { OP_ColumnName,  0, 0,       "seqno"},
4027c478bd9Sstevel@tonic-gate         { OP_ColumnName,  1, 0,       "cid"},
4037c478bd9Sstevel@tonic-gate         { OP_ColumnName,  2, 1,       "name"},
4047c478bd9Sstevel@tonic-gate       };
4057c478bd9Sstevel@tonic-gate       int i;
4067c478bd9Sstevel@tonic-gate       pTab = pIdx->pTable;
4077c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
4087c478bd9Sstevel@tonic-gate       for(i=0; i<pIdx->nColumn; i++){
4097c478bd9Sstevel@tonic-gate         int cnum = pIdx->aiColumn[i];
4107c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, i, 0);
4117c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
4127c478bd9Sstevel@tonic-gate         assert( pTab->nCol>cnum );
4137c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0, pTab->aCol[cnum].zName, 0);
4147c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Callback, 3, 0);
4157c478bd9Sstevel@tonic-gate       }
4167c478bd9Sstevel@tonic-gate     }
4177c478bd9Sstevel@tonic-gate   }else
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "index_list")==0 ){
4207c478bd9Sstevel@tonic-gate     Index *pIdx;
4217c478bd9Sstevel@tonic-gate     Table *pTab;
4227c478bd9Sstevel@tonic-gate     pTab = sqliteFindTable(db, zRight, 0);
4237c478bd9Sstevel@tonic-gate     if( pTab ){
4247c478bd9Sstevel@tonic-gate       v = sqliteGetVdbe(pParse);
4257c478bd9Sstevel@tonic-gate       pIdx = pTab->pIndex;
4267c478bd9Sstevel@tonic-gate     }
4277c478bd9Sstevel@tonic-gate     if( pTab && pIdx ){
428*1da57d55SToomas Soome       int i = 0;
4297c478bd9Sstevel@tonic-gate       static VdbeOpList indexListPreface[] = {
4307c478bd9Sstevel@tonic-gate         { OP_ColumnName,  0, 0,       "seq"},
4317c478bd9Sstevel@tonic-gate         { OP_ColumnName,  1, 0,       "name"},
4327c478bd9Sstevel@tonic-gate         { OP_ColumnName,  2, 1,       "unique"},
4337c478bd9Sstevel@tonic-gate       };
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
4367c478bd9Sstevel@tonic-gate       while(pIdx){
4377c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, i, 0);
4387c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_String, 0, 0, pIdx->zName, 0);
4397c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
4407c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Callback, 3, 0);
4417c478bd9Sstevel@tonic-gate         ++i;
4427c478bd9Sstevel@tonic-gate         pIdx = pIdx->pNext;
4437c478bd9Sstevel@tonic-gate       }
4447c478bd9Sstevel@tonic-gate     }
4457c478bd9Sstevel@tonic-gate   }else
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "foreign_key_list")==0 ){
4487c478bd9Sstevel@tonic-gate     FKey *pFK;
4497c478bd9Sstevel@tonic-gate     Table *pTab;
4507c478bd9Sstevel@tonic-gate     pTab = sqliteFindTable(db, zRight, 0);
4517c478bd9Sstevel@tonic-gate     if( pTab ){
4527c478bd9Sstevel@tonic-gate       v = sqliteGetVdbe(pParse);
4537c478bd9Sstevel@tonic-gate       pFK = pTab->pFKey;
4547c478bd9Sstevel@tonic-gate     }
4557c478bd9Sstevel@tonic-gate     if( pTab && pFK ){
456*1da57d55SToomas Soome       int i = 0;
4577c478bd9Sstevel@tonic-gate       static VdbeOpList indexListPreface[] = {
4587c478bd9Sstevel@tonic-gate         { OP_ColumnName,  0, 0,       "id"},
4597c478bd9Sstevel@tonic-gate         { OP_ColumnName,  1, 0,       "seq"},
4607c478bd9Sstevel@tonic-gate         { OP_ColumnName,  2, 0,       "table"},
4617c478bd9Sstevel@tonic-gate         { OP_ColumnName,  3, 0,       "from"},
4627c478bd9Sstevel@tonic-gate         { OP_ColumnName,  4, 1,       "to"},
4637c478bd9Sstevel@tonic-gate       };
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
4667c478bd9Sstevel@tonic-gate       while(pFK){
4677c478bd9Sstevel@tonic-gate         int j;
4687c478bd9Sstevel@tonic-gate         for(j=0; j<pFK->nCol; j++){
4697c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Integer, i, 0);
4707c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Integer, j, 0);
4717c478bd9Sstevel@tonic-gate           sqliteVdbeOp3(v, OP_String, 0, 0, pFK->zTo, 0);
4727c478bd9Sstevel@tonic-gate           sqliteVdbeOp3(v, OP_String, 0, 0,
4737c478bd9Sstevel@tonic-gate                            pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
4747c478bd9Sstevel@tonic-gate           sqliteVdbeOp3(v, OP_String, 0, 0, pFK->aCol[j].zCol, 0);
4757c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Callback, 5, 0);
4767c478bd9Sstevel@tonic-gate         }
4777c478bd9Sstevel@tonic-gate         ++i;
4787c478bd9Sstevel@tonic-gate         pFK = pFK->pNextFrom;
4797c478bd9Sstevel@tonic-gate       }
4807c478bd9Sstevel@tonic-gate     }
4817c478bd9Sstevel@tonic-gate   }else
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "database_list")==0 ){
4847c478bd9Sstevel@tonic-gate     int i;
4857c478bd9Sstevel@tonic-gate     static VdbeOpList indexListPreface[] = {
4867c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 0,       "seq"},
4877c478bd9Sstevel@tonic-gate       { OP_ColumnName,  1, 0,       "name"},
4887c478bd9Sstevel@tonic-gate       { OP_ColumnName,  2, 1,       "file"},
4897c478bd9Sstevel@tonic-gate     };
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate     sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
4927c478bd9Sstevel@tonic-gate     for(i=0; i<db->nDb; i++){
4937c478bd9Sstevel@tonic-gate       if( db->aDb[i].pBt==0 ) continue;
4947c478bd9Sstevel@tonic-gate       assert( db->aDb[i].zName!=0 );
4957c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, i, 0);
4967c478bd9Sstevel@tonic-gate       sqliteVdbeOp3(v, OP_String, 0, 0, db->aDb[i].zName, 0);
4977c478bd9Sstevel@tonic-gate       sqliteVdbeOp3(v, OP_String, 0, 0,
4987c478bd9Sstevel@tonic-gate            sqliteBtreeGetFilename(db->aDb[i].pBt), 0);
4997c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Callback, 3, 0);
5007c478bd9Sstevel@tonic-gate     }
5017c478bd9Sstevel@tonic-gate   }else
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate   /*
5057c478bd9Sstevel@tonic-gate   **   PRAGMA temp_store
5067c478bd9Sstevel@tonic-gate   **   PRAGMA temp_store = "default"|"memory"|"file"
5077c478bd9Sstevel@tonic-gate   **
5087c478bd9Sstevel@tonic-gate   ** Return or set the local value of the temp_store flag.  Changing
5097c478bd9Sstevel@tonic-gate   ** the local value does not make changes to the disk file and the default
5107c478bd9Sstevel@tonic-gate   ** value will be restored the next time the database is opened.
5117c478bd9Sstevel@tonic-gate   **
5127c478bd9Sstevel@tonic-gate   ** Note that it is possible for the library compile-time options to
5137c478bd9Sstevel@tonic-gate   ** override this setting
5147c478bd9Sstevel@tonic-gate   */
5157c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "temp_store")==0 ){
5167c478bd9Sstevel@tonic-gate     static VdbeOpList getTmpDbLoc[] = {
5177c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 1,        "temp_store"},
5187c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0},
5197c478bd9Sstevel@tonic-gate     };
5207c478bd9Sstevel@tonic-gate     if( pRight->z==pLeft->z ){
5217c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, db->temp_store, 0);
5227c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
5237c478bd9Sstevel@tonic-gate     }else{
5247c478bd9Sstevel@tonic-gate       changeTempStorage(pParse, zRight);
5257c478bd9Sstevel@tonic-gate     }
5267c478bd9Sstevel@tonic-gate   }else
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate   /*
5297c478bd9Sstevel@tonic-gate   **   PRAGMA default_temp_store
5307c478bd9Sstevel@tonic-gate   **   PRAGMA default_temp_store = "default"|"memory"|"file"
5317c478bd9Sstevel@tonic-gate   **
5327c478bd9Sstevel@tonic-gate   ** Return or set the value of the persistent temp_store flag.  Any
5337c478bd9Sstevel@tonic-gate   ** change does not take effect until the next time the database is
5347c478bd9Sstevel@tonic-gate   ** opened.
5357c478bd9Sstevel@tonic-gate   **
5367c478bd9Sstevel@tonic-gate   ** Note that it is possible for the library compile-time options to
5377c478bd9Sstevel@tonic-gate   ** override this setting
5387c478bd9Sstevel@tonic-gate   */
5397c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "default_temp_store")==0 ){
5407c478bd9Sstevel@tonic-gate     static VdbeOpList getTmpDbLoc[] = {
5417c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 1,        "temp_store"},
5427c478bd9Sstevel@tonic-gate       { OP_ReadCookie,  0, 5,        0},
5437c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0}};
5447c478bd9Sstevel@tonic-gate     if( pRight->z==pLeft->z ){
5457c478bd9Sstevel@tonic-gate       sqliteVdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc);
5467c478bd9Sstevel@tonic-gate     }else{
5477c478bd9Sstevel@tonic-gate       sqliteBeginWriteOperation(pParse, 0, 0);
5487c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, getTempStore(zRight), 0);
5497c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_SetCookie, 0, 5);
5507c478bd9Sstevel@tonic-gate       sqliteEndWriteOperation(pParse);
5517c478bd9Sstevel@tonic-gate     }
5527c478bd9Sstevel@tonic-gate   }else
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate #ifndef NDEBUG
5557c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
5567c478bd9Sstevel@tonic-gate     extern void sqliteParserTrace(FILE*, char *);
5577c478bd9Sstevel@tonic-gate     if( getBoolean(zRight) ){
5587c478bd9Sstevel@tonic-gate       sqliteParserTrace(stdout, "parser: ");
5597c478bd9Sstevel@tonic-gate     }else{
5607c478bd9Sstevel@tonic-gate       sqliteParserTrace(0, 0);
5617c478bd9Sstevel@tonic-gate     }
5627c478bd9Sstevel@tonic-gate   }else
5637c478bd9Sstevel@tonic-gate #endif
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate   if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
5667c478bd9Sstevel@tonic-gate     int i, j, addr;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate     /* Code that initializes the integrity check program.  Set the
5697c478bd9Sstevel@tonic-gate     ** error count 0
5707c478bd9Sstevel@tonic-gate     */
5717c478bd9Sstevel@tonic-gate     static VdbeOpList initCode[] = {
5727c478bd9Sstevel@tonic-gate       { OP_Integer,     0, 0,        0},
5737c478bd9Sstevel@tonic-gate       { OP_MemStore,    0, 1,        0},
5747c478bd9Sstevel@tonic-gate       { OP_ColumnName,  0, 1,        "integrity_check"},
5757c478bd9Sstevel@tonic-gate     };
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate     /* Code to do an BTree integrity check on a single database file.
5787c478bd9Sstevel@tonic-gate     */
5797c478bd9Sstevel@tonic-gate     static VdbeOpList checkDb[] = {
5807c478bd9Sstevel@tonic-gate       { OP_SetInsert,   0, 0,        "2"},
5817c478bd9Sstevel@tonic-gate       { OP_Integer,     0, 0,        0},    /* 1 */
5827c478bd9Sstevel@tonic-gate       { OP_OpenRead,    0, 2,        0},
5837c478bd9Sstevel@tonic-gate       { OP_Rewind,      0, 7,        0},    /* 3 */
5847c478bd9Sstevel@tonic-gate       { OP_Column,      0, 3,        0},    /* 4 */
5857c478bd9Sstevel@tonic-gate       { OP_SetInsert,   0, 0,        0},
5867c478bd9Sstevel@tonic-gate       { OP_Next,        0, 4,        0},    /* 6 */
5877c478bd9Sstevel@tonic-gate       { OP_IntegrityCk, 0, 0,        0},    /* 7 */
5887c478bd9Sstevel@tonic-gate       { OP_Dup,         0, 1,        0},
5897c478bd9Sstevel@tonic-gate       { OP_String,      0, 0,        "ok"},
5907c478bd9Sstevel@tonic-gate       { OP_StrEq,       0, 12,       0},    /* 10 */
5917c478bd9Sstevel@tonic-gate       { OP_MemIncr,     0, 0,        0},
5927c478bd9Sstevel@tonic-gate       { OP_String,      0, 0,        "*** in database "},
5937c478bd9Sstevel@tonic-gate       { OP_String,      0, 0,        0},    /* 13 */
5947c478bd9Sstevel@tonic-gate       { OP_String,      0, 0,        " ***\n"},
5957c478bd9Sstevel@tonic-gate       { OP_Pull,        3, 0,        0},
5967c478bd9Sstevel@tonic-gate       { OP_Concat,      4, 1,        0},
5977c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0},
5987c478bd9Sstevel@tonic-gate     };
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate     /* Code that appears at the end of the integrity check.  If no error
6017c478bd9Sstevel@tonic-gate     ** messages have been generated, output OK.  Otherwise output the
6027c478bd9Sstevel@tonic-gate     ** error message
6037c478bd9Sstevel@tonic-gate     */
6047c478bd9Sstevel@tonic-gate     static VdbeOpList endCode[] = {
6057c478bd9Sstevel@tonic-gate       { OP_MemLoad,     0, 0,        0},
6067c478bd9Sstevel@tonic-gate       { OP_Integer,     0, 0,        0},
6077c478bd9Sstevel@tonic-gate       { OP_Ne,          0, 0,        0},    /* 2 */
6087c478bd9Sstevel@tonic-gate       { OP_String,      0, 0,        "ok"},
6097c478bd9Sstevel@tonic-gate       { OP_Callback,    1, 0,        0},
6107c478bd9Sstevel@tonic-gate     };
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate     /* Initialize the VDBE program */
6137c478bd9Sstevel@tonic-gate     sqliteVdbeAddOpList(v, ArraySize(initCode), initCode);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate     /* Do an integrity check on each database file */
6167c478bd9Sstevel@tonic-gate     for(i=0; i<db->nDb; i++){
6177c478bd9Sstevel@tonic-gate       HashElem *x;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate       /* Do an integrity check of the B-Tree
6207c478bd9Sstevel@tonic-gate       */
6217c478bd9Sstevel@tonic-gate       addr = sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
6227c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP1(v, addr+1, i);
6237c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, addr+3, addr+7);
6247c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, addr+6, addr+4);
6257c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, addr+7, i);
6267c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP2(v, addr+10, addr+ArraySize(checkDb));
6277c478bd9Sstevel@tonic-gate       sqliteVdbeChangeP3(v, addr+13, db->aDb[i].zName, P3_STATIC);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate       /* Make sure all the indices are constructed correctly.
6307c478bd9Sstevel@tonic-gate       */
6317c478bd9Sstevel@tonic-gate       sqliteCodeVerifySchema(pParse, i);
6327c478bd9Sstevel@tonic-gate       for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){
6337c478bd9Sstevel@tonic-gate         Table *pTab = sqliteHashData(x);
6347c478bd9Sstevel@tonic-gate         Index *pIdx;
6357c478bd9Sstevel@tonic-gate         int loopTop;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate         if( pTab->pIndex==0 ) continue;
6387c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, i, 0);
6397c478bd9Sstevel@tonic-gate         sqliteVdbeOp3(v, OP_OpenRead, 1, pTab->tnum, pTab->zName, 0);
6407c478bd9Sstevel@tonic-gate         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
6417c478bd9Sstevel@tonic-gate           if( pIdx->tnum==0 ) continue;
6427c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
6437c478bd9Sstevel@tonic-gate           sqliteVdbeOp3(v, OP_OpenRead, j+2, pIdx->tnum, pIdx->zName, 0);
6447c478bd9Sstevel@tonic-gate         }
6457c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Integer, 0, 0);
6467c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MemStore, 1, 1);
6477c478bd9Sstevel@tonic-gate         loopTop = sqliteVdbeAddOp(v, OP_Rewind, 1, 0);
6487c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MemIncr, 1, 0);
6497c478bd9Sstevel@tonic-gate         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
6507c478bd9Sstevel@tonic-gate           int k, jmp2;
6517c478bd9Sstevel@tonic-gate           static VdbeOpList idxErr[] = {
6527c478bd9Sstevel@tonic-gate             { OP_MemIncr,     0,  0,  0},
6537c478bd9Sstevel@tonic-gate             { OP_String,      0,  0,  "rowid "},
6547c478bd9Sstevel@tonic-gate             { OP_Recno,       1,  0,  0},
6557c478bd9Sstevel@tonic-gate             { OP_String,      0,  0,  " missing from index "},
6567c478bd9Sstevel@tonic-gate             { OP_String,      0,  0,  0},    /* 4 */
6577c478bd9Sstevel@tonic-gate             { OP_Concat,      4,  0,  0},
6587c478bd9Sstevel@tonic-gate             { OP_Callback,    1,  0,  0},
6597c478bd9Sstevel@tonic-gate           };
6607c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Recno, 1, 0);
6617c478bd9Sstevel@tonic-gate           for(k=0; k<pIdx->nColumn; k++){
6627c478bd9Sstevel@tonic-gate             int idx = pIdx->aiColumn[k];
6637c478bd9Sstevel@tonic-gate             if( idx==pTab->iPKey ){
6647c478bd9Sstevel@tonic-gate               sqliteVdbeAddOp(v, OP_Recno, 1, 0);
6657c478bd9Sstevel@tonic-gate             }else{
6667c478bd9Sstevel@tonic-gate               sqliteVdbeAddOp(v, OP_Column, 1, idx);
6677c478bd9Sstevel@tonic-gate             }
6687c478bd9Sstevel@tonic-gate           }
6697c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0);
6707c478bd9Sstevel@tonic-gate           if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIdx);
6717c478bd9Sstevel@tonic-gate           jmp2 = sqliteVdbeAddOp(v, OP_Found, j+2, 0);
6727c478bd9Sstevel@tonic-gate           addr = sqliteVdbeAddOpList(v, ArraySize(idxErr), idxErr);
6737c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
6747c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP2(v, jmp2, sqliteVdbeCurrentAddr(v));
6757c478bd9Sstevel@tonic-gate         }
6767c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Next, 1, loopTop+1);
6777c478bd9Sstevel@tonic-gate         sqliteVdbeChangeP2(v, loopTop, sqliteVdbeCurrentAddr(v));
6787c478bd9Sstevel@tonic-gate         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
6797c478bd9Sstevel@tonic-gate           static VdbeOpList cntIdx[] = {
6807c478bd9Sstevel@tonic-gate              { OP_Integer,      0,  0,  0},
6817c478bd9Sstevel@tonic-gate              { OP_MemStore,     2,  1,  0},
6827c478bd9Sstevel@tonic-gate              { OP_Rewind,       0,  0,  0},  /* 2 */
6837c478bd9Sstevel@tonic-gate              { OP_MemIncr,      2,  0,  0},
6847c478bd9Sstevel@tonic-gate              { OP_Next,         0,  0,  0},  /* 4 */
6857c478bd9Sstevel@tonic-gate              { OP_MemLoad,      1,  0,  0},
6867c478bd9Sstevel@tonic-gate              { OP_MemLoad,      2,  0,  0},
6877c478bd9Sstevel@tonic-gate              { OP_Eq,           0,  0,  0},  /* 7 */
6887c478bd9Sstevel@tonic-gate              { OP_MemIncr,      0,  0,  0},
6897c478bd9Sstevel@tonic-gate              { OP_String,       0,  0,  "wrong # of entries in index "},
6907c478bd9Sstevel@tonic-gate              { OP_String,       0,  0,  0},  /* 10 */
6917c478bd9Sstevel@tonic-gate              { OP_Concat,       2,  0,  0},
6927c478bd9Sstevel@tonic-gate              { OP_Callback,     1,  0,  0},
6937c478bd9Sstevel@tonic-gate           };
6947c478bd9Sstevel@tonic-gate           if( pIdx->tnum==0 ) continue;
6957c478bd9Sstevel@tonic-gate           addr = sqliteVdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
6967c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP1(v, addr+2, j+2);
6977c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP2(v, addr+2, addr+5);
6987c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP1(v, addr+4, j+2);
6997c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP2(v, addr+4, addr+3);
7007c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP2(v, addr+7, addr+ArraySize(cntIdx));
7017c478bd9Sstevel@tonic-gate           sqliteVdbeChangeP3(v, addr+10, pIdx->zName, P3_STATIC);
7027c478bd9Sstevel@tonic-gate         }
703*1da57d55SToomas Soome       }
7047c478bd9Sstevel@tonic-gate     }
7057c478bd9Sstevel@tonic-gate     addr = sqliteVdbeAddOpList(v, ArraySize(endCode), endCode);
7067c478bd9Sstevel@tonic-gate     sqliteVdbeChangeP2(v, addr+2, addr+ArraySize(endCode));
7077c478bd9Sstevel@tonic-gate   }else
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate   {}
7107c478bd9Sstevel@tonic-gate   sqliteFree(zLeft);
7117c478bd9Sstevel@tonic-gate   sqliteFree(zRight);
7127c478bd9Sstevel@tonic-gate }
713