1 /* 2 ** 2003 April 6 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains code used to implement the PRAGMA command. 13 */ 14 #include "sqliteInt.h" 15 16 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) 17 # if defined(__APPLE__) 18 # define SQLITE_ENABLE_LOCKING_STYLE 1 19 # else 20 # define SQLITE_ENABLE_LOCKING_STYLE 0 21 # endif 22 #endif 23 24 /*************************************************************************** 25 ** The "pragma.h" include file is an automatically generated file that 26 ** that includes the PragType_XXXX macro definitions and the aPragmaName[] 27 ** object. This ensures that the aPragmaName[] table is arranged in 28 ** lexicographical order to facility a binary search of the pragma name. 29 ** Do not edit pragma.h directly. Edit and rerun the script in at 30 ** ../tool/mkpragmatab.tcl. */ 31 #include "pragma.h" 32 33 /* 34 ** Interpret the given string as a safety level. Return 0 for OFF, 35 ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or 36 ** unrecognized string argument. The FULL and EXTRA option is disallowed 37 ** if the omitFull parameter it 1. 38 ** 39 ** Note that the values returned are one less that the values that 40 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done 41 ** to support legacy SQL code. The safety level used to be boolean 42 ** and older scripts may have used numbers 0 for OFF and 1 for ON. 43 */ 44 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){ 45 /* 123456789 123456789 123 */ 46 static const char zText[] = "onoffalseyestruextrafull"; 47 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20}; 48 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4}; 49 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2}; 50 /* on no off false yes true extra full */ 51 int i, n; 52 if( sqlite3Isdigit(*z) ){ 53 return (u8)sqlite3Atoi(z); 54 } 55 n = sqlite3Strlen30(z); 56 for(i=0; i<ArraySize(iLength); i++){ 57 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 58 && (!omitFull || iValue[i]<=1) 59 ){ 60 return iValue[i]; 61 } 62 } 63 return dflt; 64 } 65 66 /* 67 ** Interpret the given string as a boolean value. 68 */ 69 u8 sqlite3GetBoolean(const char *z, u8 dflt){ 70 return getSafetyLevel(z,1,dflt)!=0; 71 } 72 73 /* The sqlite3GetBoolean() function is used by other modules but the 74 ** remainder of this file is specific to PRAGMA processing. So omit 75 ** the rest of the file if PRAGMAs are omitted from the build. 76 */ 77 #if !defined(SQLITE_OMIT_PRAGMA) 78 79 /* 80 ** Interpret the given string as a locking mode value. 81 */ 82 static int getLockingMode(const char *z){ 83 if( z ){ 84 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE; 85 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL; 86 } 87 return PAGER_LOCKINGMODE_QUERY; 88 } 89 90 #ifndef SQLITE_OMIT_AUTOVACUUM 91 /* 92 ** Interpret the given string as an auto-vacuum mode value. 93 ** 94 ** The following strings, "none", "full" and "incremental" are 95 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. 96 */ 97 static int getAutoVacuum(const char *z){ 98 int i; 99 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE; 100 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL; 101 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR; 102 i = sqlite3Atoi(z); 103 return (u8)((i>=0&&i<=2)?i:0); 104 } 105 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ 106 107 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 108 /* 109 ** Interpret the given string as a temp db location. Return 1 for file 110 ** backed temporary databases, 2 for the Red-Black tree in memory database 111 ** and 0 to use the compile-time default. 112 */ 113 static int getTempStore(const char *z){ 114 if( z[0]>='0' && z[0]<='2' ){ 115 return z[0] - '0'; 116 }else if( sqlite3StrICmp(z, "file")==0 ){ 117 return 1; 118 }else if( sqlite3StrICmp(z, "memory")==0 ){ 119 return 2; 120 }else{ 121 return 0; 122 } 123 } 124 #endif /* SQLITE_PAGER_PRAGMAS */ 125 126 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 127 /* 128 ** Invalidate temp storage, either when the temp storage is changed 129 ** from default, or when 'file' and the temp_store_directory has changed 130 */ 131 static int invalidateTempStorage(Parse *pParse){ 132 sqlite3 *db = pParse->db; 133 if( db->aDb[1].pBt!=0 ){ 134 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){ 135 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " 136 "from within a transaction"); 137 return SQLITE_ERROR; 138 } 139 sqlite3BtreeClose(db->aDb[1].pBt); 140 db->aDb[1].pBt = 0; 141 sqlite3ResetAllSchemasOfConnection(db); 142 } 143 return SQLITE_OK; 144 } 145 #endif /* SQLITE_PAGER_PRAGMAS */ 146 147 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 148 /* 149 ** If the TEMP database is open, close it and mark the database schema 150 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE 151 ** or DEFAULT_TEMP_STORE pragmas. 152 */ 153 static int changeTempStorage(Parse *pParse, const char *zStorageType){ 154 int ts = getTempStore(zStorageType); 155 sqlite3 *db = pParse->db; 156 if( db->temp_store==ts ) return SQLITE_OK; 157 if( invalidateTempStorage( pParse ) != SQLITE_OK ){ 158 return SQLITE_ERROR; 159 } 160 db->temp_store = (u8)ts; 161 return SQLITE_OK; 162 } 163 #endif /* SQLITE_PAGER_PRAGMAS */ 164 165 /* 166 ** Set the names of the first N columns to the values in azCol[] 167 */ 168 static void setAllColumnNames( 169 Vdbe *v, /* The query under construction */ 170 int N, /* Number of columns */ 171 const char **azCol /* Names of columns */ 172 ){ 173 int i; 174 sqlite3VdbeSetNumCols(v, N); 175 for(i=0; i<N; i++){ 176 sqlite3VdbeSetColName(v, i, COLNAME_NAME, azCol[i], SQLITE_STATIC); 177 } 178 } 179 static void setOneColumnName(Vdbe *v, const char *z){ 180 setAllColumnNames(v, 1, &z); 181 } 182 183 /* 184 ** Generate code to return a single integer value. 185 */ 186 static void returnSingleInt(Vdbe *v, const char *zLabel, i64 value){ 187 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64); 188 setOneColumnName(v, zLabel); 189 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 190 } 191 192 /* 193 ** Generate code to return a single text value. 194 */ 195 static void returnSingleText( 196 Vdbe *v, /* Prepared statement under construction */ 197 const char *zLabel, /* Name of the result column */ 198 const char *zValue /* Value to be returned */ 199 ){ 200 if( zValue ){ 201 sqlite3VdbeLoadString(v, 1, (const char*)zValue); 202 setOneColumnName(v, zLabel); 203 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 204 } 205 } 206 207 208 /* 209 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0 210 ** set these values for all pagers. 211 */ 212 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 213 static void setAllPagerFlags(sqlite3 *db){ 214 if( db->autoCommit ){ 215 Db *pDb = db->aDb; 216 int n = db->nDb; 217 assert( SQLITE_FullFSync==PAGER_FULLFSYNC ); 218 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC ); 219 assert( SQLITE_CacheSpill==PAGER_CACHESPILL ); 220 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL) 221 == PAGER_FLAGS_MASK ); 222 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level ); 223 while( (n--) > 0 ){ 224 if( pDb->pBt ){ 225 sqlite3BtreeSetPagerFlags(pDb->pBt, 226 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) ); 227 } 228 pDb++; 229 } 230 } 231 } 232 #else 233 # define setAllPagerFlags(X) /* no-op */ 234 #endif 235 236 237 /* 238 ** Return a human-readable name for a constraint resolution action. 239 */ 240 #ifndef SQLITE_OMIT_FOREIGN_KEY 241 static const char *actionName(u8 action){ 242 const char *zName; 243 switch( action ){ 244 case OE_SetNull: zName = "SET NULL"; break; 245 case OE_SetDflt: zName = "SET DEFAULT"; break; 246 case OE_Cascade: zName = "CASCADE"; break; 247 case OE_Restrict: zName = "RESTRICT"; break; 248 default: zName = "NO ACTION"; 249 assert( action==OE_None ); break; 250 } 251 return zName; 252 } 253 #endif 254 255 256 /* 257 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants 258 ** defined in pager.h. This function returns the associated lowercase 259 ** journal-mode name. 260 */ 261 const char *sqlite3JournalModename(int eMode){ 262 static char * const azModeName[] = { 263 "delete", "persist", "off", "truncate", "memory" 264 #ifndef SQLITE_OMIT_WAL 265 , "wal" 266 #endif 267 }; 268 assert( PAGER_JOURNALMODE_DELETE==0 ); 269 assert( PAGER_JOURNALMODE_PERSIST==1 ); 270 assert( PAGER_JOURNALMODE_OFF==2 ); 271 assert( PAGER_JOURNALMODE_TRUNCATE==3 ); 272 assert( PAGER_JOURNALMODE_MEMORY==4 ); 273 assert( PAGER_JOURNALMODE_WAL==5 ); 274 assert( eMode>=0 && eMode<=ArraySize(azModeName) ); 275 276 if( eMode==ArraySize(azModeName) ) return 0; 277 return azModeName[eMode]; 278 } 279 280 /* 281 ** Process a pragma statement. 282 ** 283 ** Pragmas are of this form: 284 ** 285 ** PRAGMA [schema.]id [= value] 286 ** 287 ** The identifier might also be a string. The value is a string, and 288 ** identifier, or a number. If minusFlag is true, then the value is 289 ** a number that was preceded by a minus sign. 290 ** 291 ** If the left side is "database.id" then pId1 is the database name 292 ** and pId2 is the id. If the left side is just "id" then pId1 is the 293 ** id and pId2 is any empty string. 294 */ 295 void sqlite3Pragma( 296 Parse *pParse, 297 Token *pId1, /* First part of [schema.]id field */ 298 Token *pId2, /* Second part of [schema.]id field, or NULL */ 299 Token *pValue, /* Token for <value>, or NULL */ 300 int minusFlag /* True if a '-' sign preceded <value> */ 301 ){ 302 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */ 303 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */ 304 const char *zDb = 0; /* The database name */ 305 Token *pId; /* Pointer to <id> token */ 306 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */ 307 int iDb; /* Database index for <database> */ 308 int lwr, upr, mid = 0; /* Binary search bounds */ 309 int rc; /* return value form SQLITE_FCNTL_PRAGMA */ 310 sqlite3 *db = pParse->db; /* The database connection */ 311 Db *pDb; /* The specific database being pragmaed */ 312 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */ 313 const struct sPragmaNames *pPragma; 314 315 if( v==0 ) return; 316 sqlite3VdbeRunOnlyOnce(v); 317 pParse->nMem = 2; 318 319 /* Interpret the [schema.] part of the pragma statement. iDb is the 320 ** index of the database this pragma is being applied to in db.aDb[]. */ 321 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); 322 if( iDb<0 ) return; 323 pDb = &db->aDb[iDb]; 324 325 /* If the temp database has been explicitly named as part of the 326 ** pragma, make sure it is open. 327 */ 328 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ 329 return; 330 } 331 332 zLeft = sqlite3NameFromToken(db, pId); 333 if( !zLeft ) return; 334 if( minusFlag ){ 335 zRight = sqlite3MPrintf(db, "-%T", pValue); 336 }else{ 337 zRight = sqlite3NameFromToken(db, pValue); 338 } 339 340 assert( pId2 ); 341 zDb = pId2->n>0 ? pDb->zName : 0; 342 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ 343 goto pragma_out; 344 } 345 346 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS 347 ** connection. If it returns SQLITE_OK, then assume that the VFS 348 ** handled the pragma and generate a no-op prepared statement. 349 ** 350 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, 351 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file 352 ** object corresponding to the database file to which the pragma 353 ** statement refers. 354 ** 355 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA 356 ** file control is an array of pointers to strings (char**) in which the 357 ** second element of the array is the name of the pragma and the third 358 ** element is the argument to the pragma or NULL if the pragma has no 359 ** argument. 360 */ 361 aFcntl[0] = 0; 362 aFcntl[1] = zLeft; 363 aFcntl[2] = zRight; 364 aFcntl[3] = 0; 365 db->busyHandler.nBusy = 0; 366 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl); 367 if( rc==SQLITE_OK ){ 368 returnSingleText(v, "result", aFcntl[0]); 369 sqlite3_free(aFcntl[0]); 370 goto pragma_out; 371 } 372 if( rc!=SQLITE_NOTFOUND ){ 373 if( aFcntl[0] ){ 374 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]); 375 sqlite3_free(aFcntl[0]); 376 } 377 pParse->nErr++; 378 pParse->rc = rc; 379 goto pragma_out; 380 } 381 382 /* Locate the pragma in the lookup table */ 383 lwr = 0; 384 upr = ArraySize(aPragmaNames)-1; 385 while( lwr<=upr ){ 386 mid = (lwr+upr)/2; 387 rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName); 388 if( rc==0 ) break; 389 if( rc<0 ){ 390 upr = mid - 1; 391 }else{ 392 lwr = mid + 1; 393 } 394 } 395 if( lwr>upr ) goto pragma_out; 396 pPragma = &aPragmaNames[mid]; 397 398 /* Make sure the database schema is loaded if the pragma requires that */ 399 if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){ 400 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 401 } 402 403 /* Jump to the appropriate pragma handler */ 404 switch( pPragma->ePragTyp ){ 405 406 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) 407 /* 408 ** PRAGMA [schema.]default_cache_size 409 ** PRAGMA [schema.]default_cache_size=N 410 ** 411 ** The first form reports the current persistent setting for the 412 ** page cache size. The value returned is the maximum number of 413 ** pages in the page cache. The second form sets both the current 414 ** page cache size value and the persistent page cache size value 415 ** stored in the database file. 416 ** 417 ** Older versions of SQLite would set the default cache size to a 418 ** negative number to indicate synchronous=OFF. These days, synchronous 419 ** is always on by default regardless of the sign of the default cache 420 ** size. But continue to take the absolute value of the default cache 421 ** size of historical compatibility. 422 */ 423 case PragTyp_DEFAULT_CACHE_SIZE: { 424 static const int iLn = VDBE_OFFSET_LINENO(2); 425 static const VdbeOpList getCacheSize[] = { 426 { OP_Transaction, 0, 0, 0}, /* 0 */ 427 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */ 428 { OP_IfPos, 1, 8, 0}, 429 { OP_Integer, 0, 2, 0}, 430 { OP_Subtract, 1, 2, 1}, 431 { OP_IfPos, 1, 8, 0}, 432 { OP_Integer, 0, 1, 0}, /* 6 */ 433 { OP_Noop, 0, 0, 0}, 434 { OP_ResultRow, 1, 1, 0}, 435 }; 436 VdbeOp *aOp; 437 sqlite3VdbeUsesBtree(v, iDb); 438 if( !zRight ){ 439 setOneColumnName(v, "cache_size"); 440 pParse->nMem += 2; 441 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize)); 442 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn); 443 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 444 aOp[0].p1 = iDb; 445 aOp[1].p1 = iDb; 446 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE; 447 }else{ 448 int size = sqlite3AbsInt32(sqlite3Atoi(zRight)); 449 sqlite3BeginWriteOperation(pParse, 0, iDb); 450 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size); 451 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 452 pDb->pSchema->cache_size = size; 453 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); 454 } 455 break; 456 } 457 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */ 458 459 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) 460 /* 461 ** PRAGMA [schema.]page_size 462 ** PRAGMA [schema.]page_size=N 463 ** 464 ** The first form reports the current setting for the 465 ** database page size in bytes. The second form sets the 466 ** database page size value. The value can only be set if 467 ** the database has not yet been created. 468 */ 469 case PragTyp_PAGE_SIZE: { 470 Btree *pBt = pDb->pBt; 471 assert( pBt!=0 ); 472 if( !zRight ){ 473 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; 474 returnSingleInt(v, "page_size", size); 475 }else{ 476 /* Malloc may fail when setting the page-size, as there is an internal 477 ** buffer that the pager module resizes using sqlite3_realloc(). 478 */ 479 db->nextPagesize = sqlite3Atoi(zRight); 480 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){ 481 sqlite3OomFault(db); 482 } 483 } 484 break; 485 } 486 487 /* 488 ** PRAGMA [schema.]secure_delete 489 ** PRAGMA [schema.]secure_delete=ON/OFF 490 ** 491 ** The first form reports the current setting for the 492 ** secure_delete flag. The second form changes the secure_delete 493 ** flag setting and reports thenew value. 494 */ 495 case PragTyp_SECURE_DELETE: { 496 Btree *pBt = pDb->pBt; 497 int b = -1; 498 assert( pBt!=0 ); 499 if( zRight ){ 500 b = sqlite3GetBoolean(zRight, 0); 501 } 502 if( pId2->n==0 && b>=0 ){ 503 int ii; 504 for(ii=0; ii<db->nDb; ii++){ 505 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b); 506 } 507 } 508 b = sqlite3BtreeSecureDelete(pBt, b); 509 returnSingleInt(v, "secure_delete", b); 510 break; 511 } 512 513 /* 514 ** PRAGMA [schema.]max_page_count 515 ** PRAGMA [schema.]max_page_count=N 516 ** 517 ** The first form reports the current setting for the 518 ** maximum number of pages in the database file. The 519 ** second form attempts to change this setting. Both 520 ** forms return the current setting. 521 ** 522 ** The absolute value of N is used. This is undocumented and might 523 ** change. The only purpose is to provide an easy way to test 524 ** the sqlite3AbsInt32() function. 525 ** 526 ** PRAGMA [schema.]page_count 527 ** 528 ** Return the number of pages in the specified database. 529 */ 530 case PragTyp_PAGE_COUNT: { 531 int iReg; 532 sqlite3CodeVerifySchema(pParse, iDb); 533 iReg = ++pParse->nMem; 534 if( sqlite3Tolower(zLeft[0])=='p' ){ 535 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); 536 }else{ 537 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, 538 sqlite3AbsInt32(sqlite3Atoi(zRight))); 539 } 540 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); 541 sqlite3VdbeSetNumCols(v, 1); 542 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); 543 break; 544 } 545 546 /* 547 ** PRAGMA [schema.]locking_mode 548 ** PRAGMA [schema.]locking_mode = (normal|exclusive) 549 */ 550 case PragTyp_LOCKING_MODE: { 551 const char *zRet = "normal"; 552 int eMode = getLockingMode(zRight); 553 554 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ 555 /* Simple "PRAGMA locking_mode;" statement. This is a query for 556 ** the current default locking mode (which may be different to 557 ** the locking-mode of the main database). 558 */ 559 eMode = db->dfltLockMode; 560 }else{ 561 Pager *pPager; 562 if( pId2->n==0 ){ 563 /* This indicates that no database name was specified as part 564 ** of the PRAGMA command. In this case the locking-mode must be 565 ** set on all attached databases, as well as the main db file. 566 ** 567 ** Also, the sqlite3.dfltLockMode variable is set so that 568 ** any subsequently attached databases also use the specified 569 ** locking mode. 570 */ 571 int ii; 572 assert(pDb==&db->aDb[0]); 573 for(ii=2; ii<db->nDb; ii++){ 574 pPager = sqlite3BtreePager(db->aDb[ii].pBt); 575 sqlite3PagerLockingMode(pPager, eMode); 576 } 577 db->dfltLockMode = (u8)eMode; 578 } 579 pPager = sqlite3BtreePager(pDb->pBt); 580 eMode = sqlite3PagerLockingMode(pPager, eMode); 581 } 582 583 assert( eMode==PAGER_LOCKINGMODE_NORMAL 584 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); 585 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ 586 zRet = "exclusive"; 587 } 588 returnSingleText(v, "locking_mode", zRet); 589 break; 590 } 591 592 /* 593 ** PRAGMA [schema.]journal_mode 594 ** PRAGMA [schema.]journal_mode = 595 ** (delete|persist|off|truncate|memory|wal|off) 596 */ 597 case PragTyp_JOURNAL_MODE: { 598 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ 599 int ii; /* Loop counter */ 600 601 setOneColumnName(v, "journal_mode"); 602 if( zRight==0 ){ 603 /* If there is no "=MODE" part of the pragma, do a query for the 604 ** current mode */ 605 eMode = PAGER_JOURNALMODE_QUERY; 606 }else{ 607 const char *zMode; 608 int n = sqlite3Strlen30(zRight); 609 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){ 610 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break; 611 } 612 if( !zMode ){ 613 /* If the "=MODE" part does not match any known journal mode, 614 ** then do a query */ 615 eMode = PAGER_JOURNALMODE_QUERY; 616 } 617 } 618 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ 619 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ 620 iDb = 0; 621 pId2->n = 1; 622 } 623 for(ii=db->nDb-1; ii>=0; ii--){ 624 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ 625 sqlite3VdbeUsesBtree(v, ii); 626 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode); 627 } 628 } 629 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 630 break; 631 } 632 633 /* 634 ** PRAGMA [schema.]journal_size_limit 635 ** PRAGMA [schema.]journal_size_limit=N 636 ** 637 ** Get or set the size limit on rollback journal files. 638 */ 639 case PragTyp_JOURNAL_SIZE_LIMIT: { 640 Pager *pPager = sqlite3BtreePager(pDb->pBt); 641 i64 iLimit = -2; 642 if( zRight ){ 643 sqlite3DecOrHexToI64(zRight, &iLimit); 644 if( iLimit<-1 ) iLimit = -1; 645 } 646 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); 647 returnSingleInt(v, "journal_size_limit", iLimit); 648 break; 649 } 650 651 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ 652 653 /* 654 ** PRAGMA [schema.]auto_vacuum 655 ** PRAGMA [schema.]auto_vacuum=N 656 ** 657 ** Get or set the value of the database 'auto-vacuum' parameter. 658 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL 659 */ 660 #ifndef SQLITE_OMIT_AUTOVACUUM 661 case PragTyp_AUTO_VACUUM: { 662 Btree *pBt = pDb->pBt; 663 assert( pBt!=0 ); 664 if( !zRight ){ 665 returnSingleInt(v, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt)); 666 }else{ 667 int eAuto = getAutoVacuum(zRight); 668 assert( eAuto>=0 && eAuto<=2 ); 669 db->nextAutovac = (u8)eAuto; 670 /* Call SetAutoVacuum() to set initialize the internal auto and 671 ** incr-vacuum flags. This is required in case this connection 672 ** creates the database file. It is important that it is created 673 ** as an auto-vacuum capable db. 674 */ 675 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); 676 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ 677 /* When setting the auto_vacuum mode to either "full" or 678 ** "incremental", write the value of meta[6] in the database 679 ** file. Before writing to meta[6], check that meta[3] indicates 680 ** that this really is an auto-vacuum capable database. 681 */ 682 static const int iLn = VDBE_OFFSET_LINENO(2); 683 static const VdbeOpList setMeta6[] = { 684 { OP_Transaction, 0, 1, 0}, /* 0 */ 685 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE}, 686 { OP_If, 1, 0, 0}, /* 2 */ 687 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ 688 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */ 689 }; 690 VdbeOp *aOp; 691 int iAddr = sqlite3VdbeCurrentAddr(v); 692 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6)); 693 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn); 694 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 695 aOp[0].p1 = iDb; 696 aOp[1].p1 = iDb; 697 aOp[2].p2 = iAddr+4; 698 aOp[4].p1 = iDb; 699 aOp[4].p3 = eAuto - 1; 700 sqlite3VdbeUsesBtree(v, iDb); 701 } 702 } 703 break; 704 } 705 #endif 706 707 /* 708 ** PRAGMA [schema.]incremental_vacuum(N) 709 ** 710 ** Do N steps of incremental vacuuming on a database. 711 */ 712 #ifndef SQLITE_OMIT_AUTOVACUUM 713 case PragTyp_INCREMENTAL_VACUUM: { 714 int iLimit, addr; 715 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ 716 iLimit = 0x7fffffff; 717 } 718 sqlite3BeginWriteOperation(pParse, 0, iDb); 719 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); 720 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v); 721 sqlite3VdbeAddOp1(v, OP_ResultRow, 1); 722 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); 723 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v); 724 sqlite3VdbeJumpHere(v, addr); 725 break; 726 } 727 #endif 728 729 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 730 /* 731 ** PRAGMA [schema.]cache_size 732 ** PRAGMA [schema.]cache_size=N 733 ** 734 ** The first form reports the current local setting for the 735 ** page cache size. The second form sets the local 736 ** page cache size value. If N is positive then that is the 737 ** number of pages in the cache. If N is negative, then the 738 ** number of pages is adjusted so that the cache uses -N kibibytes 739 ** of memory. 740 */ 741 case PragTyp_CACHE_SIZE: { 742 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 743 if( !zRight ){ 744 returnSingleInt(v, "cache_size", pDb->pSchema->cache_size); 745 }else{ 746 int size = sqlite3Atoi(zRight); 747 pDb->pSchema->cache_size = size; 748 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); 749 } 750 break; 751 } 752 753 /* 754 ** PRAGMA [schema.]cache_spill 755 ** PRAGMA cache_spill=BOOLEAN 756 ** PRAGMA [schema.]cache_spill=N 757 ** 758 ** The first form reports the current local setting for the 759 ** page cache spill size. The second form turns cache spill on 760 ** or off. When turnning cache spill on, the size is set to the 761 ** current cache_size. The third form sets a spill size that 762 ** may be different form the cache size. 763 ** If N is positive then that is the 764 ** number of pages in the cache. If N is negative, then the 765 ** number of pages is adjusted so that the cache uses -N kibibytes 766 ** of memory. 767 ** 768 ** If the number of cache_spill pages is less then the number of 769 ** cache_size pages, no spilling occurs until the page count exceeds 770 ** the number of cache_size pages. 771 ** 772 ** The cache_spill=BOOLEAN setting applies to all attached schemas, 773 ** not just the schema specified. 774 */ 775 case PragTyp_CACHE_SPILL: { 776 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 777 if( !zRight ){ 778 returnSingleInt(v, "cache_spill", 779 (db->flags & SQLITE_CacheSpill)==0 ? 0 : 780 sqlite3BtreeSetSpillSize(pDb->pBt,0)); 781 }else{ 782 int size = 1; 783 if( sqlite3GetInt32(zRight, &size) ){ 784 sqlite3BtreeSetSpillSize(pDb->pBt, size); 785 } 786 if( sqlite3GetBoolean(zRight, size!=0) ){ 787 db->flags |= SQLITE_CacheSpill; 788 }else{ 789 db->flags &= ~SQLITE_CacheSpill; 790 } 791 setAllPagerFlags(db); 792 } 793 break; 794 } 795 796 /* 797 ** PRAGMA [schema.]mmap_size(N) 798 ** 799 ** Used to set mapping size limit. The mapping size limit is 800 ** used to limit the aggregate size of all memory mapped regions of the 801 ** database file. If this parameter is set to zero, then memory mapping 802 ** is not used at all. If N is negative, then the default memory map 803 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set. 804 ** The parameter N is measured in bytes. 805 ** 806 ** This value is advisory. The underlying VFS is free to memory map 807 ** as little or as much as it wants. Except, if N is set to 0 then the 808 ** upper layers will never invoke the xFetch interfaces to the VFS. 809 */ 810 case PragTyp_MMAP_SIZE: { 811 sqlite3_int64 sz; 812 #if SQLITE_MAX_MMAP_SIZE>0 813 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); 814 if( zRight ){ 815 int ii; 816 sqlite3DecOrHexToI64(zRight, &sz); 817 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap; 818 if( pId2->n==0 ) db->szMmap = sz; 819 for(ii=db->nDb-1; ii>=0; ii--){ 820 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ 821 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); 822 } 823 } 824 } 825 sz = -1; 826 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz); 827 #else 828 sz = 0; 829 rc = SQLITE_OK; 830 #endif 831 if( rc==SQLITE_OK ){ 832 returnSingleInt(v, "mmap_size", sz); 833 }else if( rc!=SQLITE_NOTFOUND ){ 834 pParse->nErr++; 835 pParse->rc = rc; 836 } 837 break; 838 } 839 840 /* 841 ** PRAGMA temp_store 842 ** PRAGMA temp_store = "default"|"memory"|"file" 843 ** 844 ** Return or set the local value of the temp_store flag. Changing 845 ** the local value does not make changes to the disk file and the default 846 ** value will be restored the next time the database is opened. 847 ** 848 ** Note that it is possible for the library compile-time options to 849 ** override this setting 850 */ 851 case PragTyp_TEMP_STORE: { 852 if( !zRight ){ 853 returnSingleInt(v, "temp_store", db->temp_store); 854 }else{ 855 changeTempStorage(pParse, zRight); 856 } 857 break; 858 } 859 860 /* 861 ** PRAGMA temp_store_directory 862 ** PRAGMA temp_store_directory = ""|"directory_name" 863 ** 864 ** Return or set the local value of the temp_store_directory flag. Changing 865 ** the value sets a specific directory to be used for temporary files. 866 ** Setting to a null string reverts to the default temporary directory search. 867 ** If temporary directory is changed, then invalidateTempStorage. 868 ** 869 */ 870 case PragTyp_TEMP_STORE_DIRECTORY: { 871 if( !zRight ){ 872 returnSingleText(v, "temp_store_directory", sqlite3_temp_directory); 873 }else{ 874 #ifndef SQLITE_OMIT_WSD 875 if( zRight[0] ){ 876 int res; 877 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); 878 if( rc!=SQLITE_OK || res==0 ){ 879 sqlite3ErrorMsg(pParse, "not a writable directory"); 880 goto pragma_out; 881 } 882 } 883 if( SQLITE_TEMP_STORE==0 884 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) 885 || (SQLITE_TEMP_STORE==2 && db->temp_store==1) 886 ){ 887 invalidateTempStorage(pParse); 888 } 889 sqlite3_free(sqlite3_temp_directory); 890 if( zRight[0] ){ 891 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); 892 }else{ 893 sqlite3_temp_directory = 0; 894 } 895 #endif /* SQLITE_OMIT_WSD */ 896 } 897 break; 898 } 899 900 #if SQLITE_OS_WIN 901 /* 902 ** PRAGMA data_store_directory 903 ** PRAGMA data_store_directory = ""|"directory_name" 904 ** 905 ** Return or set the local value of the data_store_directory flag. Changing 906 ** the value sets a specific directory to be used for database files that 907 ** were specified with a relative pathname. Setting to a null string reverts 908 ** to the default database directory, which for database files specified with 909 ** a relative path will probably be based on the current directory for the 910 ** process. Database file specified with an absolute path are not impacted 911 ** by this setting, regardless of its value. 912 ** 913 */ 914 case PragTyp_DATA_STORE_DIRECTORY: { 915 if( !zRight ){ 916 returnSingleText(v, "data_store_directory", sqlite3_data_directory); 917 }else{ 918 #ifndef SQLITE_OMIT_WSD 919 if( zRight[0] ){ 920 int res; 921 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); 922 if( rc!=SQLITE_OK || res==0 ){ 923 sqlite3ErrorMsg(pParse, "not a writable directory"); 924 goto pragma_out; 925 } 926 } 927 sqlite3_free(sqlite3_data_directory); 928 if( zRight[0] ){ 929 sqlite3_data_directory = sqlite3_mprintf("%s", zRight); 930 }else{ 931 sqlite3_data_directory = 0; 932 } 933 #endif /* SQLITE_OMIT_WSD */ 934 } 935 break; 936 } 937 #endif 938 939 #if SQLITE_ENABLE_LOCKING_STYLE 940 /* 941 ** PRAGMA [schema.]lock_proxy_file 942 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path" 943 ** 944 ** Return or set the value of the lock_proxy_file flag. Changing 945 ** the value sets a specific file to be used for database access locks. 946 ** 947 */ 948 case PragTyp_LOCK_PROXY_FILE: { 949 if( !zRight ){ 950 Pager *pPager = sqlite3BtreePager(pDb->pBt); 951 char *proxy_file_path = NULL; 952 sqlite3_file *pFile = sqlite3PagerFile(pPager); 953 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 954 &proxy_file_path); 955 returnSingleText(v, "lock_proxy_file", proxy_file_path); 956 }else{ 957 Pager *pPager = sqlite3BtreePager(pDb->pBt); 958 sqlite3_file *pFile = sqlite3PagerFile(pPager); 959 int res; 960 if( zRight[0] ){ 961 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 962 zRight); 963 } else { 964 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 965 NULL); 966 } 967 if( res!=SQLITE_OK ){ 968 sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); 969 goto pragma_out; 970 } 971 } 972 break; 973 } 974 #endif /* SQLITE_ENABLE_LOCKING_STYLE */ 975 976 /* 977 ** PRAGMA [schema.]synchronous 978 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA 979 ** 980 ** Return or set the local value of the synchronous flag. Changing 981 ** the local value does not make changes to the disk file and the 982 ** default value will be restored the next time the database is 983 ** opened. 984 */ 985 case PragTyp_SYNCHRONOUS: { 986 if( !zRight ){ 987 returnSingleInt(v, "synchronous", pDb->safety_level-1); 988 }else{ 989 if( !db->autoCommit ){ 990 sqlite3ErrorMsg(pParse, 991 "Safety level may not be changed inside a transaction"); 992 }else{ 993 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK; 994 if( iLevel==0 ) iLevel = 1; 995 pDb->safety_level = iLevel; 996 setAllPagerFlags(db); 997 } 998 } 999 break; 1000 } 1001 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ 1002 1003 #ifndef SQLITE_OMIT_FLAG_PRAGMAS 1004 case PragTyp_FLAG: { 1005 if( zRight==0 ){ 1006 returnSingleInt(v, pPragma->zName, (db->flags & pPragma->iArg)!=0 ); 1007 }else{ 1008 int mask = pPragma->iArg; /* Mask of bits to set or clear. */ 1009 if( db->autoCommit==0 ){ 1010 /* Foreign key support may not be enabled or disabled while not 1011 ** in auto-commit mode. */ 1012 mask &= ~(SQLITE_ForeignKeys); 1013 } 1014 #if SQLITE_USER_AUTHENTICATION 1015 if( db->auth.authLevel==UAUTH_User ){ 1016 /* Do not allow non-admin users to modify the schema arbitrarily */ 1017 mask &= ~(SQLITE_WriteSchema); 1018 } 1019 #endif 1020 1021 if( sqlite3GetBoolean(zRight, 0) ){ 1022 db->flags |= mask; 1023 }else{ 1024 db->flags &= ~mask; 1025 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0; 1026 } 1027 1028 /* Many of the flag-pragmas modify the code generated by the SQL 1029 ** compiler (eg. count_changes). So add an opcode to expire all 1030 ** compiled SQL statements after modifying a pragma value. 1031 */ 1032 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); 1033 setAllPagerFlags(db); 1034 } 1035 break; 1036 } 1037 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ 1038 1039 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS 1040 /* 1041 ** PRAGMA table_info(<table>) 1042 ** 1043 ** Return a single row for each column of the named table. The columns of 1044 ** the returned data set are: 1045 ** 1046 ** cid: Column id (numbered from left to right, starting at 0) 1047 ** name: Column name 1048 ** type: Column declaration type. 1049 ** notnull: True if 'NOT NULL' is part of column declaration 1050 ** dflt_value: The default value for the column, if any. 1051 */ 1052 case PragTyp_TABLE_INFO: if( zRight ){ 1053 Table *pTab; 1054 pTab = sqlite3FindTable(db, zRight, zDb); 1055 if( pTab ){ 1056 static const char *azCol[] = { 1057 "cid", "name", "type", "notnull", "dflt_value", "pk" 1058 }; 1059 int i, k; 1060 int nHidden = 0; 1061 Column *pCol; 1062 Index *pPk = sqlite3PrimaryKeyIndex(pTab); 1063 pParse->nMem = 6; 1064 sqlite3CodeVerifySchema(pParse, iDb); 1065 setAllColumnNames(v, 6, azCol); assert( 6==ArraySize(azCol) ); 1066 sqlite3ViewGetColumnNames(pParse, pTab); 1067 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ 1068 if( IsHiddenColumn(pCol) ){ 1069 nHidden++; 1070 continue; 1071 } 1072 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ 1073 k = 0; 1074 }else if( pPk==0 ){ 1075 k = 1; 1076 }else{ 1077 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} 1078 } 1079 sqlite3VdbeMultiLoad(v, 1, "issisi", 1080 i-nHidden, 1081 pCol->zName, 1082 pCol->zType ? pCol->zType : "", 1083 pCol->notNull ? 1 : 0, 1084 pCol->zDflt, 1085 k); 1086 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); 1087 } 1088 } 1089 } 1090 break; 1091 1092 case PragTyp_STATS: { 1093 static const char *azCol[] = { "table", "index", "width", "height" }; 1094 Index *pIdx; 1095 HashElem *i; 1096 v = sqlite3GetVdbe(pParse); 1097 pParse->nMem = 4; 1098 sqlite3CodeVerifySchema(pParse, iDb); 1099 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) ); 1100 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){ 1101 Table *pTab = sqliteHashData(i); 1102 sqlite3VdbeMultiLoad(v, 1, "ssii", 1103 pTab->zName, 1104 0, 1105 (int)sqlite3LogEstToInt(pTab->szTabRow), 1106 (int)sqlite3LogEstToInt(pTab->nRowLogEst)); 1107 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); 1108 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 1109 sqlite3VdbeMultiLoad(v, 2, "sii", 1110 pIdx->zName, 1111 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 1112 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0])); 1113 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); 1114 } 1115 } 1116 } 1117 break; 1118 1119 case PragTyp_INDEX_INFO: if( zRight ){ 1120 Index *pIdx; 1121 Table *pTab; 1122 pIdx = sqlite3FindIndex(db, zRight, zDb); 1123 if( pIdx ){ 1124 static const char *azCol[] = { 1125 "seqno", "cid", "name", "desc", "coll", "key" 1126 }; 1127 int i; 1128 int mx; 1129 if( pPragma->iArg ){ 1130 /* PRAGMA index_xinfo (newer version with more rows and columns) */ 1131 mx = pIdx->nColumn; 1132 pParse->nMem = 6; 1133 }else{ 1134 /* PRAGMA index_info (legacy version) */ 1135 mx = pIdx->nKeyCol; 1136 pParse->nMem = 3; 1137 } 1138 pTab = pIdx->pTable; 1139 sqlite3CodeVerifySchema(pParse, iDb); 1140 assert( pParse->nMem<=ArraySize(azCol) ); 1141 setAllColumnNames(v, pParse->nMem, azCol); 1142 for(i=0; i<mx; i++){ 1143 i16 cnum = pIdx->aiColumn[i]; 1144 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum, 1145 cnum<0 ? 0 : pTab->aCol[cnum].zName); 1146 if( pPragma->iArg ){ 1147 sqlite3VdbeMultiLoad(v, 4, "isi", 1148 pIdx->aSortOrder[i], 1149 pIdx->azColl[i], 1150 i<pIdx->nKeyCol); 1151 } 1152 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem); 1153 } 1154 } 1155 } 1156 break; 1157 1158 case PragTyp_INDEX_LIST: if( zRight ){ 1159 Index *pIdx; 1160 Table *pTab; 1161 int i; 1162 pTab = sqlite3FindTable(db, zRight, zDb); 1163 if( pTab ){ 1164 static const char *azCol[] = { 1165 "seq", "name", "unique", "origin", "partial" 1166 }; 1167 v = sqlite3GetVdbe(pParse); 1168 pParse->nMem = 5; 1169 sqlite3CodeVerifySchema(pParse, iDb); 1170 setAllColumnNames(v, 5, azCol); assert( 5==ArraySize(azCol) ); 1171 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){ 1172 const char *azOrigin[] = { "c", "u", "pk" }; 1173 sqlite3VdbeMultiLoad(v, 1, "isisi", 1174 i, 1175 pIdx->zName, 1176 IsUniqueIndex(pIdx), 1177 azOrigin[pIdx->idxType], 1178 pIdx->pPartIdxWhere!=0); 1179 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); 1180 } 1181 } 1182 } 1183 break; 1184 1185 case PragTyp_DATABASE_LIST: { 1186 static const char *azCol[] = { "seq", "name", "file" }; 1187 int i; 1188 pParse->nMem = 3; 1189 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) ); 1190 for(i=0; i<db->nDb; i++){ 1191 if( db->aDb[i].pBt==0 ) continue; 1192 assert( db->aDb[i].zName!=0 ); 1193 sqlite3VdbeMultiLoad(v, 1, "iss", 1194 i, 1195 db->aDb[i].zName, 1196 sqlite3BtreeGetFilename(db->aDb[i].pBt)); 1197 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); 1198 } 1199 } 1200 break; 1201 1202 case PragTyp_COLLATION_LIST: { 1203 static const char *azCol[] = { "seq", "name" }; 1204 int i = 0; 1205 HashElem *p; 1206 pParse->nMem = 2; 1207 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) ); 1208 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ 1209 CollSeq *pColl = (CollSeq *)sqliteHashData(p); 1210 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName); 1211 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); 1212 } 1213 } 1214 break; 1215 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ 1216 1217 #ifndef SQLITE_OMIT_FOREIGN_KEY 1218 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){ 1219 FKey *pFK; 1220 Table *pTab; 1221 pTab = sqlite3FindTable(db, zRight, zDb); 1222 if( pTab ){ 1223 v = sqlite3GetVdbe(pParse); 1224 pFK = pTab->pFKey; 1225 if( pFK ){ 1226 static const char *azCol[] = { 1227 "id", "seq", "table", "from", "to", "on_update", "on_delete", 1228 "match" 1229 }; 1230 int i = 0; 1231 pParse->nMem = 8; 1232 sqlite3CodeVerifySchema(pParse, iDb); 1233 setAllColumnNames(v, 8, azCol); assert( 8==ArraySize(azCol) ); 1234 while(pFK){ 1235 int j; 1236 for(j=0; j<pFK->nCol; j++){ 1237 sqlite3VdbeMultiLoad(v, 1, "iissssss", 1238 i, 1239 j, 1240 pFK->zTo, 1241 pTab->aCol[pFK->aCol[j].iFrom].zName, 1242 pFK->aCol[j].zCol, 1243 actionName(pFK->aAction[1]), /* ON UPDATE */ 1244 actionName(pFK->aAction[0]), /* ON DELETE */ 1245 "NONE"); 1246 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8); 1247 } 1248 ++i; 1249 pFK = pFK->pNextFrom; 1250 } 1251 } 1252 } 1253 } 1254 break; 1255 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 1256 1257 #ifndef SQLITE_OMIT_FOREIGN_KEY 1258 #ifndef SQLITE_OMIT_TRIGGER 1259 case PragTyp_FOREIGN_KEY_CHECK: { 1260 FKey *pFK; /* A foreign key constraint */ 1261 Table *pTab; /* Child table contain "REFERENCES" keyword */ 1262 Table *pParent; /* Parent table that child points to */ 1263 Index *pIdx; /* Index in the parent table */ 1264 int i; /* Loop counter: Foreign key number for pTab */ 1265 int j; /* Loop counter: Field of the foreign key */ 1266 HashElem *k; /* Loop counter: Next table in schema */ 1267 int x; /* result variable */ 1268 int regResult; /* 3 registers to hold a result row */ 1269 int regKey; /* Register to hold key for checking the FK */ 1270 int regRow; /* Registers to hold a row from pTab */ 1271 int addrTop; /* Top of a loop checking foreign keys */ 1272 int addrOk; /* Jump here if the key is OK */ 1273 int *aiCols; /* child to parent column mapping */ 1274 static const char *azCol[] = { "table", "rowid", "parent", "fkid" }; 1275 1276 regResult = pParse->nMem+1; 1277 pParse->nMem += 4; 1278 regKey = ++pParse->nMem; 1279 regRow = ++pParse->nMem; 1280 v = sqlite3GetVdbe(pParse); 1281 setAllColumnNames(v, 4, azCol); assert( 4==ArraySize(azCol) ); 1282 sqlite3CodeVerifySchema(pParse, iDb); 1283 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash); 1284 while( k ){ 1285 if( zRight ){ 1286 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb); 1287 k = 0; 1288 }else{ 1289 pTab = (Table*)sqliteHashData(k); 1290 k = sqliteHashNext(k); 1291 } 1292 if( pTab==0 || pTab->pFKey==0 ) continue; 1293 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 1294 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow; 1295 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); 1296 sqlite3VdbeLoadString(v, regResult, pTab->zName); 1297 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ 1298 pParent = sqlite3FindTable(db, pFK->zTo, zDb); 1299 if( pParent==0 ) continue; 1300 pIdx = 0; 1301 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName); 1302 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0); 1303 if( x==0 ){ 1304 if( pIdx==0 ){ 1305 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead); 1306 }else{ 1307 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb); 1308 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 1309 } 1310 }else{ 1311 k = 0; 1312 break; 1313 } 1314 } 1315 assert( pParse->nErr>0 || pFK==0 ); 1316 if( pFK ) break; 1317 if( pParse->nTab<i ) pParse->nTab = i; 1318 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v); 1319 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ 1320 pParent = sqlite3FindTable(db, pFK->zTo, zDb); 1321 pIdx = 0; 1322 aiCols = 0; 1323 if( pParent ){ 1324 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols); 1325 assert( x==0 ); 1326 } 1327 addrOk = sqlite3VdbeMakeLabel(v); 1328 if( pParent && pIdx==0 ){ 1329 int iKey = pFK->aCol[0].iFrom; 1330 assert( iKey>=0 && iKey<pTab->nCol ); 1331 if( iKey!=pTab->iPKey ){ 1332 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow); 1333 sqlite3ColumnDefault(v, pTab, iKey, regRow); 1334 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v); 1335 sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, 1336 sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v); 1337 }else{ 1338 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow); 1339 } 1340 sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v); 1341 sqlite3VdbeGoto(v, addrOk); 1342 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); 1343 }else{ 1344 for(j=0; j<pFK->nCol; j++){ 1345 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, 1346 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j); 1347 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); 1348 } 1349 if( pParent ){ 1350 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey, 1351 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol); 1352 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0); 1353 VdbeCoverage(v); 1354 } 1355 } 1356 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1); 1357 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1); 1358 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4); 1359 sqlite3VdbeResolveLabel(v, addrOk); 1360 sqlite3DbFree(db, aiCols); 1361 } 1362 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v); 1363 sqlite3VdbeJumpHere(v, addrTop); 1364 } 1365 } 1366 break; 1367 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 1368 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 1369 1370 #ifndef NDEBUG 1371 case PragTyp_PARSER_TRACE: { 1372 if( zRight ){ 1373 if( sqlite3GetBoolean(zRight, 0) ){ 1374 sqlite3ParserTrace(stdout, "parser: "); 1375 }else{ 1376 sqlite3ParserTrace(0, 0); 1377 } 1378 } 1379 } 1380 break; 1381 #endif 1382 1383 /* Reinstall the LIKE and GLOB functions. The variant of LIKE 1384 ** used will be case sensitive or not depending on the RHS. 1385 */ 1386 case PragTyp_CASE_SENSITIVE_LIKE: { 1387 if( zRight ){ 1388 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0)); 1389 } 1390 } 1391 break; 1392 1393 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX 1394 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 1395 #endif 1396 1397 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 1398 /* Pragma "quick_check" is reduced version of 1399 ** integrity_check designed to detect most database corruption 1400 ** without most of the overhead of a full integrity-check. 1401 */ 1402 case PragTyp_INTEGRITY_CHECK: { 1403 int i, j, addr, mxErr; 1404 1405 int isQuick = (sqlite3Tolower(zLeft[0])=='q'); 1406 1407 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check", 1408 ** then iDb is set to the index of the database identified by <db>. 1409 ** In this case, the integrity of database iDb only is verified by 1410 ** the VDBE created below. 1411 ** 1412 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or 1413 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb 1414 ** to -1 here, to indicate that the VDBE should verify the integrity 1415 ** of all attached databases. */ 1416 assert( iDb>=0 ); 1417 assert( iDb==0 || pId2->z ); 1418 if( pId2->z==0 ) iDb = -1; 1419 1420 /* Initialize the VDBE program */ 1421 pParse->nMem = 6; 1422 setOneColumnName(v, "integrity_check"); 1423 1424 /* Set the maximum error count */ 1425 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; 1426 if( zRight ){ 1427 sqlite3GetInt32(zRight, &mxErr); 1428 if( mxErr<=0 ){ 1429 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; 1430 } 1431 } 1432 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ 1433 1434 /* Do an integrity check on each database file */ 1435 for(i=0; i<db->nDb; i++){ 1436 HashElem *x; 1437 Hash *pTbls; 1438 int cnt = 0; 1439 1440 if( OMIT_TEMPDB && i==1 ) continue; 1441 if( iDb>=0 && i!=iDb ) continue; 1442 1443 sqlite3CodeVerifySchema(pParse, i); 1444 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ 1445 VdbeCoverage(v); 1446 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); 1447 sqlite3VdbeJumpHere(v, addr); 1448 1449 /* Do an integrity check of the B-Tree 1450 ** 1451 ** Begin by filling registers 2, 3, ... with the root pages numbers 1452 ** for all tables and indices in the database. 1453 */ 1454 assert( sqlite3SchemaMutexHeld(db, i, 0) ); 1455 pTbls = &db->aDb[i].pSchema->tblHash; 1456 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ 1457 Table *pTab = sqliteHashData(x); 1458 Index *pIdx; 1459 if( HasRowid(pTab) ){ 1460 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt); 1461 VdbeComment((v, "%s", pTab->zName)); 1462 cnt++; 1463 } 1464 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 1465 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt); 1466 VdbeComment((v, "%s", pIdx->zName)); 1467 cnt++; 1468 } 1469 } 1470 1471 /* Make sure sufficient number of registers have been allocated */ 1472 pParse->nMem = MAX( pParse->nMem, cnt+8 ); 1473 1474 /* Do the b-tree integrity checks */ 1475 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1); 1476 sqlite3VdbeChangeP5(v, (u8)i); 1477 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); 1478 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, 1479 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), 1480 P4_DYNAMIC); 1481 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1); 1482 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); 1483 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); 1484 sqlite3VdbeJumpHere(v, addr); 1485 1486 /* Make sure all the indices are constructed correctly. 1487 */ 1488 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ 1489 Table *pTab = sqliteHashData(x); 1490 Index *pIdx, *pPk; 1491 Index *pPrior = 0; 1492 int loopTop; 1493 int iDataCur, iIdxCur; 1494 int r1 = -1; 1495 1496 if( pTab->pIndex==0 ) continue; 1497 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); 1498 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ 1499 VdbeCoverage(v); 1500 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); 1501 sqlite3VdbeJumpHere(v, addr); 1502 sqlite3ExprCacheClear(pParse); 1503 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, 1504 1, 0, &iDataCur, &iIdxCur); 1505 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); 1506 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 1507 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ 1508 } 1509 pParse->nMem = MAX(pParse->nMem, 8+j); 1510 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); 1511 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); 1512 /* Verify that all NOT NULL columns really are NOT NULL */ 1513 for(j=0; j<pTab->nCol; j++){ 1514 char *zErr; 1515 int jmp2, jmp3; 1516 if( j==pTab->iPKey ) continue; 1517 if( pTab->aCol[j].notNull==0 ) continue; 1518 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); 1519 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 1520 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); 1521 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ 1522 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, 1523 pTab->aCol[j].zName); 1524 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); 1525 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); 1526 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); 1527 sqlite3VdbeAddOp0(v, OP_Halt); 1528 sqlite3VdbeJumpHere(v, jmp2); 1529 sqlite3VdbeJumpHere(v, jmp3); 1530 } 1531 /* Validate index entries for the current row */ 1532 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 1533 int jmp2, jmp3, jmp4, jmp5; 1534 int ckUniq = sqlite3VdbeMakeLabel(v); 1535 if( pPk==pIdx ) continue; 1536 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, 1537 pPrior, r1); 1538 pPrior = pIdx; 1539 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */ 1540 /* Verify that an index entry exists for the current table row */ 1541 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, 1542 pIdx->nColumn); VdbeCoverage(v); 1543 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ 1544 sqlite3VdbeLoadString(v, 3, "row "); 1545 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); 1546 sqlite3VdbeLoadString(v, 4, " missing from index "); 1547 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); 1548 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); 1549 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); 1550 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); 1551 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); 1552 sqlite3VdbeAddOp0(v, OP_Halt); 1553 sqlite3VdbeJumpHere(v, jmp2); 1554 /* For UNIQUE indexes, verify that only one entry exists with the 1555 ** current key. The entry is unique if (1) any column is NULL 1556 ** or (2) the next entry has a different key */ 1557 if( IsUniqueIndex(pIdx) ){ 1558 int uniqOk = sqlite3VdbeMakeLabel(v); 1559 int jmp6; 1560 int kk; 1561 for(kk=0; kk<pIdx->nKeyCol; kk++){ 1562 int iCol = pIdx->aiColumn[kk]; 1563 assert( iCol!=XN_ROWID && iCol<pTab->nCol ); 1564 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue; 1565 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk); 1566 VdbeCoverage(v); 1567 } 1568 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v); 1569 sqlite3VdbeGoto(v, uniqOk); 1570 sqlite3VdbeJumpHere(v, jmp6); 1571 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1, 1572 pIdx->nKeyCol); VdbeCoverage(v); 1573 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ 1574 sqlite3VdbeLoadString(v, 3, "non-unique entry in index "); 1575 sqlite3VdbeGoto(v, jmp5); 1576 sqlite3VdbeResolveLabel(v, uniqOk); 1577 } 1578 sqlite3VdbeJumpHere(v, jmp4); 1579 sqlite3ResolvePartIdxLabel(pParse, jmp3); 1580 } 1581 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); 1582 sqlite3VdbeJumpHere(v, loopTop-1); 1583 #ifndef SQLITE_OMIT_BTREECOUNT 1584 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); 1585 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 1586 if( pPk==pIdx ) continue; 1587 addr = sqlite3VdbeCurrentAddr(v); 1588 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v); 1589 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); 1590 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); 1591 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v); 1592 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); 1593 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); 1594 sqlite3VdbeLoadString(v, 3, pIdx->zName); 1595 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7); 1596 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1); 1597 } 1598 #endif /* SQLITE_OMIT_BTREECOUNT */ 1599 } 1600 } 1601 { 1602 static const int iLn = VDBE_OFFSET_LINENO(2); 1603 static const VdbeOpList endCode[] = { 1604 { OP_AddImm, 1, 0, 0}, /* 0 */ 1605 { OP_If, 1, 4, 0}, /* 1 */ 1606 { OP_String8, 0, 3, 0}, /* 2 */ 1607 { OP_ResultRow, 3, 1, 0}, /* 3 */ 1608 }; 1609 VdbeOp *aOp; 1610 1611 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn); 1612 if( aOp ){ 1613 aOp[0].p2 = -mxErr; 1614 aOp[2].p4type = P4_STATIC; 1615 aOp[2].p4.z = "ok"; 1616 } 1617 } 1618 } 1619 break; 1620 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 1621 1622 #ifndef SQLITE_OMIT_UTF16 1623 /* 1624 ** PRAGMA encoding 1625 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" 1626 ** 1627 ** In its first form, this pragma returns the encoding of the main 1628 ** database. If the database is not initialized, it is initialized now. 1629 ** 1630 ** The second form of this pragma is a no-op if the main database file 1631 ** has not already been initialized. In this case it sets the default 1632 ** encoding that will be used for the main database file if a new file 1633 ** is created. If an existing main database file is opened, then the 1634 ** default text encoding for the existing database is used. 1635 ** 1636 ** In all cases new databases created using the ATTACH command are 1637 ** created to use the same default text encoding as the main database. If 1638 ** the main database has not been initialized and/or created when ATTACH 1639 ** is executed, this is done before the ATTACH operation. 1640 ** 1641 ** In the second form this pragma sets the text encoding to be used in 1642 ** new database files created using this database handle. It is only 1643 ** useful if invoked immediately after the main database i 1644 */ 1645 case PragTyp_ENCODING: { 1646 static const struct EncName { 1647 char *zName; 1648 u8 enc; 1649 } encnames[] = { 1650 { "UTF8", SQLITE_UTF8 }, 1651 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ 1652 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ 1653 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ 1654 { "UTF16le", SQLITE_UTF16LE }, 1655 { "UTF16be", SQLITE_UTF16BE }, 1656 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ 1657 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ 1658 { 0, 0 } 1659 }; 1660 const struct EncName *pEnc; 1661 if( !zRight ){ /* "PRAGMA encoding" */ 1662 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 1663 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); 1664 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); 1665 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); 1666 returnSingleText(v, "encoding", encnames[ENC(pParse->db)].zName); 1667 }else{ /* "PRAGMA encoding = XXX" */ 1668 /* Only change the value of sqlite.enc if the database handle is not 1669 ** initialized. If the main database exists, the new sqlite.enc value 1670 ** will be overwritten when the schema is next loaded. If it does not 1671 ** already exists, it will be created to use the new encoding value. 1672 */ 1673 if( 1674 !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 1675 DbHasProperty(db, 0, DB_Empty) 1676 ){ 1677 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ 1678 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ 1679 SCHEMA_ENC(db) = ENC(db) = 1680 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; 1681 break; 1682 } 1683 } 1684 if( !pEnc->zName ){ 1685 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); 1686 } 1687 } 1688 } 1689 } 1690 break; 1691 #endif /* SQLITE_OMIT_UTF16 */ 1692 1693 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS 1694 /* 1695 ** PRAGMA [schema.]schema_version 1696 ** PRAGMA [schema.]schema_version = <integer> 1697 ** 1698 ** PRAGMA [schema.]user_version 1699 ** PRAGMA [schema.]user_version = <integer> 1700 ** 1701 ** PRAGMA [schema.]freelist_count = <integer> 1702 ** 1703 ** PRAGMA [schema.]application_id 1704 ** PRAGMA [schema.]application_id = <integer> 1705 ** 1706 ** The pragma's schema_version and user_version are used to set or get 1707 ** the value of the schema-version and user-version, respectively. Both 1708 ** the schema-version and the user-version are 32-bit signed integers 1709 ** stored in the database header. 1710 ** 1711 ** The schema-cookie is usually only manipulated internally by SQLite. It 1712 ** is incremented by SQLite whenever the database schema is modified (by 1713 ** creating or dropping a table or index). The schema version is used by 1714 ** SQLite each time a query is executed to ensure that the internal cache 1715 ** of the schema used when compiling the SQL query matches the schema of 1716 ** the database against which the compiled query is actually executed. 1717 ** Subverting this mechanism by using "PRAGMA schema_version" to modify 1718 ** the schema-version is potentially dangerous and may lead to program 1719 ** crashes or database corruption. Use with caution! 1720 ** 1721 ** The user-version is not used internally by SQLite. It may be used by 1722 ** applications for any purpose. 1723 */ 1724 case PragTyp_HEADER_VALUE: { 1725 int iCookie = pPragma->iArg; /* Which cookie to read or write */ 1726 sqlite3VdbeUsesBtree(v, iDb); 1727 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){ 1728 /* Write the specified cookie value */ 1729 static const VdbeOpList setCookie[] = { 1730 { OP_Transaction, 0, 1, 0}, /* 0 */ 1731 { OP_SetCookie, 0, 0, 0}, /* 1 */ 1732 }; 1733 VdbeOp *aOp; 1734 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie)); 1735 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0); 1736 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 1737 aOp[0].p1 = iDb; 1738 aOp[1].p1 = iDb; 1739 aOp[1].p2 = iCookie; 1740 aOp[1].p3 = sqlite3Atoi(zRight); 1741 }else{ 1742 /* Read the specified cookie value */ 1743 static const VdbeOpList readCookie[] = { 1744 { OP_Transaction, 0, 0, 0}, /* 0 */ 1745 { OP_ReadCookie, 0, 1, 0}, /* 1 */ 1746 { OP_ResultRow, 1, 1, 0} 1747 }; 1748 VdbeOp *aOp; 1749 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie)); 1750 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0); 1751 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; 1752 aOp[0].p1 = iDb; 1753 aOp[1].p1 = iDb; 1754 aOp[1].p3 = iCookie; 1755 sqlite3VdbeSetNumCols(v, 1); 1756 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); 1757 } 1758 } 1759 break; 1760 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ 1761 1762 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 1763 /* 1764 ** PRAGMA compile_options 1765 ** 1766 ** Return the names of all compile-time options used in this build, 1767 ** one option per row. 1768 */ 1769 case PragTyp_COMPILE_OPTIONS: { 1770 int i = 0; 1771 const char *zOpt; 1772 pParse->nMem = 1; 1773 setOneColumnName(v, "compile_option"); 1774 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ 1775 sqlite3VdbeLoadString(v, 1, zOpt); 1776 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 1777 } 1778 } 1779 break; 1780 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 1781 1782 #ifndef SQLITE_OMIT_WAL 1783 /* 1784 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate 1785 ** 1786 ** Checkpoint the database. 1787 */ 1788 case PragTyp_WAL_CHECKPOINT: { 1789 static const char *azCol[] = { "busy", "log", "checkpointed" }; 1790 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED); 1791 int eMode = SQLITE_CHECKPOINT_PASSIVE; 1792 if( zRight ){ 1793 if( sqlite3StrICmp(zRight, "full")==0 ){ 1794 eMode = SQLITE_CHECKPOINT_FULL; 1795 }else if( sqlite3StrICmp(zRight, "restart")==0 ){ 1796 eMode = SQLITE_CHECKPOINT_RESTART; 1797 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){ 1798 eMode = SQLITE_CHECKPOINT_TRUNCATE; 1799 } 1800 } 1801 setAllColumnNames(v, 3, azCol); assert( 3==ArraySize(azCol) ); 1802 pParse->nMem = 3; 1803 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1); 1804 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); 1805 } 1806 break; 1807 1808 /* 1809 ** PRAGMA wal_autocheckpoint 1810 ** PRAGMA wal_autocheckpoint = N 1811 ** 1812 ** Configure a database connection to automatically checkpoint a database 1813 ** after accumulating N frames in the log. Or query for the current value 1814 ** of N. 1815 */ 1816 case PragTyp_WAL_AUTOCHECKPOINT: { 1817 if( zRight ){ 1818 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); 1819 } 1820 returnSingleInt(v, "wal_autocheckpoint", 1821 db->xWalCallback==sqlite3WalDefaultHook ? 1822 SQLITE_PTR_TO_INT(db->pWalArg) : 0); 1823 } 1824 break; 1825 #endif 1826 1827 /* 1828 ** PRAGMA shrink_memory 1829 ** 1830 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database 1831 ** connection on which it is invoked to free up as much memory as it 1832 ** can, by calling sqlite3_db_release_memory(). 1833 */ 1834 case PragTyp_SHRINK_MEMORY: { 1835 sqlite3_db_release_memory(db); 1836 break; 1837 } 1838 1839 /* 1840 ** PRAGMA busy_timeout 1841 ** PRAGMA busy_timeout = N 1842 ** 1843 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value 1844 ** if one is set. If no busy handler or a different busy handler is set 1845 ** then 0 is returned. Setting the busy_timeout to 0 or negative 1846 ** disables the timeout. 1847 */ 1848 /*case PragTyp_BUSY_TIMEOUT*/ default: { 1849 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT ); 1850 if( zRight ){ 1851 sqlite3_busy_timeout(db, sqlite3Atoi(zRight)); 1852 } 1853 returnSingleInt(v, "timeout", db->busyTimeout); 1854 break; 1855 } 1856 1857 /* 1858 ** PRAGMA soft_heap_limit 1859 ** PRAGMA soft_heap_limit = N 1860 ** 1861 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the 1862 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is 1863 ** specified and is a non-negative integer. 1864 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always 1865 ** returns the same integer that would be returned by the 1866 ** sqlite3_soft_heap_limit64(-1) C-language function. 1867 */ 1868 case PragTyp_SOFT_HEAP_LIMIT: { 1869 sqlite3_int64 N; 1870 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ 1871 sqlite3_soft_heap_limit64(N); 1872 } 1873 returnSingleInt(v, "soft_heap_limit", sqlite3_soft_heap_limit64(-1)); 1874 break; 1875 } 1876 1877 /* 1878 ** PRAGMA threads 1879 ** PRAGMA threads = N 1880 ** 1881 ** Configure the maximum number of worker threads. Return the new 1882 ** maximum, which might be less than requested. 1883 */ 1884 case PragTyp_THREADS: { 1885 sqlite3_int64 N; 1886 if( zRight 1887 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK 1888 && N>=0 1889 ){ 1890 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); 1891 } 1892 returnSingleInt(v, "threads", 1893 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); 1894 break; 1895 } 1896 1897 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 1898 /* 1899 ** Report the current state of file logs for all databases 1900 */ 1901 case PragTyp_LOCK_STATUS: { 1902 static const char *const azLockName[] = { 1903 "unlocked", "shared", "reserved", "pending", "exclusive" 1904 }; 1905 static const char *azCol[] = { "database", "status" }; 1906 int i; 1907 setAllColumnNames(v, 2, azCol); assert( 2==ArraySize(azCol) ); 1908 pParse->nMem = 2; 1909 for(i=0; i<db->nDb; i++){ 1910 Btree *pBt; 1911 const char *zState = "unknown"; 1912 int j; 1913 if( db->aDb[i].zName==0 ) continue; 1914 pBt = db->aDb[i].pBt; 1915 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){ 1916 zState = "closed"; 1917 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 1918 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ 1919 zState = azLockName[j]; 1920 } 1921 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zName, zState); 1922 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); 1923 } 1924 break; 1925 } 1926 #endif 1927 1928 #ifdef SQLITE_HAS_CODEC 1929 case PragTyp_KEY: { 1930 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); 1931 break; 1932 } 1933 case PragTyp_REKEY: { 1934 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); 1935 break; 1936 } 1937 case PragTyp_HEXKEY: { 1938 if( zRight ){ 1939 u8 iByte; 1940 int i; 1941 char zKey[40]; 1942 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){ 1943 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]); 1944 if( (i&1)!=0 ) zKey[i/2] = iByte; 1945 } 1946 if( (zLeft[3] & 0xf)==0xb ){ 1947 sqlite3_key_v2(db, zDb, zKey, i/2); 1948 }else{ 1949 sqlite3_rekey_v2(db, zDb, zKey, i/2); 1950 } 1951 } 1952 break; 1953 } 1954 #endif 1955 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) 1956 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){ 1957 #ifdef SQLITE_HAS_CODEC 1958 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){ 1959 sqlite3_activate_see(&zRight[4]); 1960 } 1961 #endif 1962 #ifdef SQLITE_ENABLE_CEROD 1963 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ 1964 sqlite3_activate_cerod(&zRight[6]); 1965 } 1966 #endif 1967 } 1968 break; 1969 #endif 1970 1971 } /* End of the PRAGMA switch */ 1972 1973 pragma_out: 1974 sqlite3DbFree(db, zLeft); 1975 sqlite3DbFree(db, zRight); 1976 } 1977 1978 #endif /* SQLITE_OMIT_PRAGMA */ 1979