OLD | NEW |
(Empty) | |
| 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 result column names for a pragma. |
| 167 */ |
| 168 static void setPragmaResultColumnNames( |
| 169 Vdbe *v, /* The query under construction */ |
| 170 const PragmaName *pPragma /* The pragma */ |
| 171 ){ |
| 172 u8 n = pPragma->nPragCName; |
| 173 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n); |
| 174 if( n==0 ){ |
| 175 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC); |
| 176 }else{ |
| 177 int i, j; |
| 178 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){ |
| 179 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC); |
| 180 } |
| 181 } |
| 182 } |
| 183 |
| 184 /* |
| 185 ** Generate code to return a single integer value. |
| 186 */ |
| 187 static void returnSingleInt(Vdbe *v, i64 value){ |
| 188 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64); |
| 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 *zValue /* Value to be returned */ |
| 198 ){ |
| 199 if( zValue ){ |
| 200 sqlite3VdbeLoadString(v, 1, (const char*)zValue); |
| 201 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 202 } |
| 203 } |
| 204 |
| 205 |
| 206 /* |
| 207 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0 |
| 208 ** set these values for all pagers. |
| 209 */ |
| 210 #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 211 static void setAllPagerFlags(sqlite3 *db){ |
| 212 if( db->autoCommit ){ |
| 213 Db *pDb = db->aDb; |
| 214 int n = db->nDb; |
| 215 assert( SQLITE_FullFSync==PAGER_FULLFSYNC ); |
| 216 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC ); |
| 217 assert( SQLITE_CacheSpill==PAGER_CACHESPILL ); |
| 218 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL) |
| 219 == PAGER_FLAGS_MASK ); |
| 220 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level ); |
| 221 while( (n--) > 0 ){ |
| 222 if( pDb->pBt ){ |
| 223 sqlite3BtreeSetPagerFlags(pDb->pBt, |
| 224 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) ); |
| 225 } |
| 226 pDb++; |
| 227 } |
| 228 } |
| 229 } |
| 230 #else |
| 231 # define setAllPagerFlags(X) /* no-op */ |
| 232 #endif |
| 233 |
| 234 |
| 235 /* |
| 236 ** Return a human-readable name for a constraint resolution action. |
| 237 */ |
| 238 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 239 static const char *actionName(u8 action){ |
| 240 const char *zName; |
| 241 switch( action ){ |
| 242 case OE_SetNull: zName = "SET NULL"; break; |
| 243 case OE_SetDflt: zName = "SET DEFAULT"; break; |
| 244 case OE_Cascade: zName = "CASCADE"; break; |
| 245 case OE_Restrict: zName = "RESTRICT"; break; |
| 246 default: zName = "NO ACTION"; |
| 247 assert( action==OE_None ); break; |
| 248 } |
| 249 return zName; |
| 250 } |
| 251 #endif |
| 252 |
| 253 |
| 254 /* |
| 255 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants |
| 256 ** defined in pager.h. This function returns the associated lowercase |
| 257 ** journal-mode name. |
| 258 */ |
| 259 const char *sqlite3JournalModename(int eMode){ |
| 260 static char * const azModeName[] = { |
| 261 "delete", "persist", "off", "truncate", "memory" |
| 262 #ifndef SQLITE_OMIT_WAL |
| 263 , "wal" |
| 264 #endif |
| 265 }; |
| 266 assert( PAGER_JOURNALMODE_DELETE==0 ); |
| 267 assert( PAGER_JOURNALMODE_PERSIST==1 ); |
| 268 assert( PAGER_JOURNALMODE_OFF==2 ); |
| 269 assert( PAGER_JOURNALMODE_TRUNCATE==3 ); |
| 270 assert( PAGER_JOURNALMODE_MEMORY==4 ); |
| 271 assert( PAGER_JOURNALMODE_WAL==5 ); |
| 272 assert( eMode>=0 && eMode<=ArraySize(azModeName) ); |
| 273 |
| 274 if( eMode==ArraySize(azModeName) ) return 0; |
| 275 return azModeName[eMode]; |
| 276 } |
| 277 |
| 278 /* |
| 279 ** Locate a pragma in the aPragmaName[] array. |
| 280 */ |
| 281 static const PragmaName *pragmaLocate(const char *zName){ |
| 282 int upr, lwr, mid = 0, rc; |
| 283 lwr = 0; |
| 284 upr = ArraySize(aPragmaName)-1; |
| 285 while( lwr<=upr ){ |
| 286 mid = (lwr+upr)/2; |
| 287 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName); |
| 288 if( rc==0 ) break; |
| 289 if( rc<0 ){ |
| 290 upr = mid - 1; |
| 291 }else{ |
| 292 lwr = mid + 1; |
| 293 } |
| 294 } |
| 295 return lwr>upr ? 0 : &aPragmaName[mid]; |
| 296 } |
| 297 |
| 298 /* |
| 299 ** Process a pragma statement. |
| 300 ** |
| 301 ** Pragmas are of this form: |
| 302 ** |
| 303 ** PRAGMA [schema.]id [= value] |
| 304 ** |
| 305 ** The identifier might also be a string. The value is a string, and |
| 306 ** identifier, or a number. If minusFlag is true, then the value is |
| 307 ** a number that was preceded by a minus sign. |
| 308 ** |
| 309 ** If the left side is "database.id" then pId1 is the database name |
| 310 ** and pId2 is the id. If the left side is just "id" then pId1 is the |
| 311 ** id and pId2 is any empty string. |
| 312 */ |
| 313 void sqlite3Pragma( |
| 314 Parse *pParse, |
| 315 Token *pId1, /* First part of [schema.]id field */ |
| 316 Token *pId2, /* Second part of [schema.]id field, or NULL */ |
| 317 Token *pValue, /* Token for <value>, or NULL */ |
| 318 int minusFlag /* True if a '-' sign preceded <value> */ |
| 319 ){ |
| 320 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */ |
| 321 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */ |
| 322 const char *zDb = 0; /* The database name */ |
| 323 Token *pId; /* Pointer to <id> token */ |
| 324 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */ |
| 325 int iDb; /* Database index for <database> */ |
| 326 int rc; /* return value form SQLITE_FCNTL_PRAGMA */ |
| 327 sqlite3 *db = pParse->db; /* The database connection */ |
| 328 Db *pDb; /* The specific database being pragmaed */ |
| 329 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */ |
| 330 const PragmaName *pPragma; /* The pragma */ |
| 331 |
| 332 if( v==0 ) return; |
| 333 sqlite3VdbeRunOnlyOnce(v); |
| 334 pParse->nMem = 2; |
| 335 |
| 336 /* Interpret the [schema.] part of the pragma statement. iDb is the |
| 337 ** index of the database this pragma is being applied to in db.aDb[]. */ |
| 338 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); |
| 339 if( iDb<0 ) return; |
| 340 pDb = &db->aDb[iDb]; |
| 341 |
| 342 /* If the temp database has been explicitly named as part of the |
| 343 ** pragma, make sure it is open. |
| 344 */ |
| 345 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ |
| 346 return; |
| 347 } |
| 348 |
| 349 zLeft = sqlite3NameFromToken(db, pId); |
| 350 if( !zLeft ) return; |
| 351 if( minusFlag ){ |
| 352 zRight = sqlite3MPrintf(db, "-%T", pValue); |
| 353 }else{ |
| 354 zRight = sqlite3NameFromToken(db, pValue); |
| 355 } |
| 356 |
| 357 assert( pId2 ); |
| 358 zDb = pId2->n>0 ? pDb->zDbSName : 0; |
| 359 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ |
| 360 goto pragma_out; |
| 361 } |
| 362 |
| 363 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS |
| 364 ** connection. If it returns SQLITE_OK, then assume that the VFS |
| 365 ** handled the pragma and generate a no-op prepared statement. |
| 366 ** |
| 367 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, |
| 368 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file |
| 369 ** object corresponding to the database file to which the pragma |
| 370 ** statement refers. |
| 371 ** |
| 372 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA |
| 373 ** file control is an array of pointers to strings (char**) in which the |
| 374 ** second element of the array is the name of the pragma and the third |
| 375 ** element is the argument to the pragma or NULL if the pragma has no |
| 376 ** argument. |
| 377 */ |
| 378 aFcntl[0] = 0; |
| 379 aFcntl[1] = zLeft; |
| 380 aFcntl[2] = zRight; |
| 381 aFcntl[3] = 0; |
| 382 db->busyHandler.nBusy = 0; |
| 383 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl); |
| 384 if( rc==SQLITE_OK ){ |
| 385 sqlite3VdbeSetNumCols(v, 1); |
| 386 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT); |
| 387 returnSingleText(v, aFcntl[0]); |
| 388 sqlite3_free(aFcntl[0]); |
| 389 goto pragma_out; |
| 390 } |
| 391 if( rc!=SQLITE_NOTFOUND ){ |
| 392 if( aFcntl[0] ){ |
| 393 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]); |
| 394 sqlite3_free(aFcntl[0]); |
| 395 } |
| 396 pParse->nErr++; |
| 397 pParse->rc = rc; |
| 398 goto pragma_out; |
| 399 } |
| 400 |
| 401 /* Locate the pragma in the lookup table */ |
| 402 pPragma = pragmaLocate(zLeft); |
| 403 if( pPragma==0 ) goto pragma_out; |
| 404 |
| 405 /* Make sure the database schema is loaded if the pragma requires that */ |
| 406 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){ |
| 407 if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 408 } |
| 409 |
| 410 /* Register the result column names for pragmas that return results */ |
| 411 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 |
| 412 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0) |
| 413 ){ |
| 414 setPragmaResultColumnNames(v, pPragma); |
| 415 } |
| 416 |
| 417 /* Jump to the appropriate pragma handler */ |
| 418 switch( pPragma->ePragTyp ){ |
| 419 |
| 420 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) |
| 421 /* |
| 422 ** PRAGMA [schema.]default_cache_size |
| 423 ** PRAGMA [schema.]default_cache_size=N |
| 424 ** |
| 425 ** The first form reports the current persistent setting for the |
| 426 ** page cache size. The value returned is the maximum number of |
| 427 ** pages in the page cache. The second form sets both the current |
| 428 ** page cache size value and the persistent page cache size value |
| 429 ** stored in the database file. |
| 430 ** |
| 431 ** Older versions of SQLite would set the default cache size to a |
| 432 ** negative number to indicate synchronous=OFF. These days, synchronous |
| 433 ** is always on by default regardless of the sign of the default cache |
| 434 ** size. But continue to take the absolute value of the default cache |
| 435 ** size of historical compatibility. |
| 436 */ |
| 437 case PragTyp_DEFAULT_CACHE_SIZE: { |
| 438 static const int iLn = VDBE_OFFSET_LINENO(2); |
| 439 static const VdbeOpList getCacheSize[] = { |
| 440 { OP_Transaction, 0, 0, 0}, /* 0 */ |
| 441 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */ |
| 442 { OP_IfPos, 1, 8, 0}, |
| 443 { OP_Integer, 0, 2, 0}, |
| 444 { OP_Subtract, 1, 2, 1}, |
| 445 { OP_IfPos, 1, 8, 0}, |
| 446 { OP_Integer, 0, 1, 0}, /* 6 */ |
| 447 { OP_Noop, 0, 0, 0}, |
| 448 { OP_ResultRow, 1, 1, 0}, |
| 449 }; |
| 450 VdbeOp *aOp; |
| 451 sqlite3VdbeUsesBtree(v, iDb); |
| 452 if( !zRight ){ |
| 453 pParse->nMem += 2; |
| 454 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize)); |
| 455 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn); |
| 456 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
| 457 aOp[0].p1 = iDb; |
| 458 aOp[1].p1 = iDb; |
| 459 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE; |
| 460 }else{ |
| 461 int size = sqlite3AbsInt32(sqlite3Atoi(zRight)); |
| 462 sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 463 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size); |
| 464 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 465 pDb->pSchema->cache_size = size; |
| 466 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
| 467 } |
| 468 break; |
| 469 } |
| 470 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */ |
| 471 |
| 472 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
| 473 /* |
| 474 ** PRAGMA [schema.]page_size |
| 475 ** PRAGMA [schema.]page_size=N |
| 476 ** |
| 477 ** The first form reports the current setting for the |
| 478 ** database page size in bytes. The second form sets the |
| 479 ** database page size value. The value can only be set if |
| 480 ** the database has not yet been created. |
| 481 */ |
| 482 case PragTyp_PAGE_SIZE: { |
| 483 Btree *pBt = pDb->pBt; |
| 484 assert( pBt!=0 ); |
| 485 if( !zRight ){ |
| 486 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; |
| 487 returnSingleInt(v, size); |
| 488 }else{ |
| 489 /* Malloc may fail when setting the page-size, as there is an internal |
| 490 ** buffer that the pager module resizes using sqlite3_realloc(). |
| 491 */ |
| 492 db->nextPagesize = sqlite3Atoi(zRight); |
| 493 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){ |
| 494 sqlite3OomFault(db); |
| 495 } |
| 496 } |
| 497 break; |
| 498 } |
| 499 |
| 500 /* |
| 501 ** PRAGMA [schema.]secure_delete |
| 502 ** PRAGMA [schema.]secure_delete=ON/OFF |
| 503 ** |
| 504 ** The first form reports the current setting for the |
| 505 ** secure_delete flag. The second form changes the secure_delete |
| 506 ** flag setting and reports thenew value. |
| 507 */ |
| 508 case PragTyp_SECURE_DELETE: { |
| 509 Btree *pBt = pDb->pBt; |
| 510 int b = -1; |
| 511 assert( pBt!=0 ); |
| 512 if( zRight ){ |
| 513 b = sqlite3GetBoolean(zRight, 0); |
| 514 } |
| 515 if( pId2->n==0 && b>=0 ){ |
| 516 int ii; |
| 517 for(ii=0; ii<db->nDb; ii++){ |
| 518 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b); |
| 519 } |
| 520 } |
| 521 b = sqlite3BtreeSecureDelete(pBt, b); |
| 522 returnSingleInt(v, b); |
| 523 break; |
| 524 } |
| 525 |
| 526 /* |
| 527 ** PRAGMA [schema.]max_page_count |
| 528 ** PRAGMA [schema.]max_page_count=N |
| 529 ** |
| 530 ** The first form reports the current setting for the |
| 531 ** maximum number of pages in the database file. The |
| 532 ** second form attempts to change this setting. Both |
| 533 ** forms return the current setting. |
| 534 ** |
| 535 ** The absolute value of N is used. This is undocumented and might |
| 536 ** change. The only purpose is to provide an easy way to test |
| 537 ** the sqlite3AbsInt32() function. |
| 538 ** |
| 539 ** PRAGMA [schema.]page_count |
| 540 ** |
| 541 ** Return the number of pages in the specified database. |
| 542 */ |
| 543 case PragTyp_PAGE_COUNT: { |
| 544 int iReg; |
| 545 sqlite3CodeVerifySchema(pParse, iDb); |
| 546 iReg = ++pParse->nMem; |
| 547 if( sqlite3Tolower(zLeft[0])=='p' ){ |
| 548 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); |
| 549 }else{ |
| 550 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, |
| 551 sqlite3AbsInt32(sqlite3Atoi(zRight))); |
| 552 } |
| 553 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); |
| 554 break; |
| 555 } |
| 556 |
| 557 /* |
| 558 ** PRAGMA [schema.]locking_mode |
| 559 ** PRAGMA [schema.]locking_mode = (normal|exclusive) |
| 560 */ |
| 561 case PragTyp_LOCKING_MODE: { |
| 562 const char *zRet = "normal"; |
| 563 int eMode = getLockingMode(zRight); |
| 564 |
| 565 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ |
| 566 /* Simple "PRAGMA locking_mode;" statement. This is a query for |
| 567 ** the current default locking mode (which may be different to |
| 568 ** the locking-mode of the main database). |
| 569 */ |
| 570 eMode = db->dfltLockMode; |
| 571 }else{ |
| 572 Pager *pPager; |
| 573 if( pId2->n==0 ){ |
| 574 /* This indicates that no database name was specified as part |
| 575 ** of the PRAGMA command. In this case the locking-mode must be |
| 576 ** set on all attached databases, as well as the main db file. |
| 577 ** |
| 578 ** Also, the sqlite3.dfltLockMode variable is set so that |
| 579 ** any subsequently attached databases also use the specified |
| 580 ** locking mode. |
| 581 */ |
| 582 int ii; |
| 583 assert(pDb==&db->aDb[0]); |
| 584 for(ii=2; ii<db->nDb; ii++){ |
| 585 pPager = sqlite3BtreePager(db->aDb[ii].pBt); |
| 586 sqlite3PagerLockingMode(pPager, eMode); |
| 587 } |
| 588 db->dfltLockMode = (u8)eMode; |
| 589 } |
| 590 pPager = sqlite3BtreePager(pDb->pBt); |
| 591 eMode = sqlite3PagerLockingMode(pPager, eMode); |
| 592 } |
| 593 |
| 594 assert( eMode==PAGER_LOCKINGMODE_NORMAL |
| 595 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 596 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ |
| 597 zRet = "exclusive"; |
| 598 } |
| 599 returnSingleText(v, zRet); |
| 600 break; |
| 601 } |
| 602 |
| 603 /* |
| 604 ** PRAGMA [schema.]journal_mode |
| 605 ** PRAGMA [schema.]journal_mode = |
| 606 ** (delete|persist|off|truncate|memory|wal|off) |
| 607 */ |
| 608 case PragTyp_JOURNAL_MODE: { |
| 609 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ |
| 610 int ii; /* Loop counter */ |
| 611 |
| 612 if( zRight==0 ){ |
| 613 /* If there is no "=MODE" part of the pragma, do a query for the |
| 614 ** current mode */ |
| 615 eMode = PAGER_JOURNALMODE_QUERY; |
| 616 }else{ |
| 617 const char *zMode; |
| 618 int n = sqlite3Strlen30(zRight); |
| 619 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){ |
| 620 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break; |
| 621 } |
| 622 if( !zMode ){ |
| 623 /* If the "=MODE" part does not match any known journal mode, |
| 624 ** then do a query */ |
| 625 eMode = PAGER_JOURNALMODE_QUERY; |
| 626 } |
| 627 } |
| 628 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ |
| 629 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ |
| 630 iDb = 0; |
| 631 pId2->n = 1; |
| 632 } |
| 633 for(ii=db->nDb-1; ii>=0; ii--){ |
| 634 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ |
| 635 sqlite3VdbeUsesBtree(v, ii); |
| 636 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode); |
| 637 } |
| 638 } |
| 639 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 640 break; |
| 641 } |
| 642 |
| 643 /* |
| 644 ** PRAGMA [schema.]journal_size_limit |
| 645 ** PRAGMA [schema.]journal_size_limit=N |
| 646 ** |
| 647 ** Get or set the size limit on rollback journal files. |
| 648 */ |
| 649 case PragTyp_JOURNAL_SIZE_LIMIT: { |
| 650 Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 651 i64 iLimit = -2; |
| 652 if( zRight ){ |
| 653 sqlite3DecOrHexToI64(zRight, &iLimit); |
| 654 if( iLimit<-1 ) iLimit = -1; |
| 655 } |
| 656 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); |
| 657 returnSingleInt(v, iLimit); |
| 658 break; |
| 659 } |
| 660 |
| 661 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
| 662 |
| 663 /* |
| 664 ** PRAGMA [schema.]auto_vacuum |
| 665 ** PRAGMA [schema.]auto_vacuum=N |
| 666 ** |
| 667 ** Get or set the value of the database 'auto-vacuum' parameter. |
| 668 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL |
| 669 */ |
| 670 #ifndef SQLITE_OMIT_AUTOVACUUM |
| 671 case PragTyp_AUTO_VACUUM: { |
| 672 Btree *pBt = pDb->pBt; |
| 673 assert( pBt!=0 ); |
| 674 if( !zRight ){ |
| 675 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt)); |
| 676 }else{ |
| 677 int eAuto = getAutoVacuum(zRight); |
| 678 assert( eAuto>=0 && eAuto<=2 ); |
| 679 db->nextAutovac = (u8)eAuto; |
| 680 /* Call SetAutoVacuum() to set initialize the internal auto and |
| 681 ** incr-vacuum flags. This is required in case this connection |
| 682 ** creates the database file. It is important that it is created |
| 683 ** as an auto-vacuum capable db. |
| 684 */ |
| 685 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); |
| 686 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ |
| 687 /* When setting the auto_vacuum mode to either "full" or |
| 688 ** "incremental", write the value of meta[6] in the database |
| 689 ** file. Before writing to meta[6], check that meta[3] indicates |
| 690 ** that this really is an auto-vacuum capable database. |
| 691 */ |
| 692 static const int iLn = VDBE_OFFSET_LINENO(2); |
| 693 static const VdbeOpList setMeta6[] = { |
| 694 { OP_Transaction, 0, 1, 0}, /* 0 */ |
| 695 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE}, |
| 696 { OP_If, 1, 0, 0}, /* 2 */ |
| 697 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ |
| 698 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */ |
| 699 }; |
| 700 VdbeOp *aOp; |
| 701 int iAddr = sqlite3VdbeCurrentAddr(v); |
| 702 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6)); |
| 703 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn); |
| 704 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
| 705 aOp[0].p1 = iDb; |
| 706 aOp[1].p1 = iDb; |
| 707 aOp[2].p2 = iAddr+4; |
| 708 aOp[4].p1 = iDb; |
| 709 aOp[4].p3 = eAuto - 1; |
| 710 sqlite3VdbeUsesBtree(v, iDb); |
| 711 } |
| 712 } |
| 713 break; |
| 714 } |
| 715 #endif |
| 716 |
| 717 /* |
| 718 ** PRAGMA [schema.]incremental_vacuum(N) |
| 719 ** |
| 720 ** Do N steps of incremental vacuuming on a database. |
| 721 */ |
| 722 #ifndef SQLITE_OMIT_AUTOVACUUM |
| 723 case PragTyp_INCREMENTAL_VACUUM: { |
| 724 int iLimit, addr; |
| 725 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ |
| 726 iLimit = 0x7fffffff; |
| 727 } |
| 728 sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 729 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); |
| 730 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v); |
| 731 sqlite3VdbeAddOp1(v, OP_ResultRow, 1); |
| 732 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); |
| 733 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v); |
| 734 sqlite3VdbeJumpHere(v, addr); |
| 735 break; |
| 736 } |
| 737 #endif |
| 738 |
| 739 #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 740 /* |
| 741 ** PRAGMA [schema.]cache_size |
| 742 ** PRAGMA [schema.]cache_size=N |
| 743 ** |
| 744 ** The first form reports the current local setting for the |
| 745 ** page cache size. The second form sets the local |
| 746 ** page cache size value. If N is positive then that is the |
| 747 ** number of pages in the cache. If N is negative, then the |
| 748 ** number of pages is adjusted so that the cache uses -N kibibytes |
| 749 ** of memory. |
| 750 */ |
| 751 case PragTyp_CACHE_SIZE: { |
| 752 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 753 if( !zRight ){ |
| 754 returnSingleInt(v, pDb->pSchema->cache_size); |
| 755 }else{ |
| 756 int size = sqlite3Atoi(zRight); |
| 757 pDb->pSchema->cache_size = size; |
| 758 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
| 759 } |
| 760 break; |
| 761 } |
| 762 |
| 763 /* |
| 764 ** PRAGMA [schema.]cache_spill |
| 765 ** PRAGMA cache_spill=BOOLEAN |
| 766 ** PRAGMA [schema.]cache_spill=N |
| 767 ** |
| 768 ** The first form reports the current local setting for the |
| 769 ** page cache spill size. The second form turns cache spill on |
| 770 ** or off. When turnning cache spill on, the size is set to the |
| 771 ** current cache_size. The third form sets a spill size that |
| 772 ** may be different form the cache size. |
| 773 ** If N is positive then that is the |
| 774 ** number of pages in the cache. If N is negative, then the |
| 775 ** number of pages is adjusted so that the cache uses -N kibibytes |
| 776 ** of memory. |
| 777 ** |
| 778 ** If the number of cache_spill pages is less then the number of |
| 779 ** cache_size pages, no spilling occurs until the page count exceeds |
| 780 ** the number of cache_size pages. |
| 781 ** |
| 782 ** The cache_spill=BOOLEAN setting applies to all attached schemas, |
| 783 ** not just the schema specified. |
| 784 */ |
| 785 case PragTyp_CACHE_SPILL: { |
| 786 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 787 if( !zRight ){ |
| 788 returnSingleInt(v, |
| 789 (db->flags & SQLITE_CacheSpill)==0 ? 0 : |
| 790 sqlite3BtreeSetSpillSize(pDb->pBt,0)); |
| 791 }else{ |
| 792 int size = 1; |
| 793 if( sqlite3GetInt32(zRight, &size) ){ |
| 794 sqlite3BtreeSetSpillSize(pDb->pBt, size); |
| 795 } |
| 796 if( sqlite3GetBoolean(zRight, size!=0) ){ |
| 797 db->flags |= SQLITE_CacheSpill; |
| 798 }else{ |
| 799 db->flags &= ~SQLITE_CacheSpill; |
| 800 } |
| 801 setAllPagerFlags(db); |
| 802 } |
| 803 break; |
| 804 } |
| 805 |
| 806 /* |
| 807 ** PRAGMA [schema.]mmap_size(N) |
| 808 ** |
| 809 ** Used to set mapping size limit. The mapping size limit is |
| 810 ** used to limit the aggregate size of all memory mapped regions of the |
| 811 ** database file. If this parameter is set to zero, then memory mapping |
| 812 ** is not used at all. If N is negative, then the default memory map |
| 813 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set. |
| 814 ** The parameter N is measured in bytes. |
| 815 ** |
| 816 ** This value is advisory. The underlying VFS is free to memory map |
| 817 ** as little or as much as it wants. Except, if N is set to 0 then the |
| 818 ** upper layers will never invoke the xFetch interfaces to the VFS. |
| 819 */ |
| 820 case PragTyp_MMAP_SIZE: { |
| 821 sqlite3_int64 sz; |
| 822 #if SQLITE_MAX_MMAP_SIZE>0 |
| 823 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 824 if( zRight ){ |
| 825 int ii; |
| 826 sqlite3DecOrHexToI64(zRight, &sz); |
| 827 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap; |
| 828 if( pId2->n==0 ) db->szMmap = sz; |
| 829 for(ii=db->nDb-1; ii>=0; ii--){ |
| 830 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ |
| 831 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); |
| 832 } |
| 833 } |
| 834 } |
| 835 sz = -1; |
| 836 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz); |
| 837 #else |
| 838 sz = 0; |
| 839 rc = SQLITE_OK; |
| 840 #endif |
| 841 if( rc==SQLITE_OK ){ |
| 842 returnSingleInt(v, sz); |
| 843 }else if( rc!=SQLITE_NOTFOUND ){ |
| 844 pParse->nErr++; |
| 845 pParse->rc = rc; |
| 846 } |
| 847 break; |
| 848 } |
| 849 |
| 850 /* |
| 851 ** PRAGMA temp_store |
| 852 ** PRAGMA temp_store = "default"|"memory"|"file" |
| 853 ** |
| 854 ** Return or set the local value of the temp_store flag. Changing |
| 855 ** the local value does not make changes to the disk file and the default |
| 856 ** value will be restored the next time the database is opened. |
| 857 ** |
| 858 ** Note that it is possible for the library compile-time options to |
| 859 ** override this setting |
| 860 */ |
| 861 case PragTyp_TEMP_STORE: { |
| 862 if( !zRight ){ |
| 863 returnSingleInt(v, db->temp_store); |
| 864 }else{ |
| 865 changeTempStorage(pParse, zRight); |
| 866 } |
| 867 break; |
| 868 } |
| 869 |
| 870 /* |
| 871 ** PRAGMA temp_store_directory |
| 872 ** PRAGMA temp_store_directory = ""|"directory_name" |
| 873 ** |
| 874 ** Return or set the local value of the temp_store_directory flag. Changing |
| 875 ** the value sets a specific directory to be used for temporary files. |
| 876 ** Setting to a null string reverts to the default temporary directory search. |
| 877 ** If temporary directory is changed, then invalidateTempStorage. |
| 878 ** |
| 879 */ |
| 880 case PragTyp_TEMP_STORE_DIRECTORY: { |
| 881 if( !zRight ){ |
| 882 returnSingleText(v, sqlite3_temp_directory); |
| 883 }else{ |
| 884 #ifndef SQLITE_OMIT_WSD |
| 885 if( zRight[0] ){ |
| 886 int res; |
| 887 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); |
| 888 if( rc!=SQLITE_OK || res==0 ){ |
| 889 sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 890 goto pragma_out; |
| 891 } |
| 892 } |
| 893 if( SQLITE_TEMP_STORE==0 |
| 894 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) |
| 895 || (SQLITE_TEMP_STORE==2 && db->temp_store==1) |
| 896 ){ |
| 897 invalidateTempStorage(pParse); |
| 898 } |
| 899 sqlite3_free(sqlite3_temp_directory); |
| 900 if( zRight[0] ){ |
| 901 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); |
| 902 }else{ |
| 903 sqlite3_temp_directory = 0; |
| 904 } |
| 905 #endif /* SQLITE_OMIT_WSD */ |
| 906 } |
| 907 break; |
| 908 } |
| 909 |
| 910 #if SQLITE_OS_WIN |
| 911 /* |
| 912 ** PRAGMA data_store_directory |
| 913 ** PRAGMA data_store_directory = ""|"directory_name" |
| 914 ** |
| 915 ** Return or set the local value of the data_store_directory flag. Changing |
| 916 ** the value sets a specific directory to be used for database files that |
| 917 ** were specified with a relative pathname. Setting to a null string reverts |
| 918 ** to the default database directory, which for database files specified with |
| 919 ** a relative path will probably be based on the current directory for the |
| 920 ** process. Database file specified with an absolute path are not impacted |
| 921 ** by this setting, regardless of its value. |
| 922 ** |
| 923 */ |
| 924 case PragTyp_DATA_STORE_DIRECTORY: { |
| 925 if( !zRight ){ |
| 926 returnSingleText(v, sqlite3_data_directory); |
| 927 }else{ |
| 928 #ifndef SQLITE_OMIT_WSD |
| 929 if( zRight[0] ){ |
| 930 int res; |
| 931 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); |
| 932 if( rc!=SQLITE_OK || res==0 ){ |
| 933 sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 934 goto pragma_out; |
| 935 } |
| 936 } |
| 937 sqlite3_free(sqlite3_data_directory); |
| 938 if( zRight[0] ){ |
| 939 sqlite3_data_directory = sqlite3_mprintf("%s", zRight); |
| 940 }else{ |
| 941 sqlite3_data_directory = 0; |
| 942 } |
| 943 #endif /* SQLITE_OMIT_WSD */ |
| 944 } |
| 945 break; |
| 946 } |
| 947 #endif |
| 948 |
| 949 #if SQLITE_ENABLE_LOCKING_STYLE |
| 950 /* |
| 951 ** PRAGMA [schema.]lock_proxy_file |
| 952 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path" |
| 953 ** |
| 954 ** Return or set the value of the lock_proxy_file flag. Changing |
| 955 ** the value sets a specific file to be used for database access locks. |
| 956 ** |
| 957 */ |
| 958 case PragTyp_LOCK_PROXY_FILE: { |
| 959 if( !zRight ){ |
| 960 Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 961 char *proxy_file_path = NULL; |
| 962 sqlite3_file *pFile = sqlite3PagerFile(pPager); |
| 963 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, |
| 964 &proxy_file_path); |
| 965 returnSingleText(v, proxy_file_path); |
| 966 }else{ |
| 967 Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 968 sqlite3_file *pFile = sqlite3PagerFile(pPager); |
| 969 int res; |
| 970 if( zRight[0] ){ |
| 971 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, |
| 972 zRight); |
| 973 } else { |
| 974 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, |
| 975 NULL); |
| 976 } |
| 977 if( res!=SQLITE_OK ){ |
| 978 sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); |
| 979 goto pragma_out; |
| 980 } |
| 981 } |
| 982 break; |
| 983 } |
| 984 #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 985 |
| 986 /* |
| 987 ** PRAGMA [schema.]synchronous |
| 988 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA |
| 989 ** |
| 990 ** Return or set the local value of the synchronous flag. Changing |
| 991 ** the local value does not make changes to the disk file and the |
| 992 ** default value will be restored the next time the database is |
| 993 ** opened. |
| 994 */ |
| 995 case PragTyp_SYNCHRONOUS: { |
| 996 if( !zRight ){ |
| 997 returnSingleInt(v, pDb->safety_level-1); |
| 998 }else{ |
| 999 if( !db->autoCommit ){ |
| 1000 sqlite3ErrorMsg(pParse, |
| 1001 "Safety level may not be changed inside a transaction"); |
| 1002 }else{ |
| 1003 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK; |
| 1004 if( iLevel==0 ) iLevel = 1; |
| 1005 pDb->safety_level = iLevel; |
| 1006 pDb->bSyncSet = 1; |
| 1007 setAllPagerFlags(db); |
| 1008 } |
| 1009 } |
| 1010 break; |
| 1011 } |
| 1012 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
| 1013 |
| 1014 #ifndef SQLITE_OMIT_FLAG_PRAGMAS |
| 1015 case PragTyp_FLAG: { |
| 1016 if( zRight==0 ){ |
| 1017 setPragmaResultColumnNames(v, pPragma); |
| 1018 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 ); |
| 1019 }else{ |
| 1020 int mask = pPragma->iArg; /* Mask of bits to set or clear. */ |
| 1021 if( db->autoCommit==0 ){ |
| 1022 /* Foreign key support may not be enabled or disabled while not |
| 1023 ** in auto-commit mode. */ |
| 1024 mask &= ~(SQLITE_ForeignKeys); |
| 1025 } |
| 1026 #if SQLITE_USER_AUTHENTICATION |
| 1027 if( db->auth.authLevel==UAUTH_User ){ |
| 1028 /* Do not allow non-admin users to modify the schema arbitrarily */ |
| 1029 mask &= ~(SQLITE_WriteSchema); |
| 1030 } |
| 1031 #endif |
| 1032 |
| 1033 if( sqlite3GetBoolean(zRight, 0) ){ |
| 1034 db->flags |= mask; |
| 1035 }else{ |
| 1036 db->flags &= ~mask; |
| 1037 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0; |
| 1038 } |
| 1039 |
| 1040 /* Many of the flag-pragmas modify the code generated by the SQL |
| 1041 ** compiler (eg. count_changes). So add an opcode to expire all |
| 1042 ** compiled SQL statements after modifying a pragma value. |
| 1043 */ |
| 1044 sqlite3VdbeAddOp0(v, OP_Expire); |
| 1045 setAllPagerFlags(db); |
| 1046 } |
| 1047 break; |
| 1048 } |
| 1049 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ |
| 1050 |
| 1051 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS |
| 1052 /* |
| 1053 ** PRAGMA table_info(<table>) |
| 1054 ** |
| 1055 ** Return a single row for each column of the named table. The columns of |
| 1056 ** the returned data set are: |
| 1057 ** |
| 1058 ** cid: Column id (numbered from left to right, starting at 0) |
| 1059 ** name: Column name |
| 1060 ** type: Column declaration type. |
| 1061 ** notnull: True if 'NOT NULL' is part of column declaration |
| 1062 ** dflt_value: The default value for the column, if any. |
| 1063 */ |
| 1064 case PragTyp_TABLE_INFO: if( zRight ){ |
| 1065 Table *pTab; |
| 1066 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); |
| 1067 if( pTab ){ |
| 1068 int i, k; |
| 1069 int nHidden = 0; |
| 1070 Column *pCol; |
| 1071 Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1072 pParse->nMem = 6; |
| 1073 sqlite3CodeVerifySchema(pParse, iDb); |
| 1074 sqlite3ViewGetColumnNames(pParse, pTab); |
| 1075 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
| 1076 if( IsHiddenColumn(pCol) ){ |
| 1077 nHidden++; |
| 1078 continue; |
| 1079 } |
| 1080 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ |
| 1081 k = 0; |
| 1082 }else if( pPk==0 ){ |
| 1083 k = 1; |
| 1084 }else{ |
| 1085 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} |
| 1086 } |
| 1087 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN ); |
| 1088 sqlite3VdbeMultiLoad(v, 1, "issisi", |
| 1089 i-nHidden, |
| 1090 pCol->zName, |
| 1091 sqlite3ColumnType(pCol,""), |
| 1092 pCol->notNull ? 1 : 0, |
| 1093 pCol->pDflt ? pCol->pDflt->u.zToken : 0, |
| 1094 k); |
| 1095 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); |
| 1096 } |
| 1097 } |
| 1098 } |
| 1099 break; |
| 1100 |
| 1101 case PragTyp_STATS: { |
| 1102 Index *pIdx; |
| 1103 HashElem *i; |
| 1104 pParse->nMem = 4; |
| 1105 sqlite3CodeVerifySchema(pParse, iDb); |
| 1106 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){ |
| 1107 Table *pTab = sqliteHashData(i); |
| 1108 sqlite3VdbeMultiLoad(v, 1, "ssii", |
| 1109 pTab->zName, |
| 1110 0, |
| 1111 pTab->szTabRow, |
| 1112 pTab->nRowLogEst); |
| 1113 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); |
| 1114 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1115 sqlite3VdbeMultiLoad(v, 2, "sii", |
| 1116 pIdx->zName, |
| 1117 pIdx->szIdxRow, |
| 1118 pIdx->aiRowLogEst[0]); |
| 1119 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4); |
| 1120 } |
| 1121 } |
| 1122 } |
| 1123 break; |
| 1124 |
| 1125 case PragTyp_INDEX_INFO: if( zRight ){ |
| 1126 Index *pIdx; |
| 1127 Table *pTab; |
| 1128 pIdx = sqlite3FindIndex(db, zRight, zDb); |
| 1129 if( pIdx ){ |
| 1130 int i; |
| 1131 int mx; |
| 1132 if( pPragma->iArg ){ |
| 1133 /* PRAGMA index_xinfo (newer version with more rows and columns) */ |
| 1134 mx = pIdx->nColumn; |
| 1135 pParse->nMem = 6; |
| 1136 }else{ |
| 1137 /* PRAGMA index_info (legacy version) */ |
| 1138 mx = pIdx->nKeyCol; |
| 1139 pParse->nMem = 3; |
| 1140 } |
| 1141 pTab = pIdx->pTable; |
| 1142 sqlite3CodeVerifySchema(pParse, iDb); |
| 1143 assert( pParse->nMem<=pPragma->nPragCName ); |
| 1144 for(i=0; i<mx; i++){ |
| 1145 i16 cnum = pIdx->aiColumn[i]; |
| 1146 sqlite3VdbeMultiLoad(v, 1, "iis", i, cnum, |
| 1147 cnum<0 ? 0 : pTab->aCol[cnum].zName); |
| 1148 if( pPragma->iArg ){ |
| 1149 sqlite3VdbeMultiLoad(v, 4, "isi", |
| 1150 pIdx->aSortOrder[i], |
| 1151 pIdx->azColl[i], |
| 1152 i<pIdx->nKeyCol); |
| 1153 } |
| 1154 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem); |
| 1155 } |
| 1156 } |
| 1157 } |
| 1158 break; |
| 1159 |
| 1160 case PragTyp_INDEX_LIST: if( zRight ){ |
| 1161 Index *pIdx; |
| 1162 Table *pTab; |
| 1163 int i; |
| 1164 pTab = sqlite3FindTable(db, zRight, zDb); |
| 1165 if( pTab ){ |
| 1166 pParse->nMem = 5; |
| 1167 sqlite3CodeVerifySchema(pParse, iDb); |
| 1168 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){ |
| 1169 const char *azOrigin[] = { "c", "u", "pk" }; |
| 1170 sqlite3VdbeMultiLoad(v, 1, "isisi", |
| 1171 i, |
| 1172 pIdx->zName, |
| 1173 IsUniqueIndex(pIdx), |
| 1174 azOrigin[pIdx->idxType], |
| 1175 pIdx->pPartIdxWhere!=0); |
| 1176 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); |
| 1177 } |
| 1178 } |
| 1179 } |
| 1180 break; |
| 1181 |
| 1182 case PragTyp_DATABASE_LIST: { |
| 1183 int i; |
| 1184 pParse->nMem = 3; |
| 1185 for(i=0; i<db->nDb; i++){ |
| 1186 if( db->aDb[i].pBt==0 ) continue; |
| 1187 assert( db->aDb[i].zDbSName!=0 ); |
| 1188 sqlite3VdbeMultiLoad(v, 1, "iss", |
| 1189 i, |
| 1190 db->aDb[i].zDbSName, |
| 1191 sqlite3BtreeGetFilename(db->aDb[i].pBt)); |
| 1192 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); |
| 1193 } |
| 1194 } |
| 1195 break; |
| 1196 |
| 1197 case PragTyp_COLLATION_LIST: { |
| 1198 int i = 0; |
| 1199 HashElem *p; |
| 1200 pParse->nMem = 2; |
| 1201 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ |
| 1202 CollSeq *pColl = (CollSeq *)sqliteHashData(p); |
| 1203 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName); |
| 1204 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); |
| 1205 } |
| 1206 } |
| 1207 break; |
| 1208 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ |
| 1209 |
| 1210 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 1211 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){ |
| 1212 FKey *pFK; |
| 1213 Table *pTab; |
| 1214 pTab = sqlite3FindTable(db, zRight, zDb); |
| 1215 if( pTab ){ |
| 1216 pFK = pTab->pFKey; |
| 1217 if( pFK ){ |
| 1218 int i = 0; |
| 1219 pParse->nMem = 8; |
| 1220 sqlite3CodeVerifySchema(pParse, iDb); |
| 1221 while(pFK){ |
| 1222 int j; |
| 1223 for(j=0; j<pFK->nCol; j++){ |
| 1224 sqlite3VdbeMultiLoad(v, 1, "iissssss", |
| 1225 i, |
| 1226 j, |
| 1227 pFK->zTo, |
| 1228 pTab->aCol[pFK->aCol[j].iFrom].zName, |
| 1229 pFK->aCol[j].zCol, |
| 1230 actionName(pFK->aAction[1]), /* ON UPDATE */ |
| 1231 actionName(pFK->aAction[0]), /* ON DELETE */ |
| 1232 "NONE"); |
| 1233 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8); |
| 1234 } |
| 1235 ++i; |
| 1236 pFK = pFK->pNextFrom; |
| 1237 } |
| 1238 } |
| 1239 } |
| 1240 } |
| 1241 break; |
| 1242 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
| 1243 |
| 1244 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 1245 #ifndef SQLITE_OMIT_TRIGGER |
| 1246 case PragTyp_FOREIGN_KEY_CHECK: { |
| 1247 FKey *pFK; /* A foreign key constraint */ |
| 1248 Table *pTab; /* Child table contain "REFERENCES" keyword */ |
| 1249 Table *pParent; /* Parent table that child points to */ |
| 1250 Index *pIdx; /* Index in the parent table */ |
| 1251 int i; /* Loop counter: Foreign key number for pTab */ |
| 1252 int j; /* Loop counter: Field of the foreign key */ |
| 1253 HashElem *k; /* Loop counter: Next table in schema */ |
| 1254 int x; /* result variable */ |
| 1255 int regResult; /* 3 registers to hold a result row */ |
| 1256 int regKey; /* Register to hold key for checking the FK */ |
| 1257 int regRow; /* Registers to hold a row from pTab */ |
| 1258 int addrTop; /* Top of a loop checking foreign keys */ |
| 1259 int addrOk; /* Jump here if the key is OK */ |
| 1260 int *aiCols; /* child to parent column mapping */ |
| 1261 |
| 1262 regResult = pParse->nMem+1; |
| 1263 pParse->nMem += 4; |
| 1264 regKey = ++pParse->nMem; |
| 1265 regRow = ++pParse->nMem; |
| 1266 sqlite3CodeVerifySchema(pParse, iDb); |
| 1267 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash); |
| 1268 while( k ){ |
| 1269 if( zRight ){ |
| 1270 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb); |
| 1271 k = 0; |
| 1272 }else{ |
| 1273 pTab = (Table*)sqliteHashData(k); |
| 1274 k = sqliteHashNext(k); |
| 1275 } |
| 1276 if( pTab==0 || pTab->pFKey==0 ) continue; |
| 1277 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 1278 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow; |
| 1279 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); |
| 1280 sqlite3VdbeLoadString(v, regResult, pTab->zName); |
| 1281 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ |
| 1282 pParent = sqlite3FindTable(db, pFK->zTo, zDb); |
| 1283 if( pParent==0 ) continue; |
| 1284 pIdx = 0; |
| 1285 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName); |
| 1286 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0); |
| 1287 if( x==0 ){ |
| 1288 if( pIdx==0 ){ |
| 1289 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead); |
| 1290 }else{ |
| 1291 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb); |
| 1292 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 1293 } |
| 1294 }else{ |
| 1295 k = 0; |
| 1296 break; |
| 1297 } |
| 1298 } |
| 1299 assert( pParse->nErr>0 || pFK==0 ); |
| 1300 if( pFK ) break; |
| 1301 if( pParse->nTab<i ) pParse->nTab = i; |
| 1302 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v); |
| 1303 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){ |
| 1304 pParent = sqlite3FindTable(db, pFK->zTo, zDb); |
| 1305 pIdx = 0; |
| 1306 aiCols = 0; |
| 1307 if( pParent ){ |
| 1308 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols); |
| 1309 assert( x==0 ); |
| 1310 } |
| 1311 addrOk = sqlite3VdbeMakeLabel(v); |
| 1312 if( pParent && pIdx==0 ){ |
| 1313 int iKey = pFK->aCol[0].iFrom; |
| 1314 assert( iKey>=0 && iKey<pTab->nCol ); |
| 1315 if( iKey!=pTab->iPKey ){ |
| 1316 sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow); |
| 1317 sqlite3ColumnDefault(v, pTab, iKey, regRow); |
| 1318 sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v); |
| 1319 }else{ |
| 1320 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow); |
| 1321 } |
| 1322 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, 0, regRow); VdbeCoverage(v); |
| 1323 sqlite3VdbeGoto(v, addrOk); |
| 1324 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); |
| 1325 }else{ |
| 1326 for(j=0; j<pFK->nCol; j++){ |
| 1327 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, |
| 1328 aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j); |
| 1329 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); |
| 1330 } |
| 1331 if( pParent ){ |
| 1332 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey, |
| 1333 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol); |
| 1334 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0); |
| 1335 VdbeCoverage(v); |
| 1336 } |
| 1337 } |
| 1338 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1); |
| 1339 sqlite3VdbeMultiLoad(v, regResult+2, "si", pFK->zTo, i-1); |
| 1340 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4); |
| 1341 sqlite3VdbeResolveLabel(v, addrOk); |
| 1342 sqlite3DbFree(db, aiCols); |
| 1343 } |
| 1344 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v); |
| 1345 sqlite3VdbeJumpHere(v, addrTop); |
| 1346 } |
| 1347 } |
| 1348 break; |
| 1349 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
| 1350 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
| 1351 |
| 1352 #ifndef NDEBUG |
| 1353 case PragTyp_PARSER_TRACE: { |
| 1354 if( zRight ){ |
| 1355 if( sqlite3GetBoolean(zRight, 0) ){ |
| 1356 sqlite3ParserTrace(stdout, "parser: "); |
| 1357 }else{ |
| 1358 sqlite3ParserTrace(0, 0); |
| 1359 } |
| 1360 } |
| 1361 } |
| 1362 break; |
| 1363 #endif |
| 1364 |
| 1365 /* Reinstall the LIKE and GLOB functions. The variant of LIKE |
| 1366 ** used will be case sensitive or not depending on the RHS. |
| 1367 */ |
| 1368 case PragTyp_CASE_SENSITIVE_LIKE: { |
| 1369 if( zRight ){ |
| 1370 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0)); |
| 1371 } |
| 1372 } |
| 1373 break; |
| 1374 |
| 1375 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX |
| 1376 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 |
| 1377 #endif |
| 1378 |
| 1379 #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
| 1380 /* Pragma "quick_check" is reduced version of |
| 1381 ** integrity_check designed to detect most database corruption |
| 1382 ** without most of the overhead of a full integrity-check. |
| 1383 */ |
| 1384 case PragTyp_INTEGRITY_CHECK: { |
| 1385 int i, j, addr, mxErr; |
| 1386 |
| 1387 int isQuick = (sqlite3Tolower(zLeft[0])=='q'); |
| 1388 |
| 1389 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check", |
| 1390 ** then iDb is set to the index of the database identified by <db>. |
| 1391 ** In this case, the integrity of database iDb only is verified by |
| 1392 ** the VDBE created below. |
| 1393 ** |
| 1394 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or |
| 1395 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb |
| 1396 ** to -1 here, to indicate that the VDBE should verify the integrity |
| 1397 ** of all attached databases. */ |
| 1398 assert( iDb>=0 ); |
| 1399 assert( iDb==0 || pId2->z ); |
| 1400 if( pId2->z==0 ) iDb = -1; |
| 1401 |
| 1402 /* Initialize the VDBE program */ |
| 1403 pParse->nMem = 6; |
| 1404 |
| 1405 /* Set the maximum error count */ |
| 1406 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; |
| 1407 if( zRight ){ |
| 1408 sqlite3GetInt32(zRight, &mxErr); |
| 1409 if( mxErr<=0 ){ |
| 1410 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; |
| 1411 } |
| 1412 } |
| 1413 sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ |
| 1414 |
| 1415 /* Do an integrity check on each database file */ |
| 1416 for(i=0; i<db->nDb; i++){ |
| 1417 HashElem *x; |
| 1418 Hash *pTbls; |
| 1419 int *aRoot; |
| 1420 int cnt = 0; |
| 1421 int mxIdx = 0; |
| 1422 int nIdx; |
| 1423 |
| 1424 if( OMIT_TEMPDB && i==1 ) continue; |
| 1425 if( iDb>=0 && i!=iDb ) continue; |
| 1426 |
| 1427 sqlite3CodeVerifySchema(pParse, i); |
| 1428 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ |
| 1429 VdbeCoverage(v); |
| 1430 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
| 1431 sqlite3VdbeJumpHere(v, addr); |
| 1432 |
| 1433 /* Do an integrity check of the B-Tree |
| 1434 ** |
| 1435 ** Begin by finding the root pages numbers |
| 1436 ** for all tables and indices in the database. |
| 1437 */ |
| 1438 assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 1439 pTbls = &db->aDb[i].pSchema->tblHash; |
| 1440 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
| 1441 Table *pTab = sqliteHashData(x); |
| 1442 Index *pIdx; |
| 1443 if( HasRowid(pTab) ) cnt++; |
| 1444 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; } |
| 1445 if( nIdx>mxIdx ) mxIdx = nIdx; |
| 1446 } |
| 1447 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1)); |
| 1448 if( aRoot==0 ) break; |
| 1449 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
| 1450 Table *pTab = sqliteHashData(x); |
| 1451 Index *pIdx; |
| 1452 if( HasRowid(pTab) ) aRoot[cnt++] = pTab->tnum; |
| 1453 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1454 aRoot[cnt++] = pIdx->tnum; |
| 1455 } |
| 1456 } |
| 1457 aRoot[cnt] = 0; |
| 1458 |
| 1459 /* Make sure sufficient number of registers have been allocated */ |
| 1460 pParse->nMem = MAX( pParse->nMem, 8+mxIdx ); |
| 1461 |
| 1462 /* Do the b-tree integrity checks */ |
| 1463 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY); |
| 1464 sqlite3VdbeChangeP5(v, (u8)i); |
| 1465 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); |
| 1466 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
| 1467 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName), |
| 1468 P4_DYNAMIC); |
| 1469 sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1); |
| 1470 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); |
| 1471 sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); |
| 1472 sqlite3VdbeJumpHere(v, addr); |
| 1473 |
| 1474 /* Make sure all the indices are constructed correctly. |
| 1475 */ |
| 1476 for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ |
| 1477 Table *pTab = sqliteHashData(x); |
| 1478 Index *pIdx, *pPk; |
| 1479 Index *pPrior = 0; |
| 1480 int loopTop; |
| 1481 int iDataCur, iIdxCur; |
| 1482 int r1 = -1; |
| 1483 |
| 1484 if( pTab->pIndex==0 ) continue; |
| 1485 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); |
| 1486 addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ |
| 1487 VdbeCoverage(v); |
| 1488 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
| 1489 sqlite3VdbeJumpHere(v, addr); |
| 1490 sqlite3ExprCacheClear(pParse); |
| 1491 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, |
| 1492 1, 0, &iDataCur, &iIdxCur); |
| 1493 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); |
| 1494 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 1495 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ |
| 1496 } |
| 1497 assert( pParse->nMem>=8+j ); |
| 1498 assert( sqlite3NoTempsInRange(pParse,1,7+j) ); |
| 1499 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); |
| 1500 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); |
| 1501 /* Verify that all NOT NULL columns really are NOT NULL */ |
| 1502 for(j=0; j<pTab->nCol; j++){ |
| 1503 char *zErr; |
| 1504 int jmp2, jmp3; |
| 1505 if( j==pTab->iPKey ) continue; |
| 1506 if( pTab->aCol[j].notNull==0 ) continue; |
| 1507 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); |
| 1508 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 1509 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); |
| 1510 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ |
| 1511 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, |
| 1512 pTab->aCol[j].zName); |
| 1513 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
| 1514 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); |
| 1515 jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); |
| 1516 sqlite3VdbeAddOp0(v, OP_Halt); |
| 1517 sqlite3VdbeJumpHere(v, jmp2); |
| 1518 sqlite3VdbeJumpHere(v, jmp3); |
| 1519 } |
| 1520 /* Validate index entries for the current row */ |
| 1521 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 1522 int jmp2, jmp3, jmp4, jmp5; |
| 1523 int ckUniq = sqlite3VdbeMakeLabel(v); |
| 1524 if( pPk==pIdx ) continue; |
| 1525 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, |
| 1526 pPrior, r1); |
| 1527 pPrior = pIdx; |
| 1528 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1); /* increment entry count */ |
| 1529 /* Verify that an index entry exists for the current table row */ |
| 1530 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, |
| 1531 pIdx->nColumn); VdbeCoverage(v); |
| 1532 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ |
| 1533 sqlite3VdbeLoadString(v, 3, "row "); |
| 1534 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); |
| 1535 sqlite3VdbeLoadString(v, 4, " missing from index "); |
| 1536 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); |
| 1537 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); |
| 1538 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); |
| 1539 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); |
| 1540 jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v); |
| 1541 sqlite3VdbeAddOp0(v, OP_Halt); |
| 1542 sqlite3VdbeJumpHere(v, jmp2); |
| 1543 /* For UNIQUE indexes, verify that only one entry exists with the |
| 1544 ** current key. The entry is unique if (1) any column is NULL |
| 1545 ** or (2) the next entry has a different key */ |
| 1546 if( IsUniqueIndex(pIdx) ){ |
| 1547 int uniqOk = sqlite3VdbeMakeLabel(v); |
| 1548 int jmp6; |
| 1549 int kk; |
| 1550 for(kk=0; kk<pIdx->nKeyCol; kk++){ |
| 1551 int iCol = pIdx->aiColumn[kk]; |
| 1552 assert( iCol!=XN_ROWID && iCol<pTab->nCol ); |
| 1553 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue; |
| 1554 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk); |
| 1555 VdbeCoverage(v); |
| 1556 } |
| 1557 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v); |
| 1558 sqlite3VdbeGoto(v, uniqOk); |
| 1559 sqlite3VdbeJumpHere(v, jmp6); |
| 1560 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1, |
| 1561 pIdx->nKeyCol); VdbeCoverage(v); |
| 1562 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */ |
| 1563 sqlite3VdbeLoadString(v, 3, "non-unique entry in index "); |
| 1564 sqlite3VdbeGoto(v, jmp5); |
| 1565 sqlite3VdbeResolveLabel(v, uniqOk); |
| 1566 } |
| 1567 sqlite3VdbeJumpHere(v, jmp4); |
| 1568 sqlite3ResolvePartIdxLabel(pParse, jmp3); |
| 1569 } |
| 1570 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); |
| 1571 sqlite3VdbeJumpHere(v, loopTop-1); |
| 1572 #ifndef SQLITE_OMIT_BTREECOUNT |
| 1573 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); |
| 1574 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 1575 if( pPk==pIdx ) continue; |
| 1576 addr = sqlite3VdbeCurrentAddr(v); |
| 1577 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v); |
| 1578 sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
| 1579 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); |
| 1580 sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v); |
| 1581 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
| 1582 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); |
| 1583 sqlite3VdbeLoadString(v, 3, pIdx->zName); |
| 1584 sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7); |
| 1585 sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1); |
| 1586 } |
| 1587 #endif /* SQLITE_OMIT_BTREECOUNT */ |
| 1588 } |
| 1589 } |
| 1590 { |
| 1591 static const int iLn = VDBE_OFFSET_LINENO(2); |
| 1592 static const VdbeOpList endCode[] = { |
| 1593 { OP_AddImm, 1, 0, 0}, /* 0 */ |
| 1594 { OP_If, 1, 4, 0}, /* 1 */ |
| 1595 { OP_String8, 0, 3, 0}, /* 2 */ |
| 1596 { OP_ResultRow, 3, 1, 0}, /* 3 */ |
| 1597 }; |
| 1598 VdbeOp *aOp; |
| 1599 |
| 1600 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn); |
| 1601 if( aOp ){ |
| 1602 aOp[0].p2 = -mxErr; |
| 1603 aOp[2].p4type = P4_STATIC; |
| 1604 aOp[2].p4.z = "ok"; |
| 1605 } |
| 1606 } |
| 1607 } |
| 1608 break; |
| 1609 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 1610 |
| 1611 #ifndef SQLITE_OMIT_UTF16 |
| 1612 /* |
| 1613 ** PRAGMA encoding |
| 1614 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" |
| 1615 ** |
| 1616 ** In its first form, this pragma returns the encoding of the main |
| 1617 ** database. If the database is not initialized, it is initialized now. |
| 1618 ** |
| 1619 ** The second form of this pragma is a no-op if the main database file |
| 1620 ** has not already been initialized. In this case it sets the default |
| 1621 ** encoding that will be used for the main database file if a new file |
| 1622 ** is created. If an existing main database file is opened, then the |
| 1623 ** default text encoding for the existing database is used. |
| 1624 ** |
| 1625 ** In all cases new databases created using the ATTACH command are |
| 1626 ** created to use the same default text encoding as the main database. If |
| 1627 ** the main database has not been initialized and/or created when ATTACH |
| 1628 ** is executed, this is done before the ATTACH operation. |
| 1629 ** |
| 1630 ** In the second form this pragma sets the text encoding to be used in |
| 1631 ** new database files created using this database handle. It is only |
| 1632 ** useful if invoked immediately after the main database i |
| 1633 */ |
| 1634 case PragTyp_ENCODING: { |
| 1635 static const struct EncName { |
| 1636 char *zName; |
| 1637 u8 enc; |
| 1638 } encnames[] = { |
| 1639 { "UTF8", SQLITE_UTF8 }, |
| 1640 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ |
| 1641 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ |
| 1642 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ |
| 1643 { "UTF16le", SQLITE_UTF16LE }, |
| 1644 { "UTF16be", SQLITE_UTF16BE }, |
| 1645 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ |
| 1646 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ |
| 1647 { 0, 0 } |
| 1648 }; |
| 1649 const struct EncName *pEnc; |
| 1650 if( !zRight ){ /* "PRAGMA encoding" */ |
| 1651 if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 1652 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); |
| 1653 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); |
| 1654 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); |
| 1655 returnSingleText(v, encnames[ENC(pParse->db)].zName); |
| 1656 }else{ /* "PRAGMA encoding = XXX" */ |
| 1657 /* Only change the value of sqlite.enc if the database handle is not |
| 1658 ** initialized. If the main database exists, the new sqlite.enc value |
| 1659 ** will be overwritten when the schema is next loaded. If it does not |
| 1660 ** already exists, it will be created to use the new encoding value. |
| 1661 */ |
| 1662 if( |
| 1663 !(DbHasProperty(db, 0, DB_SchemaLoaded)) || |
| 1664 DbHasProperty(db, 0, DB_Empty) |
| 1665 ){ |
| 1666 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ |
| 1667 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ |
| 1668 SCHEMA_ENC(db) = ENC(db) = |
| 1669 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; |
| 1670 break; |
| 1671 } |
| 1672 } |
| 1673 if( !pEnc->zName ){ |
| 1674 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); |
| 1675 } |
| 1676 } |
| 1677 } |
| 1678 } |
| 1679 break; |
| 1680 #endif /* SQLITE_OMIT_UTF16 */ |
| 1681 |
| 1682 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS |
| 1683 /* |
| 1684 ** PRAGMA [schema.]schema_version |
| 1685 ** PRAGMA [schema.]schema_version = <integer> |
| 1686 ** |
| 1687 ** PRAGMA [schema.]user_version |
| 1688 ** PRAGMA [schema.]user_version = <integer> |
| 1689 ** |
| 1690 ** PRAGMA [schema.]freelist_count |
| 1691 ** |
| 1692 ** PRAGMA [schema.]data_version |
| 1693 ** |
| 1694 ** PRAGMA [schema.]application_id |
| 1695 ** PRAGMA [schema.]application_id = <integer> |
| 1696 ** |
| 1697 ** The pragma's schema_version and user_version are used to set or get |
| 1698 ** the value of the schema-version and user-version, respectively. Both |
| 1699 ** the schema-version and the user-version are 32-bit signed integers |
| 1700 ** stored in the database header. |
| 1701 ** |
| 1702 ** The schema-cookie is usually only manipulated internally by SQLite. It |
| 1703 ** is incremented by SQLite whenever the database schema is modified (by |
| 1704 ** creating or dropping a table or index). The schema version is used by |
| 1705 ** SQLite each time a query is executed to ensure that the internal cache |
| 1706 ** of the schema used when compiling the SQL query matches the schema of |
| 1707 ** the database against which the compiled query is actually executed. |
| 1708 ** Subverting this mechanism by using "PRAGMA schema_version" to modify |
| 1709 ** the schema-version is potentially dangerous and may lead to program |
| 1710 ** crashes or database corruption. Use with caution! |
| 1711 ** |
| 1712 ** The user-version is not used internally by SQLite. It may be used by |
| 1713 ** applications for any purpose. |
| 1714 */ |
| 1715 case PragTyp_HEADER_VALUE: { |
| 1716 int iCookie = pPragma->iArg; /* Which cookie to read or write */ |
| 1717 sqlite3VdbeUsesBtree(v, iDb); |
| 1718 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){ |
| 1719 /* Write the specified cookie value */ |
| 1720 static const VdbeOpList setCookie[] = { |
| 1721 { OP_Transaction, 0, 1, 0}, /* 0 */ |
| 1722 { OP_SetCookie, 0, 0, 0}, /* 1 */ |
| 1723 }; |
| 1724 VdbeOp *aOp; |
| 1725 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie)); |
| 1726 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0); |
| 1727 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
| 1728 aOp[0].p1 = iDb; |
| 1729 aOp[1].p1 = iDb; |
| 1730 aOp[1].p2 = iCookie; |
| 1731 aOp[1].p3 = sqlite3Atoi(zRight); |
| 1732 }else{ |
| 1733 /* Read the specified cookie value */ |
| 1734 static const VdbeOpList readCookie[] = { |
| 1735 { OP_Transaction, 0, 0, 0}, /* 0 */ |
| 1736 { OP_ReadCookie, 0, 1, 0}, /* 1 */ |
| 1737 { OP_ResultRow, 1, 1, 0} |
| 1738 }; |
| 1739 VdbeOp *aOp; |
| 1740 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie)); |
| 1741 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0); |
| 1742 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
| 1743 aOp[0].p1 = iDb; |
| 1744 aOp[1].p1 = iDb; |
| 1745 aOp[1].p3 = iCookie; |
| 1746 sqlite3VdbeReusable(v); |
| 1747 } |
| 1748 } |
| 1749 break; |
| 1750 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ |
| 1751 |
| 1752 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 1753 /* |
| 1754 ** PRAGMA compile_options |
| 1755 ** |
| 1756 ** Return the names of all compile-time options used in this build, |
| 1757 ** one option per row. |
| 1758 */ |
| 1759 case PragTyp_COMPILE_OPTIONS: { |
| 1760 int i = 0; |
| 1761 const char *zOpt; |
| 1762 pParse->nMem = 1; |
| 1763 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ |
| 1764 sqlite3VdbeLoadString(v, 1, zOpt); |
| 1765 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 1766 } |
| 1767 sqlite3VdbeReusable(v); |
| 1768 } |
| 1769 break; |
| 1770 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 1771 |
| 1772 #ifndef SQLITE_OMIT_WAL |
| 1773 /* |
| 1774 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate |
| 1775 ** |
| 1776 ** Checkpoint the database. |
| 1777 */ |
| 1778 case PragTyp_WAL_CHECKPOINT: { |
| 1779 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED); |
| 1780 int eMode = SQLITE_CHECKPOINT_PASSIVE; |
| 1781 if( zRight ){ |
| 1782 if( sqlite3StrICmp(zRight, "full")==0 ){ |
| 1783 eMode = SQLITE_CHECKPOINT_FULL; |
| 1784 }else if( sqlite3StrICmp(zRight, "restart")==0 ){ |
| 1785 eMode = SQLITE_CHECKPOINT_RESTART; |
| 1786 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){ |
| 1787 eMode = SQLITE_CHECKPOINT_TRUNCATE; |
| 1788 } |
| 1789 } |
| 1790 pParse->nMem = 3; |
| 1791 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1); |
| 1792 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); |
| 1793 } |
| 1794 break; |
| 1795 |
| 1796 /* |
| 1797 ** PRAGMA wal_autocheckpoint |
| 1798 ** PRAGMA wal_autocheckpoint = N |
| 1799 ** |
| 1800 ** Configure a database connection to automatically checkpoint a database |
| 1801 ** after accumulating N frames in the log. Or query for the current value |
| 1802 ** of N. |
| 1803 */ |
| 1804 case PragTyp_WAL_AUTOCHECKPOINT: { |
| 1805 if( zRight ){ |
| 1806 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); |
| 1807 } |
| 1808 returnSingleInt(v, |
| 1809 db->xWalCallback==sqlite3WalDefaultHook ? |
| 1810 SQLITE_PTR_TO_INT(db->pWalArg) : 0); |
| 1811 } |
| 1812 break; |
| 1813 #endif |
| 1814 |
| 1815 /* |
| 1816 ** PRAGMA shrink_memory |
| 1817 ** |
| 1818 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database |
| 1819 ** connection on which it is invoked to free up as much memory as it |
| 1820 ** can, by calling sqlite3_db_release_memory(). |
| 1821 */ |
| 1822 case PragTyp_SHRINK_MEMORY: { |
| 1823 sqlite3_db_release_memory(db); |
| 1824 break; |
| 1825 } |
| 1826 |
| 1827 /* |
| 1828 ** PRAGMA busy_timeout |
| 1829 ** PRAGMA busy_timeout = N |
| 1830 ** |
| 1831 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value |
| 1832 ** if one is set. If no busy handler or a different busy handler is set |
| 1833 ** then 0 is returned. Setting the busy_timeout to 0 or negative |
| 1834 ** disables the timeout. |
| 1835 */ |
| 1836 /*case PragTyp_BUSY_TIMEOUT*/ default: { |
| 1837 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT ); |
| 1838 if( zRight ){ |
| 1839 sqlite3_busy_timeout(db, sqlite3Atoi(zRight)); |
| 1840 } |
| 1841 returnSingleInt(v, db->busyTimeout); |
| 1842 break; |
| 1843 } |
| 1844 |
| 1845 /* |
| 1846 ** PRAGMA soft_heap_limit |
| 1847 ** PRAGMA soft_heap_limit = N |
| 1848 ** |
| 1849 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the |
| 1850 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is |
| 1851 ** specified and is a non-negative integer. |
| 1852 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always |
| 1853 ** returns the same integer that would be returned by the |
| 1854 ** sqlite3_soft_heap_limit64(-1) C-language function. |
| 1855 */ |
| 1856 case PragTyp_SOFT_HEAP_LIMIT: { |
| 1857 sqlite3_int64 N; |
| 1858 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ |
| 1859 sqlite3_soft_heap_limit64(N); |
| 1860 } |
| 1861 returnSingleInt(v, sqlite3_soft_heap_limit64(-1)); |
| 1862 break; |
| 1863 } |
| 1864 |
| 1865 /* |
| 1866 ** PRAGMA threads |
| 1867 ** PRAGMA threads = N |
| 1868 ** |
| 1869 ** Configure the maximum number of worker threads. Return the new |
| 1870 ** maximum, which might be less than requested. |
| 1871 */ |
| 1872 case PragTyp_THREADS: { |
| 1873 sqlite3_int64 N; |
| 1874 if( zRight |
| 1875 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK |
| 1876 && N>=0 |
| 1877 ){ |
| 1878 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); |
| 1879 } |
| 1880 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); |
| 1881 break; |
| 1882 } |
| 1883 |
| 1884 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 1885 /* |
| 1886 ** Report the current state of file logs for all databases |
| 1887 */ |
| 1888 case PragTyp_LOCK_STATUS: { |
| 1889 static const char *const azLockName[] = { |
| 1890 "unlocked", "shared", "reserved", "pending", "exclusive" |
| 1891 }; |
| 1892 int i; |
| 1893 pParse->nMem = 2; |
| 1894 for(i=0; i<db->nDb; i++){ |
| 1895 Btree *pBt; |
| 1896 const char *zState = "unknown"; |
| 1897 int j; |
| 1898 if( db->aDb[i].zDbSName==0 ) continue; |
| 1899 pBt = db->aDb[i].pBt; |
| 1900 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){ |
| 1901 zState = "closed"; |
| 1902 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0, |
| 1903 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ |
| 1904 zState = azLockName[j]; |
| 1905 } |
| 1906 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState); |
| 1907 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); |
| 1908 } |
| 1909 break; |
| 1910 } |
| 1911 #endif |
| 1912 |
| 1913 #ifdef SQLITE_HAS_CODEC |
| 1914 case PragTyp_KEY: { |
| 1915 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 1916 break; |
| 1917 } |
| 1918 case PragTyp_REKEY: { |
| 1919 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight)); |
| 1920 break; |
| 1921 } |
| 1922 case PragTyp_HEXKEY: { |
| 1923 if( zRight ){ |
| 1924 u8 iByte; |
| 1925 int i; |
| 1926 char zKey[40]; |
| 1927 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){ |
| 1928 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]); |
| 1929 if( (i&1)!=0 ) zKey[i/2] = iByte; |
| 1930 } |
| 1931 if( (zLeft[3] & 0xf)==0xb ){ |
| 1932 sqlite3_key_v2(db, zDb, zKey, i/2); |
| 1933 }else{ |
| 1934 sqlite3_rekey_v2(db, zDb, zKey, i/2); |
| 1935 } |
| 1936 } |
| 1937 break; |
| 1938 } |
| 1939 #endif |
| 1940 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD) |
| 1941 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){ |
| 1942 #ifdef SQLITE_HAS_CODEC |
| 1943 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){ |
| 1944 sqlite3_activate_see(&zRight[4]); |
| 1945 } |
| 1946 #endif |
| 1947 #ifdef SQLITE_ENABLE_CEROD |
| 1948 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ |
| 1949 sqlite3_activate_cerod(&zRight[6]); |
| 1950 } |
| 1951 #endif |
| 1952 } |
| 1953 break; |
| 1954 #endif |
| 1955 |
| 1956 } /* End of the PRAGMA switch */ |
| 1957 |
| 1958 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only |
| 1959 ** purpose is to execute assert() statements to verify that if the |
| 1960 ** PragFlg_NoColumns1 flag is set and the caller specified an argument |
| 1961 ** to the PRAGMA, the implementation has not added any OP_ResultRow |
| 1962 ** instructions to the VM. */ |
| 1963 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){ |
| 1964 sqlite3VdbeVerifyNoResultRow(v); |
| 1965 } |
| 1966 |
| 1967 pragma_out: |
| 1968 sqlite3DbFree(db, zLeft); |
| 1969 sqlite3DbFree(db, zRight); |
| 1970 } |
| 1971 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1972 /***************************************************************************** |
| 1973 ** Implementation of an eponymous virtual table that runs a pragma. |
| 1974 ** |
| 1975 */ |
| 1976 typedef struct PragmaVtab PragmaVtab; |
| 1977 typedef struct PragmaVtabCursor PragmaVtabCursor; |
| 1978 struct PragmaVtab { |
| 1979 sqlite3_vtab base; /* Base class. Must be first */ |
| 1980 sqlite3 *db; /* The database connection to which it belongs */ |
| 1981 const PragmaName *pName; /* Name of the pragma */ |
| 1982 u8 nHidden; /* Number of hidden columns */ |
| 1983 u8 iHidden; /* Index of the first hidden column */ |
| 1984 }; |
| 1985 struct PragmaVtabCursor { |
| 1986 sqlite3_vtab_cursor base; /* Base class. Must be first */ |
| 1987 sqlite3_stmt *pPragma; /* The pragma statement to run */ |
| 1988 sqlite_int64 iRowid; /* Current rowid */ |
| 1989 char *azArg[2]; /* Value of the argument and schema */ |
| 1990 }; |
| 1991 |
| 1992 /* |
| 1993 ** Pragma virtual table module xConnect method. |
| 1994 */ |
| 1995 static int pragmaVtabConnect( |
| 1996 sqlite3 *db, |
| 1997 void *pAux, |
| 1998 int argc, const char *const*argv, |
| 1999 sqlite3_vtab **ppVtab, |
| 2000 char **pzErr |
| 2001 ){ |
| 2002 const PragmaName *pPragma = (const PragmaName*)pAux; |
| 2003 PragmaVtab *pTab = 0; |
| 2004 int rc; |
| 2005 int i, j; |
| 2006 char cSep = '('; |
| 2007 StrAccum acc; |
| 2008 char zBuf[200]; |
| 2009 |
| 2010 UNUSED_PARAMETER(argc); |
| 2011 UNUSED_PARAMETER(argv); |
| 2012 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 2013 sqlite3StrAccumAppendAll(&acc, "CREATE TABLE x"); |
| 2014 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){ |
| 2015 sqlite3XPrintf(&acc, "%c\"%s\"", cSep, pragCName[j]); |
| 2016 cSep = ','; |
| 2017 } |
| 2018 if( i==0 ){ |
| 2019 sqlite3XPrintf(&acc, "(\"%s\"", pPragma->zName); |
| 2020 cSep = ','; |
| 2021 i++; |
| 2022 } |
| 2023 j = 0; |
| 2024 if( pPragma->mPragFlg & PragFlg_Result1 ){ |
| 2025 sqlite3StrAccumAppendAll(&acc, ",arg HIDDEN"); |
| 2026 j++; |
| 2027 } |
| 2028 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){ |
| 2029 sqlite3StrAccumAppendAll(&acc, ",schema HIDDEN"); |
| 2030 j++; |
| 2031 } |
| 2032 sqlite3StrAccumAppend(&acc, ")", 1); |
| 2033 sqlite3StrAccumFinish(&acc); |
| 2034 assert( strlen(zBuf) < sizeof(zBuf)-1 ); |
| 2035 rc = sqlite3_declare_vtab(db, zBuf); |
| 2036 if( rc==SQLITE_OK ){ |
| 2037 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab)); |
| 2038 if( pTab==0 ){ |
| 2039 rc = SQLITE_NOMEM; |
| 2040 }else{ |
| 2041 memset(pTab, 0, sizeof(PragmaVtab)); |
| 2042 pTab->pName = pPragma; |
| 2043 pTab->db = db; |
| 2044 pTab->iHidden = i; |
| 2045 pTab->nHidden = j; |
| 2046 } |
| 2047 }else{ |
| 2048 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
| 2049 } |
| 2050 |
| 2051 *ppVtab = (sqlite3_vtab*)pTab; |
| 2052 return rc; |
| 2053 } |
| 2054 |
| 2055 /* |
| 2056 ** Pragma virtual table module xDisconnect method. |
| 2057 */ |
| 2058 static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){ |
| 2059 PragmaVtab *pTab = (PragmaVtab*)pVtab; |
| 2060 sqlite3_free(pTab); |
| 2061 return SQLITE_OK; |
| 2062 } |
| 2063 |
| 2064 /* Figure out the best index to use to search a pragma virtual table. |
| 2065 ** |
| 2066 ** There are not really any index choices. But we want to encourage the |
| 2067 ** query planner to give == constraints on as many hidden parameters as |
| 2068 ** possible, and especially on the first hidden parameter. So return a |
| 2069 ** high cost if hidden parameters are unconstrained. |
| 2070 */ |
| 2071 static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 2072 PragmaVtab *pTab = (PragmaVtab*)tab; |
| 2073 const struct sqlite3_index_constraint *pConstraint; |
| 2074 int i, j; |
| 2075 int seen[2]; |
| 2076 |
| 2077 pIdxInfo->estimatedCost = (double)1; |
| 2078 if( pTab->nHidden==0 ){ return SQLITE_OK; } |
| 2079 pConstraint = pIdxInfo->aConstraint; |
| 2080 seen[0] = 0; |
| 2081 seen[1] = 0; |
| 2082 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 2083 if( pConstraint->usable==0 ) continue; |
| 2084 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 2085 if( pConstraint->iColumn < pTab->iHidden ) continue; |
| 2086 j = pConstraint->iColumn - pTab->iHidden; |
| 2087 assert( j < 2 ); |
| 2088 seen[j] = i+1; |
| 2089 } |
| 2090 if( seen[0]==0 ){ |
| 2091 pIdxInfo->estimatedCost = (double)2147483647; |
| 2092 pIdxInfo->estimatedRows = 2147483647; |
| 2093 return SQLITE_OK; |
| 2094 } |
| 2095 j = seen[0]-1; |
| 2096 pIdxInfo->aConstraintUsage[j].argvIndex = 1; |
| 2097 pIdxInfo->aConstraintUsage[j].omit = 1; |
| 2098 if( seen[1]==0 ) return SQLITE_OK; |
| 2099 pIdxInfo->estimatedCost = (double)20; |
| 2100 pIdxInfo->estimatedRows = 20; |
| 2101 j = seen[1]-1; |
| 2102 pIdxInfo->aConstraintUsage[j].argvIndex = 2; |
| 2103 pIdxInfo->aConstraintUsage[j].omit = 1; |
| 2104 return SQLITE_OK; |
| 2105 } |
| 2106 |
| 2107 /* Create a new cursor for the pragma virtual table */ |
| 2108 static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){ |
| 2109 PragmaVtabCursor *pCsr; |
| 2110 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr)); |
| 2111 if( pCsr==0 ) return SQLITE_NOMEM; |
| 2112 memset(pCsr, 0, sizeof(PragmaVtabCursor)); |
| 2113 pCsr->base.pVtab = pVtab; |
| 2114 *ppCursor = &pCsr->base; |
| 2115 return SQLITE_OK; |
| 2116 } |
| 2117 |
| 2118 /* Clear all content from pragma virtual table cursor. */ |
| 2119 static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){ |
| 2120 int i; |
| 2121 sqlite3_finalize(pCsr->pPragma); |
| 2122 pCsr->pPragma = 0; |
| 2123 for(i=0; i<ArraySize(pCsr->azArg); i++){ |
| 2124 sqlite3_free(pCsr->azArg[i]); |
| 2125 pCsr->azArg[i] = 0; |
| 2126 } |
| 2127 } |
| 2128 |
| 2129 /* Close a pragma virtual table cursor */ |
| 2130 static int pragmaVtabClose(sqlite3_vtab_cursor *cur){ |
| 2131 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur; |
| 2132 pragmaVtabCursorClear(pCsr); |
| 2133 sqlite3_free(pCsr); |
| 2134 return SQLITE_OK; |
| 2135 } |
| 2136 |
| 2137 /* Advance the pragma virtual table cursor to the next row */ |
| 2138 static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){ |
| 2139 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2140 int rc = SQLITE_OK; |
| 2141 |
| 2142 /* Increment the xRowid value */ |
| 2143 pCsr->iRowid++; |
| 2144 assert( pCsr->pPragma ); |
| 2145 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){ |
| 2146 rc = sqlite3_finalize(pCsr->pPragma); |
| 2147 pCsr->pPragma = 0; |
| 2148 pragmaVtabCursorClear(pCsr); |
| 2149 } |
| 2150 return rc; |
| 2151 } |
| 2152 |
| 2153 /* |
| 2154 ** Pragma virtual table module xFilter method. |
| 2155 */ |
| 2156 static int pragmaVtabFilter( |
| 2157 sqlite3_vtab_cursor *pVtabCursor, |
| 2158 int idxNum, const char *idxStr, |
| 2159 int argc, sqlite3_value **argv |
| 2160 ){ |
| 2161 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2162 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); |
| 2163 int rc; |
| 2164 int i, j; |
| 2165 StrAccum acc; |
| 2166 char *zSql; |
| 2167 |
| 2168 UNUSED_PARAMETER(idxNum); |
| 2169 UNUSED_PARAMETER(idxStr); |
| 2170 pragmaVtabCursorClear(pCsr); |
| 2171 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1; |
| 2172 for(i=0; i<argc; i++, j++){ |
| 2173 assert( j<ArraySize(pCsr->azArg) ); |
| 2174 pCsr->azArg[j] = sqlite3_mprintf("%s", sqlite3_value_text(argv[i])); |
| 2175 if( pCsr->azArg[j]==0 ){ |
| 2176 return SQLITE_NOMEM; |
| 2177 } |
| 2178 } |
| 2179 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]); |
| 2180 sqlite3StrAccumAppendAll(&acc, "PRAGMA "); |
| 2181 if( pCsr->azArg[1] ){ |
| 2182 sqlite3XPrintf(&acc, "%Q.", pCsr->azArg[1]); |
| 2183 } |
| 2184 sqlite3StrAccumAppendAll(&acc, pTab->pName->zName); |
| 2185 if( pCsr->azArg[0] ){ |
| 2186 sqlite3XPrintf(&acc, "=%Q", pCsr->azArg[0]); |
| 2187 } |
| 2188 zSql = sqlite3StrAccumFinish(&acc); |
| 2189 if( zSql==0 ) return SQLITE_NOMEM; |
| 2190 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0); |
| 2191 sqlite3_free(zSql); |
| 2192 if( rc!=SQLITE_OK ){ |
| 2193 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); |
| 2194 return rc; |
| 2195 } |
| 2196 return pragmaVtabNext(pVtabCursor); |
| 2197 } |
| 2198 |
| 2199 /* |
| 2200 ** Pragma virtual table module xEof method. |
| 2201 */ |
| 2202 static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){ |
| 2203 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2204 return (pCsr->pPragma==0); |
| 2205 } |
| 2206 |
| 2207 /* The xColumn method simply returns the corresponding column from |
| 2208 ** the PRAGMA. |
| 2209 */ |
| 2210 static int pragmaVtabColumn( |
| 2211 sqlite3_vtab_cursor *pVtabCursor, |
| 2212 sqlite3_context *ctx, |
| 2213 int i |
| 2214 ){ |
| 2215 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2216 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); |
| 2217 if( i<pTab->iHidden ){ |
| 2218 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i)); |
| 2219 }else{ |
| 2220 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT); |
| 2221 } |
| 2222 return SQLITE_OK; |
| 2223 } |
| 2224 |
| 2225 /* |
| 2226 ** Pragma virtual table module xRowid method. |
| 2227 */ |
| 2228 static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){ |
| 2229 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2230 *p = pCsr->iRowid; |
| 2231 return SQLITE_OK; |
| 2232 } |
| 2233 |
| 2234 /* The pragma virtual table object */ |
| 2235 static const sqlite3_module pragmaVtabModule = { |
| 2236 0, /* iVersion */ |
| 2237 0, /* xCreate - create a table */ |
| 2238 pragmaVtabConnect, /* xConnect - connect to an existing table */ |
| 2239 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */ |
| 2240 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */ |
| 2241 0, /* xDestroy - Drop a table */ |
| 2242 pragmaVtabOpen, /* xOpen - open a cursor */ |
| 2243 pragmaVtabClose, /* xClose - close a cursor */ |
| 2244 pragmaVtabFilter, /* xFilter - configure scan constraints */ |
| 2245 pragmaVtabNext, /* xNext - advance a cursor */ |
| 2246 pragmaVtabEof, /* xEof */ |
| 2247 pragmaVtabColumn, /* xColumn - read data */ |
| 2248 pragmaVtabRowid, /* xRowid - read data */ |
| 2249 0, /* xUpdate - write data */ |
| 2250 0, /* xBegin - begin transaction */ |
| 2251 0, /* xSync - sync transaction */ |
| 2252 0, /* xCommit - commit transaction */ |
| 2253 0, /* xRollback - rollback transaction */ |
| 2254 0, /* xFindFunction - function overloading */ |
| 2255 0, /* xRename - rename the table */ |
| 2256 0, /* xSavepoint */ |
| 2257 0, /* xRelease */ |
| 2258 0 /* xRollbackTo */ |
| 2259 }; |
| 2260 |
| 2261 /* |
| 2262 ** Check to see if zTabName is really the name of a pragma. If it is, |
| 2263 ** then register an eponymous virtual table for that pragma and return |
| 2264 ** a pointer to the Module object for the new virtual table. |
| 2265 */ |
| 2266 Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){ |
| 2267 const PragmaName *pName; |
| 2268 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 ); |
| 2269 pName = pragmaLocate(zName+7); |
| 2270 if( pName==0 ) return 0; |
| 2271 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0; |
| 2272 assert( sqlite3HashFind(&db->aModule, zName)==0 ); |
| 2273 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0); |
| 2274 } |
| 2275 |
| 2276 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2277 |
| 2278 #endif /* SQLITE_OMIT_PRAGMA */ |
OLD | NEW |