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 /* Ignore this whole file if pragmas are disabled 17 */ 18 #if !defined(SQLITE_OMIT_PRAGMA) 19 20 /* 21 ** Interpret the given string as a safety level. Return 0 for OFF, 22 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or 23 ** unrecognized string argument. 24 ** 25 ** Note that the values returned are one less that the values that 26 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done 27 ** to support legacy SQL code. The safety level used to be boolean 28 ** and older scripts may have used numbers 0 for OFF and 1 for ON. 29 */ 30 static u8 getSafetyLevel(const char *z){ 31 /* 123456789 123456789 */ 32 static const char zText[] = "onoffalseyestruefull"; 33 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16}; 34 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4}; 35 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2}; 36 int i, n; 37 if( sqlite3Isdigit(*z) ){ 38 return (u8)atoi(z); 39 } 40 n = sqlite3Strlen30(z); 41 for(i=0; i<ArraySize(iLength); i++){ 42 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){ 43 return iValue[i]; 44 } 45 } 46 return 1; 47 } 48 49 /* 50 ** Interpret the given string as a boolean value. 51 */ 52 static u8 getBoolean(const char *z){ 53 return getSafetyLevel(z)&1; 54 } 55 56 /* 57 ** Interpret the given string as a locking mode value. 58 */ 59 static int getLockingMode(const char *z){ 60 if( z ){ 61 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE; 62 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL; 63 } 64 return PAGER_LOCKINGMODE_QUERY; 65 } 66 67 #ifndef SQLITE_OMIT_AUTOVACUUM 68 /* 69 ** Interpret the given string as an auto-vacuum mode value. 70 ** 71 ** The following strings, "none", "full" and "incremental" are 72 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. 73 */ 74 static int getAutoVacuum(const char *z){ 75 int i; 76 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE; 77 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL; 78 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR; 79 i = atoi(z); 80 return (u8)((i>=0&&i<=2)?i:0); 81 } 82 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ 83 84 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 85 /* 86 ** Interpret the given string as a temp db location. Return 1 for file 87 ** backed temporary databases, 2 for the Red-Black tree in memory database 88 ** and 0 to use the compile-time default. 89 */ 90 static int getTempStore(const char *z){ 91 if( z[0]>='0' && z[0]<='2' ){ 92 return z[0] - '0'; 93 }else if( sqlite3StrICmp(z, "file")==0 ){ 94 return 1; 95 }else if( sqlite3StrICmp(z, "memory")==0 ){ 96 return 2; 97 }else{ 98 return 0; 99 } 100 } 101 #endif /* SQLITE_PAGER_PRAGMAS */ 102 103 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 104 /* 105 ** Invalidate temp storage, either when the temp storage is changed 106 ** from default, or when 'file' and the temp_store_directory has changed 107 */ 108 static int invalidateTempStorage(Parse *pParse){ 109 sqlite3 *db = pParse->db; 110 if( db->aDb[1].pBt!=0 ){ 111 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){ 112 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " 113 "from within a transaction"); 114 return SQLITE_ERROR; 115 } 116 sqlite3BtreeClose(db->aDb[1].pBt); 117 db->aDb[1].pBt = 0; 118 sqlite3ResetInternalSchema(db, 0); 119 } 120 return SQLITE_OK; 121 } 122 #endif /* SQLITE_PAGER_PRAGMAS */ 123 124 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 125 /* 126 ** If the TEMP database is open, close it and mark the database schema 127 ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE 128 ** or DEFAULT_TEMP_STORE pragmas. 129 */ 130 static int changeTempStorage(Parse *pParse, const char *zStorageType){ 131 int ts = getTempStore(zStorageType); 132 sqlite3 *db = pParse->db; 133 if( db->temp_store==ts ) return SQLITE_OK; 134 if( invalidateTempStorage( pParse ) != SQLITE_OK ){ 135 return SQLITE_ERROR; 136 } 137 db->temp_store = (u8)ts; 138 return SQLITE_OK; 139 } 140 #endif /* SQLITE_PAGER_PRAGMAS */ 141 142 /* 143 ** Generate code to return a single integer value. 144 */ 145 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){ 146 Vdbe *v = sqlite3GetVdbe(pParse); 147 int mem = ++pParse->nMem; 148 i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value)); 149 if( pI64 ){ 150 memcpy(pI64, &value, sizeof(value)); 151 } 152 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64); 153 sqlite3VdbeSetNumCols(v, 1); 154 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC); 155 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1); 156 } 157 158 #ifndef SQLITE_OMIT_FLAG_PRAGMAS 159 /* 160 ** Check to see if zRight and zLeft refer to a pragma that queries 161 ** or changes one of the flags in db->flags. Return 1 if so and 0 if not. 162 ** Also, implement the pragma. 163 */ 164 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){ 165 static const struct sPragmaType { 166 const char *zName; /* Name of the pragma */ 167 int mask; /* Mask for the db->flags value */ 168 } aPragma[] = { 169 { "full_column_names", SQLITE_FullColNames }, 170 { "short_column_names", SQLITE_ShortColNames }, 171 { "count_changes", SQLITE_CountRows }, 172 { "empty_result_callbacks", SQLITE_NullCallback }, 173 { "legacy_file_format", SQLITE_LegacyFileFmt }, 174 { "fullfsync", SQLITE_FullFSync }, 175 { "reverse_unordered_selects", SQLITE_ReverseOrder }, 176 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX 177 { "automatic_index", SQLITE_AutoIndex }, 178 #endif 179 #ifdef SQLITE_DEBUG 180 { "sql_trace", SQLITE_SqlTrace }, 181 { "vdbe_listing", SQLITE_VdbeListing }, 182 { "vdbe_trace", SQLITE_VdbeTrace }, 183 #endif 184 #ifndef SQLITE_OMIT_CHECK 185 { "ignore_check_constraints", SQLITE_IgnoreChecks }, 186 #endif 187 /* The following is VERY experimental */ 188 { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode }, 189 { "omit_readlock", SQLITE_NoReadlock }, 190 191 /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted 192 ** flag if there are any active statements. */ 193 { "read_uncommitted", SQLITE_ReadUncommitted }, 194 { "recursive_triggers", SQLITE_RecTriggers }, 195 196 /* This flag may only be set if both foreign-key and trigger support 197 ** are present in the build. */ 198 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) 199 { "foreign_keys", SQLITE_ForeignKeys }, 200 #endif 201 }; 202 int i; 203 const struct sPragmaType *p; 204 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){ 205 if( sqlite3StrICmp(zLeft, p->zName)==0 ){ 206 sqlite3 *db = pParse->db; 207 Vdbe *v; 208 v = sqlite3GetVdbe(pParse); 209 assert( v!=0 ); /* Already allocated by sqlite3Pragma() */ 210 if( ALWAYS(v) ){ 211 if( zRight==0 ){ 212 returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 ); 213 }else{ 214 int mask = p->mask; /* Mask of bits to set or clear. */ 215 if( db->autoCommit==0 ){ 216 /* Foreign key support may not be enabled or disabled while not 217 ** in auto-commit mode. */ 218 mask &= ~(SQLITE_ForeignKeys); 219 } 220 221 if( getBoolean(zRight) ){ 222 db->flags |= mask; 223 }else{ 224 db->flags &= ~mask; 225 } 226 227 /* Many of the flag-pragmas modify the code generated by the SQL 228 ** compiler (eg. count_changes). So add an opcode to expire all 229 ** compiled SQL statements after modifying a pragma value. 230 */ 231 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); 232 } 233 } 234 235 return 1; 236 } 237 } 238 return 0; 239 } 240 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ 241 242 /* 243 ** Return a human-readable name for a constraint resolution action. 244 */ 245 #ifndef SQLITE_OMIT_FOREIGN_KEY 246 static const char *actionName(u8 action){ 247 const char *zName; 248 switch( action ){ 249 case OE_SetNull: zName = "SET NULL"; break; 250 case OE_SetDflt: zName = "SET DEFAULT"; break; 251 case OE_Cascade: zName = "CASCADE"; break; 252 case OE_Restrict: zName = "RESTRICT"; break; 253 default: zName = "NO ACTION"; 254 assert( action==OE_None ); break; 255 } 256 return zName; 257 } 258 #endif 259 260 261 /* 262 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants 263 ** defined in pager.h. This function returns the associated lowercase 264 ** journal-mode name. 265 */ 266 const char *sqlite3JournalModename(int eMode){ 267 static char * const azModeName[] = { 268 "delete", "persist", "off", "truncate", "memory" 269 #ifndef SQLITE_OMIT_WAL 270 , "wal" 271 #endif 272 }; 273 assert( PAGER_JOURNALMODE_DELETE==0 ); 274 assert( PAGER_JOURNALMODE_PERSIST==1 ); 275 assert( PAGER_JOURNALMODE_OFF==2 ); 276 assert( PAGER_JOURNALMODE_TRUNCATE==3 ); 277 assert( PAGER_JOURNALMODE_MEMORY==4 ); 278 assert( PAGER_JOURNALMODE_WAL==5 ); 279 assert( eMode>=0 && eMode<=ArraySize(azModeName) ); 280 281 if( eMode==ArraySize(azModeName) ) return 0; 282 return azModeName[eMode]; 283 } 284 285 /* 286 ** Process a pragma statement. 287 ** 288 ** Pragmas are of this form: 289 ** 290 ** PRAGMA [database.]id [= value] 291 ** 292 ** The identifier might also be a string. The value is a string, and 293 ** identifier, or a number. If minusFlag is true, then the value is 294 ** a number that was preceded by a minus sign. 295 ** 296 ** If the left side is "database.id" then pId1 is the database name 297 ** and pId2 is the id. If the left side is just "id" then pId1 is the 298 ** id and pId2 is any empty string. 299 */ 300 void sqlite3Pragma( 301 Parse *pParse, 302 Token *pId1, /* First part of [database.]id field */ 303 Token *pId2, /* Second part of [database.]id field, or NULL */ 304 Token *pValue, /* Token for <value>, or NULL */ 305 int minusFlag /* True if a '-' sign preceded <value> */ 306 ){ 307 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */ 308 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */ 309 const char *zDb = 0; /* The database name */ 310 Token *pId; /* Pointer to <id> token */ 311 int iDb; /* Database index for <database> */ 312 sqlite3 *db = pParse->db; 313 Db *pDb; 314 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); 315 if( v==0 ) return; 316 sqlite3VdbeRunOnlyOnce(v); 317 pParse->nMem = 2; 318 319 /* Interpret the [database.] 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 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 347 /* 348 ** PRAGMA [database.]default_cache_size 349 ** PRAGMA [database.]default_cache_size=N 350 ** 351 ** The first form reports the current persistent setting for the 352 ** page cache size. The value returned is the maximum number of 353 ** pages in the page cache. The second form sets both the current 354 ** page cache size value and the persistent page cache size value 355 ** stored in the database file. 356 ** 357 ** Older versions of SQLite would set the default cache size to a 358 ** negative number to indicate synchronous=OFF. These days, synchronous 359 ** is always on by default regardless of the sign of the default cache 360 ** size. But continue to take the absolute value of the default cache 361 ** size of historical compatibility. 362 */ 363 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){ 364 static const VdbeOpList getCacheSize[] = { 365 { OP_Transaction, 0, 0, 0}, /* 0 */ 366 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */ 367 { OP_IfPos, 1, 7, 0}, 368 { OP_Integer, 0, 2, 0}, 369 { OP_Subtract, 1, 2, 1}, 370 { OP_IfPos, 1, 7, 0}, 371 { OP_Integer, 0, 1, 0}, /* 6 */ 372 { OP_ResultRow, 1, 1, 0}, 373 }; 374 int addr; 375 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 376 sqlite3VdbeUsesBtree(v, iDb); 377 if( !zRight ){ 378 sqlite3VdbeSetNumCols(v, 1); 379 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC); 380 pParse->nMem += 2; 381 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); 382 sqlite3VdbeChangeP1(v, addr, iDb); 383 sqlite3VdbeChangeP1(v, addr+1, iDb); 384 sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE); 385 }else{ 386 int size = atoi(zRight); 387 if( size<0 ) size = -size; 388 sqlite3BeginWriteOperation(pParse, 0, iDb); 389 sqlite3VdbeAddOp2(v, OP_Integer, size, 1); 390 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1); 391 pDb->pSchema->cache_size = size; 392 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); 393 } 394 }else 395 396 /* 397 ** PRAGMA [database.]page_size 398 ** PRAGMA [database.]page_size=N 399 ** 400 ** The first form reports the current setting for the 401 ** database page size in bytes. The second form sets the 402 ** database page size value. The value can only be set if 403 ** the database has not yet been created. 404 */ 405 if( sqlite3StrICmp(zLeft,"page_size")==0 ){ 406 Btree *pBt = pDb->pBt; 407 assert( pBt!=0 ); 408 if( !zRight ){ 409 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; 410 returnSingleInt(pParse, "page_size", size); 411 }else{ 412 /* Malloc may fail when setting the page-size, as there is an internal 413 ** buffer that the pager module resizes using sqlite3_realloc(). 414 */ 415 db->nextPagesize = atoi(zRight); 416 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ 417 db->mallocFailed = 1; 418 } 419 } 420 }else 421 422 /* 423 ** PRAGMA [database.]max_page_count 424 ** PRAGMA [database.]max_page_count=N 425 ** 426 ** The first form reports the current setting for the 427 ** maximum number of pages in the database file. The 428 ** second form attempts to change this setting. Both 429 ** forms return the current setting. 430 */ 431 if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){ 432 Btree *pBt = pDb->pBt; 433 int newMax = 0; 434 assert( pBt!=0 ); 435 if( zRight ){ 436 newMax = atoi(zRight); 437 } 438 if( ALWAYS(pBt) ){ 439 newMax = sqlite3BtreeMaxPageCount(pBt, newMax); 440 } 441 returnSingleInt(pParse, "max_page_count", newMax); 442 }else 443 444 /* 445 ** PRAGMA [database.]secure_delete 446 ** PRAGMA [database.]secure_delete=ON/OFF 447 ** 448 ** The first form reports the current setting for the 449 ** secure_delete flag. The second form changes the secure_delete 450 ** flag setting and reports thenew value. 451 */ 452 if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){ 453 Btree *pBt = pDb->pBt; 454 int b = -1; 455 assert( pBt!=0 ); 456 if( zRight ){ 457 b = getBoolean(zRight); 458 } 459 if( pId2->n==0 && b>=0 ){ 460 int ii; 461 for(ii=0; ii<db->nDb; ii++){ 462 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b); 463 } 464 } 465 b = sqlite3BtreeSecureDelete(pBt, b); 466 returnSingleInt(pParse, "secure_delete", b); 467 }else 468 469 /* 470 ** PRAGMA [database.]page_count 471 ** 472 ** Return the number of pages in the specified database. 473 */ 474 if( sqlite3StrICmp(zLeft,"page_count")==0 ){ 475 int iReg; 476 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 477 sqlite3CodeVerifySchema(pParse, iDb); 478 iReg = ++pParse->nMem; 479 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); 480 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); 481 sqlite3VdbeSetNumCols(v, 1); 482 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC); 483 }else 484 485 /* 486 ** PRAGMA [database.]locking_mode 487 ** PRAGMA [database.]locking_mode = (normal|exclusive) 488 */ 489 if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){ 490 const char *zRet = "normal"; 491 int eMode = getLockingMode(zRight); 492 493 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ 494 /* Simple "PRAGMA locking_mode;" statement. This is a query for 495 ** the current default locking mode (which may be different to 496 ** the locking-mode of the main database). 497 */ 498 eMode = db->dfltLockMode; 499 }else{ 500 Pager *pPager; 501 if( pId2->n==0 ){ 502 /* This indicates that no database name was specified as part 503 ** of the PRAGMA command. In this case the locking-mode must be 504 ** set on all attached databases, as well as the main db file. 505 ** 506 ** Also, the sqlite3.dfltLockMode variable is set so that 507 ** any subsequently attached databases also use the specified 508 ** locking mode. 509 */ 510 int ii; 511 assert(pDb==&db->aDb[0]); 512 for(ii=2; ii<db->nDb; ii++){ 513 pPager = sqlite3BtreePager(db->aDb[ii].pBt); 514 sqlite3PagerLockingMode(pPager, eMode); 515 } 516 db->dfltLockMode = (u8)eMode; 517 } 518 pPager = sqlite3BtreePager(pDb->pBt); 519 eMode = sqlite3PagerLockingMode(pPager, eMode); 520 } 521 522 assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE); 523 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ 524 zRet = "exclusive"; 525 } 526 sqlite3VdbeSetNumCols(v, 1); 527 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC); 528 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0); 529 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 530 }else 531 532 /* 533 ** PRAGMA [database.]journal_mode 534 ** PRAGMA [database.]journal_mode = 535 ** (delete|persist|off|truncate|memory|wal|off) 536 */ 537 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){ 538 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ 539 int ii; /* Loop counter */ 540 541 /* Force the schema to be loaded on all databases. This cases all 542 ** database files to be opened and the journal_modes set. */ 543 if( sqlite3ReadSchema(pParse) ){ 544 goto pragma_out; 545 } 546 547 sqlite3VdbeSetNumCols(v, 1); 548 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC); 549 550 if( zRight==0 ){ 551 /* If there is no "=MODE" part of the pragma, do a query for the 552 ** current mode */ 553 eMode = PAGER_JOURNALMODE_QUERY; 554 }else{ 555 const char *zMode; 556 int n = sqlite3Strlen30(zRight); 557 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){ 558 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break; 559 } 560 if( !zMode ){ 561 /* If the "=MODE" part does not match any known journal mode, 562 ** then do a query */ 563 eMode = PAGER_JOURNALMODE_QUERY; 564 } 565 } 566 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ 567 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ 568 iDb = 0; 569 pId2->n = 1; 570 } 571 for(ii=db->nDb-1; ii>=0; ii--){ 572 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ 573 sqlite3VdbeUsesBtree(v, ii); 574 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode); 575 } 576 } 577 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 578 }else 579 580 /* 581 ** PRAGMA [database.]journal_size_limit 582 ** PRAGMA [database.]journal_size_limit=N 583 ** 584 ** Get or set the size limit on rollback journal files. 585 */ 586 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){ 587 Pager *pPager = sqlite3BtreePager(pDb->pBt); 588 i64 iLimit = -2; 589 if( zRight ){ 590 sqlite3Atoi64(zRight, &iLimit); 591 if( iLimit<-1 ) iLimit = -1; 592 } 593 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); 594 returnSingleInt(pParse, "journal_size_limit", iLimit); 595 }else 596 597 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ 598 599 /* 600 ** PRAGMA [database.]auto_vacuum 601 ** PRAGMA [database.]auto_vacuum=N 602 ** 603 ** Get or set the value of the database 'auto-vacuum' parameter. 604 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL 605 */ 606 #ifndef SQLITE_OMIT_AUTOVACUUM 607 if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){ 608 Btree *pBt = pDb->pBt; 609 assert( pBt!=0 ); 610 if( sqlite3ReadSchema(pParse) ){ 611 goto pragma_out; 612 } 613 if( !zRight ){ 614 int auto_vacuum; 615 if( ALWAYS(pBt) ){ 616 auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt); 617 }else{ 618 auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM; 619 } 620 returnSingleInt(pParse, "auto_vacuum", auto_vacuum); 621 }else{ 622 int eAuto = getAutoVacuum(zRight); 623 assert( eAuto>=0 && eAuto<=2 ); 624 db->nextAutovac = (u8)eAuto; 625 if( ALWAYS(eAuto>=0) ){ 626 /* Call SetAutoVacuum() to set initialize the internal auto and 627 ** incr-vacuum flags. This is required in case this connection 628 ** creates the database file. It is important that it is created 629 ** as an auto-vacuum capable db. 630 */ 631 int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); 632 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ 633 /* When setting the auto_vacuum mode to either "full" or 634 ** "incremental", write the value of meta[6] in the database 635 ** file. Before writing to meta[6], check that meta[3] indicates 636 ** that this really is an auto-vacuum capable database. 637 */ 638 static const VdbeOpList setMeta6[] = { 639 { OP_Transaction, 0, 1, 0}, /* 0 */ 640 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE}, 641 { OP_If, 1, 0, 0}, /* 2 */ 642 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ 643 { OP_Integer, 0, 1, 0}, /* 4 */ 644 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */ 645 }; 646 int iAddr; 647 iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6); 648 sqlite3VdbeChangeP1(v, iAddr, iDb); 649 sqlite3VdbeChangeP1(v, iAddr+1, iDb); 650 sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4); 651 sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1); 652 sqlite3VdbeChangeP1(v, iAddr+5, iDb); 653 sqlite3VdbeUsesBtree(v, iDb); 654 } 655 } 656 } 657 }else 658 #endif 659 660 /* 661 ** PRAGMA [database.]incremental_vacuum(N) 662 ** 663 ** Do N steps of incremental vacuuming on a database. 664 */ 665 #ifndef SQLITE_OMIT_AUTOVACUUM 666 if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){ 667 int iLimit, addr; 668 if( sqlite3ReadSchema(pParse) ){ 669 goto pragma_out; 670 } 671 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ 672 iLimit = 0x7fffffff; 673 } 674 sqlite3BeginWriteOperation(pParse, 0, iDb); 675 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); 676 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); 677 sqlite3VdbeAddOp1(v, OP_ResultRow, 1); 678 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); 679 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); 680 sqlite3VdbeJumpHere(v, addr); 681 }else 682 #endif 683 684 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 685 /* 686 ** PRAGMA [database.]cache_size 687 ** PRAGMA [database.]cache_size=N 688 ** 689 ** The first form reports the current local setting for the 690 ** page cache size. The local setting can be different from 691 ** the persistent cache size value that is stored in the database 692 ** file itself. The value returned is the maximum number of 693 ** pages in the page cache. The second form sets the local 694 ** page cache size value. It does not change the persistent 695 ** cache size stored on the disk so the cache size will revert 696 ** to its default value when the database is closed and reopened. 697 ** N should be a positive integer. 698 */ 699 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){ 700 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 701 if( !zRight ){ 702 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size); 703 }else{ 704 int size = atoi(zRight); 705 if( size<0 ) size = -size; 706 pDb->pSchema->cache_size = size; 707 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); 708 } 709 }else 710 711 /* 712 ** PRAGMA temp_store 713 ** PRAGMA temp_store = "default"|"memory"|"file" 714 ** 715 ** Return or set the local value of the temp_store flag. Changing 716 ** the local value does not make changes to the disk file and the default 717 ** value will be restored the next time the database is opened. 718 ** 719 ** Note that it is possible for the library compile-time options to 720 ** override this setting 721 */ 722 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){ 723 if( !zRight ){ 724 returnSingleInt(pParse, "temp_store", db->temp_store); 725 }else{ 726 changeTempStorage(pParse, zRight); 727 } 728 }else 729 730 /* 731 ** PRAGMA temp_store_directory 732 ** PRAGMA temp_store_directory = ""|"directory_name" 733 ** 734 ** Return or set the local value of the temp_store_directory flag. Changing 735 ** the value sets a specific directory to be used for temporary files. 736 ** Setting to a null string reverts to the default temporary directory search. 737 ** If temporary directory is changed, then invalidateTempStorage. 738 ** 739 */ 740 if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){ 741 if( !zRight ){ 742 if( sqlite3_temp_directory ){ 743 sqlite3VdbeSetNumCols(v, 1); 744 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 745 "temp_store_directory", SQLITE_STATIC); 746 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0); 747 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 748 } 749 }else{ 750 #ifndef SQLITE_OMIT_WSD 751 if( zRight[0] ){ 752 int rc; 753 int res; 754 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); 755 if( rc!=SQLITE_OK || res==0 ){ 756 sqlite3ErrorMsg(pParse, "not a writable directory"); 757 goto pragma_out; 758 } 759 } 760 if( SQLITE_TEMP_STORE==0 761 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) 762 || (SQLITE_TEMP_STORE==2 && db->temp_store==1) 763 ){ 764 invalidateTempStorage(pParse); 765 } 766 sqlite3_free(sqlite3_temp_directory); 767 if( zRight[0] ){ 768 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); 769 }else{ 770 sqlite3_temp_directory = 0; 771 } 772 #endif /* SQLITE_OMIT_WSD */ 773 } 774 }else 775 776 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) 777 # if defined(__APPLE__) 778 # define SQLITE_ENABLE_LOCKING_STYLE 1 779 # else 780 # define SQLITE_ENABLE_LOCKING_STYLE 0 781 # endif 782 #endif 783 #if SQLITE_ENABLE_LOCKING_STYLE 784 /* 785 ** PRAGMA [database.]lock_proxy_file 786 ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path" 787 ** 788 ** Return or set the value of the lock_proxy_file flag. Changing 789 ** the value sets a specific file to be used for database access locks. 790 ** 791 */ 792 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){ 793 if( !zRight ){ 794 Pager *pPager = sqlite3BtreePager(pDb->pBt); 795 char *proxy_file_path = NULL; 796 sqlite3_file *pFile = sqlite3PagerFile(pPager); 797 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE, 798 &proxy_file_path); 799 800 if( proxy_file_path ){ 801 sqlite3VdbeSetNumCols(v, 1); 802 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 803 "lock_proxy_file", SQLITE_STATIC); 804 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0); 805 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 806 } 807 }else{ 808 Pager *pPager = sqlite3BtreePager(pDb->pBt); 809 sqlite3_file *pFile = sqlite3PagerFile(pPager); 810 int res; 811 if( zRight[0] ){ 812 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 813 zRight); 814 } else { 815 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 816 NULL); 817 } 818 if( res!=SQLITE_OK ){ 819 sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); 820 goto pragma_out; 821 } 822 } 823 }else 824 #endif /* SQLITE_ENABLE_LOCKING_STYLE */ 825 826 /* 827 ** PRAGMA [database.]synchronous 828 ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL 829 ** 830 ** Return or set the local value of the synchronous flag. Changing 831 ** the local value does not make changes to the disk file and the 832 ** default value will be restored the next time the database is 833 ** opened. 834 */ 835 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){ 836 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 837 if( !zRight ){ 838 returnSingleInt(pParse, "synchronous", pDb->safety_level-1); 839 }else{ 840 if( !db->autoCommit ){ 841 sqlite3ErrorMsg(pParse, 842 "Safety level may not be changed inside a transaction"); 843 }else{ 844 pDb->safety_level = getSafetyLevel(zRight)+1; 845 } 846 } 847 }else 848 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ 849 850 #ifndef SQLITE_OMIT_FLAG_PRAGMAS 851 if( flagPragma(pParse, zLeft, zRight) ){ 852 /* The flagPragma() subroutine also generates any necessary code 853 ** there is nothing more to do here */ 854 }else 855 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ 856 857 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS 858 /* 859 ** PRAGMA table_info(<table>) 860 ** 861 ** Return a single row for each column of the named table. The columns of 862 ** the returned data set are: 863 ** 864 ** cid: Column id (numbered from left to right, starting at 0) 865 ** name: Column name 866 ** type: Column declaration type. 867 ** notnull: True if 'NOT NULL' is part of column declaration 868 ** dflt_value: The default value for the column, if any. 869 */ 870 if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){ 871 Table *pTab; 872 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 873 pTab = sqlite3FindTable(db, zRight, zDb); 874 if( pTab ){ 875 int i; 876 int nHidden = 0; 877 Column *pCol; 878 sqlite3VdbeSetNumCols(v, 6); 879 pParse->nMem = 6; 880 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC); 881 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); 882 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC); 883 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC); 884 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC); 885 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC); 886 sqlite3ViewGetColumnNames(pParse, pTab); 887 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ 888 if( IsHiddenColumn(pCol) ){ 889 nHidden++; 890 continue; 891 } 892 sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1); 893 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0); 894 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, 895 pCol->zType ? pCol->zType : "", 0); 896 sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4); 897 if( pCol->zDflt ){ 898 sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0); 899 }else{ 900 sqlite3VdbeAddOp2(v, OP_Null, 0, 5); 901 } 902 sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6); 903 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); 904 } 905 } 906 }else 907 908 if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){ 909 Index *pIdx; 910 Table *pTab; 911 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 912 pIdx = sqlite3FindIndex(db, zRight, zDb); 913 if( pIdx ){ 914 int i; 915 pTab = pIdx->pTable; 916 sqlite3VdbeSetNumCols(v, 3); 917 pParse->nMem = 3; 918 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC); 919 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC); 920 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC); 921 for(i=0; i<pIdx->nColumn; i++){ 922 int cnum = pIdx->aiColumn[i]; 923 sqlite3VdbeAddOp2(v, OP_Integer, i, 1); 924 sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2); 925 assert( pTab->nCol>cnum ); 926 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0); 927 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); 928 } 929 } 930 }else 931 932 if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){ 933 Index *pIdx; 934 Table *pTab; 935 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 936 pTab = sqlite3FindTable(db, zRight, zDb); 937 if( pTab ){ 938 v = sqlite3GetVdbe(pParse); 939 pIdx = pTab->pIndex; 940 if( pIdx ){ 941 int i = 0; 942 sqlite3VdbeSetNumCols(v, 3); 943 pParse->nMem = 3; 944 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); 945 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); 946 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC); 947 while(pIdx){ 948 sqlite3VdbeAddOp2(v, OP_Integer, i, 1); 949 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0); 950 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3); 951 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); 952 ++i; 953 pIdx = pIdx->pNext; 954 } 955 } 956 } 957 }else 958 959 if( sqlite3StrICmp(zLeft, "database_list")==0 ){ 960 int i; 961 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 962 sqlite3VdbeSetNumCols(v, 3); 963 pParse->nMem = 3; 964 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); 965 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); 966 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC); 967 for(i=0; i<db->nDb; i++){ 968 if( db->aDb[i].pBt==0 ) continue; 969 assert( db->aDb[i].zName!=0 ); 970 sqlite3VdbeAddOp2(v, OP_Integer, i, 1); 971 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0); 972 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, 973 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0); 974 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); 975 } 976 }else 977 978 if( sqlite3StrICmp(zLeft, "collation_list")==0 ){ 979 int i = 0; 980 HashElem *p; 981 sqlite3VdbeSetNumCols(v, 2); 982 pParse->nMem = 2; 983 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); 984 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); 985 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ 986 CollSeq *pColl = (CollSeq *)sqliteHashData(p); 987 sqlite3VdbeAddOp2(v, OP_Integer, i++, 1); 988 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0); 989 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); 990 } 991 }else 992 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ 993 994 #ifndef SQLITE_OMIT_FOREIGN_KEY 995 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){ 996 FKey *pFK; 997 Table *pTab; 998 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 999 pTab = sqlite3FindTable(db, zRight, zDb); 1000 if( pTab ){ 1001 v = sqlite3GetVdbe(pParse); 1002 pFK = pTab->pFKey; 1003 if( pFK ){ 1004 int i = 0; 1005 sqlite3VdbeSetNumCols(v, 8); 1006 pParse->nMem = 8; 1007 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC); 1008 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC); 1009 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC); 1010 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC); 1011 sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC); 1012 sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC); 1013 sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC); 1014 sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC); 1015 while(pFK){ 1016 int j; 1017 for(j=0; j<pFK->nCol; j++){ 1018 char *zCol = pFK->aCol[j].zCol; 1019 char *zOnDelete = (char *)actionName(pFK->aAction[0]); 1020 char *zOnUpdate = (char *)actionName(pFK->aAction[1]); 1021 sqlite3VdbeAddOp2(v, OP_Integer, i, 1); 1022 sqlite3VdbeAddOp2(v, OP_Integer, j, 2); 1023 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0); 1024 sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, 1025 pTab->aCol[pFK->aCol[j].iFrom].zName, 0); 1026 sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0); 1027 sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0); 1028 sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0); 1029 sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0); 1030 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8); 1031 } 1032 ++i; 1033 pFK = pFK->pNextFrom; 1034 } 1035 } 1036 } 1037 }else 1038 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ 1039 1040 #ifndef NDEBUG 1041 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){ 1042 if( zRight ){ 1043 if( getBoolean(zRight) ){ 1044 sqlite3ParserTrace(stderr, "parser: "); 1045 }else{ 1046 sqlite3ParserTrace(0, 0); 1047 } 1048 } 1049 }else 1050 #endif 1051 1052 /* Reinstall the LIKE and GLOB functions. The variant of LIKE 1053 ** used will be case sensitive or not depending on the RHS. 1054 */ 1055 if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){ 1056 if( zRight ){ 1057 sqlite3RegisterLikeFunctions(db, getBoolean(zRight)); 1058 } 1059 }else 1060 1061 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX 1062 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 1063 #endif 1064 1065 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 1066 /* Pragma "quick_check" is an experimental reduced version of 1067 ** integrity_check designed to detect most database corruption 1068 ** without most of the overhead of a full integrity-check. 1069 */ 1070 if( sqlite3StrICmp(zLeft, "integrity_check")==0 1071 || sqlite3StrICmp(zLeft, "quick_check")==0 1072 ){ 1073 int i, j, addr, mxErr; 1074 1075 /* Code that appears at the end of the integrity check. If no error 1076 ** messages have been generated, output OK. Otherwise output the 1077 ** error message 1078 */ 1079 static const VdbeOpList endCode[] = { 1080 { OP_AddImm, 1, 0, 0}, /* 0 */ 1081 { OP_IfNeg, 1, 0, 0}, /* 1 */ 1082 { OP_String8, 0, 3, 0}, /* 2 */ 1083 { OP_ResultRow, 3, 1, 0}, 1084 }; 1085 1086 int isQuick = (zLeft[0]=='q'); 1087 1088 /* Initialize the VDBE program */ 1089 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 1090 pParse->nMem = 6; 1091 sqlite3VdbeSetNumCols(v, 1); 1092 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC); 1093 1094 /* Set the maximum error count */ 1095 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; 1096 if( zRight ){ 1097 mxErr = atoi(zRight); 1098 if( mxErr<=0 ){ 1099 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; 1100 } 1101 } 1102 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ 1103 1104 /* Do an integrity check on each database file */ 1105 for(i=0; i<db->nDb; i++){ 1106 HashElem *x; 1107 Hash *pTbls; 1108 int cnt = 0; 1109 1110 if( OMIT_TEMPDB && i==1 ) continue; 1111 1112 sqlite3CodeVerifySchema(pParse, i); 1113 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ 1114 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); 1115 sqlite3VdbeJumpHere(v, addr); 1116 1117 /* Do an integrity check of the B-Tree 1118 ** 1119 ** Begin by filling registers 2, 3, ... with the root pages numbers 1120 ** for all tables and indices in the database. 1121 */ 1122 pTbls = &db->aDb[i].pSchema->tblHash; 1123 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ 1124 Table *pTab = sqliteHashData(x); 1125 Index *pIdx; 1126 sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt); 1127 cnt++; 1128 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 1129 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt); 1130 cnt++; 1131 } 1132 } 1133 1134 /* Make sure sufficient number of registers have been allocated */ 1135 if( pParse->nMem < cnt+4 ){ 1136 pParse->nMem = cnt+4; 1137 } 1138 1139 /* Do the b-tree integrity checks */ 1140 sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1); 1141 sqlite3VdbeChangeP5(v, (u8)i); 1142 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); 1143 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, 1144 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), 1145 P4_DYNAMIC); 1146 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1); 1147 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); 1148 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); 1149 sqlite3VdbeJumpHere(v, addr); 1150 1151 /* Make sure all the indices are constructed correctly. 1152 */ 1153 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ 1154 Table *pTab = sqliteHashData(x); 1155 Index *pIdx; 1156 int loopTop; 1157 1158 if( pTab->pIndex==0 ) continue; 1159 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ 1160 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); 1161 sqlite3VdbeJumpHere(v, addr); 1162 sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead); 1163 sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */ 1164 loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0); 1165 sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */ 1166 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 1167 int jmp2; 1168 int r1; 1169 static const VdbeOpList idxErr[] = { 1170 { OP_AddImm, 1, -1, 0}, 1171 { OP_String8, 0, 3, 0}, /* 1 */ 1172 { OP_Rowid, 1, 4, 0}, 1173 { OP_String8, 0, 5, 0}, /* 3 */ 1174 { OP_String8, 0, 6, 0}, /* 4 */ 1175 { OP_Concat, 4, 3, 3}, 1176 { OP_Concat, 5, 3, 3}, 1177 { OP_Concat, 6, 3, 3}, 1178 { OP_ResultRow, 3, 1, 0}, 1179 { OP_IfPos, 1, 0, 0}, /* 9 */ 1180 { OP_Halt, 0, 0, 0}, 1181 }; 1182 r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0); 1183 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1); 1184 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr); 1185 sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC); 1186 sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC); 1187 sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC); 1188 sqlite3VdbeJumpHere(v, addr+9); 1189 sqlite3VdbeJumpHere(v, jmp2); 1190 } 1191 sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1); 1192 sqlite3VdbeJumpHere(v, loopTop); 1193 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 1194 static const VdbeOpList cntIdx[] = { 1195 { OP_Integer, 0, 3, 0}, 1196 { OP_Rewind, 0, 0, 0}, /* 1 */ 1197 { OP_AddImm, 3, 1, 0}, 1198 { OP_Next, 0, 0, 0}, /* 3 */ 1199 { OP_Eq, 2, 0, 3}, /* 4 */ 1200 { OP_AddImm, 1, -1, 0}, 1201 { OP_String8, 0, 2, 0}, /* 6 */ 1202 { OP_String8, 0, 3, 0}, /* 7 */ 1203 { OP_Concat, 3, 2, 2}, 1204 { OP_ResultRow, 2, 1, 0}, 1205 }; 1206 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); 1207 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); 1208 sqlite3VdbeJumpHere(v, addr); 1209 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx); 1210 sqlite3VdbeChangeP1(v, addr+1, j+2); 1211 sqlite3VdbeChangeP2(v, addr+1, addr+4); 1212 sqlite3VdbeChangeP1(v, addr+3, j+2); 1213 sqlite3VdbeChangeP2(v, addr+3, addr+2); 1214 sqlite3VdbeJumpHere(v, addr+4); 1215 sqlite3VdbeChangeP4(v, addr+6, 1216 "wrong # of entries in index ", P4_STATIC); 1217 sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC); 1218 } 1219 } 1220 } 1221 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode); 1222 sqlite3VdbeChangeP2(v, addr, -mxErr); 1223 sqlite3VdbeJumpHere(v, addr+1); 1224 sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC); 1225 }else 1226 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 1227 1228 #ifndef SQLITE_OMIT_UTF16 1229 /* 1230 ** PRAGMA encoding 1231 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" 1232 ** 1233 ** In its first form, this pragma returns the encoding of the main 1234 ** database. If the database is not initialized, it is initialized now. 1235 ** 1236 ** The second form of this pragma is a no-op if the main database file 1237 ** has not already been initialized. In this case it sets the default 1238 ** encoding that will be used for the main database file if a new file 1239 ** is created. If an existing main database file is opened, then the 1240 ** default text encoding for the existing database is used. 1241 ** 1242 ** In all cases new databases created using the ATTACH command are 1243 ** created to use the same default text encoding as the main database. If 1244 ** the main database has not been initialized and/or created when ATTACH 1245 ** is executed, this is done before the ATTACH operation. 1246 ** 1247 ** In the second form this pragma sets the text encoding to be used in 1248 ** new database files created using this database handle. It is only 1249 ** useful if invoked immediately after the main database i 1250 */ 1251 if( sqlite3StrICmp(zLeft, "encoding")==0 ){ 1252 static const struct EncName { 1253 char *zName; 1254 u8 enc; 1255 } encnames[] = { 1256 { "UTF8", SQLITE_UTF8 }, 1257 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ 1258 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ 1259 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ 1260 { "UTF16le", SQLITE_UTF16LE }, 1261 { "UTF16be", SQLITE_UTF16BE }, 1262 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ 1263 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ 1264 { 0, 0 } 1265 }; 1266 const struct EncName *pEnc; 1267 if( !zRight ){ /* "PRAGMA encoding" */ 1268 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 1269 sqlite3VdbeSetNumCols(v, 1); 1270 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC); 1271 sqlite3VdbeAddOp2(v, OP_String8, 0, 1); 1272 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); 1273 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); 1274 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); 1275 sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC); 1276 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 1277 }else{ /* "PRAGMA encoding = XXX" */ 1278 /* Only change the value of sqlite.enc if the database handle is not 1279 ** initialized. If the main database exists, the new sqlite.enc value 1280 ** will be overwritten when the schema is next loaded. If it does not 1281 ** already exists, it will be created to use the new encoding value. 1282 */ 1283 if( 1284 !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 1285 DbHasProperty(db, 0, DB_Empty) 1286 ){ 1287 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ 1288 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ 1289 ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; 1290 break; 1291 } 1292 } 1293 if( !pEnc->zName ){ 1294 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); 1295 } 1296 } 1297 } 1298 }else 1299 #endif /* SQLITE_OMIT_UTF16 */ 1300 1301 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS 1302 /* 1303 ** PRAGMA [database.]schema_version 1304 ** PRAGMA [database.]schema_version = <integer> 1305 ** 1306 ** PRAGMA [database.]user_version 1307 ** PRAGMA [database.]user_version = <integer> 1308 ** 1309 ** The pragma's schema_version and user_version are used to set or get 1310 ** the value of the schema-version and user-version, respectively. Both 1311 ** the schema-version and the user-version are 32-bit signed integers 1312 ** stored in the database header. 1313 ** 1314 ** The schema-cookie is usually only manipulated internally by SQLite. It 1315 ** is incremented by SQLite whenever the database schema is modified (by 1316 ** creating or dropping a table or index). The schema version is used by 1317 ** SQLite each time a query is executed to ensure that the internal cache 1318 ** of the schema used when compiling the SQL query matches the schema of 1319 ** the database against which the compiled query is actually executed. 1320 ** Subverting this mechanism by using "PRAGMA schema_version" to modify 1321 ** the schema-version is potentially dangerous and may lead to program 1322 ** crashes or database corruption. Use with caution! 1323 ** 1324 ** The user-version is not used internally by SQLite. It may be used by 1325 ** applications for any purpose. 1326 */ 1327 if( sqlite3StrICmp(zLeft, "schema_version")==0 1328 || sqlite3StrICmp(zLeft, "user_version")==0 1329 || sqlite3StrICmp(zLeft, "freelist_count")==0 1330 ){ 1331 int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */ 1332 sqlite3VdbeUsesBtree(v, iDb); 1333 switch( zLeft[0] ){ 1334 case 'f': case 'F': 1335 iCookie = BTREE_FREE_PAGE_COUNT; 1336 break; 1337 case 's': case 'S': 1338 iCookie = BTREE_SCHEMA_VERSION; 1339 break; 1340 default: 1341 iCookie = BTREE_USER_VERSION; 1342 break; 1343 } 1344 1345 if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){ 1346 /* Write the specified cookie value */ 1347 static const VdbeOpList setCookie[] = { 1348 { OP_Transaction, 0, 1, 0}, /* 0 */ 1349 { OP_Integer, 0, 1, 0}, /* 1 */ 1350 { OP_SetCookie, 0, 0, 1}, /* 2 */ 1351 }; 1352 int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie); 1353 sqlite3VdbeChangeP1(v, addr, iDb); 1354 sqlite3VdbeChangeP1(v, addr+1, atoi(zRight)); 1355 sqlite3VdbeChangeP1(v, addr+2, iDb); 1356 sqlite3VdbeChangeP2(v, addr+2, iCookie); 1357 }else{ 1358 /* Read the specified cookie value */ 1359 static const VdbeOpList readCookie[] = { 1360 { OP_Transaction, 0, 0, 0}, /* 0 */ 1361 { OP_ReadCookie, 0, 1, 0}, /* 1 */ 1362 { OP_ResultRow, 1, 1, 0} 1363 }; 1364 int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie); 1365 sqlite3VdbeChangeP1(v, addr, iDb); 1366 sqlite3VdbeChangeP1(v, addr+1, iDb); 1367 sqlite3VdbeChangeP3(v, addr+1, iCookie); 1368 sqlite3VdbeSetNumCols(v, 1); 1369 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); 1370 } 1371 }else 1372 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ 1373 1374 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 1375 /* 1376 ** PRAGMA compile_options 1377 ** 1378 ** Return the names of all compile-time options used in this build, 1379 ** one option per row. 1380 */ 1381 if( sqlite3StrICmp(zLeft, "compile_options")==0 ){ 1382 int i = 0; 1383 const char *zOpt; 1384 sqlite3VdbeSetNumCols(v, 1); 1385 pParse->nMem = 1; 1386 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC); 1387 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ 1388 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0); 1389 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); 1390 } 1391 }else 1392 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 1393 1394 #ifndef SQLITE_OMIT_WAL 1395 /* 1396 ** PRAGMA [database.]wal_checkpoint 1397 ** 1398 ** Checkpoint the database. 1399 */ 1400 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){ 1401 if( sqlite3ReadSchema(pParse) ) goto pragma_out; 1402 sqlite3VdbeAddOp3(v, OP_Checkpoint, pId2->z?iDb:SQLITE_MAX_ATTACHED, 0, 0); 1403 }else 1404 1405 /* 1406 ** PRAGMA wal_autocheckpoint 1407 ** PRAGMA wal_autocheckpoint = N 1408 ** 1409 ** Configure a database connection to automatically checkpoint a database 1410 ** after accumulating N frames in the log. Or query for the current value 1411 ** of N. 1412 */ 1413 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){ 1414 if( zRight ){ 1415 int nAuto = atoi(zRight); 1416 sqlite3_wal_autocheckpoint(db, nAuto); 1417 } 1418 returnSingleInt(pParse, "wal_autocheckpoint", 1419 db->xWalCallback==sqlite3WalDefaultHook ? 1420 SQLITE_PTR_TO_INT(db->pWalArg) : 0); 1421 }else 1422 #endif 1423 1424 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 1425 /* 1426 ** Report the current state of file logs for all databases 1427 */ 1428 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){ 1429 static const char *const azLockName[] = { 1430 "unlocked", "shared", "reserved", "pending", "exclusive" 1431 }; 1432 int i; 1433 sqlite3VdbeSetNumCols(v, 2); 1434 pParse->nMem = 2; 1435 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC); 1436 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC); 1437 for(i=0; i<db->nDb; i++){ 1438 Btree *pBt; 1439 Pager *pPager; 1440 const char *zState = "unknown"; 1441 int j; 1442 if( db->aDb[i].zName==0 ) continue; 1443 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC); 1444 pBt = db->aDb[i].pBt; 1445 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){ 1446 zState = "closed"; 1447 }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 1448 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ 1449 zState = azLockName[j]; 1450 } 1451 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC); 1452 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); 1453 } 1454 1455 }else 1456 #endif 1457 1458 #ifdef SQLITE_HAS_CODEC 1459 if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){ 1460 sqlite3_key(db, zRight, sqlite3Strlen30(zRight)); 1461 }else 1462 if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){ 1463 sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight)); 1464 }else 1465 if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 || 1466 sqlite3StrICmp(zLeft, "hexrekey")==0) ){ 1467 int i, h1, h2; 1468 char zKey[40]; 1469 for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){ 1470 h1 += 9*(1&(h1>>6)); 1471 h2 += 9*(1&(h2>>6)); 1472 zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4); 1473 } 1474 if( (zLeft[3] & 0xf)==0xb ){ 1475 sqlite3_key(db, zKey, i/2); 1476 }else{ 1477 sqlite3_rekey(db, zKey, i/2); 1478 } 1479 }else 1480 #endif 1481 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) 1482 if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){ 1483 #ifdef SQLITE_HAS_CODEC 1484 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){ 1485 sqlite3_activate_see(&zRight[4]); 1486 } 1487 #endif 1488 #ifdef SQLITE_ENABLE_CEROD 1489 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ 1490 sqlite3_activate_cerod(&zRight[6]); 1491 } 1492 #endif 1493 }else 1494 #endif 1495 1496 1497 {/* Empty ELSE clause */} 1498 1499 /* 1500 ** Reset the safety level, in case the fullfsync flag or synchronous 1501 ** setting changed. 1502 */ 1503 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 1504 if( db->autoCommit ){ 1505 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level, 1506 (db->flags&SQLITE_FullFSync)!=0); 1507 } 1508 #endif 1509 pragma_out: 1510 sqlite3DbFree(db, zLeft); 1511 sqlite3DbFree(db, zRight); 1512 } 1513 1514 #endif /* SQLITE_OMIT_PRAGMA */ 1515