Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/dbpage.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / dbpage.c` 505 lines ยท 14824 bytes Raw Blame History 1 /* 2 ** 2017-10-11 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 ** 13 ** This file contains an implementation of the "sqlite_dbpage" virtual table. 14 ** 15 ** The sqlite_dbpage virtual table is used to read or write whole raw 16 ** pages of the database file. The pager interface is used so that 17 ** uncommitted changes and changes recorded in the WAL file are correctly 18 ** retrieved. 19 ** 20 ** Usage example: 21 ** 22 ** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123; 23 ** 24 ** This is an eponymous virtual table so it does not need to be created before 25 ** use. The optional argument to the sqlite_dbpage() table name is the 26 ** schema for the database file that is to be read. The default schema is 27 ** "main". 28 ** 29 ** The data field of sqlite_dbpage table can be updated. The new 30 ** value must be a BLOB which is the correct page size, otherwise the 31 ** update fails. INSERT operations also work, and operate as if they 32 ** where REPLACE. The size of the database can be extended by INSERT-ing 33 ** new pages on the end. 34 ** 35 ** Rows may not be deleted. However, doing an INSERT to page number N 36 ** with NULL page data causes the N-th page and all subsequent pages to be 37 ** deleted and the database to be truncated. 38 */ 39 40 #include "sqliteInt.h" /* Requires access to internal data structures */ 41 #if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \ 42 && !defined(SQLITE_OMIT_VIRTUALTABLE) 43 44 typedef struct DbpageTable DbpageTable; 45 typedef struct DbpageCursor DbpageCursor; 46 47 struct DbpageCursor { 48 sqlite3_vtab_cursor base; /* Base class. Must be first */ 49 Pgno pgno; /* Current page number */ 50 Pgno mxPgno; /* Last page to visit on this scan */ 51 Pager *pPager; /* Pager being read/written */ 52 DbPage *pPage1; /* Page 1 of the database */ 53 int iDb; /* Index of database to analyze */ 54 int szPage; /* Size of each page in bytes */ 55 }; 56 57 struct DbpageTable { 58 sqlite3_vtab base; /* Base class. Must be first */ 59 sqlite3 *db; /* The database */ 60 int iDbTrunc; /* Database to truncate */ 61 Pgno pgnoTrunc; /* Size to truncate to */ 62 }; 63 64 /* Columns */ 65 #define DBPAGE_COLUMN_PGNO 0 66 #define DBPAGE_COLUMN_DATA 1 67 #define DBPAGE_COLUMN_SCHEMA 2 68 69 70 /* 71 ** Connect to or create a dbpagevfs virtual table. 72 */ 73 static int dbpageConnect( 74 sqlite3 *db, 75 void *pAux, 76 int argc, const char *const*argv, 77 sqlite3_vtab **ppVtab, 78 char **pzErr 79 ){ 80 DbpageTable *pTab = 0; 81 int rc = SQLITE_OK; 82 (void)pAux; 83 (void)argc; 84 (void)argv; 85 (void)pzErr; 86 87 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); 88 sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS); 89 rc = sqlite3_declare_vtab(db, 90 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)"); 91 if( rc==SQLITE_OK ){ 92 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable)); 93 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT; 94 } 95 96 assert( rc==SQLITE_OK || pTab==0 ); 97 if( rc==SQLITE_OK ){ 98 memset(pTab, 0, sizeof(DbpageTable)); 99 pTab->db = db; 100 } 101 102 *ppVtab = (sqlite3_vtab*)pTab; 103 return rc; 104 } 105 106 /* 107 ** Disconnect from or destroy a dbpagevfs virtual table. 108 */ 109 static int dbpageDisconnect(sqlite3_vtab *pVtab){ 110 sqlite3_free(pVtab); 111 return SQLITE_OK; 112 } 113 114 /* 115 ** idxNum: 116 ** 117 ** 0 schema=main, full table scan 118 ** 1 schema=main, pgno=?1 119 ** 2 schema=?1, full table scan 120 ** 3 schema=?1, pgno=?2 121 */ 122 static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ 123 int i; 124 int iPlan = 0; 125 (void)tab; 126 127 /* If there is a schema= constraint, it must be honored. Report a 128 ** ridiculously large estimated cost if the schema= constraint is 129 ** unavailable 130 */ 131 for(i=0; inConstraint; i++){ 132 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; 133 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue; 134 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; 135 if( !p->usable ){ 136 /* No solution. */ 137 return SQLITE_CONSTRAINT; 138 } 139 iPlan = 2; 140 pIdxInfo->aConstraintUsage[i].argvIndex = 1; 141 pIdxInfo->aConstraintUsage[i].omit = 1; 142 break; 143 } 144 145 /* If we reach this point, it means that either there is no schema= 146 ** constraint (in which case we use the "main" schema) or else the 147 ** schema constraint was accepted. Lower the estimated cost accordingly 148 */ 149 pIdxInfo->estimatedCost = 1.0e6; 150 151 /* Check for constraints against pgno */ 152 for(i=0; inConstraint; i++){ 153 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; 154 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ 155 pIdxInfo->estimatedRows = 1; 156 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE; 157 pIdxInfo->estimatedCost = 1.0; 158 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1; 159 pIdxInfo->aConstraintUsage[i].omit = 1; 160 iPlan |= 1; 161 break; 162 } 163 } 164 pIdxInfo->idxNum = iPlan; 165 166 if( pIdxInfo->nOrderBy>=1 167 && pIdxInfo->aOrderBy[0].iColumn<=0 168 && pIdxInfo->aOrderBy[0].desc==0 169 ){ 170 pIdxInfo->orderByConsumed = 1; 171 } 172 return SQLITE_OK; 173 } 174 175 /* 176 ** Open a new dbpagevfs cursor. 177 */ 178 static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ 179 DbpageCursor *pCsr; 180 181 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor)); 182 if( pCsr==0 ){ 183 return SQLITE_NOMEM_BKPT; 184 }else{ 185 memset(pCsr, 0, sizeof(DbpageCursor)); 186 pCsr->base.pVtab = pVTab; 187 pCsr->pgno = 0; 188 } 189 190 *ppCursor = (sqlite3_vtab_cursor *)pCsr; 191 return SQLITE_OK; 192 } 193 194 /* 195 ** Close a dbpagevfs cursor. 196 */ 197 static int dbpageClose(sqlite3_vtab_cursor *pCursor){ 198 DbpageCursor *pCsr = (DbpageCursor *)pCursor; 199 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1); 200 sqlite3_free(pCsr); 201 return SQLITE_OK; 202 } 203 204 /* 205 ** Move a dbpagevfs cursor to the next entry in the file. 206 */ 207 static int dbpageNext(sqlite3_vtab_cursor *pCursor){ 208 int rc = SQLITE_OK; 209 DbpageCursor *pCsr = (DbpageCursor *)pCursor; 210 pCsr->pgno++; 211 return rc; 212 } 213 214 static int dbpageEof(sqlite3_vtab_cursor *pCursor){ 215 DbpageCursor *pCsr = (DbpageCursor *)pCursor; 216 return pCsr->pgno > pCsr->mxPgno; 217 } 218 219 /* 220 ** idxNum: 221 ** 222 ** 0 schema=main, full table scan 223 ** 1 schema=main, pgno=?1 224 ** 2 schema=?1, full table scan 225 ** 3 schema=?1, pgno=?2 226 ** 227 ** idxStr is not used 228 */ 229 static int dbpageFilter( 230 sqlite3_vtab_cursor *pCursor, 231 int idxNum, const char *idxStr, 232 int argc, sqlite3_value **argv 233 ){ 234 DbpageCursor *pCsr = (DbpageCursor *)pCursor; 235 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab; 236 int rc; 237 sqlite3 *db = pTab->db; 238 Btree *pBt; 239 240 UNUSED_PARAMETER(idxStr); 241 UNUSED_PARAMETER(argc); 242 243 /* Default setting is no rows of result */ 244 pCsr->pgno = 1; 245 pCsr->mxPgno = 0; 246 247 if( idxNum & 2 ){ 248 const char *zSchema; 249 assert( argc>=1 ); 250 zSchema = (const char*)sqlite3_value_text(argv[0]); 251 pCsr->iDb = sqlite3FindDbName(db, zSchema); 252 if( pCsr->iDb<0 ) return SQLITE_OK; 253 }else{ 254 pCsr->iDb = 0; 255 } 256 pBt = db->aDb[pCsr->iDb].pBt; 257 if( NEVER(pBt==0) ) return SQLITE_OK; 258 pCsr->pPager = sqlite3BtreePager(pBt); 259 pCsr->szPage = sqlite3BtreeGetPageSize(pBt); 260 pCsr->mxPgno = sqlite3BtreeLastPage(pBt); 261 if( idxNum & 1 ){ 262 i64 iPg = sqlite3_value_int64(argv[idxNum>>1]); 263 assert( argc>(idxNum>>1) ); 264 if( iPg<1 || iPg>pCsr->mxPgno ){ 265 pCsr->pgno = 1; 266 pCsr->mxPgno = 0; 267 }else{ 268 pCsr->pgno = (Pgno)iPg; 269 pCsr->mxPgno = pCsr->pgno; 270 } 271 }else{ 272 assert( pCsr->pgno==1 ); 273 } 274 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1); 275 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0); 276 return rc; 277 } 278 279 static int dbpageColumn( 280 sqlite3_vtab_cursor *pCursor, 281 sqlite3_context *ctx, 282 int i 283 ){ 284 DbpageCursor *pCsr = (DbpageCursor *)pCursor; 285 int rc = SQLITE_OK; 286 switch( i ){ 287 case 0: { /* pgno */ 288 sqlite3_result_int64(ctx, (sqlite3_int64)pCsr->pgno); 289 break; 290 } 291 case 1: { /* data */ 292 DbPage *pDbPage = 0; 293 if( pCsr->pgno==(Pgno)((PENDING_BYTE/pCsr->szPage)+1) ){ 294 /* The pending byte page. Assume it is zeroed out. Attempting to 295 ** request this page from the page is an SQLITE_CORRUPT error. */ 296 sqlite3_result_zeroblob(ctx, pCsr->szPage); 297 }else{ 298 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0); 299 if( rc==SQLITE_OK ){ 300 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage, 301 SQLITE_TRANSIENT); 302 } 303 sqlite3PagerUnref(pDbPage); 304 } 305 break; 306 } 307 default: { /* schema */ 308 sqlite3 *db = sqlite3_context_db_handle(ctx); 309 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC); 310 break; 311 } 312 } 313 return rc; 314 } 315 316 static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ 317 DbpageCursor *pCsr = (DbpageCursor *)pCursor; 318 *pRowid = pCsr->pgno; 319 return SQLITE_OK; 320 } 321 322 /* 323 ** Open write transactions. Since we do not know in advance which database 324 ** files will be written by the sqlite_dbpage virtual table, start a write 325 ** transaction on them all. 326 ** 327 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. 328 */ 329 static int dbpageBeginTrans(DbpageTable *pTab){ 330 sqlite3 *db = pTab->db; 331 int rc = SQLITE_OK; 332 int i; 333 for(i=0; rc==SQLITE_OK && inDb; i++){ 334 Btree *pBt = db->aDb[i].pBt; 335 if( pBt ) rc = sqlite3BtreeBeginTrans(pBt, 1, 0); 336 } 337 return rc; 338 } 339 340 static int dbpageUpdate( 341 sqlite3_vtab *pVtab, 342 int argc, 343 sqlite3_value **argv, 344 sqlite_int64 *pRowid 345 ){ 346 DbpageTable *pTab = (DbpageTable *)pVtab; 347 Pgno pgno; 348 sqlite3_int64 pgno64; 349 DbPage *pDbPage = 0; 350 int rc = SQLITE_OK; 351 char *zErr = 0; 352 int iDb; 353 Btree *pBt; 354 Pager *pPager; 355 int szPage; 356 int isInsert; 357 358 (void)pRowid; 359 if( pTab->db->flags & SQLITE_Defensive ){ 360 zErr = "read-only"; 361 goto update_fail; 362 } 363 if( argc==1 ){ 364 zErr = "cannot delete"; 365 goto update_fail; 366 } 367 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 368 pgno64 = sqlite3_value_int64(argv[2]); 369 isInsert = 1; 370 }else{ 371 pgno64 = (Pgno)sqlite3_value_int64(argv[0]); 372 if( sqlite3_value_int64(argv[1])!=pgno64 ){ 373 zErr = "cannot insert"; 374 goto update_fail; 375 } 376 isInsert = 0; 377 } 378 if( sqlite3_value_type(argv[4])==SQLITE_NULL ){ 379 iDb = 0; 380 }else{ 381 const char *zSchema = (const char*)sqlite3_value_text(argv[4]); 382 iDb = sqlite3FindDbName(pTab->db, zSchema); 383 if( iDb<0 ){ 384 zErr = "no such schema"; 385 goto update_fail; 386 } 387 } 388 pBt = pTab->db->aDb[iDb].pBt; 389 if( pgno64<1 || pgno64>4294967294U || NEVER(pBt==0) ){ 390 zErr = "bad page number"; 391 goto update_fail; 392 } 393 pgno = (Pgno)pgno64; 394 szPage = sqlite3BtreeGetPageSize(pBt); 395 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB 396 || sqlite3_value_bytes(argv[3])!=szPage 397 ){ 398 if( sqlite3_value_type(argv[3])==SQLITE_NULL && isInsert && pgno>1 ){ 399 /* "INSERT INTO dbpage($PGNO,NULL)" causes page number $PGNO and 400 ** all subsequent pages to be deleted. */ 401 pTab->iDbTrunc = iDb; 402 pTab->pgnoTrunc = pgno-1; 403 pgno = 1; 404 }else{ 405 zErr = "bad page value"; 406 goto update_fail; 407 } 408 } 409 410 if( dbpageBeginTrans(pTab)!=SQLITE_OK ){ 411 zErr = "failed to open transaction"; 412 goto update_fail; 413 } 414 415 pPager = sqlite3BtreePager(pBt); 416 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0); 417 if( rc==SQLITE_OK ){ 418 const void *pData = sqlite3_value_blob(argv[3]); 419 if( (rc = sqlite3PagerWrite(pDbPage))==SQLITE_OK && pData ){ 420 unsigned char *aPage = sqlite3PagerGetData(pDbPage); 421 memcpy(aPage, pData, szPage); 422 pTab->pgnoTrunc = 0; 423 } 424 } 425 if( rc!=SQLITE_OK ){ 426 pTab->pgnoTrunc = 0; 427 } 428 sqlite3PagerUnref(pDbPage); 429 return rc; 430 431 update_fail: 432 pTab->pgnoTrunc = 0; 433 sqlite3_free(pVtab->zErrMsg); 434 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr); 435 return SQLITE_ERROR; 436 } 437 438 static int dbpageBegin(sqlite3_vtab *pVtab){ 439 DbpageTable *pTab = (DbpageTable *)pVtab; 440 pTab->pgnoTrunc = 0; 441 return SQLITE_OK; 442 } 443 444 /* Invoke sqlite3PagerTruncate() as necessary, just prior to COMMIT 445 */ 446 static int dbpageSync(sqlite3_vtab *pVtab){ 447 DbpageTable *pTab = (DbpageTable *)pVtab; 448 if( pTab->pgnoTrunc>0 ){ 449 Btree *pBt = pTab->db->aDb[pTab->iDbTrunc].pBt; 450 Pager *pPager = sqlite3BtreePager(pBt); 451 sqlite3BtreeEnter(pBt); 452 if( pTab->pgnoTruncpgnoTrunc); 454 } 455 sqlite3BtreeLeave(pBt); 456 } 457 pTab->pgnoTrunc = 0; 458 return SQLITE_OK; 459 } 460 461 /* Cancel any pending truncate. 462 */ 463 static int dbpageRollbackTo(sqlite3_vtab *pVtab, int notUsed1){ 464 DbpageTable *pTab = (DbpageTable *)pVtab; 465 pTab->pgnoTrunc = 0; 466 (void)notUsed1; 467 return SQLITE_OK; 468 } 469 470 /* 471 ** Invoke this routine to register the "dbpage" virtual table module 472 */ 473 int sqlite3DbpageRegister(sqlite3 *db){ 474 static sqlite3_module dbpage_module = { 475 2, /* iVersion */ 476 dbpageConnect, /* xCreate */ 477 dbpageConnect, /* xConnect */ 478 dbpageBestIndex, /* xBestIndex */ 479 dbpageDisconnect, /* xDisconnect */ 480 dbpageDisconnect, /* xDestroy */ 481 dbpageOpen, /* xOpen - open a cursor */ 482 dbpageClose, /* xClose - close a cursor */ 483 dbpageFilter, /* xFilter - configure scan constraints */ 484 dbpageNext, /* xNext - advance a cursor */ 485 dbpageEof, /* xEof - check for end of scan */ 486 dbpageColumn, /* xColumn - read data */ 487 dbpageRowid, /* xRowid - read data */ 488 dbpageUpdate, /* xUpdate */ 489 dbpageBegin, /* xBegin */ 490 dbpageSync, /* xSync */ 491 0, /* xCommit */ 492 0, /* xRollback */ 493 0, /* xFindMethod */ 494 0, /* xRename */ 495 0, /* xSavepoint */ 496 0, /* xRelease */ 497 dbpageRollbackTo, /* xRollbackTo */ 498 0, /* xShadowName */ 499 0 /* xIntegrity */ 500 }; 501 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0); 502 } 503 #elif defined(SQLITE_ENABLE_DBPAGE_VTAB) 504 int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; } 505 #endif /* SQLITE_ENABLE_DBSTAT_VTAB */