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 ** $Id: pragma.c,v 1.42 2004/06/10 10:50:25 danielk1977 Exp $ 15 */ 16 #include "sqliteInt.h" 17 #include <ctype.h> 18 19 #ifdef SQLITE_DEBUG 20 # include "pager.h" 21 # include "btree.h" 22 #endif 23 24 /* 25 ** Interpret the given string as a boolean value. 26 */ 27 static int getBoolean(const char *z){ 28 static char *azTrue[] = { "yes", "on", "true" }; 29 int i; 30 if( z[0]==0 ) return 0; 31 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ 32 return atoi(z); 33 } 34 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){ 35 if( sqlite3StrICmp(z,azTrue[i])==0 ) return 1; 36 } 37 return 0; 38 } 39 40 /* 41 ** Interpret the given string as a safety level. Return 0 for OFF, 42 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or 43 ** unrecognized string argument. 44 ** 45 ** Note that the values returned are one less that the values that 46 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done 47 ** to support legacy SQL code. The safety level used to be boolean 48 ** and older scripts may have used numbers 0 for OFF and 1 for ON. 49 */ 50 static int getSafetyLevel(char *z){ 51 static const struct { 52 const char *zWord; 53 int val; 54 } aKey[] = { 55 { "no", 0 }, 56 { "off", 0 }, 57 { "false", 0 }, 58 { "yes", 1 }, 59 { "on", 1 }, 60 { "true", 1 }, 61 { "full", 2 }, 62 }; 63 int i; 64 if( z[0]==0 ) return 1; 65 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){ 66 return atoi(z); 67 } 68 for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){ 69 if( sqlite3StrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val; 70 } 71 return 1; 72 } 73 74 /* 75 ** Interpret the given string as a temp db location. Return 1 for file 76 ** backed temporary databases, 2 for the Red-Black tree in memory database 77 ** and 0 to use the compile-time default. 78 */ 79 static int getTempStore(const char *z){ 80 if( z[0]>='0' && z[0]<='2' ){ 81 return z[0] - '0'; 82 }else if( sqlite3StrICmp(z, "file")==0 ){ 83 return 1; 84 }else if( sqlite3StrICmp(z, "memory")==0 ){ 85 return 2; 86 }else{ 87 return 0; 88 } 89 } 90 91 /* 92 ** If the TEMP database is open, close it and mark the database schema 93 ** as needing reloading. This must be done when using the TEMP_STORE 94 ** or DEFAULT_TEMP_STORE pragmas. 95 */ 96 static int changeTempStorage(Parse *pParse, const char *zStorageType){ 97 int ts = getTempStore(zStorageType); 98 sqlite *db = pParse->db; 99 if( db->temp_store==ts ) return SQLITE_OK; 100 if( db->aDb[1].pBt!=0 ){ 101 if( !db->autoCommit ){ 102 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " 103 "from within a transaction"); 104 return SQLITE_ERROR; 105 } 106 sqlite3BtreeClose(db->aDb[1].pBt); 107 db->aDb[1].pBt = 0; 108 sqlite3ResetInternalSchema(db, 0); 109 } 110 db->temp_store = ts; 111 return SQLITE_OK; 112 } 113 114 /* 115 ** Check to see if zRight and zLeft refer to a pragma that queries 116 ** or changes one of the flags in db->flags. Return 1 if so and 0 if not. 117 ** Also, implement the pragma. 118 */ 119 static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){ 120 static const struct { 121 const char *zName; /* Name of the pragma */ 122 int mask; /* Mask for the db->flags value */ 123 } aPragma[] = { 124 { "vdbe_trace", SQLITE_VdbeTrace }, 125 { "sql_trace", SQLITE_SqlTrace }, 126 { "vdbe_listing", SQLITE_VdbeListing }, 127 { "full_column_names", SQLITE_FullColNames }, 128 { "short_column_names", SQLITE_ShortColNames }, 129 { "count_changes", SQLITE_CountRows }, 130 { "empty_result_callbacks", SQLITE_NullCallback }, 131 }; 132 int i; 133 for(i=0; i<sizeof(aPragma)/sizeof(aPragma[0]); i++){ 134 if( sqlite3StrICmp(zLeft, aPragma[i].zName)==0 ){ 135 sqlite *db = pParse->db; 136 Vdbe *v; 137 if( strcmp(zLeft,zRight)==0 && (v = sqlite3GetVdbe(pParse))!=0 ){ 138 sqlite3VdbeSetNumCols(v, 1); 139 sqlite3VdbeSetColName(v, 0, aPragma[i].zName, P3_STATIC); 140 sqlite3VdbeCode(v, OP_Integer, (db->flags & aPragma[i].mask)!=0, 0, 141 OP_Callback, 1, 0, 0); 142 }else if( getBoolean(zRight) ){ 143 db->flags |= aPragma[i].mask; 144 }else{ 145 db->flags &= ~aPragma[i].mask; 146 } 147 return 1; 148 } 149 } 150 return 0; 151 } 152 153 /* 154 ** Process a pragma statement. 155 ** 156 ** Pragmas are of this form: 157 ** 158 ** PRAGMA id = value 159 ** 160 ** The identifier might also be a string. The value is a string, and 161 ** identifier, or a number. If minusFlag is true, then the value is 162 ** a number that was preceded by a minus sign. 163 */ 164 void sqlite3Pragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){ 165 char *zLeft = 0; 166 char *zRight = 0; 167 sqlite *db = pParse->db; 168 Vdbe *v = sqlite3GetVdbe(pParse); 169 if( v==0 ) return; 170 171 zLeft = sqliteStrNDup(pLeft->z, pLeft->n); 172 sqlite3Dequote(zLeft); 173 if( minusFlag ){ 174 zRight = 0; 175 sqlite3SetNString(&zRight, "-", 1, pRight->z, pRight->n, 0); 176 }else{ 177 zRight = sqliteStrNDup(pRight->z, pRight->n); 178 sqlite3Dequote(zRight); 179 } 180 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, 0) ){ 181 sqliteFree(zLeft); 182 sqliteFree(zRight); 183 return; 184 } 185 186 /* 187 ** PRAGMA default_cache_size 188 ** PRAGMA default_cache_size=N 189 ** 190 ** The first form reports the current persistent setting for the 191 ** page cache size. The value returned is the maximum number of 192 ** pages in the page cache. The second form sets both the current 193 ** page cache size value and the persistent page cache size value 194 ** stored in the database file. 195 ** 196 ** The default cache size is stored in meta-value 2 of page 1 of the 197 ** database file. The cache size is actually the absolute value of 198 ** this memory location. The sign of meta-value 2 determines the 199 ** synchronous setting. A negative value means synchronous is off 200 ** and a positive value means synchronous is on. 201 */ 202 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){ 203 static VdbeOpList getCacheSize[] = { 204 { OP_ReadCookie, 0, 2, 0}, 205 { OP_AbsValue, 0, 0, 0}, 206 { OP_Dup, 0, 0, 0}, 207 { OP_Integer, 0, 0, 0}, 208 { OP_Ne, 0, 6, 0}, 209 { OP_Integer, 0, 0, 0}, /* 5 */ 210 { OP_Callback, 1, 0, 0}, 211 }; 212 int addr; 213 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 214 pParse->nErr++; 215 return; 216 } 217 if( pRight->z==pLeft->z ){ 218 sqlite3VdbeSetNumCols(v, 1); 219 sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC); 220 addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); 221 sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES); 222 }else{ 223 int size = atoi(zRight); 224 if( size<0 ) size = -size; 225 sqlite3BeginWriteOperation(pParse, 0, 0); 226 sqlite3VdbeAddOp(v, OP_Integer, size, 0); 227 sqlite3VdbeAddOp(v, OP_ReadCookie, 0, 2); 228 addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 229 sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3); 230 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 231 sqlite3VdbeAddOp(v, OP_SetCookie, 0, 2); 232 sqlite3EndWriteOperation(pParse); 233 db->cache_size = db->cache_size<0 ? -size : size; 234 sqlite3BtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); 235 } 236 }else 237 238 /* 239 ** PRAGMA cache_size 240 ** PRAGMA cache_size=N 241 ** 242 ** The first form reports the current local setting for the 243 ** page cache size. The local setting can be different from 244 ** the persistent cache size value that is stored in the database 245 ** file itself. The value returned is the maximum number of 246 ** pages in the page cache. The second form sets the local 247 ** page cache size value. It does not change the persistent 248 ** cache size stored on the disk so the cache size will revert 249 ** to its default value when the database is closed and reopened. 250 ** N should be a positive integer. 251 */ 252 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){ 253 static VdbeOpList getCacheSize[] = { 254 { OP_Callback, 1, 0, 0}, 255 }; 256 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 257 pParse->nErr++; 258 return; 259 } 260 if( pRight->z==pLeft->z ){ 261 int size = db->cache_size;; 262 if( size<0 ) size = -size; 263 sqlite3VdbeAddOp(v, OP_Integer, size, 0); 264 sqlite3VdbeSetNumCols(v, 1); 265 sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC); 266 sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); 267 }else{ 268 int size = atoi(zRight); 269 if( size<0 ) size = -size; 270 if( db->cache_size<0 ) size = -size; 271 db->cache_size = size; 272 sqlite3BtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); 273 } 274 }else 275 276 /* 277 ** PRAGMA default_synchronous 278 ** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL 279 ** 280 ** The first form returns the persistent value of the "synchronous" setting 281 ** that is stored in the database. This is the synchronous setting that 282 ** is used whenever the database is opened unless overridden by a separate 283 ** "synchronous" pragma. The second form changes the persistent and the 284 ** local synchronous setting to the value given. 285 ** 286 ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls 287 ** to make sure data is committed to disk. Write operations are very fast, 288 ** but a power failure can leave the database in an inconsistent state. 289 ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to 290 ** make sure data is being written to disk. The risk of corruption due to 291 ** a power loss in this mode is negligible but non-zero. If synchronous 292 ** is FULL, extra fsync()s occur to reduce the risk of corruption to near 293 ** zero, but with a write performance penalty. The default mode is NORMAL. 294 */ 295 if( sqlite3StrICmp(zLeft,"default_synchronous")==0 ){ 296 static VdbeOpList getSync[] = { 297 { OP_ReadCookie, 0, 3, 0}, 298 { OP_Dup, 0, 0, 0}, 299 { OP_If, 0, 0, 0}, /* 2 */ 300 { OP_ReadCookie, 0, 2, 0}, 301 { OP_Integer, 0, 0, 0}, 302 { OP_Lt, 0, 5, 0}, 303 { OP_AddImm, 1, 0, 0}, 304 { OP_Callback, 1, 0, 0}, 305 { OP_Halt, 0, 0, 0}, 306 { OP_AddImm, -1, 0, 0}, /* 9 */ 307 { OP_Callback, 1, 0, 0} 308 }; 309 int addr; 310 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 311 pParse->nErr++; 312 return; 313 } 314 if( pRight->z==pLeft->z ){ 315 sqlite3VdbeSetNumCols(v, 1); 316 sqlite3VdbeSetColName(v, 0, "synchronous", P3_STATIC); 317 addr = sqlite3VdbeAddOpList(v, ArraySize(getSync), getSync); 318 sqlite3VdbeChangeP2(v, addr+2, addr+9); 319 }else{ 320 int size = db->cache_size; 321 if( size<0 ) size = -size; 322 sqlite3BeginWriteOperation(pParse, 0, 0); 323 sqlite3VdbeAddOp(v, OP_ReadCookie, 0, 2); 324 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 325 addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 326 sqlite3VdbeAddOp(v, OP_Ne, 0, addr+3); 327 sqlite3VdbeAddOp(v, OP_AddImm, MAX_PAGES, 0); 328 sqlite3VdbeAddOp(v, OP_AbsValue, 0, 0); 329 db->safety_level = getSafetyLevel(zRight)+1; 330 if( db->safety_level==1 ){ 331 sqlite3VdbeAddOp(v, OP_Negative, 0, 0); 332 size = -size; 333 } 334 sqlite3VdbeAddOp(v, OP_SetCookie, 0, 2); 335 sqlite3VdbeAddOp(v, OP_Integer, db->safety_level, 0); 336 sqlite3VdbeAddOp(v, OP_SetCookie, 0, 3); 337 sqlite3EndWriteOperation(pParse); 338 db->cache_size = size; 339 sqlite3BtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); 340 sqlite3BtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level); 341 } 342 }else 343 344 /* 345 ** PRAGMA synchronous 346 ** PRAGMA synchronous=OFF|ON|NORMAL|FULL 347 ** 348 ** Return or set the local value of the synchronous flag. Changing 349 ** the local value does not make changes to the disk file and the 350 ** default value will be restored the next time the database is 351 ** opened. 352 */ 353 if( sqlite3StrICmp(zLeft,"synchronous")==0 ){ 354 static VdbeOpList getSync[] = { 355 { OP_Callback, 1, 0, 0}, 356 }; 357 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 358 pParse->nErr++; 359 return; 360 } 361 if( pRight->z==pLeft->z ){ 362 sqlite3VdbeSetNumCols(v, 1); 363 sqlite3VdbeSetColName(v, 0, "synchronous", P3_STATIC); 364 sqlite3VdbeAddOp(v, OP_Integer, db->safety_level-1, 0); 365 sqlite3VdbeAddOpList(v, ArraySize(getSync), getSync); 366 }else{ 367 int size = db->cache_size; 368 if( size<0 ) size = -size; 369 db->safety_level = getSafetyLevel(zRight)+1; 370 if( db->safety_level==1 ) size = -size; 371 db->cache_size = size; 372 sqlite3BtreeSetCacheSize(db->aDb[0].pBt, db->cache_size); 373 sqlite3BtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level); 374 } 375 }else 376 377 #ifndef NDEBUG 378 if( sqlite3StrICmp(zLeft, "trigger_overhead_test")==0 ){ 379 if( getBoolean(zRight) ){ 380 always_code_trigger_setup = 1; 381 }else{ 382 always_code_trigger_setup = 0; 383 } 384 }else 385 #endif 386 387 if( flagPragma(pParse, zLeft, zRight) ){ 388 /* The flagPragma() call also generates any necessary code */ 389 }else 390 391 if( sqlite3StrICmp(zLeft, "table_info")==0 ){ 392 Table *pTab; 393 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 394 pParse->nErr++; 395 return; 396 } 397 pTab = sqlite3FindTable(db, zRight, 0); 398 if( pTab ){ 399 int i; 400 sqlite3VdbeSetNumCols(v, 6); 401 sqlite3VdbeSetColName(v, 0, "cid", P3_STATIC); 402 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC); 403 sqlite3VdbeSetColName(v, 2, "type", P3_STATIC); 404 sqlite3VdbeSetColName(v, 3, "notnull", P3_STATIC); 405 sqlite3VdbeSetColName(v, 4, "dflt_value", P3_STATIC); 406 sqlite3VdbeSetColName(v, 5, "pk", P3_STATIC); 407 sqlite3ViewGetColumnNames(pParse, pTab); 408 for(i=0; i<pTab->nCol; i++){ 409 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 410 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[i].zName, 0); 411 sqlite3VdbeOp3(v, OP_String8, 0, 0, 412 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", 0); 413 sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0); 414 sqlite3VdbeOp3(v, OP_String8, 0, 0, 415 pTab->aCol[i].zDflt, P3_STATIC); 416 sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].isPrimKey, 0); 417 sqlite3VdbeAddOp(v, OP_Callback, 6, 0); 418 } 419 } 420 }else 421 422 if( sqlite3StrICmp(zLeft, "index_info")==0 ){ 423 Index *pIdx; 424 Table *pTab; 425 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 426 pParse->nErr++; 427 return; 428 } 429 pIdx = sqlite3FindIndex(db, zRight, 0); 430 if( pIdx ){ 431 int i; 432 pTab = pIdx->pTable; 433 sqlite3VdbeSetNumCols(v, 3); 434 sqlite3VdbeSetColName(v, 0, "seqno", P3_STATIC); 435 sqlite3VdbeSetColName(v, 1, "cid", P3_STATIC); 436 sqlite3VdbeSetColName(v, 2, "name", P3_STATIC); 437 for(i=0; i<pIdx->nColumn; i++){ 438 int cnum = pIdx->aiColumn[i]; 439 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 440 sqlite3VdbeAddOp(v, OP_Integer, cnum, 0); 441 assert( pTab->nCol>cnum ); 442 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0); 443 sqlite3VdbeAddOp(v, OP_Callback, 3, 0); 444 } 445 } 446 }else 447 448 if( sqlite3StrICmp(zLeft, "index_list")==0 ){ 449 Index *pIdx; 450 Table *pTab; 451 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 452 pParse->nErr++; 453 return; 454 } 455 pTab = sqlite3FindTable(db, zRight, 0); 456 if( pTab ){ 457 v = sqlite3GetVdbe(pParse); 458 pIdx = pTab->pIndex; 459 } 460 if( pTab && pIdx ){ 461 int i = 0; 462 sqlite3VdbeSetNumCols(v, 3); 463 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC); 464 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC); 465 sqlite3VdbeSetColName(v, 2, "unique", P3_STATIC); 466 while(pIdx){ 467 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 468 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0); 469 sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0); 470 sqlite3VdbeAddOp(v, OP_Callback, 3, 0); 471 ++i; 472 pIdx = pIdx->pNext; 473 } 474 } 475 }else 476 477 if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 ){ 478 FKey *pFK; 479 Table *pTab; 480 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 481 pParse->nErr++; 482 return; 483 } 484 pTab = sqlite3FindTable(db, zRight, 0); 485 if( pTab ){ 486 v = sqlite3GetVdbe(pParse); 487 pFK = pTab->pFKey; 488 } 489 if( pTab && pFK ){ 490 int i = 0; 491 sqlite3VdbeSetNumCols(v, 5); 492 sqlite3VdbeSetColName(v, 0, "id", P3_STATIC); 493 sqlite3VdbeSetColName(v, 1, "seq", P3_STATIC); 494 sqlite3VdbeSetColName(v, 2, "table", P3_STATIC); 495 sqlite3VdbeSetColName(v, 3, "from", P3_STATIC); 496 sqlite3VdbeSetColName(v, 4, "to", P3_STATIC); 497 while(pFK){ 498 int j; 499 for(j=0; j<pFK->nCol; j++){ 500 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 501 sqlite3VdbeAddOp(v, OP_Integer, j, 0); 502 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0); 503 sqlite3VdbeOp3(v, OP_String8, 0, 0, 504 pTab->aCol[pFK->aCol[j].iFrom].zName, 0); 505 sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->aCol[j].zCol, 0); 506 sqlite3VdbeAddOp(v, OP_Callback, 5, 0); 507 } 508 ++i; 509 pFK = pFK->pNextFrom; 510 } 511 } 512 }else 513 514 if( sqlite3StrICmp(zLeft, "database_list")==0 ){ 515 int i; 516 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 517 pParse->nErr++; 518 return; 519 } 520 sqlite3VdbeSetNumCols(v, 3); 521 sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC); 522 sqlite3VdbeSetColName(v, 1, "name", P3_STATIC); 523 sqlite3VdbeSetColName(v, 2, "file", P3_STATIC); 524 for(i=0; i<db->nDb; i++){ 525 if( db->aDb[i].pBt==0 ) continue; 526 assert( db->aDb[i].zName!=0 ); 527 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 528 sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0); 529 sqlite3VdbeOp3(v, OP_String8, 0, 0, 530 sqlite3BtreeGetFilename(db->aDb[i].pBt), 0); 531 sqlite3VdbeAddOp(v, OP_Callback, 3, 0); 532 } 533 }else 534 535 536 /* 537 ** PRAGMA temp_store 538 ** PRAGMA temp_store = "default"|"memory"|"file" 539 ** 540 ** Return or set the local value of the temp_store flag. Changing 541 ** the local value does not make changes to the disk file and the default 542 ** value will be restored the next time the database is opened. 543 ** 544 ** Note that it is possible for the library compile-time options to 545 ** override this setting 546 */ 547 if( sqlite3StrICmp(zLeft, "temp_store")==0 ){ 548 static VdbeOpList getTmpDbLoc[] = { 549 { OP_Callback, 1, 0, 0}, 550 }; 551 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 552 pParse->nErr++; 553 return; 554 } 555 if( pRight->z==pLeft->z ){ 556 sqlite3VdbeAddOp(v, OP_Integer, db->temp_store, 0); 557 sqlite3VdbeSetNumCols(v, 1); 558 sqlite3VdbeSetColName(v, 0, "temp_store", P3_STATIC); 559 sqlite3VdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc); 560 }else{ 561 changeTempStorage(pParse, zRight); 562 } 563 }else 564 565 /* 566 ** PRAGMA default_temp_store 567 ** PRAGMA default_temp_store = "default"|"memory"|"file" 568 ** 569 ** Return or set the value of the persistent temp_store flag. Any 570 ** change does not take effect until the next time the database is 571 ** opened. 572 ** 573 ** Note that it is possible for the library compile-time options to 574 ** override this setting 575 */ 576 if( sqlite3StrICmp(zLeft, "default_temp_store")==0 ){ 577 static VdbeOpList getTmpDbLoc[] = { 578 { OP_ReadCookie, 0, 5, 0}, 579 { OP_Callback, 1, 0, 0}}; 580 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 581 pParse->nErr++; 582 return; 583 } 584 if( pRight->z==pLeft->z ){ 585 sqlite3VdbeSetNumCols(v, 1); 586 sqlite3VdbeSetColName(v, 0, "temp_store", P3_STATIC); 587 sqlite3VdbeAddOpList(v, ArraySize(getTmpDbLoc), getTmpDbLoc); 588 }else{ 589 sqlite3BeginWriteOperation(pParse, 0, 0); 590 sqlite3VdbeAddOp(v, OP_Integer, getTempStore(zRight), 0); 591 sqlite3VdbeAddOp(v, OP_SetCookie, 0, 5); 592 sqlite3EndWriteOperation(pParse); 593 } 594 }else 595 596 #ifndef NDEBUG 597 if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){ 598 extern void sqlite3ParserTrace(FILE*, char *); 599 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 600 pParse->nErr++; 601 return; 602 } 603 if( getBoolean(zRight) ){ 604 sqlite3ParserTrace(stdout, "parser: "); 605 }else{ 606 sqlite3ParserTrace(0, 0); 607 } 608 }else 609 #endif 610 611 if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){ 612 int i, j, addr; 613 614 /* Code that initializes the integrity check program. Set the 615 ** error count 0 616 */ 617 static VdbeOpList initCode[] = { 618 { OP_Integer, 0, 0, 0}, 619 { OP_MemStore, 0, 1, 0}, 620 }; 621 622 /* Code that appears at the end of the integrity check. If no error 623 ** messages have been generated, output OK. Otherwise output the 624 ** error message 625 */ 626 static VdbeOpList endCode[] = { 627 { OP_MemLoad, 0, 0, 0}, 628 { OP_Integer, 0, 0, 0}, 629 { OP_Ne, 0, 0, 0}, /* 2 */ 630 { OP_String8, 0, 0, "ok"}, 631 { OP_Callback, 1, 0, 0}, 632 }; 633 634 /* Initialize the VDBE program */ 635 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 636 pParse->nErr++; 637 return; 638 } 639 sqlite3VdbeSetNumCols(v, 1); 640 sqlite3VdbeSetColName(v, 0, "integrity_check", P3_STATIC); 641 sqlite3VdbeAddOpList(v, ArraySize(initCode), initCode); 642 643 /* Do an integrity check on each database file */ 644 for(i=0; i<db->nDb; i++){ 645 HashElem *x; 646 int cnt = 0; 647 648 sqlite3CodeVerifySchema(pParse, i); 649 650 /* Do an integrity check of the B-Tree 651 */ 652 for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){ 653 Table *pTab = sqliteHashData(x); 654 Index *pIdx; 655 sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0); 656 cnt++; 657 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 658 if( sqlite3CheckIndexCollSeq(pParse, pIdx) ) return; 659 sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0); 660 cnt++; 661 } 662 } 663 sqlite3VdbeAddOp(v, OP_IntegrityCk, cnt, i); 664 sqlite3VdbeAddOp(v, OP_Dup, 0, 1); 665 addr = sqlite3VdbeOp3(v, OP_String8, 0, 0, "ok", P3_STATIC); 666 sqlite3VdbeAddOp(v, OP_Eq, 0, addr+6); 667 sqlite3VdbeOp3(v, OP_String8, 0, 0, 668 sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName), 669 P3_DYNAMIC); 670 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 671 sqlite3VdbeAddOp(v, OP_Concat, 2, 1); 672 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); 673 674 /* Make sure all the indices are constructed correctly. 675 */ 676 sqlite3CodeVerifySchema(pParse, i); 677 for(x=sqliteHashFirst(&db->aDb[i].tblHash); x; x=sqliteHashNext(x)){ 678 Table *pTab = sqliteHashData(x); 679 Index *pIdx; 680 int loopTop; 681 682 if( pTab->pIndex==0 ) continue; 683 sqlite3VdbeAddOp(v, OP_Integer, i, 0); 684 sqlite3VdbeAddOp(v, OP_OpenRead, 1, pTab->tnum); 685 sqlite3VdbeAddOp(v, OP_SetNumColumns, 1, pTab->nCol); 686 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 687 if( pIdx->tnum==0 ) continue; 688 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); 689 sqlite3VdbeOp3(v, OP_OpenRead, j+2, pIdx->tnum, 690 (char*)&pIdx->keyInfo, P3_KEYINFO); 691 } 692 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 693 sqlite3VdbeAddOp(v, OP_MemStore, 1, 1); 694 loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0); 695 sqlite3VdbeAddOp(v, OP_MemIncr, 1, 0); 696 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 697 int jmp2; 698 static VdbeOpList idxErr[] = { 699 { OP_MemIncr, 0, 0, 0}, 700 { OP_String8, 0, 0, "rowid "}, 701 { OP_Recno, 1, 0, 0}, 702 { OP_String8, 0, 0, " missing from index "}, 703 { OP_String8, 0, 0, 0}, /* 4 */ 704 { OP_Concat, 4, 0, 0}, 705 { OP_Callback, 1, 0, 0}, 706 }; 707 sqlite3GenerateIndexKey(v, pIdx, 1); 708 jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0); 709 addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr); 710 sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC); 711 sqlite3VdbeChangeP2(v, jmp2, sqlite3VdbeCurrentAddr(v)); 712 } 713 sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1); 714 sqlite3VdbeChangeP2(v, loopTop, sqlite3VdbeCurrentAddr(v)); 715 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ 716 static VdbeOpList cntIdx[] = { 717 { OP_Integer, 0, 0, 0}, 718 { OP_MemStore, 2, 1, 0}, 719 { OP_Rewind, 0, 0, 0}, /* 2 */ 720 { OP_MemIncr, 2, 0, 0}, 721 { OP_Next, 0, 0, 0}, /* 4 */ 722 { OP_MemLoad, 1, 0, 0}, 723 { OP_MemLoad, 2, 0, 0}, 724 { OP_Eq, 0, 0, 0}, /* 7 */ 725 { OP_MemIncr, 0, 0, 0}, 726 { OP_String8, 0, 0, "wrong # of entries in index "}, 727 { OP_String8, 0, 0, 0}, /* 10 */ 728 { OP_Concat, 2, 0, 0}, 729 { OP_Callback, 1, 0, 0}, 730 }; 731 if( pIdx->tnum==0 ) continue; 732 addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx); 733 sqlite3VdbeChangeP1(v, addr+2, j+2); 734 sqlite3VdbeChangeP2(v, addr+2, addr+5); 735 sqlite3VdbeChangeP1(v, addr+4, j+2); 736 sqlite3VdbeChangeP2(v, addr+4, addr+3); 737 sqlite3VdbeChangeP2(v, addr+7, addr+ArraySize(cntIdx)); 738 sqlite3VdbeChangeP3(v, addr+10, pIdx->zName, P3_STATIC); 739 } 740 } 741 } 742 addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode); 743 sqlite3VdbeChangeP2(v, addr+2, addr+ArraySize(endCode)); 744 }else 745 /* 746 ** PRAGMA encoding 747 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" 748 ** 749 ** In it's first form, this pragma returns the encoding of the main 750 ** database. If the database is not initialized, it is initialized now. 751 ** 752 ** The second form of this pragma is a no-op if the main database file 753 ** has not already been initialized. In this case it sets the default 754 ** encoding that will be used for the main database file if a new file 755 ** is created. If an existing main database file is opened, then the 756 ** default text encoding for the existing database is used. 757 ** 758 ** In all cases new databases created using the ATTACH command are 759 ** created to use the same default text encoding as the main database. If 760 ** the main database has not been initialized and/or created when ATTACH 761 ** is executed, this is done before the ATTACH operation. 762 ** 763 ** In the second form this pragma sets the text encoding to be used in 764 ** new database files created using this database handle. It is only 765 ** useful if invoked immediately after the main database i 766 */ 767 if( sqlite3StrICmp(zLeft, "encoding")==0 ){ 768 struct EncName { 769 char *zName; 770 u8 enc; 771 } encnames[] = { 772 { "UTF-8", TEXT_Utf8 }, 773 { "UTF-16le", TEXT_Utf16le }, 774 { "UTF-16be", TEXT_Utf16be }, 775 { "UTF-16", TEXT_Utf16 }, 776 { "UTF8", TEXT_Utf8 }, 777 { "UTF16le", TEXT_Utf16le }, 778 { "UTF16be", TEXT_Utf16be }, 779 { "UTF16", TEXT_Utf16 }, 780 { 0, 0 } 781 }; 782 struct EncName *pEnc; 783 if( pRight->z==pLeft->z ){ /* "PRAGMA encoding" */ 784 if( SQLITE_OK!=sqlite3ReadSchema(pParse->db, &pParse->zErrMsg) ){ 785 pParse->nErr++; 786 return; 787 } 788 sqlite3VdbeSetNumCols(v, 1); 789 sqlite3VdbeSetColName(v, 0, "encoding", P3_STATIC); 790 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 791 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ 792 if( pEnc->enc==pParse->db->enc ){ 793 sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC); 794 break; 795 } 796 } 797 sqlite3VdbeAddOp(v, OP_Callback, 1, 0); 798 }else{ /* "PRAGMA encoding = XXX" */ 799 /* Only change the value of sqlite.enc if the database handle is not 800 ** initialized. If the main database exists, the new sqlite.enc value 801 ** will be overwritten when the schema is next loaded. If it does not 802 ** already exists, it will be created to use the new encoding value. 803 */ 804 if( !(pParse->db->flags&SQLITE_Initialized) ){ 805 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ 806 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ 807 pParse->db->enc = pEnc->enc; 808 break; 809 } 810 } 811 if( !pEnc->zName ){ 812 sqlite3Error(pParse->db, SQLITE_ERROR, 813 "Unsupported encoding: %s", zRight); 814 } 815 } 816 } 817 }else 818 819 #ifdef SQLITE_DEBUG 820 /* 821 ** Report the current state of file logs for all databases 822 */ 823 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){ 824 static char *azLockName[] = { 825 "unlocked", "shared", "reserved", "pending", "exclusive" 826 }; 827 int i; 828 Vdbe *v = sqlite3GetVdbe(pParse); 829 sqlite3VdbeSetNumCols(v, 2); 830 sqlite3VdbeSetColName(v, 0, "database", P3_STATIC); 831 sqlite3VdbeSetColName(v, 1, "status", P3_STATIC); 832 for(i=0; i<db->nDb; i++){ 833 Btree *pBt; 834 Pager *pPager; 835 if( db->aDb[i].zName==0 ) continue; 836 sqlite3VdbeOp3(v, OP_String, 0, 0, db->aDb[i].zName, P3_STATIC); 837 pBt = db->aDb[i].pBt; 838 if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){ 839 sqlite3VdbeOp3(v, OP_String, 0, 0, "closed", P3_STATIC); 840 }else{ 841 int j = sqlite3pager_lockstate(pPager); 842 sqlite3VdbeOp3(v, OP_String, 0, 0, 843 (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC); 844 } 845 sqlite3VdbeAddOp(v, OP_Callback, 2, 0); 846 } 847 }else 848 #endif 849 850 {} 851 sqliteFree(zLeft); 852 sqliteFree(zRight); 853 } 854