000001 /* 000002 ** 2003 April 6 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** This file contains code used to implement the PRAGMA command. 000013 */ 000014 #include "sqliteInt.h" 000015 000016 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) 000017 # if defined(__APPLE__) 000018 # define SQLITE_ENABLE_LOCKING_STYLE 1 000019 # else 000020 # define SQLITE_ENABLE_LOCKING_STYLE 0 000021 # endif 000022 #endif 000023 000024 /*************************************************************************** 000025 ** The "pragma.h" include file is an automatically generated file that 000026 ** that includes the PragType_XXXX macro definitions and the aPragmaName[] 000027 ** object. This ensures that the aPragmaName[] table is arranged in 000028 ** lexicographical order to facility a binary search of the pragma name. 000029 ** Do not edit pragma.h directly. Edit and rerun the script in at 000030 ** ../tool/mkpragmatab.tcl. */ 000031 #include "pragma.h" 000032 000033 /* 000034 ** Interpret the given string as a safety level. Return 0 for OFF, 000035 ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or 000036 ** unrecognized string argument. The FULL and EXTRA option is disallowed 000037 ** if the omitFull parameter it 1. 000038 ** 000039 ** Note that the values returned are one less that the values that 000040 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done 000041 ** to support legacy SQL code. The safety level used to be boolean 000042 ** and older scripts may have used numbers 0 for OFF and 1 for ON. 000043 */ 000044 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){ 000045 /* 123456789 123456789 123 */ 000046 static const char zText[] = "onoffalseyestruextrafull"; 000047 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20}; 000048 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4}; 000049 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2}; 000050 /* on no off false yes true extra full */ 000051 int i, n; 000052 if( sqlite3Isdigit(*z) ){ 000053 return (u8)sqlite3Atoi(z); 000054 } 000055 n = sqlite3Strlen30(z); 000056 for(i=0; i<ArraySize(iLength); i++){ 000057 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 000058 && (!omitFull || iValue[i]<=1) 000059 ){ 000060 return iValue[i]; 000061 } 000062 } 000063 return dflt; 000064 } 000065 000066 /* 000067 ** Interpret the given string as a boolean value. 000068 */ 000069 u8 sqlite3GetBoolean(const char *z, u8 dflt){ 000070 return getSafetyLevel(z,1,dflt)!=0; 000071 } 000072 000073 /* The sqlite3GetBoolean() function is used by other modules but the 000074 ** remainder of this file is specific to PRAGMA processing. So omit 000075 ** the rest of the file if PRAGMAs are omitted from the build. 000076 */ 000077 #if !defined(SQLITE_OMIT_PRAGMA) 000078 000079 /* 000080 ** Interpret the given string as a locking mode value. 000081 */ 000082 static int getLockingMode(const char *z){ 000083 if( z ){ 000084 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE; 000085 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL; 000086 } 000087 return PAGER_LOCKINGMODE_QUERY; 000088 } 000089 000090 #ifndef SQLITE_OMIT_AUTOVACUUM 000091 /* 000092 ** Interpret the given string as an auto-vacuum mode value. 000093 ** 000094 ** The following strings, "none", "full" and "incremental" are 000095 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. 000096 */ 000097 static int getAutoVacuum(const char *z){ 000098 int i; 000099 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE; 000100 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL; 000101 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR; 000102 i = sqlite3Atoi(z); 000103 return (u8)((i>=0&&i<=2)?i:0); 000104 } 000105 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ 000106 000107 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 000108 /* 000109 ** Interpret the given string as a temp db location. Return 1 for file 000110 ** backed temporary databases, 2 for the Red-Black tree in memory database 000111 ** and 0 to use the compile-time default. 000112 */ 000113 static int getTempStore(const char *z){ 000114 if( z[0]>='0' && z[0]<='2' ){ 000115 return z[0] - '0'; 000116 }else if( sqlite3StrICmp(z, "file")==0 ){ 000117 return 1; 000118 }else if( sqlite3StrICmp(z, "memory")==0 ){ 000119 return 2; 000120 }else{ 000121 return 0; 000122 } 000123 } 000124 #endif /* SQLITE_PAGER_PRAGMAS */ 000125 000126 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 000127 /* 000128 ** Invalidate temp storage, either when the temp storage is changed 000129 ** from default, or when 'file' and the temp_store_directory has changed 000130 */ 000131 static int invalidateTempStorage(Parse *pParse){ 000132 sqlite3 *db = pParse->db; 000133 if( db->aDb[1].pBt!=0 ){ 000134 if( !db->autoCommit 000135 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE 000136 ){ 000137 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " 000138 "from within a transaction"); 000139 return SQLITE_ERROR; 000140 } 000141 sqlite3BtreeClose(db->aDb[1].pBt); 000142 db->aDb[1].pBt = 0; 000143 sqlite3ResetAllSchemasOfConnection(db); 000144 } 000145 return SQLITE_OK; 000146 } 000147 #endif /* SQLITE_PAGER_PRAGMAS */ 000148 000149 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 000150 /* 000151 ** If the TEMP database is open, close it and mark the database schema 000152 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE 000153 ** or DEFAULT_TEMP_STORE pragmas. 000154 */ 000155 static int changeTempStorage(Parse *pParse, const char *zStorageType){ 000156 int ts = getTempStore(zStorageType); 000157 sqlite3 *db = pParse->db; 000158 if( db->temp_store==ts ) return SQLITE_OK; 000159 if( invalidateTempStorage( pParse ) != SQLITE_OK ){ 000160 return SQLITE_ERROR; 000161 } 000162 db->temp_store = (u8)ts; 000163 return SQLITE_OK; 000164 } 000165 #endif /* SQLITE_PAGER_PRAGMAS */ 000166 000167 /* 000168 ** Set result column names for a pragma. 000169 */ 000170 static void setPragmaResultColumnNames( 000171 Vdbe *v, /* The query under construction */ 000172 const PragmaName *pPragma /* The pragma */ 000173 ){ 000174 u8 n = pPragma->nPragCName; 000175 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n); 000176 if( n==0 ){ 000177 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC); 000178 }else{ 000179 int i, j; 000180 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){ 000181 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC); 000182 } 000183 } 000184 } 000185 000186 /* 000187 ** Generate code to return a single integer value. 000188 */ 000189 static void returnSingleInt(Vdbe *v, i64 value){ 000190 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64); 000191 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 000192 } 000193 000194 /* 000195 ** Generate code to return a single text value. 000196 */ 000197 static void returnSingleText( 000198 Vdbe *v, /* Prepared statement under construction */ 000199 const char *zValue /* Value to be returned */ 000200 ){ 000201 if( zValue ){ 000202 sqlite3VdbeLoadString(v, 1, (const char*)zValue); 000203 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 000204 } 000205 } 000206 000207 000208 /* 000209 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0 000210 ** set these values for all pagers. 000211 */ 000212 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 000213 static void setAllPagerFlags(sqlite3 *db){ 000214 if( db->autoCommit ){ 000215 Db *pDb = db->aDb; 000216 int n = db->nDb; 000217 assert( SQLITE_FullFSync==PAGER_FULLFSYNC ); 000218 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC ); 000219 assert( SQLITE_CacheSpill==PAGER_CACHESPILL ); 000220 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL) 000221 == PAGER_FLAGS_MASK ); 000222 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level ); 000223 while( (n--) > 0 ){ 000224 if( pDb->pBt ){ 000225 sqlite3BtreeSetPagerFlags(pDb->pBt, 000226 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) ); 000227 } 000228 pDb++; 000229 } 000230 } 000231 } 000232 #else 000233 # define setAllPagerFlags(X) /* no-op */ 000234 #endif 000235 000236 000237 /* 000238 ** Return a human-readable name for a constraint resolution action. 000239 */ 000240 #ifndef SQLITE_OMIT_FOREIGN_KEY 000241 static const char *actionName(u8 action){ 000242 const char *zName; 000243 switch( action ){ 000244 case OE_SetNull: zName = "SET NULL"; break; 000245 case OE_SetDflt: zName = "SET DEFAULT"; break; 000246 case OE_Cascade: zName = "CASCADE"; break; 000247 case OE_Restrict: zName = "RESTRICT"; break; 000248 default: zName = "NO ACTION"; 000249 assert( action==OE_None ); break; 000250 } 000251 return zName; 000252 } 000253 #endif 000254 000255 000256 /* 000257 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants 000258 ** defined in pager.h. This function returns the associated lowercase 000259 ** journal-mode name. 000260 */ 000261 const char *sqlite3JournalModename(int eMode){ 000262 static char * const azModeName[] = { 000263 "delete", "persist", "off", "truncate", "memory" 000264 #ifndef SQLITE_OMIT_WAL 000265 , "wal" 000266 #endif 000267 }; 000268 assert( PAGER_JOURNALMODE_DELETE==0 ); 000269 assert( PAGER_JOURNALMODE_PERSIST==1 ); 000270 assert( PAGER_JOURNALMODE_OFF==2 ); 000271 assert( PAGER_JOURNALMODE_TRUNCATE==3 ); 000272 assert( PAGER_JOURNALMODE_MEMORY==4 ); 000273 assert( PAGER_JOURNALMODE_WAL==5 ); 000274 assert( eMode>=0 && eMode<=ArraySize(azModeName) ); 000275 000276 if( eMode==ArraySize(azModeName) ) return 0; 000277 return azModeName[eMode]; 000278 } 000279 000280 /* 000281 ** Locate a pragma in the aPragmaName[] array. 000282 */ 000283 static const PragmaName *pragmaLocate(const char *zName){ 000284 int upr, lwr, mid = 0, rc; 000285 lwr = 0; 000286 upr = ArraySize(aPragmaName)-1; 000287 while( lwr<=upr ){ 000288 mid = (lwr+upr)/2; 000289 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName); 000290 if( rc==0 ) break; 000291 if( rc<0 ){ 000292 upr = mid - 1; 000293 }else{ 000294 lwr = mid + 1; 000295 } 000296 } 000297 return lwr>upr ? 0 : &aPragmaName[mid]; 000298 } 000299 000300 /* 000301 ** Create zero or more entries in the output for the SQL functions 000302 ** defined by FuncDef p. 000303 */ 000304 static void pragmaFunclistLine( 000305 Vdbe *v, /* The prepared statement being created */ 000306 FuncDef *p, /* A particular function definition */ 000307 int isBuiltin, /* True if this is a built-in function */ 000308 int showInternFuncs /* True if showing internal functions */ 000309 ){ 000310 u32 mask = 000311 SQLITE_DETERMINISTIC | 000312 SQLITE_DIRECTONLY | 000313 SQLITE_SUBTYPE | 000314 SQLITE_INNOCUOUS | 000315 SQLITE_FUNC_INTERNAL 000316 ; 000317 if( showInternFuncs ) mask = 0xffffffff; 000318 for(; p; p=p->pNext){ 000319 const char *zType; 000320 static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" }; 000321 000322 assert( SQLITE_FUNC_ENCMASK==0x3 ); 000323 assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 ); 000324 assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 ); 000325 assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 ); 000326 000327 if( p->xSFunc==0 ) continue; 000328 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0 000329 && showInternFuncs==0 000330 ){ 000331 continue; 000332 } 000333 if( p->xValue!=0 ){ 000334 zType = "w"; 000335 }else if( p->xFinalize!=0 ){ 000336 zType = "a"; 000337 }else{ 000338 zType = "s"; 000339 } 000340 sqlite3VdbeMultiLoad(v, 1, "sissii", 000341 p->zName, isBuiltin, 000342 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK], 000343 p->nArg, 000344 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS 000345 ); 000346 } 000347 } 000348 000349 000350 /* 000351 ** Helper subroutine for PRAGMA integrity_check: 000352 ** 000353 ** Generate code to output a single-column result row with a value of the 000354 ** string held in register 3. Decrement the result count in register 1 000355 ** and halt if the maximum number of result rows have been issued. 000356 */ 000357 static int integrityCheckResultRow(Vdbe *v){ 000358 int addr; 000359 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); 000360 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1); 000361 VdbeCoverage(v); 000362 sqlite3VdbeAddOp0(v, OP_Halt); 000363 return addr; 000364 } 000365 000366 /* 000367 ** Process a pragma statement. 000368 ** 000369 ** Pragmas are of this form: 000370 ** 000371 ** PRAGMA [schema.]id [= value] 000372 ** 000373 ** The identifier might also be a string. The value is a string, and 000374 ** identifier, or a number. If minusFlag is true, then the value is 000375 ** a number that was preceded by a minus sign. 000376 ** 000377 ** If the left side is "database.id" then pId1 is the database name 000378 ** and pId2 is the id. If the left side is just "id" then pId1 is the 000379 ** id and pId2 is any empty string. 000380 */ 000381 void sqlite3Pragma( 000382 Parse *pParse, 000383 Token *pId1, /* First part of [schema.]id field */ 000384 Token *pId2, /* Second part of [schema.]id field, or NULL */ 000385 Token *pValue, /* Token for <value>, or NULL */ 000386 int minusFlag /* True if a '-' sign preceded <value> */ 000387 ){ 000388 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */ 000389 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */ 000390 const char *zDb = 0; /* The database name */ 000391 Token *pId; /* Pointer to <id> token */ 000392 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */ 000393 int iDb; /* Database index for <database> */ 000394 int rc; /* return value form SQLITE_FCNTL_PRAGMA */ 000395 sqlite3 *db = pParse->db; /* The database connection */ 000396 Db *pDb; /* The specific database being pragmaed */ 000397 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */ 000398 const PragmaName *pPragma; /* The pragma */ 000399 000400 if( v==0 ) return; 000401 sqlite3VdbeRunOnlyOnce(v); 000402 pParse->nMem = 2; 000403 000404 /* Interpret the [schema.] part of the pragma statement. iDb is the 000405 ** index of the database this pragma is being applied to in db.aDb[]. */ 000406 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); 000407 if( iDb<0 ) return; 000408 pDb = &db->aDb[iDb]; 000409 000410 /* If the temp database has been explicitly named as part of the 000411 ** pragma, make sure it is open. 000412 */ 000413 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ 000414 return; 000415 } 000416 000417 zLeft = sqlite3NameFromToken(db, pId); 000418 if( !zLeft ) return; 000419 if( minusFlag ){ 000420 zRight = sqlite3MPrintf(db, "-%T", pValue); 000421 }else{ 000422 zRight = sqlite3NameFromToken(db, pValue); 000423 } 000424 000425 assert( pId2 ); 000426 zDb = pId2->n>0 ? pDb->zDbSName : 0; 000427 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ 000428 goto pragma_out; 000429 } 000430 000431 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS 000432 ** connection. If it returns SQLITE_OK, then assume that the VFS 000433 ** handled the pragma and generate a no-op prepared statement. 000434 ** 000435 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, 000436 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file 000437 ** object corresponding to the database file to which the pragma 000438 ** statement refers. 000439 ** 000440 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA 000441 ** file control is an array of pointers to strings (char**) in which the 000442 ** second element of the array is the name of the pragma and the third 000443 ** element is the argument to the pragma or NULL if the pragma has no 000444 ** argument. 000445 */ 000446 aFcntl[0] = 0; 000447 aFcntl[1] = zLeft; 000448 aFcntl[2] = zRight; 000449 aFcntl[3] = 0; 000450 db->busyHandler.nBusy = 0; 000451 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl); 000452 if( rc==SQLITE_OK ){ 000453 sqlite3VdbeSetNumCols(v, 1); 000454 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT); 000455 returnSingleText(v, aFcntl[0]); 000456 sqlite3_free(aFcntl[0]); 000457 goto pragma_out; 000458 } 000459 if( rc!=SQLITE_NOTFOUND ){ 000460 if( aFcntl[0] ){ 000461 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]); 000462 sqlite3_free(aFcntl[0]); 000463 } 000464 pParse->nErr++; 000465 pParse->rc = rc; 000466 goto pragma_out; 000467 } 000468 000469 /* Locate the pragma in the lookup table */ 000470 pPragma = pragmaLocate(zLeft); 000471 if( pPragma==0 ){ 000472 /* IMP: R-43042-22504 No error messages are generated if an 000473 ** unknown pragma is issued. */ 000474 goto pragma_out; 000475 } 000476 000477 /* Make sure the database schema is loaded if the pragma requires that */ 000478 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){ 000479 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 000480 } 000481 000482 /* Register the result column names for pragmas that return results */ 000483 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 000484 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0) 000485 ){ 000486 setPragmaResultColumnNames(v, pPragma); 000487 } 000488 000489 /* Jump to the appropriate pragma handler */ 000490 switch( pPragma->ePragTyp ){ 000491 000492 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) 000493 /* 000494 ** PRAGMA [schema.]default_cache_size 000495 ** PRAGMA [schema.]default_cache_size=N 000496 ** 000497 ** The first form reports the current persistent setting for the 000498 ** page cache size. The value returned is the maximum number of 000499 ** pages in the page cache. The second form sets both the current 000500 ** page cache size value and the persistent page cache size value 000501 ** stored in the database file. 000502 ** 000503 ** Older versions of SQLite would set the default cache size to a 000504 ** negative number to indicate synchronous=OFF. These days, synchronous 000505 ** is always on by default regardless of the sign of the default cache 000506 ** size. But continue to take the absolute value of the default cache 000507 ** size of historical compatibility. 000508 */ 000509 case PragTyp_DEFAULT_CACHE_SIZE: { 000510 static const int iLn = VDBE_OFFSET_LINENO(2); 000511 static const VdbeOpList getCacheSize[] = { 000512 { OP_Transaction, 0, 0, 0}, /* 0 */ 000513 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */ 000514 { OP_IfPos, 1, 8, 0}, 000515 { OP_Integer, 0, 2, 0}, 000516 { OP_Subtract, 1, 2, 1}, 000517 { OP_IfPos, 1, 8, 0}, 000518 { OP_Integer, 0, 1, 0}, /* 6 */ 000519 { OP_Noop, 0, 0, 0}, 000520 { OP_ResultRow, 1, 1, 0}, 000521 }; 000522 VdbeOp *aOp; 000523 sqlite3VdbeUsesBtree(v, iDb); 000524 if( !zRight ){ 000525 pParse->nMem += 2; 000526 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize)); 000527 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn); 000528 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 000529 aOp[0].p1 = iDb; 000530 aOp[1].p1 = iDb; 000531 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE; 000532 }else{ 000533 int size = sqlite3AbsInt32(sqlite3Atoi(zRight)); 000534 sqlite3BeginWriteOperation(pParse, 0, iDb); 000535 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size); 000536 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 000537 pDb->pSchema->cache_size = size; 000538 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); 000539 } 000540 break; 000541 } 000542 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */ 000543 000544 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) 000545 /* 000546 ** PRAGMA [schema.]page_size 000547 ** PRAGMA [schema.]page_size=N 000548 ** 000549 ** The first form reports the current setting for the 000550 ** database page size in bytes. The second form sets the 000551 ** database page size value. The value can only be set if 000552 ** the database has not yet been created. 000553 */ 000554 case PragTyp_PAGE_SIZE: { 000555 Btree *pBt = pDb->pBt; 000556 assert( pBt!=0 ); 000557 if( !zRight ){ 000558 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; 000559 returnSingleInt(v, size); 000560 }else{ 000561 /* Malloc may fail when setting the page-size, as there is an internal 000562 ** buffer that the pager module resizes using sqlite3_realloc(). 000563 */ 000564 db->nextPagesize = sqlite3Atoi(zRight); 000565 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){ 000566 sqlite3OomFault(db); 000567 } 000568 } 000569 break; 000570 } 000571 000572 /* 000573 ** PRAGMA [schema.]secure_delete 000574 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST 000575 ** 000576 ** The first form reports the current setting for the 000577 ** secure_delete flag. The second form changes the secure_delete 000578 ** flag setting and reports the new value. 000579 */ 000580 case PragTyp_SECURE_DELETE: { 000581 Btree *pBt = pDb->pBt; 000582 int b = -1; 000583 assert( pBt!=0 ); 000584 if( zRight ){ 000585 if( sqlite3_stricmp(zRight, "fast")==0 ){ 000586 b = 2; 000587 }else{ 000588 b = sqlite3GetBoolean(zRight, 0); 000589 } 000590 } 000591 if( pId2->n==0 && b>=0 ){ 000592 int ii; 000593 for(ii=0; ii<db->nDb; ii++){ 000594 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b); 000595 } 000596 } 000597 b = sqlite3BtreeSecureDelete(pBt, b); 000598 returnSingleInt(v, b); 000599 break; 000600 } 000601 000602 /* 000603 ** PRAGMA [schema.]max_page_count 000604 ** PRAGMA [schema.]max_page_count=N 000605 ** 000606 ** The first form reports the current setting for the 000607 ** maximum number of pages in the database file. The 000608 ** second form attempts to change this setting. Both 000609 ** forms return the current setting. 000610 ** 000611 ** The absolute value of N is used. This is undocumented and might 000612 ** change. The only purpose is to provide an easy way to test 000613 ** the sqlite3AbsInt32() function. 000614 ** 000615 ** PRAGMA [schema.]page_count 000616 ** 000617 ** Return the number of pages in the specified database. 000618 */ 000619 case PragTyp_PAGE_COUNT: { 000620 int iReg; 000621 i64 x = 0; 000622 sqlite3CodeVerifySchema(pParse, iDb); 000623 iReg = ++pParse->nMem; 000624 if( sqlite3Tolower(zLeft[0])=='p' ){ 000625 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); 000626 }else{ 000627 if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){ 000628 if( x<0 ) x = 0; 000629 else if( x>0xfffffffe ) x = 0xfffffffe; 000630 }else{ 000631 x = 0; 000632 } 000633 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x); 000634 } 000635 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); 000636 break; 000637 } 000638 000639 /* 000640 ** PRAGMA [schema.]locking_mode 000641 ** PRAGMA [schema.]locking_mode = (normal|exclusive) 000642 */ 000643 case PragTyp_LOCKING_MODE: { 000644 const char *zRet = "normal"; 000645 int eMode = getLockingMode(zRight); 000646 000647 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ 000648 /* Simple "PRAGMA locking_mode;" statement. This is a query for 000649 ** the current default locking mode (which may be different to 000650 ** the locking-mode of the main database). 000651 */ 000652 eMode = db->dfltLockMode; 000653 }else{ 000654 Pager *pPager; 000655 if( pId2->n==0 ){ 000656 /* This indicates that no database name was specified as part 000657 ** of the PRAGMA command. In this case the locking-mode must be 000658 ** set on all attached databases, as well as the main db file. 000659 ** 000660 ** Also, the sqlite3.dfltLockMode variable is set so that 000661 ** any subsequently attached databases also use the specified 000662 ** locking mode. 000663 */ 000664 int ii; 000665 assert(pDb==&db->aDb[0]); 000666 for(ii=2; ii<db->nDb; ii++){ 000667 pPager = sqlite3BtreePager(db->aDb[ii].pBt); 000668 sqlite3PagerLockingMode(pPager, eMode); 000669 } 000670 db->dfltLockMode = (u8)eMode; 000671 } 000672 pPager = sqlite3BtreePager(pDb->pBt); 000673 eMode = sqlite3PagerLockingMode(pPager, eMode); 000674 } 000675 000676 assert( eMode==PAGER_LOCKINGMODE_NORMAL 000677 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); 000678 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ 000679 zRet = "exclusive"; 000680 } 000681 returnSingleText(v, zRet); 000682 break; 000683 } 000684 000685 /* 000686 ** PRAGMA [schema.]journal_mode 000687 ** PRAGMA [schema.]journal_mode = 000688 ** (delete|persist|off|truncate|memory|wal|off) 000689 */ 000690 case PragTyp_JOURNAL_MODE: { 000691 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ 000692 int ii; /* Loop counter */ 000693 000694 if( zRight==0 ){ 000695 /* If there is no "=MODE" part of the pragma, do a query for the 000696 ** current mode */ 000697 eMode = PAGER_JOURNALMODE_QUERY; 000698 }else{ 000699 const char *zMode; 000700 int n = sqlite3Strlen30(zRight); 000701 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){ 000702 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break; 000703 } 000704 if( !zMode ){ 000705 /* If the "=MODE" part does not match any known journal mode, 000706 ** then do a query */ 000707 eMode = PAGER_JOURNALMODE_QUERY; 000708 } 000709 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){ 000710 /* Do not allow journal-mode "OFF" in defensive since the database 000711 ** can become corrupted using ordinary SQL when the journal is off */ 000712 eMode = PAGER_JOURNALMODE_QUERY; 000713 } 000714 } 000715 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ 000716 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ 000717 iDb = 0; 000718 pId2->n = 1; 000719 } 000720 for(ii=db->nDb-1; ii>=0; ii--){ 000721 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ 000722 sqlite3VdbeUsesBtree(v, ii); 000723 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode); 000724 } 000725 } 000726 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 000727 break; 000728 } 000729 000730 /* 000731 ** PRAGMA [schema.]journal_size_limit 000732 ** PRAGMA [schema.]journal_size_limit=N 000733 ** 000734 ** Get or set the size limit on rollback journal files. 000735 */ 000736 case PragTyp_JOURNAL_SIZE_LIMIT: { 000737 Pager *pPager = sqlite3BtreePager(pDb->pBt); 000738 i64 iLimit = -2; 000739 if( zRight ){ 000740 sqlite3DecOrHexToI64(zRight, &iLimit); 000741 if( iLimit<-1 ) iLimit = -1; 000742 } 000743 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); 000744 returnSingleInt(v, iLimit); 000745 break; 000746 } 000747 000748 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ 000749 000750 /* 000751 ** PRAGMA [schema.]auto_vacuum 000752 ** PRAGMA [schema.]auto_vacuum=N 000753 ** 000754 ** Get or set the value of the database 'auto-vacuum' parameter. 000755 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL 000756 */ 000757 #ifndef SQLITE_OMIT_AUTOVACUUM 000758 case PragTyp_AUTO_VACUUM: { 000759 Btree *pBt = pDb->pBt; 000760 assert( pBt!=0 ); 000761 if( !zRight ){ 000762 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt)); 000763 }else{ 000764 int eAuto = getAutoVacuum(zRight); 000765 assert( eAuto>=0 && eAuto<=2 ); 000766 db->nextAutovac = (u8)eAuto; 000767 /* Call SetAutoVacuum() to set initialize the internal auto and 000768 ** incr-vacuum flags. This is required in case this connection 000769 ** creates the database file. It is important that it is created 000770 ** as an auto-vacuum capable db. 000771 */ 000772 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); 000773 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ 000774 /* When setting the auto_vacuum mode to either "full" or 000775 ** "incremental", write the value of meta[6] in the database 000776 ** file. Before writing to meta[6], check that meta[3] indicates 000777 ** that this really is an auto-vacuum capable database. 000778 */ 000779 static const int iLn = VDBE_OFFSET_LINENO(2); 000780 static const VdbeOpList setMeta6[] = { 000781 { OP_Transaction, 0, 1, 0}, /* 0 */ 000782 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE}, 000783 { OP_If, 1, 0, 0}, /* 2 */ 000784 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ 000785 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */ 000786 }; 000787 VdbeOp *aOp; 000788 int iAddr = sqlite3VdbeCurrentAddr(v); 000789 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6)); 000790 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn); 000791 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 000792 aOp[0].p1 = iDb; 000793 aOp[1].p1 = iDb; 000794 aOp[2].p2 = iAddr+4; 000795 aOp[4].p1 = iDb; 000796 aOp[4].p3 = eAuto - 1; 000797 sqlite3VdbeUsesBtree(v, iDb); 000798 } 000799 } 000800 break; 000801 } 000802 #endif 000803 000804 /* 000805 ** PRAGMA [schema.]incremental_vacuum(N) 000806 ** 000807 ** Do N steps of incremental vacuuming on a database. 000808 */ 000809 #ifndef SQLITE_OMIT_AUTOVACUUM 000810 case PragTyp_INCREMENTAL_VACUUM: { 000811 int iLimit = 0, addr; 000812 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ 000813 iLimit = 0x7fffffff; 000814 } 000815 sqlite3BeginWriteOperation(pParse, 0, iDb); 000816 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); 000817 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v); 000818 sqlite3VdbeAddOp1(v, OP_ResultRow, 1); 000819 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); 000820 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v); 000821 sqlite3VdbeJumpHere(v, addr); 000822 break; 000823 } 000824 #endif 000825 000826 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 000827 /* 000828 ** PRAGMA [schema.]cache_size 000829 ** PRAGMA [schema.]cache_size=N 000830 ** 000831 ** The first form reports the current local setting for the 000832 ** page cache size. The second form sets the local 000833 ** page cache size value. If N is positive then that is the 000834 ** number of pages in the cache. If N is negative, then the 000835 ** number of pages is adjusted so that the cache uses -N kibibytes 000836 ** of memory. 000837 */ 000838 case PragTyp_CACHE_SIZE: { 000839 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 000840 if( !zRight ){ 000841 returnSingleInt(v, pDb->pSchema->cache_size); 000842 }else{ 000843 int size = sqlite3Atoi(zRight); 000844 pDb->pSchema->cache_size = size; 000845 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); 000846 } 000847 break; 000848 } 000849 000850 /* 000851 ** PRAGMA [schema.]cache_spill 000852 ** PRAGMA cache_spill=BOOLEAN 000853 ** PRAGMA [schema.]cache_spill=N 000854 ** 000855 ** The first form reports the current local setting for the 000856 ** page cache spill size. The second form turns cache spill on 000857 ** or off. When turning cache spill on, the size is set to the 000858 ** current cache_size. The third form sets a spill size that 000859 ** may be different form the cache size. 000860 ** If N is positive then that is the 000861 ** number of pages in the cache. If N is negative, then the 000862 ** number of pages is adjusted so that the cache uses -N kibibytes 000863 ** of memory. 000864 ** 000865 ** If the number of cache_spill pages is less then the number of 000866 ** cache_size pages, no spilling occurs until the page count exceeds 000867 ** the number of cache_size pages. 000868 ** 000869 ** The cache_spill=BOOLEAN setting applies to all attached schemas, 000870 ** not just the schema specified. 000871 */ 000872 case PragTyp_CACHE_SPILL: { 000873 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 000874 if( !zRight ){ 000875 returnSingleInt(v, 000876 (db->flags & SQLITE_CacheSpill)==0 ? 0 : 000877 sqlite3BtreeSetSpillSize(pDb->pBt,0)); 000878 }else{ 000879 int size = 1; 000880 if( sqlite3GetInt32(zRight, &size) ){ 000881 sqlite3BtreeSetSpillSize(pDb->pBt, size); 000882 } 000883 if( sqlite3GetBoolean(zRight, size!=0) ){ 000884 db->flags |= SQLITE_CacheSpill; 000885 }else{ 000886 db->flags &= ~(u64)SQLITE_CacheSpill; 000887 } 000888 setAllPagerFlags(db); 000889 } 000890 break; 000891 } 000892 000893 /* 000894 ** PRAGMA [schema.]mmap_size(N) 000895 ** 000896 ** Used to set mapping size limit. The mapping size limit is 000897 ** used to limit the aggregate size of all memory mapped regions of the 000898 ** database file. If this parameter is set to zero, then memory mapping 000899 ** is not used at all. If N is negative, then the default memory map 000900 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set. 000901 ** The parameter N is measured in bytes. 000902 ** 000903 ** This value is advisory. The underlying VFS is free to memory map 000904 ** as little or as much as it wants. Except, if N is set to 0 then the 000905 ** upper layers will never invoke the xFetch interfaces to the VFS. 000906 */ 000907 case PragTyp_MMAP_SIZE: { 000908 sqlite3_int64 sz; 000909 #if SQLITE_MAX_MMAP_SIZE>0 000910 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 000911 if( zRight ){ 000912 int ii; 000913 sqlite3DecOrHexToI64(zRight, &sz); 000914 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap; 000915 if( pId2->n==0 ) db->szMmap = sz; 000916 for(ii=db->nDb-1; ii>=0; ii--){ 000917 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ 000918 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); 000919 } 000920 } 000921 } 000922 sz = -1; 000923 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz); 000924 #else 000925 sz = 0; 000926 rc = SQLITE_OK; 000927 #endif 000928 if( rc==SQLITE_OK ){ 000929 returnSingleInt(v, sz); 000930 }else if( rc!=SQLITE_NOTFOUND ){ 000931 pParse->nErr++; 000932 pParse->rc = rc; 000933 } 000934 break; 000935 } 000936 000937 /* 000938 ** PRAGMA temp_store 000939 ** PRAGMA temp_store = "default"|"memory"|"file" 000940 ** 000941 ** Return or set the local value of the temp_store flag. Changing 000942 ** the local value does not make changes to the disk file and the default 000943 ** value will be restored the next time the database is opened. 000944 ** 000945 ** Note that it is possible for the library compile-time options to 000946 ** override this setting 000947 */ 000948 case PragTyp_TEMP_STORE: { 000949 if( !zRight ){ 000950 returnSingleInt(v, db->temp_store); 000951 }else{ 000952 changeTempStorage(pParse, zRight); 000953 } 000954 break; 000955 } 000956 000957 /* 000958 ** PRAGMA temp_store_directory 000959 ** PRAGMA temp_store_directory = ""|"directory_name" 000960 ** 000961 ** Return or set the local value of the temp_store_directory flag. Changing 000962 ** the value sets a specific directory to be used for temporary files. 000963 ** Setting to a null string reverts to the default temporary directory search. 000964 ** If temporary directory is changed, then invalidateTempStorage. 000965 ** 000966 */ 000967 case PragTyp_TEMP_STORE_DIRECTORY: { 000968 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); 000969 if( !zRight ){ 000970 returnSingleText(v, sqlite3_temp_directory); 000971 }else{ 000972 #ifndef SQLITE_OMIT_WSD 000973 if( zRight[0] ){ 000974 int res; 000975 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); 000976 if( rc!=SQLITE_OK || res==0 ){ 000977 sqlite3ErrorMsg(pParse, "not a writable directory"); 000978 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); 000979 goto pragma_out; 000980 } 000981 } 000982 if( SQLITE_TEMP_STORE==0 000983 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) 000984 || (SQLITE_TEMP_STORE==2 && db->temp_store==1) 000985 ){ 000986 invalidateTempStorage(pParse); 000987 } 000988 sqlite3_free(sqlite3_temp_directory); 000989 if( zRight[0] ){ 000990 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); 000991 }else{ 000992 sqlite3_temp_directory = 0; 000993 } 000994 #endif /* SQLITE_OMIT_WSD */ 000995 } 000996 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); 000997 break; 000998 } 000999 001000 #if SQLITE_OS_WIN 001001 /* 001002 ** PRAGMA data_store_directory 001003 ** PRAGMA data_store_directory = ""|"directory_name" 001004 ** 001005 ** Return or set the local value of the data_store_directory flag. Changing 001006 ** the value sets a specific directory to be used for database files that 001007 ** were specified with a relative pathname. Setting to a null string reverts 001008 ** to the default database directory, which for database files specified with 001009 ** a relative path will probably be based on the current directory for the 001010 ** process. Database file specified with an absolute path are not impacted 001011 ** by this setting, regardless of its value. 001012 ** 001013 */ 001014 case PragTyp_DATA_STORE_DIRECTORY: { 001015 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); 001016 if( !zRight ){ 001017 returnSingleText(v, sqlite3_data_directory); 001018 }else{ 001019 #ifndef SQLITE_OMIT_WSD 001020 if( zRight[0] ){ 001021 int res; 001022 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); 001023 if( rc!=SQLITE_OK || res==0 ){ 001024 sqlite3ErrorMsg(pParse, "not a writable directory"); 001025 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); 001026 goto pragma_out; 001027 } 001028 } 001029 sqlite3_free(sqlite3_data_directory); 001030 if( zRight[0] ){ 001031 sqlite3_data_directory = sqlite3_mprintf("%s", zRight); 001032 }else{ 001033 sqlite3_data_directory = 0; 001034 } 001035 #endif /* SQLITE_OMIT_WSD */ 001036 } 001037 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); 001038 break; 001039 } 001040 #endif 001041 001042 #if SQLITE_ENABLE_LOCKING_STYLE 001043 /* 001044 ** PRAGMA [schema.]lock_proxy_file 001045 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path" 001046 ** 001047 ** Return or set the value of the lock_proxy_file flag. Changing 001048 ** the value sets a specific file to be used for database access locks. 001049 ** 001050 */ 001051 case PragTyp_LOCK_PROXY_FILE: { 001052 if( !zRight ){ 001053 Pager *pPager = sqlite3BtreePager(pDb->pBt); 001054 char *proxy_file_path = NULL; 001055 sqlite3_file *pFile = sqlite3PagerFile(pPager); 001056 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 001057 &proxy_file_path); 001058 returnSingleText(v, proxy_file_path); 001059 }else{ 001060 Pager *pPager = sqlite3BtreePager(pDb->pBt); 001061 sqlite3_file *pFile = sqlite3PagerFile(pPager); 001062 int res; 001063 if( zRight[0] ){ 001064 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 001065 zRight); 001066 } else { 001067 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 001068 NULL); 001069 } 001070 if( res!=SQLITE_OK ){ 001071 sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); 001072 goto pragma_out; 001073 } 001074 } 001075 break; 001076 } 001077 #endif /* SQLITE_ENABLE_LOCKING_STYLE */ 001078 001079 /* 001080 ** PRAGMA [schema.]synchronous 001081 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA 001082 ** 001083 ** Return or set the local value of the synchronous flag. Changing 001084 ** the local value does not make changes to the disk file and the 001085 ** default value will be restored the next time the database is 001086 ** opened. 001087 */ 001088 case PragTyp_SYNCHRONOUS: { 001089 if( !zRight ){ 001090 returnSingleInt(v, pDb->safety_level-1); 001091 }else{ 001092 if( !db->autoCommit ){ 001093 sqlite3ErrorMsg(pParse, 001094 "Safety level may not be changed inside a transaction"); 001095 }else if( iDb!=1 ){ 001096 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK; 001097 if( iLevel==0 ) iLevel = 1; 001098 pDb->safety_level = iLevel; 001099 pDb->bSyncSet = 1; 001100 setAllPagerFlags(db); 001101 } 001102 } 001103 break; 001104 } 001105 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ 001106 001107 #ifndef SQLITE_OMIT_FLAG_PRAGMAS 001108 case PragTyp_FLAG: { 001109 if( zRight==0 ){ 001110 setPragmaResultColumnNames(v, pPragma); 001111 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 ); 001112 }else{ 001113 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */ 001114 if( db->autoCommit==0 ){ 001115 /* Foreign key support may not be enabled or disabled while not 001116 ** in auto-commit mode. */ 001117 mask &= ~(SQLITE_ForeignKeys); 001118 } 001119 #if SQLITE_USER_AUTHENTICATION 001120 if( db->auth.authLevel==UAUTH_User ){ 001121 /* Do not allow non-admin users to modify the schema arbitrarily */ 001122 mask &= ~(SQLITE_WriteSchema); 001123 } 001124 #endif 001125 001126 if( sqlite3GetBoolean(zRight, 0) ){ 001127 if( (mask & SQLITE_WriteSchema)==0 001128 || (db->flags & SQLITE_Defensive)==0 001129 ){ 001130 db->flags |= mask; 001131 } 001132 }else{ 001133 db->flags &= ~mask; 001134 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0; 001135 if( (mask & SQLITE_WriteSchema)!=0 001136 && sqlite3_stricmp(zRight, "reset")==0 001137 ){ 001138 /* IMP: R-60817-01178 If the argument is "RESET" then schema 001139 ** writing is disabled (as with "PRAGMA writable_schema=OFF") and, 001140 ** in addition, the schema is reloaded. */ 001141 sqlite3ResetAllSchemasOfConnection(db); 001142 } 001143 } 001144 001145 /* Many of the flag-pragmas modify the code generated by the SQL 001146 ** compiler (eg. count_changes). So add an opcode to expire all 001147 ** compiled SQL statements after modifying a pragma value. 001148 */ 001149 sqlite3VdbeAddOp0(v, OP_Expire); 001150 setAllPagerFlags(db); 001151 } 001152 break; 001153 } 001154 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ 001155 001156 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS 001157 /* 001158 ** PRAGMA table_info(<table>) 001159 ** 001160 ** Return a single row for each column of the named table. The columns of 001161 ** the returned data set are: 001162 ** 001163 ** cid: Column id (numbered from left to right, starting at 0) 001164 ** name: Column name 001165 ** type: Column declaration type. 001166 ** notnull: True if 'NOT NULL' is part of column declaration 001167 ** dflt_value: The default value for the column, if any. 001168 ** pk: Non-zero for PK fields. 001169 */ 001170 case PragTyp_TABLE_INFO: if( zRight ){ 001171 Table *pTab; 001172 sqlite3CodeVerifyNamedSchema(pParse, zDb); 001173 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); 001174 if( pTab ){ 001175 int i, k; 001176 int nHidden = 0; 001177 Column *pCol; 001178 Index *pPk = sqlite3PrimaryKeyIndex(pTab); 001179 pParse->nMem = 7; 001180 sqlite3ViewGetColumnNames(pParse, pTab); 001181 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ 001182 int isHidden = 0; 001183 const Expr *pColExpr; 001184 if( pCol->colFlags & COLFLAG_NOINSERT ){ 001185 if( pPragma->iArg==0 ){ 001186 nHidden++; 001187 continue; 001188 } 001189 if( pCol->colFlags & COLFLAG_VIRTUAL ){ 001190 isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */ 001191 }else if( pCol->colFlags & COLFLAG_STORED ){ 001192 isHidden = 3; /* GENERATED ALWAYS AS ... STORED */ 001193 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN ); 001194 isHidden = 1; /* HIDDEN */ 001195 } 001196 } 001197 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ 001198 k = 0; 001199 }else if( pPk==0 ){ 001200 k = 1; 001201 }else{ 001202 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} 001203 } 001204 pColExpr = sqlite3ColumnExpr(pTab,pCol); 001205 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 ); 001206 assert( pColExpr==0 || !ExprHasProperty(pColExpr, EP_IntValue) 001207 || isHidden>=2 ); 001208 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi", 001209 i-nHidden, 001210 pCol->zCnName, 001211 sqlite3ColumnType(pCol,""), 001212 pCol->notNull ? 1 : 0, 001213 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken, 001214 k, 001215 isHidden); 001216 } 001217 } 001218 } 001219 break; 001220 001221 /* 001222 ** PRAGMA table_list 001223 ** 001224 ** Return a single row for each table, virtual table, or view in the 001225 ** entire schema. 001226 ** 001227 ** schema: Name of attached database hold this table 001228 ** name: Name of the table itself 001229 ** type: "table", "view", "virtual", "shadow" 001230 ** ncol: Number of columns 001231 ** wr: True for a WITHOUT ROWID table 001232 ** strict: True for a STRICT table 001233 */ 001234 case PragTyp_TABLE_LIST: { 001235 int ii; 001236 pParse->nMem = 6; 001237 sqlite3CodeVerifyNamedSchema(pParse, zDb); 001238 for(ii=0; ii<db->nDb; ii++){ 001239 HashElem *k; 001240 Hash *pHash; 001241 int initNCol; 001242 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue; 001243 001244 /* Ensure that the Table.nCol field is initialized for all views 001245 ** and virtual tables. Each time we initialize a Table.nCol value 001246 ** for a table, that can potentially disrupt the hash table, so restart 001247 ** the initialization scan. 001248 */ 001249 pHash = &db->aDb[ii].pSchema->tblHash; 001250 initNCol = sqliteHashCount(pHash); 001251 while( initNCol-- ){ 001252 for(k=sqliteHashFirst(pHash); 1; k=sqliteHashNext(k) ){ 001253 Table *pTab; 001254 if( k==0 ){ initNCol = 0; break; } 001255 pTab = sqliteHashData(k); 001256 if( pTab->nCol==0 ){ 001257 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName); 001258 if( zSql ){ 001259 sqlite3_stmt *pDummy = 0; 001260 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0); 001261 (void)sqlite3_finalize(pDummy); 001262 sqlite3DbFree(db, zSql); 001263 } 001264 if( db->mallocFailed ){ 001265 sqlite3ErrorMsg(db->pParse, "out of memory"); 001266 db->pParse->rc = SQLITE_NOMEM_BKPT; 001267 } 001268 pHash = &db->aDb[ii].pSchema->tblHash; 001269 break; 001270 } 001271 } 001272 } 001273 001274 for(k=sqliteHashFirst(pHash); k; k=sqliteHashNext(k) ){ 001275 Table *pTab = sqliteHashData(k); 001276 const char *zType; 001277 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue; 001278 if( IsView(pTab) ){ 001279 zType = "view"; 001280 }else if( IsVirtual(pTab) ){ 001281 zType = "virtual"; 001282 }else if( pTab->tabFlags & TF_Shadow ){ 001283 zType = "shadow"; 001284 }else{ 001285 zType = "table"; 001286 } 001287 sqlite3VdbeMultiLoad(v, 1, "sssiii", 001288 db->aDb[ii].zDbSName, 001289 sqlite3PreferredTableName(pTab->zName), 001290 zType, 001291 pTab->nCol, 001292 (pTab->tabFlags & TF_WithoutRowid)!=0, 001293 (pTab->tabFlags & TF_Strict)!=0 001294 ); 001295 } 001296 } 001297 } 001298 break; 001299 001300 #ifdef SQLITE_DEBUG 001301 case PragTyp_STATS: { 001302 Index *pIdx; 001303 HashElem *i; 001304 pParse->nMem = 5; 001305 sqlite3CodeVerifySchema(pParse, iDb); 001306 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){ 001307 Table *pTab = sqliteHashData(i); 001308 sqlite3VdbeMultiLoad(v, 1, "ssiii", 001309 sqlite3PreferredTableName(pTab->zName), 001310 0, 001311 pTab->szTabRow, 001312 pTab->nRowLogEst, 001313 pTab->tabFlags); 001314 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 001315 sqlite3VdbeMultiLoad(v, 2, "siiiX", 001316 pIdx->zName, 001317 pIdx->szIdxRow, 001318 pIdx->aiRowLogEst[0], 001319 pIdx->hasStat1); 001320 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); 001321 } 001322 } 001323 } 001324 break; 001325 #endif 001326 001327 case PragTyp_INDEX_INFO: if( zRight ){ 001328 Index *pIdx; 001329 Table *pTab; 001330 pIdx = sqlite3FindIndex(db, zRight, zDb); 001331 if( pIdx==0 ){ 001332 /* If there is no index named zRight, check to see if there is a 001333 ** WITHOUT ROWID table named zRight, and if there is, show the 001334 ** structure of the PRIMARY KEY index for that table. */ 001335 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); 001336 if( pTab && !HasRowid(pTab) ){ 001337 pIdx = sqlite3PrimaryKeyIndex(pTab); 001338 } 001339 } 001340 if( pIdx ){ 001341 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema); 001342 int i; 001343 int mx; 001344 if( pPragma->iArg ){ 001345 /* PRAGMA index_xinfo (newer version with more rows and columns) */ 001346 mx = pIdx->nColumn; 001347 pParse->nMem = 6; 001348 }else{ 001349 /* PRAGMA index_info (legacy version) */ 001350 mx = pIdx->nKeyCol; 001351 pParse->nMem = 3; 001352 } 001353 pTab = pIdx->pTable; 001354 sqlite3CodeVerifySchema(pParse, iIdxDb); 001355 assert( pParse->nMem<=pPragma->nPragCName ); 001356 for(i=0; i<mx; i++){ 001357 i16 cnum = pIdx->aiColumn[i]; 001358 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum, 001359 cnum<0 ? 0 : pTab->aCol[cnum].zCnName); 001360 if( pPragma->iArg ){ 001361 sqlite3VdbeMultiLoad(v, 4, "isiX", 001362 pIdx->aSortOrder[i], 001363 pIdx->azColl[i], 001364 i<pIdx->nKeyCol); 001365 } 001366 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem); 001367 } 001368 } 001369 } 001370 break; 001371 001372 case PragTyp_INDEX_LIST: if( zRight ){ 001373 Index *pIdx; 001374 Table *pTab; 001375 int i; 001376 pTab = sqlite3FindTable(db, zRight, zDb); 001377 if( pTab ){ 001378 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); 001379 pParse->nMem = 5; 001380 sqlite3CodeVerifySchema(pParse, iTabDb); 001381 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){ 001382 const char *azOrigin[] = { "c", "u", "pk" }; 001383 sqlite3VdbeMultiLoad(v, 1, "isisi", 001384 i, 001385 pIdx->zName, 001386 IsUniqueIndex(pIdx), 001387 azOrigin[pIdx->idxType], 001388 pIdx->pPartIdxWhere!=0); 001389 } 001390 } 001391 } 001392 break; 001393 001394 case PragTyp_DATABASE_LIST: { 001395 int i; 001396 pParse->nMem = 3; 001397 for(i=0; i<db->nDb; i++){ 001398 if( db->aDb[i].pBt==0 ) continue; 001399 assert( db->aDb[i].zDbSName!=0 ); 001400 sqlite3VdbeMultiLoad(v, 1, "iss", 001401 i, 001402 db->aDb[i].zDbSName, 001403 sqlite3BtreeGetFilename(db->aDb[i].pBt)); 001404 } 001405 } 001406 break; 001407 001408 case PragTyp_COLLATION_LIST: { 001409 int i = 0; 001410 HashElem *p; 001411 pParse->nMem = 2; 001412 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ 001413 CollSeq *pColl = (CollSeq *)sqliteHashData(p); 001414 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName); 001415 } 001416 } 001417 break; 001418 001419 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS 001420 case PragTyp_FUNCTION_LIST: { 001421 int i; 001422 HashElem *j; 001423 FuncDef *p; 001424 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0; 001425 pParse->nMem = 6; 001426 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ 001427 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){ 001428 assert( p->funcFlags & SQLITE_FUNC_BUILTIN ); 001429 pragmaFunclistLine(v, p, 1, showInternFunc); 001430 } 001431 } 001432 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){ 001433 p = (FuncDef*)sqliteHashData(j); 001434 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 ); 001435 pragmaFunclistLine(v, p, 0, showInternFunc); 001436 } 001437 } 001438 break; 001439 001440 #ifndef SQLITE_OMIT_VIRTUALTABLE 001441 case PragTyp_MODULE_LIST: { 001442 HashElem *j; 001443 pParse->nMem = 1; 001444 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){ 001445 Module *pMod = (Module*)sqliteHashData(j); 001446 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName); 001447 } 001448 } 001449 break; 001450 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 001451 001452 case PragTyp_PRAGMA_LIST: { 001453 int i; 001454 for(i=0; i<ArraySize(aPragmaName); i++){ 001455 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName); 001456 } 001457 } 001458 break; 001459 #endif /* SQLITE_INTROSPECTION_PRAGMAS */ 001460 001461 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ 001462 001463 #ifndef SQLITE_OMIT_FOREIGN_KEY 001464 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){ 001465 FKey *pFK; 001466 Table *pTab; 001467 pTab = sqlite3FindTable(db, zRight, zDb); 001468 if( pTab && IsOrdinaryTable(pTab) ){ 001469 pFK = pTab->u.tab.pFKey; 001470 if( pFK ){ 001471 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); 001472 int i = 0; 001473 pParse->nMem = 8; 001474 sqlite3CodeVerifySchema(pParse, iTabDb); 001475 while(pFK){ 001476 int j; 001477 for(j=0; j<pFK->nCol; j++){ 001478 sqlite3VdbeMultiLoad(v, 1, "iissssss", 001479 i, 001480 j, 001481 pFK->zTo, 001482 pTab->aCol[pFK->aCol[j].iFrom].zCnName, 001483 pFK->aCol[j].zCol, 001484 actionName(pFK->aAction[1]), /* ON UPDATE */ 001485 actionName(pFK->aAction[0]), /* ON DELETE */ 001486 "NONE"); 001487 } 001488 ++i; 001489 pFK = pFK->pNextFrom; 001490 } 001491 } 001492 } 001493 } 001494 break; 001495 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 001496 001497 #ifndef SQLITE_OMIT_FOREIGN_KEY 001498 #ifndef SQLITE_OMIT_TRIGGER 001499 case PragTyp_FOREIGN_KEY_CHECK: { 001500 FKey *pFK; /* A foreign key constraint */ 001501 Table *pTab; /* Child table contain "REFERENCES" keyword */ 001502 Table *pParent; /* Parent table that child points to */ 001503 Index *pIdx; /* Index in the parent table */ 001504 int i; /* Loop counter: Foreign key number for pTab */ 001505 int j; /* Loop counter: Field of the foreign key */ 001506 HashElem *k; /* Loop counter: Next table in schema */ 001507 int x; /* result variable */ 001508 int regResult; /* 3 registers to hold a result row */ 001509 int regRow; /* Registers to hold a row from pTab */ 001510 int addrTop; /* Top of a loop checking foreign keys */ 001511 int addrOk; /* Jump here if the key is OK */ 001512 int *aiCols; /* child to parent column mapping */ 001513 001514 regResult = pParse->nMem+1; 001515 pParse->nMem += 4; 001516 regRow = ++pParse->nMem; 001517 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash); 001518 while( k ){ 001519 if( zRight ){ 001520 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb); 001521 k = 0; 001522 }else{ 001523 pTab = (Table*)sqliteHashData(k); 001524 k = sqliteHashNext(k); 001525 } 001526 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue; 001527 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 001528 zDb = db->aDb[iDb].zDbSName; 001529 sqlite3CodeVerifySchema(pParse, iDb); 001530 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 001531 sqlite3TouchRegister(pParse, pTab->nCol+regRow); 001532 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); 001533 sqlite3VdbeLoadString(v, regResult, pTab->zName); 001534 assert( IsOrdinaryTable(pTab) ); 001535 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){ 001536 pParent = sqlite3FindTable(db, pFK->zTo, zDb); 001537 if( pParent==0 ) continue; 001538 pIdx = 0; 001539 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName); 001540 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0); 001541 if( x==0 ){ 001542 if( pIdx==0 ){ 001543 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead); 001544 }else{ 001545 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb); 001546 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 001547 } 001548 }else{ 001549 k = 0; 001550 break; 001551 } 001552 } 001553 assert( pParse->nErr>0 || pFK==0 ); 001554 if( pFK ) break; 001555 if( pParse->nTab<i ) pParse->nTab = i; 001556 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v); 001557 assert( IsOrdinaryTable(pTab) ); 001558 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){ 001559 pParent = sqlite3FindTable(db, pFK->zTo, zDb); 001560 pIdx = 0; 001561 aiCols = 0; 001562 if( pParent ){ 001563 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols); 001564 assert( x==0 || db->mallocFailed ); 001565 } 001566 addrOk = sqlite3VdbeMakeLabel(pParse); 001567 001568 /* Generate code to read the child key values into registers 001569 ** regRow..regRow+n. If any of the child key values are NULL, this 001570 ** row cannot cause an FK violation. Jump directly to addrOk in 001571 ** this case. */ 001572 sqlite3TouchRegister(pParse, regRow + pFK->nCol); 001573 for(j=0; j<pFK->nCol; j++){ 001574 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom; 001575 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j); 001576 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); 001577 } 001578 001579 /* Generate code to query the parent index for a matching parent 001580 ** key. If a match is found, jump to addrOk. */ 001581 if( pIdx ){ 001582 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0, 001583 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol); 001584 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol); 001585 VdbeCoverage(v); 001586 }else if( pParent ){ 001587 int jmp = sqlite3VdbeCurrentAddr(v)+2; 001588 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v); 001589 sqlite3VdbeGoto(v, addrOk); 001590 assert( pFK->nCol==1 || db->mallocFailed ); 001591 } 001592 001593 /* Generate code to report an FK violation to the caller. */ 001594 if( HasRowid(pTab) ){ 001595 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1); 001596 }else{ 001597 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1); 001598 } 001599 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1); 001600 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4); 001601 sqlite3VdbeResolveLabel(v, addrOk); 001602 sqlite3DbFree(db, aiCols); 001603 } 001604 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v); 001605 sqlite3VdbeJumpHere(v, addrTop); 001606 } 001607 } 001608 break; 001609 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 001610 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 001611 001612 #ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA 001613 /* Reinstall the LIKE and GLOB functions. The variant of LIKE 001614 ** used will be case sensitive or not depending on the RHS. 001615 */ 001616 case PragTyp_CASE_SENSITIVE_LIKE: { 001617 if( zRight ){ 001618 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0)); 001619 } 001620 } 001621 break; 001622 #endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */ 001623 001624 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX 001625 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 001626 #endif 001627 001628 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 001629 /* PRAGMA integrity_check 001630 ** PRAGMA integrity_check(N) 001631 ** PRAGMA quick_check 001632 ** PRAGMA quick_check(N) 001633 ** 001634 ** Verify the integrity of the database. 001635 ** 001636 ** The "quick_check" is reduced version of 001637 ** integrity_check designed to detect most database corruption 001638 ** without the overhead of cross-checking indexes. Quick_check 001639 ** is linear time whereas integrity_check is O(NlogN). 001640 ** 001641 ** The maximum number of errors is 100 by default. A different default 001642 ** can be specified using a numeric parameter N. 001643 ** 001644 ** Or, the parameter N can be the name of a table. In that case, only 001645 ** the one table named is verified. The freelist is only verified if 001646 ** the named table is "sqlite_schema" (or one of its aliases). 001647 ** 001648 ** All schemas are checked by default. To check just a single 001649 ** schema, use the form: 001650 ** 001651 ** PRAGMA schema.integrity_check; 001652 */ 001653 case PragTyp_INTEGRITY_CHECK: { 001654 int i, j, addr, mxErr; 001655 Table *pObjTab = 0; /* Check only this one table, if not NULL */ 001656 001657 int isQuick = (sqlite3Tolower(zLeft[0])=='q'); 001658 001659 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check", 001660 ** then iDb is set to the index of the database identified by <db>. 001661 ** In this case, the integrity of database iDb only is verified by 001662 ** the VDBE created below. 001663 ** 001664 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or 001665 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb 001666 ** to -1 here, to indicate that the VDBE should verify the integrity 001667 ** of all attached databases. */ 001668 assert( iDb>=0 ); 001669 assert( iDb==0 || pId2->z ); 001670 if( pId2->z==0 ) iDb = -1; 001671 001672 /* Initialize the VDBE program */ 001673 pParse->nMem = 6; 001674 001675 /* Set the maximum error count */ 001676 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; 001677 if( zRight ){ 001678 if( sqlite3GetInt32(zRight, &mxErr) ){ 001679 if( mxErr<=0 ){ 001680 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; 001681 } 001682 }else{ 001683 pObjTab = sqlite3LocateTable(pParse, 0, zRight, 001684 iDb>=0 ? db->aDb[iDb].zDbSName : 0); 001685 } 001686 } 001687 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */ 001688 001689 /* Do an integrity check on each database file */ 001690 for(i=0; i<db->nDb; i++){ 001691 HashElem *x; /* For looping over tables in the schema */ 001692 Hash *pTbls; /* Set of all tables in the schema */ 001693 int *aRoot; /* Array of root page numbers of all btrees */ 001694 int cnt = 0; /* Number of entries in aRoot[] */ 001695 int mxIdx = 0; /* Maximum number of indexes for any table */ 001696 001697 if( OMIT_TEMPDB && i==1 ) continue; 001698 if( iDb>=0 && i!=iDb ) continue; 001699 001700 sqlite3CodeVerifySchema(pParse, i); 001701 pParse->okConstFactor = 0; /* tag-20230327-1 */ 001702 001703 /* Do an integrity check of the B-Tree 001704 ** 001705 ** Begin by finding the root pages numbers 001706 ** for all tables and indices in the database. 001707 */ 001708 assert( sqlite3SchemaMutexHeld(db, i, 0) ); 001709 pTbls = &db->aDb[i].pSchema->tblHash; 001710 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ 001711 Table *pTab = sqliteHashData(x); /* Current table */ 001712 Index *pIdx; /* An index on pTab */ 001713 int nIdx; /* Number of indexes on pTab */ 001714 if( pObjTab && pObjTab!=pTab ) continue; 001715 if( HasRowid(pTab) ) cnt++; 001716 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; } 001717 if( nIdx>mxIdx ) mxIdx = nIdx; 001718 } 001719 if( cnt==0 ) continue; 001720 if( pObjTab ) cnt++; 001721 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1)); 001722 if( aRoot==0 ) break; 001723 cnt = 0; 001724 if( pObjTab ) aRoot[++cnt] = 0; 001725 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ 001726 Table *pTab = sqliteHashData(x); 001727 Index *pIdx; 001728 if( pObjTab && pObjTab!=pTab ) continue; 001729 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum; 001730 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 001731 aRoot[++cnt] = pIdx->tnum; 001732 } 001733 } 001734 aRoot[0] = cnt; 001735 001736 /* Make sure sufficient number of registers have been allocated */ 001737 sqlite3TouchRegister(pParse, 8+mxIdx); 001738 sqlite3ClearTempRegCache(pParse); 001739 001740 /* Do the b-tree integrity checks */ 001741 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY); 001742 sqlite3VdbeChangeP5(v, (u8)i); 001743 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); 001744 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, 001745 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName), 001746 P4_DYNAMIC); 001747 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3); 001748 integrityCheckResultRow(v); 001749 sqlite3VdbeJumpHere(v, addr); 001750 001751 /* Make sure all the indices are constructed correctly. 001752 */ 001753 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ 001754 Table *pTab = sqliteHashData(x); 001755 Index *pIdx, *pPk; 001756 Index *pPrior = 0; /* Previous index */ 001757 int loopTop; 001758 int iDataCur, iIdxCur; 001759 int r1 = -1; 001760 int bStrict; /* True for a STRICT table */ 001761 int r2; /* Previous key for WITHOUT ROWID tables */ 001762 int mxCol; /* Maximum non-virtual column number */ 001763 001764 if( pObjTab && pObjTab!=pTab ) continue; 001765 if( !IsOrdinaryTable(pTab) ) continue; 001766 if( isQuick || HasRowid(pTab) ){ 001767 pPk = 0; 001768 r2 = 0; 001769 }else{ 001770 pPk = sqlite3PrimaryKeyIndex(pTab); 001771 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol); 001772 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1); 001773 } 001774 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, 001775 1, 0, &iDataCur, &iIdxCur); 001776 /* reg[7] counts the number of entries in the table. 001777 ** reg[8+i] counts the number of entries in the i-th index 001778 */ 001779 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); 001780 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 001781 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ 001782 } 001783 assert( pParse->nMem>=8+j ); 001784 assert( sqlite3NoTempsInRange(pParse,1,7+j) ); 001785 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); 001786 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); 001787 001788 /* Fetch the right-most column from the table. This will cause 001789 ** the entire record header to be parsed and sanity checked. It 001790 ** will also prepopulate the cursor column cache that is used 001791 ** by the OP_IsType code, so it is a required step. 001792 */ 001793 assert( !IsVirtual(pTab) ); 001794 if( HasRowid(pTab) ){ 001795 mxCol = -1; 001796 for(j=0; j<pTab->nCol; j++){ 001797 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++; 001798 } 001799 if( mxCol==pTab->iPKey ) mxCol--; 001800 }else{ 001801 /* COLFLAG_VIRTUAL columns are not included in the WITHOUT ROWID 001802 ** PK index column-count, so there is no need to account for them 001803 ** in this case. */ 001804 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1; 001805 } 001806 if( mxCol>=0 ){ 001807 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, mxCol, 3); 001808 sqlite3VdbeTypeofColumn(v, 3); 001809 } 001810 001811 if( !isQuick ){ 001812 if( pPk ){ 001813 /* Verify WITHOUT ROWID keys are in ascending order */ 001814 int a1; 001815 char *zErr; 001816 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol); 001817 VdbeCoverage(v); 001818 sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v); 001819 zErr = sqlite3MPrintf(db, 001820 "row not in PRIMARY KEY order for %s", 001821 pTab->zName); 001822 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); 001823 integrityCheckResultRow(v); 001824 sqlite3VdbeJumpHere(v, a1); 001825 sqlite3VdbeJumpHere(v, a1+1); 001826 for(j=0; j<pPk->nKeyCol; j++){ 001827 sqlite3ExprCodeLoadIndexColumn(pParse, pPk, iDataCur, j, r2+j); 001828 } 001829 } 001830 } 001831 /* Verify datatypes for all columns: 001832 ** 001833 ** (1) NOT NULL columns may not contain a NULL 001834 ** (2) Datatype must be exact for non-ANY columns in STRICT tables 001835 ** (3) Datatype for TEXT columns in non-STRICT tables must be 001836 ** NULL, TEXT, or BLOB. 001837 ** (4) Datatype for numeric columns in non-STRICT tables must not 001838 ** be a TEXT value that can be losslessly converted to numeric. 001839 */ 001840 bStrict = (pTab->tabFlags & TF_Strict)!=0; 001841 for(j=0; j<pTab->nCol; j++){ 001842 char *zErr; 001843 Column *pCol = pTab->aCol + j; /* The column to be checked */ 001844 int labelError; /* Jump here to report an error */ 001845 int labelOk; /* Jump here if all looks ok */ 001846 int p1, p3, p4; /* Operands to the OP_IsType opcode */ 001847 int doTypeCheck; /* Check datatypes (besides NOT NULL) */ 001848 001849 if( j==pTab->iPKey ) continue; 001850 if( bStrict ){ 001851 doTypeCheck = pCol->eCType>COLTYPE_ANY; 001852 }else{ 001853 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB; 001854 } 001855 if( pCol->notNull==0 && !doTypeCheck ) continue; 001856 001857 /* Compute the operands that will be needed for OP_IsType */ 001858 p4 = SQLITE_NULL; 001859 if( pCol->colFlags & COLFLAG_VIRTUAL ){ 001860 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); 001861 p1 = -1; 001862 p3 = 3; 001863 }else{ 001864 if( pCol->iDflt ){ 001865 sqlite3_value *pDfltValue = 0; 001866 sqlite3ValueFromExpr(db, sqlite3ColumnExpr(pTab,pCol), ENC(db), 001867 pCol->affinity, &pDfltValue); 001868 if( pDfltValue ){ 001869 p4 = sqlite3_value_type(pDfltValue); 001870 sqlite3ValueFree(pDfltValue); 001871 } 001872 } 001873 p1 = iDataCur; 001874 if( !HasRowid(pTab) ){ 001875 testcase( j!=sqlite3TableColumnToStorage(pTab, j) ); 001876 p3 = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), j); 001877 }else{ 001878 p3 = sqlite3TableColumnToStorage(pTab,j); 001879 testcase( p3!=j); 001880 } 001881 } 001882 001883 labelError = sqlite3VdbeMakeLabel(pParse); 001884 labelOk = sqlite3VdbeMakeLabel(pParse); 001885 if( pCol->notNull ){ 001886 /* (1) NOT NULL columns may not contain a NULL */ 001887 int jmp3; 001888 int jmp2 = sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); 001889 VdbeCoverage(v); 001890 if( p1<0 ){ 001891 sqlite3VdbeChangeP5(v, 0x0f); /* INT, REAL, TEXT, or BLOB */ 001892 jmp3 = jmp2; 001893 }else{ 001894 sqlite3VdbeChangeP5(v, 0x0d); /* INT, TEXT, or BLOB */ 001895 /* OP_IsType does not detect NaN values in the database file 001896 ** which should be treated as a NULL. So if the header type 001897 ** is REAL, we have to load the actual data using OP_Column 001898 ** to reliably determine if the value is a NULL. */ 001899 sqlite3VdbeAddOp3(v, OP_Column, p1, p3, 3); 001900 sqlite3ColumnDefault(v, pTab, j, 3); 001901 jmp3 = sqlite3VdbeAddOp2(v, OP_NotNull, 3, labelOk); 001902 VdbeCoverage(v); 001903 } 001904 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, 001905 pCol->zCnName); 001906 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); 001907 if( doTypeCheck ){ 001908 sqlite3VdbeGoto(v, labelError); 001909 sqlite3VdbeJumpHere(v, jmp2); 001910 sqlite3VdbeJumpHere(v, jmp3); 001911 }else{ 001912 /* VDBE byte code will fall thru */ 001913 } 001914 } 001915 if( bStrict && doTypeCheck ){ 001916 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/ 001917 static unsigned char aStdTypeMask[] = { 001918 0x1f, /* ANY */ 001919 0x18, /* BLOB */ 001920 0x11, /* INT */ 001921 0x11, /* INTEGER */ 001922 0x13, /* REAL */ 001923 0x14 /* TEXT */ 001924 }; 001925 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); 001926 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) ); 001927 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]); 001928 VdbeCoverage(v); 001929 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s", 001930 sqlite3StdType[pCol->eCType-1], 001931 pTab->zName, pTab->aCol[j].zCnName); 001932 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); 001933 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){ 001934 /* (3) Datatype for TEXT columns in non-STRICT tables must be 001935 ** NULL, TEXT, or BLOB. */ 001936 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); 001937 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */ 001938 VdbeCoverage(v); 001939 zErr = sqlite3MPrintf(db, "NUMERIC value in %s.%s", 001940 pTab->zName, pTab->aCol[j].zCnName); 001941 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); 001942 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){ 001943 /* (4) Datatype for numeric columns in non-STRICT tables must not 001944 ** be a TEXT value that can be converted to numeric. */ 001945 sqlite3VdbeAddOp4Int(v, OP_IsType, p1, labelOk, p3, p4); 001946 sqlite3VdbeChangeP5(v, 0x1b); /* NULL, INT, FLOAT, or BLOB */ 001947 VdbeCoverage(v); 001948 if( p1>=0 ){ 001949 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); 001950 } 001951 sqlite3VdbeAddOp4(v, OP_Affinity, 3, 1, 0, "C", P4_STATIC); 001952 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4); 001953 sqlite3VdbeChangeP5(v, 0x1c); /* NULL, TEXT, or BLOB */ 001954 VdbeCoverage(v); 001955 zErr = sqlite3MPrintf(db, "TEXT value in %s.%s", 001956 pTab->zName, pTab->aCol[j].zCnName); 001957 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); 001958 } 001959 sqlite3VdbeResolveLabel(v, labelError); 001960 integrityCheckResultRow(v); 001961 sqlite3VdbeResolveLabel(v, labelOk); 001962 } 001963 /* Verify CHECK constraints */ 001964 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ 001965 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0); 001966 if( db->mallocFailed==0 ){ 001967 int addrCkFault = sqlite3VdbeMakeLabel(pParse); 001968 int addrCkOk = sqlite3VdbeMakeLabel(pParse); 001969 char *zErr; 001970 int k; 001971 pParse->iSelfTab = iDataCur + 1; 001972 for(k=pCheck->nExpr-1; k>0; k--){ 001973 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0); 001974 } 001975 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 001976 SQLITE_JUMPIFNULL); 001977 sqlite3VdbeResolveLabel(v, addrCkFault); 001978 pParse->iSelfTab = 0; 001979 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s", 001980 pTab->zName); 001981 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); 001982 integrityCheckResultRow(v); 001983 sqlite3VdbeResolveLabel(v, addrCkOk); 001984 } 001985 sqlite3ExprListDelete(db, pCheck); 001986 } 001987 if( !isQuick ){ /* Omit the remaining tests for quick_check */ 001988 /* Validate index entries for the current row */ 001989 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 001990 int jmp2, jmp3, jmp4, jmp5, label6; 001991 int kk; 001992 int ckUniq = sqlite3VdbeMakeLabel(pParse); 001993 if( pPk==pIdx ) continue; 001994 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, 001995 pPrior, r1); 001996 pPrior = pIdx; 001997 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */ 001998 /* Verify that an index entry exists for the current table row */ 001999 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, 002000 pIdx->nColumn); VdbeCoverage(v); 002001 sqlite3VdbeLoadString(v, 3, "row "); 002002 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); 002003 sqlite3VdbeLoadString(v, 4, " missing from index "); 002004 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); 002005 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); 002006 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); 002007 jmp4 = integrityCheckResultRow(v); 002008 sqlite3VdbeJumpHere(v, jmp2); 002009 002010 /* The OP_IdxRowid opcode is an optimized version of OP_Column 002011 ** that extracts the rowid off the end of the index record. 002012 ** But it only works correctly if index record does not have 002013 ** any extra bytes at the end. Verify that this is the case. */ 002014 if( HasRowid(pTab) ){ 002015 int jmp7; 002016 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur+j, 3); 002017 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1); 002018 VdbeCoverageNeverNull(v); 002019 sqlite3VdbeLoadString(v, 3, 002020 "rowid not at end-of-record for row "); 002021 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); 002022 sqlite3VdbeLoadString(v, 4, " of index "); 002023 sqlite3VdbeGoto(v, jmp5-1); 002024 sqlite3VdbeJumpHere(v, jmp7); 002025 } 002026 002027 /* Any indexed columns with non-BINARY collations must still hold 002028 ** the exact same text value as the table. */ 002029 label6 = 0; 002030 for(kk=0; kk<pIdx->nKeyCol; kk++){ 002031 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue; 002032 if( label6==0 ) label6 = sqlite3VdbeMakeLabel(pParse); 002033 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur+j, kk, 3); 002034 sqlite3VdbeAddOp3(v, OP_Ne, 3, label6, r1+kk); VdbeCoverage(v); 002035 } 002036 if( label6 ){ 002037 int jmp6 = sqlite3VdbeAddOp0(v, OP_Goto); 002038 sqlite3VdbeResolveLabel(v, label6); 002039 sqlite3VdbeLoadString(v, 3, "row "); 002040 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); 002041 sqlite3VdbeLoadString(v, 4, " values differ from index "); 002042 sqlite3VdbeGoto(v, jmp5-1); 002043 sqlite3VdbeJumpHere(v, jmp6); 002044 } 002045 002046 /* For UNIQUE indexes, verify that only one entry exists with the 002047 ** current key. The entry is unique if (1) any column is NULL 002048 ** or (2) the next entry has a different key */ 002049 if( IsUniqueIndex(pIdx) ){ 002050 int uniqOk = sqlite3VdbeMakeLabel(pParse); 002051 int jmp6; 002052 for(kk=0; kk<pIdx->nKeyCol; kk++){ 002053 int iCol = pIdx->aiColumn[kk]; 002054 assert( iCol!=XN_ROWID && iCol<pTab->nCol ); 002055 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue; 002056 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk); 002057 VdbeCoverage(v); 002058 } 002059 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v); 002060 sqlite3VdbeGoto(v, uniqOk); 002061 sqlite3VdbeJumpHere(v, jmp6); 002062 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1, 002063 pIdx->nKeyCol); VdbeCoverage(v); 002064 sqlite3VdbeLoadString(v, 3, "non-unique entry in index "); 002065 sqlite3VdbeGoto(v, jmp5); 002066 sqlite3VdbeResolveLabel(v, uniqOk); 002067 } 002068 sqlite3VdbeJumpHere(v, jmp4); 002069 sqlite3ResolvePartIdxLabel(pParse, jmp3); 002070 } 002071 } 002072 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); 002073 sqlite3VdbeJumpHere(v, loopTop-1); 002074 if( !isQuick ){ 002075 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); 002076 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 002077 if( pPk==pIdx ) continue; 002078 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); 002079 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v); 002080 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); 002081 sqlite3VdbeLoadString(v, 4, pIdx->zName); 002082 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3); 002083 integrityCheckResultRow(v); 002084 sqlite3VdbeJumpHere(v, addr); 002085 } 002086 if( pPk ){ 002087 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol); 002088 } 002089 } 002090 } 002091 002092 #ifndef SQLITE_OMIT_VIRTUALTABLE 002093 /* Second pass to invoke the xIntegrity method on all virtual 002094 ** tables. 002095 */ 002096 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ 002097 Table *pTab = sqliteHashData(x); 002098 sqlite3_vtab *pVTab; 002099 int a1; 002100 if( pObjTab && pObjTab!=pTab ) continue; 002101 if( IsOrdinaryTable(pTab) ) continue; 002102 if( !IsVirtual(pTab) ) continue; 002103 if( pTab->nCol<=0 ){ 002104 const char *zMod = pTab->u.vtab.azArg[0]; 002105 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue; 002106 } 002107 sqlite3ViewGetColumnNames(pParse, pTab); 002108 if( pTab->u.vtab.p==0 ) continue; 002109 pVTab = pTab->u.vtab.p->pVtab; 002110 if( NEVER(pVTab==0) ) continue; 002111 if( NEVER(pVTab->pModule==0) ) continue; 002112 if( pVTab->pModule->iVersion<4 ) continue; 002113 if( pVTab->pModule->xIntegrity==0 ) continue; 002114 sqlite3VdbeAddOp3(v, OP_VCheck, i, 3, isQuick); 002115 pTab->nTabRef++; 002116 sqlite3VdbeAppendP4(v, pTab, P4_TABLEREF); 002117 a1 = sqlite3VdbeAddOp1(v, OP_IsNull, 3); VdbeCoverage(v); 002118 integrityCheckResultRow(v); 002119 sqlite3VdbeJumpHere(v, a1); 002120 continue; 002121 } 002122 #endif 002123 } 002124 { 002125 static const int iLn = VDBE_OFFSET_LINENO(2); 002126 static const VdbeOpList endCode[] = { 002127 { OP_AddImm, 1, 0, 0}, /* 0 */ 002128 { OP_IfNotZero, 1, 4, 0}, /* 1 */ 002129 { OP_String8, 0, 3, 0}, /* 2 */ 002130 { OP_ResultRow, 3, 1, 0}, /* 3 */ 002131 { OP_Halt, 0, 0, 0}, /* 4 */ 002132 { OP_String8, 0, 3, 0}, /* 5 */ 002133 { OP_Goto, 0, 3, 0}, /* 6 */ 002134 }; 002135 VdbeOp *aOp; 002136 002137 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn); 002138 if( aOp ){ 002139 aOp[0].p2 = 1-mxErr; 002140 aOp[2].p4type = P4_STATIC; 002141 aOp[2].p4.z = "ok"; 002142 aOp[5].p4type = P4_STATIC; 002143 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT); 002144 } 002145 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2); 002146 } 002147 } 002148 break; 002149 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 002150 002151 #ifndef SQLITE_OMIT_UTF16 002152 /* 002153 ** PRAGMA encoding 002154 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" 002155 ** 002156 ** In its first form, this pragma returns the encoding of the main 002157 ** database. If the database is not initialized, it is initialized now. 002158 ** 002159 ** The second form of this pragma is a no-op if the main database file 002160 ** has not already been initialized. In this case it sets the default 002161 ** encoding that will be used for the main database file if a new file 002162 ** is created. If an existing main database file is opened, then the 002163 ** default text encoding for the existing database is used. 002164 ** 002165 ** In all cases new databases created using the ATTACH command are 002166 ** created to use the same default text encoding as the main database. If 002167 ** the main database has not been initialized and/or created when ATTACH 002168 ** is executed, this is done before the ATTACH operation. 002169 ** 002170 ** In the second form this pragma sets the text encoding to be used in 002171 ** new database files created using this database handle. It is only 002172 ** useful if invoked immediately after the main database i 002173 */ 002174 case PragTyp_ENCODING: { 002175 static const struct EncName { 002176 char *zName; 002177 u8 enc; 002178 } encnames[] = { 002179 { "UTF8", SQLITE_UTF8 }, 002180 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ 002181 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ 002182 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ 002183 { "UTF16le", SQLITE_UTF16LE }, 002184 { "UTF16be", SQLITE_UTF16BE }, 002185 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ 002186 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ 002187 { 0, 0 } 002188 }; 002189 const struct EncName *pEnc; 002190 if( !zRight ){ /* "PRAGMA encoding" */ 002191 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 002192 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); 002193 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); 002194 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); 002195 returnSingleText(v, encnames[ENC(pParse->db)].zName); 002196 }else{ /* "PRAGMA encoding = XXX" */ 002197 /* Only change the value of sqlite.enc if the database handle is not 002198 ** initialized. If the main database exists, the new sqlite.enc value 002199 ** will be overwritten when the schema is next loaded. If it does not 002200 ** already exists, it will be created to use the new encoding value. 002201 */ 002202 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){ 002203 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ 002204 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ 002205 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; 002206 SCHEMA_ENC(db) = enc; 002207 sqlite3SetTextEncoding(db, enc); 002208 break; 002209 } 002210 } 002211 if( !pEnc->zName ){ 002212 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); 002213 } 002214 } 002215 } 002216 } 002217 break; 002218 #endif /* SQLITE_OMIT_UTF16 */ 002219 002220 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS 002221 /* 002222 ** PRAGMA [schema.]schema_version 002223 ** PRAGMA [schema.]schema_version = <integer> 002224 ** 002225 ** PRAGMA [schema.]user_version 002226 ** PRAGMA [schema.]user_version = <integer> 002227 ** 002228 ** PRAGMA [schema.]freelist_count 002229 ** 002230 ** PRAGMA [schema.]data_version 002231 ** 002232 ** PRAGMA [schema.]application_id 002233 ** PRAGMA [schema.]application_id = <integer> 002234 ** 002235 ** The pragma's schema_version and user_version are used to set or get 002236 ** the value of the schema-version and user-version, respectively. Both 002237 ** the schema-version and the user-version are 32-bit signed integers 002238 ** stored in the database header. 002239 ** 002240 ** The schema-cookie is usually only manipulated internally by SQLite. It 002241 ** is incremented by SQLite whenever the database schema is modified (by 002242 ** creating or dropping a table or index). The schema version is used by 002243 ** SQLite each time a query is executed to ensure that the internal cache 002244 ** of the schema used when compiling the SQL query matches the schema of 002245 ** the database against which the compiled query is actually executed. 002246 ** Subverting this mechanism by using "PRAGMA schema_version" to modify 002247 ** the schema-version is potentially dangerous and may lead to program 002248 ** crashes or database corruption. Use with caution! 002249 ** 002250 ** The user-version is not used internally by SQLite. It may be used by 002251 ** applications for any purpose. 002252 */ 002253 case PragTyp_HEADER_VALUE: { 002254 int iCookie = pPragma->iArg; /* Which cookie to read or write */ 002255 sqlite3VdbeUsesBtree(v, iDb); 002256 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){ 002257 /* Write the specified cookie value */ 002258 static const VdbeOpList setCookie[] = { 002259 { OP_Transaction, 0, 1, 0}, /* 0 */ 002260 { OP_SetCookie, 0, 0, 0}, /* 1 */ 002261 }; 002262 VdbeOp *aOp; 002263 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie)); 002264 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0); 002265 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 002266 aOp[0].p1 = iDb; 002267 aOp[1].p1 = iDb; 002268 aOp[1].p2 = iCookie; 002269 aOp[1].p3 = sqlite3Atoi(zRight); 002270 aOp[1].p5 = 1; 002271 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){ 002272 /* Do not allow the use of PRAGMA schema_version=VALUE in defensive 002273 ** mode. Change the OP_SetCookie opcode into a no-op. */ 002274 aOp[1].opcode = OP_Noop; 002275 } 002276 }else{ 002277 /* Read the specified cookie value */ 002278 static const VdbeOpList readCookie[] = { 002279 { OP_Transaction, 0, 0, 0}, /* 0 */ 002280 { OP_ReadCookie, 0, 1, 0}, /* 1 */ 002281 { OP_ResultRow, 1, 1, 0} 002282 }; 002283 VdbeOp *aOp; 002284 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie)); 002285 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0); 002286 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 002287 aOp[0].p1 = iDb; 002288 aOp[1].p1 = iDb; 002289 aOp[1].p3 = iCookie; 002290 sqlite3VdbeReusable(v); 002291 } 002292 } 002293 break; 002294 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ 002295 002296 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 002297 /* 002298 ** PRAGMA compile_options 002299 ** 002300 ** Return the names of all compile-time options used in this build, 002301 ** one option per row. 002302 */ 002303 case PragTyp_COMPILE_OPTIONS: { 002304 int i = 0; 002305 const char *zOpt; 002306 pParse->nMem = 1; 002307 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ 002308 sqlite3VdbeLoadString(v, 1, zOpt); 002309 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 002310 } 002311 sqlite3VdbeReusable(v); 002312 } 002313 break; 002314 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 002315 002316 #ifndef SQLITE_OMIT_WAL 002317 /* 002318 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate 002319 ** 002320 ** Checkpoint the database. 002321 */ 002322 case PragTyp_WAL_CHECKPOINT: { 002323 int iBt = (pId2->z?iDb:SQLITE_MAX_DB); 002324 int eMode = SQLITE_CHECKPOINT_PASSIVE; 002325 if( zRight ){ 002326 if( sqlite3StrICmp(zRight, "full")==0 ){ 002327 eMode = SQLITE_CHECKPOINT_FULL; 002328 }else if( sqlite3StrICmp(zRight, "restart")==0 ){ 002329 eMode = SQLITE_CHECKPOINT_RESTART; 002330 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){ 002331 eMode = SQLITE_CHECKPOINT_TRUNCATE; 002332 } 002333 } 002334 pParse->nMem = 3; 002335 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1); 002336 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); 002337 } 002338 break; 002339 002340 /* 002341 ** PRAGMA wal_autocheckpoint 002342 ** PRAGMA wal_autocheckpoint = N 002343 ** 002344 ** Configure a database connection to automatically checkpoint a database 002345 ** after accumulating N frames in the log. Or query for the current value 002346 ** of N. 002347 */ 002348 case PragTyp_WAL_AUTOCHECKPOINT: { 002349 if( zRight ){ 002350 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); 002351 } 002352 returnSingleInt(v, 002353 db->xWalCallback==sqlite3WalDefaultHook ? 002354 SQLITE_PTR_TO_INT(db->pWalArg) : 0); 002355 } 002356 break; 002357 #endif 002358 002359 /* 002360 ** PRAGMA shrink_memory 002361 ** 002362 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database 002363 ** connection on which it is invoked to free up as much memory as it 002364 ** can, by calling sqlite3_db_release_memory(). 002365 */ 002366 case PragTyp_SHRINK_MEMORY: { 002367 sqlite3_db_release_memory(db); 002368 break; 002369 } 002370 002371 /* 002372 ** PRAGMA optimize 002373 ** PRAGMA optimize(MASK) 002374 ** PRAGMA schema.optimize 002375 ** PRAGMA schema.optimize(MASK) 002376 ** 002377 ** Attempt to optimize the database. All schemas are optimized in the first 002378 ** two forms, and only the specified schema is optimized in the latter two. 002379 ** 002380 ** The details of optimizations performed by this pragma are expected 002381 ** to change and improve over time. Applications should anticipate that 002382 ** this pragma will perform new optimizations in future releases. 002383 ** 002384 ** The optional argument is a bitmask of optimizations to perform: 002385 ** 002386 ** 0x0001 Debugging mode. Do not actually perform any optimizations 002387 ** but instead return one line of text for each optimization 002388 ** that would have been done. Off by default. 002389 ** 002390 ** 0x0002 Run ANALYZE on tables that might benefit. On by default. 002391 ** See below for additional information. 002392 ** 002393 ** 0x0004 (Not yet implemented) Record usage and performance 002394 ** information from the current session in the 002395 ** database file so that it will be available to "optimize" 002396 ** pragmas run by future database connections. 002397 ** 002398 ** 0x0008 (Not yet implemented) Create indexes that might have 002399 ** been helpful to recent queries 002400 ** 002401 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all 002402 ** of the optimizations listed above except Debug Mode, including new 002403 ** optimizations that have not yet been invented. If new optimizations are 002404 ** ever added that should be off by default, those off-by-default 002405 ** optimizations will have bitmasks of 0x10000 or larger. 002406 ** 002407 ** DETERMINATION OF WHEN TO RUN ANALYZE 002408 ** 002409 ** In the current implementation, a table is analyzed if only if all of 002410 ** the following are true: 002411 ** 002412 ** (1) MASK bit 0x02 is set. 002413 ** 002414 ** (2) The query planner used sqlite_stat1-style statistics for one or 002415 ** more indexes of the table at some point during the lifetime of 002416 ** the current connection. 002417 ** 002418 ** (3) One or more indexes of the table are currently unanalyzed OR 002419 ** the number of rows in the table has increased by 25 times or more 002420 ** since the last time ANALYZE was run. 002421 ** 002422 ** The rules for when tables are analyzed are likely to change in 002423 ** future releases. 002424 */ 002425 case PragTyp_OPTIMIZE: { 002426 int iDbLast; /* Loop termination point for the schema loop */ 002427 int iTabCur; /* Cursor for a table whose size needs checking */ 002428 HashElem *k; /* Loop over tables of a schema */ 002429 Schema *pSchema; /* The current schema */ 002430 Table *pTab; /* A table in the schema */ 002431 Index *pIdx; /* An index of the table */ 002432 LogEst szThreshold; /* Size threshold above which reanalysis needed */ 002433 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */ 002434 u32 opMask; /* Mask of operations to perform */ 002435 002436 if( zRight ){ 002437 opMask = (u32)sqlite3Atoi(zRight); 002438 if( (opMask & 0x02)==0 ) break; 002439 }else{ 002440 opMask = 0xfffe; 002441 } 002442 iTabCur = pParse->nTab++; 002443 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){ 002444 if( iDb==1 ) continue; 002445 sqlite3CodeVerifySchema(pParse, iDb); 002446 pSchema = db->aDb[iDb].pSchema; 002447 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ 002448 pTab = (Table*)sqliteHashData(k); 002449 002450 /* If table pTab has not been used in a way that would benefit from 002451 ** having analysis statistics during the current session, then skip it. 002452 ** This also has the effect of skipping virtual tables and views */ 002453 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue; 002454 002455 /* Reanalyze if the table is 25 times larger than the last analysis */ 002456 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 ); 002457 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 002458 if( !pIdx->hasStat1 ){ 002459 szThreshold = 0; /* Always analyze if any index lacks statistics */ 002460 break; 002461 } 002462 } 002463 if( szThreshold ){ 002464 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); 002465 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur, 002466 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold); 002467 VdbeCoverage(v); 002468 } 002469 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"", 002470 db->aDb[iDb].zDbSName, pTab->zName); 002471 if( opMask & 0x01 ){ 002472 int r1 = sqlite3GetTempReg(pParse); 002473 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC); 002474 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1); 002475 }else{ 002476 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC); 002477 } 002478 } 002479 } 002480 sqlite3VdbeAddOp0(v, OP_Expire); 002481 break; 002482 } 002483 002484 /* 002485 ** PRAGMA busy_timeout 002486 ** PRAGMA busy_timeout = N 002487 ** 002488 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value 002489 ** if one is set. If no busy handler or a different busy handler is set 002490 ** then 0 is returned. Setting the busy_timeout to 0 or negative 002491 ** disables the timeout. 002492 */ 002493 /*case PragTyp_BUSY_TIMEOUT*/ default: { 002494 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT ); 002495 if( zRight ){ 002496 sqlite3_busy_timeout(db, sqlite3Atoi(zRight)); 002497 } 002498 returnSingleInt(v, db->busyTimeout); 002499 break; 002500 } 002501 002502 /* 002503 ** PRAGMA soft_heap_limit 002504 ** PRAGMA soft_heap_limit = N 002505 ** 002506 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the 002507 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is 002508 ** specified and is a non-negative integer. 002509 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always 002510 ** returns the same integer that would be returned by the 002511 ** sqlite3_soft_heap_limit64(-1) C-language function. 002512 */ 002513 case PragTyp_SOFT_HEAP_LIMIT: { 002514 sqlite3_int64 N; 002515 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ 002516 sqlite3_soft_heap_limit64(N); 002517 } 002518 returnSingleInt(v, sqlite3_soft_heap_limit64(-1)); 002519 break; 002520 } 002521 002522 /* 002523 ** PRAGMA hard_heap_limit 002524 ** PRAGMA hard_heap_limit = N 002525 ** 002526 ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap 002527 ** limit. The hard heap limit can be activated or lowered by this 002528 ** pragma, but not raised or deactivated. Only the 002529 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate 002530 ** the hard heap limit. This allows an application to set a heap limit 002531 ** constraint that cannot be relaxed by an untrusted SQL script. 002532 */ 002533 case PragTyp_HARD_HEAP_LIMIT: { 002534 sqlite3_int64 N; 002535 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ 002536 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1); 002537 if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N); 002538 } 002539 returnSingleInt(v, sqlite3_hard_heap_limit64(-1)); 002540 break; 002541 } 002542 002543 /* 002544 ** PRAGMA threads 002545 ** PRAGMA threads = N 002546 ** 002547 ** Configure the maximum number of worker threads. Return the new 002548 ** maximum, which might be less than requested. 002549 */ 002550 case PragTyp_THREADS: { 002551 sqlite3_int64 N; 002552 if( zRight 002553 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK 002554 && N>=0 002555 ){ 002556 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); 002557 } 002558 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); 002559 break; 002560 } 002561 002562 /* 002563 ** PRAGMA analysis_limit 002564 ** PRAGMA analysis_limit = N 002565 ** 002566 ** Configure the maximum number of rows that ANALYZE will examine 002567 ** in each index that it looks at. Return the new limit. 002568 */ 002569 case PragTyp_ANALYSIS_LIMIT: { 002570 sqlite3_int64 N; 002571 if( zRight 002572 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */ 002573 && N>=0 002574 ){ 002575 db->nAnalysisLimit = (int)(N&0x7fffffff); 002576 } 002577 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */ 002578 break; 002579 } 002580 002581 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 002582 /* 002583 ** Report the current state of file logs for all databases 002584 */ 002585 case PragTyp_LOCK_STATUS: { 002586 static const char *const azLockName[] = { 002587 "unlocked", "shared", "reserved", "pending", "exclusive" 002588 }; 002589 int i; 002590 pParse->nMem = 2; 002591 for(i=0; i<db->nDb; i++){ 002592 Btree *pBt; 002593 const char *zState = "unknown"; 002594 int j; 002595 if( db->aDb[i].zDbSName==0 ) continue; 002596 pBt = db->aDb[i].pBt; 002597 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){ 002598 zState = "closed"; 002599 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0, 002600 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ 002601 zState = azLockName[j]; 002602 } 002603 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState); 002604 } 002605 break; 002606 } 002607 #endif 002608 002609 #if defined(SQLITE_ENABLE_CEROD) 002610 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){ 002611 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ 002612 sqlite3_activate_cerod(&zRight[6]); 002613 } 002614 } 002615 break; 002616 #endif 002617 002618 } /* End of the PRAGMA switch */ 002619 002620 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only 002621 ** purpose is to execute assert() statements to verify that if the 002622 ** PragFlg_NoColumns1 flag is set and the caller specified an argument 002623 ** to the PRAGMA, the implementation has not added any OP_ResultRow 002624 ** instructions to the VM. */ 002625 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){ 002626 sqlite3VdbeVerifyNoResultRow(v); 002627 } 002628 002629 pragma_out: 002630 sqlite3DbFree(db, zLeft); 002631 sqlite3DbFree(db, zRight); 002632 } 002633 #ifndef SQLITE_OMIT_VIRTUALTABLE 002634 /***************************************************************************** 002635 ** Implementation of an eponymous virtual table that runs a pragma. 002636 ** 002637 */ 002638 typedef struct PragmaVtab PragmaVtab; 002639 typedef struct PragmaVtabCursor PragmaVtabCursor; 002640 struct PragmaVtab { 002641 sqlite3_vtab base; /* Base class. Must be first */ 002642 sqlite3 *db; /* The database connection to which it belongs */ 002643 const PragmaName *pName; /* Name of the pragma */ 002644 u8 nHidden; /* Number of hidden columns */ 002645 u8 iHidden; /* Index of the first hidden column */ 002646 }; 002647 struct PragmaVtabCursor { 002648 sqlite3_vtab_cursor base; /* Base class. Must be first */ 002649 sqlite3_stmt *pPragma; /* The pragma statement to run */ 002650 sqlite_int64 iRowid; /* Current rowid */ 002651 char *azArg[2]; /* Value of the argument and schema */ 002652 }; 002653 002654 /* 002655 ** Pragma virtual table module xConnect method. 002656 */ 002657 static int pragmaVtabConnect( 002658 sqlite3 *db, 002659 void *pAux, 002660 int argc, const char *const*argv, 002661 sqlite3_vtab **ppVtab, 002662 char **pzErr 002663 ){ 002664 const PragmaName *pPragma = (const PragmaName*)pAux; 002665 PragmaVtab *pTab = 0; 002666 int rc; 002667 int i, j; 002668 char cSep = '('; 002669 StrAccum acc; 002670 char zBuf[200]; 002671 002672 UNUSED_PARAMETER(argc); 002673 UNUSED_PARAMETER(argv); 002674 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); 002675 sqlite3_str_appendall(&acc, "CREATE TABLE x"); 002676 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){ 002677 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]); 002678 cSep = ','; 002679 } 002680 if( i==0 ){ 002681 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName); 002682 i++; 002683 } 002684 j = 0; 002685 if( pPragma->mPragFlg & PragFlg_Result1 ){ 002686 sqlite3_str_appendall(&acc, ",arg HIDDEN"); 002687 j++; 002688 } 002689 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){ 002690 sqlite3_str_appendall(&acc, ",schema HIDDEN"); 002691 j++; 002692 } 002693 sqlite3_str_append(&acc, ")", 1); 002694 sqlite3StrAccumFinish(&acc); 002695 assert( strlen(zBuf) < sizeof(zBuf)-1 ); 002696 rc = sqlite3_declare_vtab(db, zBuf); 002697 if( rc==SQLITE_OK ){ 002698 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab)); 002699 if( pTab==0 ){ 002700 rc = SQLITE_NOMEM; 002701 }else{ 002702 memset(pTab, 0, sizeof(PragmaVtab)); 002703 pTab->pName = pPragma; 002704 pTab->db = db; 002705 pTab->iHidden = i; 002706 pTab->nHidden = j; 002707 } 002708 }else{ 002709 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); 002710 } 002711 002712 *ppVtab = (sqlite3_vtab*)pTab; 002713 return rc; 002714 } 002715 002716 /* 002717 ** Pragma virtual table module xDisconnect method. 002718 */ 002719 static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){ 002720 PragmaVtab *pTab = (PragmaVtab*)pVtab; 002721 sqlite3_free(pTab); 002722 return SQLITE_OK; 002723 } 002724 002725 /* Figure out the best index to use to search a pragma virtual table. 002726 ** 002727 ** There are not really any index choices. But we want to encourage the 002728 ** query planner to give == constraints on as many hidden parameters as 002729 ** possible, and especially on the first hidden parameter. So return a 002730 ** high cost if hidden parameters are unconstrained. 002731 */ 002732 static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ 002733 PragmaVtab *pTab = (PragmaVtab*)tab; 002734 const struct sqlite3_index_constraint *pConstraint; 002735 int i, j; 002736 int seen[2]; 002737 002738 pIdxInfo->estimatedCost = (double)1; 002739 if( pTab->nHidden==0 ){ return SQLITE_OK; } 002740 pConstraint = pIdxInfo->aConstraint; 002741 seen[0] = 0; 002742 seen[1] = 0; 002743 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ 002744 if( pConstraint->usable==0 ) continue; 002745 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; 002746 if( pConstraint->iColumn < pTab->iHidden ) continue; 002747 j = pConstraint->iColumn - pTab->iHidden; 002748 assert( j < 2 ); 002749 seen[j] = i+1; 002750 } 002751 if( seen[0]==0 ){ 002752 pIdxInfo->estimatedCost = (double)2147483647; 002753 pIdxInfo->estimatedRows = 2147483647; 002754 return SQLITE_OK; 002755 } 002756 j = seen[0]-1; 002757 pIdxInfo->aConstraintUsage[j].argvIndex = 1; 002758 pIdxInfo->aConstraintUsage[j].omit = 1; 002759 if( seen[1]==0 ) return SQLITE_OK; 002760 pIdxInfo->estimatedCost = (double)20; 002761 pIdxInfo->estimatedRows = 20; 002762 j = seen[1]-1; 002763 pIdxInfo->aConstraintUsage[j].argvIndex = 2; 002764 pIdxInfo->aConstraintUsage[j].omit = 1; 002765 return SQLITE_OK; 002766 } 002767 002768 /* Create a new cursor for the pragma virtual table */ 002769 static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){ 002770 PragmaVtabCursor *pCsr; 002771 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr)); 002772 if( pCsr==0 ) return SQLITE_NOMEM; 002773 memset(pCsr, 0, sizeof(PragmaVtabCursor)); 002774 pCsr->base.pVtab = pVtab; 002775 *ppCursor = &pCsr->base; 002776 return SQLITE_OK; 002777 } 002778 002779 /* Clear all content from pragma virtual table cursor. */ 002780 static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){ 002781 int i; 002782 sqlite3_finalize(pCsr->pPragma); 002783 pCsr->pPragma = 0; 002784 for(i=0; i<ArraySize(pCsr->azArg); i++){ 002785 sqlite3_free(pCsr->azArg[i]); 002786 pCsr->azArg[i] = 0; 002787 } 002788 } 002789 002790 /* Close a pragma virtual table cursor */ 002791 static int pragmaVtabClose(sqlite3_vtab_cursor *cur){ 002792 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur; 002793 pragmaVtabCursorClear(pCsr); 002794 sqlite3_free(pCsr); 002795 return SQLITE_OK; 002796 } 002797 002798 /* Advance the pragma virtual table cursor to the next row */ 002799 static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){ 002800 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; 002801 int rc = SQLITE_OK; 002802 002803 /* Increment the xRowid value */ 002804 pCsr->iRowid++; 002805 assert( pCsr->pPragma ); 002806 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){ 002807 rc = sqlite3_finalize(pCsr->pPragma); 002808 pCsr->pPragma = 0; 002809 pragmaVtabCursorClear(pCsr); 002810 } 002811 return rc; 002812 } 002813 002814 /* 002815 ** Pragma virtual table module xFilter method. 002816 */ 002817 static int pragmaVtabFilter( 002818 sqlite3_vtab_cursor *pVtabCursor, 002819 int idxNum, const char *idxStr, 002820 int argc, sqlite3_value **argv 002821 ){ 002822 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; 002823 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); 002824 int rc; 002825 int i, j; 002826 StrAccum acc; 002827 char *zSql; 002828 002829 UNUSED_PARAMETER(idxNum); 002830 UNUSED_PARAMETER(idxStr); 002831 pragmaVtabCursorClear(pCsr); 002832 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1; 002833 for(i=0; i<argc; i++, j++){ 002834 const char *zText = (const char*)sqlite3_value_text(argv[i]); 002835 assert( j<ArraySize(pCsr->azArg) ); 002836 assert( pCsr->azArg[j]==0 ); 002837 if( zText ){ 002838 pCsr->azArg[j] = sqlite3_mprintf("%s", zText); 002839 if( pCsr->azArg[j]==0 ){ 002840 return SQLITE_NOMEM; 002841 } 002842 } 002843 } 002844 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]); 002845 sqlite3_str_appendall(&acc, "PRAGMA "); 002846 if( pCsr->azArg[1] ){ 002847 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]); 002848 } 002849 sqlite3_str_appendall(&acc, pTab->pName->zName); 002850 if( pCsr->azArg[0] ){ 002851 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]); 002852 } 002853 zSql = sqlite3StrAccumFinish(&acc); 002854 if( zSql==0 ) return SQLITE_NOMEM; 002855 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0); 002856 sqlite3_free(zSql); 002857 if( rc!=SQLITE_OK ){ 002858 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); 002859 return rc; 002860 } 002861 return pragmaVtabNext(pVtabCursor); 002862 } 002863 002864 /* 002865 ** Pragma virtual table module xEof method. 002866 */ 002867 static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){ 002868 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; 002869 return (pCsr->pPragma==0); 002870 } 002871 002872 /* The xColumn method simply returns the corresponding column from 002873 ** the PRAGMA. 002874 */ 002875 static int pragmaVtabColumn( 002876 sqlite3_vtab_cursor *pVtabCursor, 002877 sqlite3_context *ctx, 002878 int i 002879 ){ 002880 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; 002881 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); 002882 if( i<pTab->iHidden ){ 002883 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i)); 002884 }else{ 002885 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT); 002886 } 002887 return SQLITE_OK; 002888 } 002889 002890 /* 002891 ** Pragma virtual table module xRowid method. 002892 */ 002893 static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){ 002894 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; 002895 *p = pCsr->iRowid; 002896 return SQLITE_OK; 002897 } 002898 002899 /* The pragma virtual table object */ 002900 static const sqlite3_module pragmaVtabModule = { 002901 0, /* iVersion */ 002902 0, /* xCreate - create a table */ 002903 pragmaVtabConnect, /* xConnect - connect to an existing table */ 002904 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */ 002905 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */ 002906 0, /* xDestroy - Drop a table */ 002907 pragmaVtabOpen, /* xOpen - open a cursor */ 002908 pragmaVtabClose, /* xClose - close a cursor */ 002909 pragmaVtabFilter, /* xFilter - configure scan constraints */ 002910 pragmaVtabNext, /* xNext - advance a cursor */ 002911 pragmaVtabEof, /* xEof */ 002912 pragmaVtabColumn, /* xColumn - read data */ 002913 pragmaVtabRowid, /* xRowid - read data */ 002914 0, /* xUpdate - write data */ 002915 0, /* xBegin - begin transaction */ 002916 0, /* xSync - sync transaction */ 002917 0, /* xCommit - commit transaction */ 002918 0, /* xRollback - rollback transaction */ 002919 0, /* xFindFunction - function overloading */ 002920 0, /* xRename - rename the table */ 002921 0, /* xSavepoint */ 002922 0, /* xRelease */ 002923 0, /* xRollbackTo */ 002924 0, /* xShadowName */ 002925 0 /* xIntegrity */ 002926 }; 002927 002928 /* 002929 ** Check to see if zTabName is really the name of a pragma. If it is, 002930 ** then register an eponymous virtual table for that pragma and return 002931 ** a pointer to the Module object for the new virtual table. 002932 */ 002933 Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){ 002934 const PragmaName *pName; 002935 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 ); 002936 pName = pragmaLocate(zName+7); 002937 if( pName==0 ) return 0; 002938 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0; 002939 assert( sqlite3HashFind(&db->aModule, zName)==0 ); 002940 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0); 002941 } 002942 002943 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 002944 002945 #endif /* SQLITE_OMIT_PRAGMA */