dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2010 July 12 |
| 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 | ****************************************************************************** |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 12 | ** |
| 13 | ** This file contains an implementation of the "dbstat" virtual table. |
| 14 | ** |
| 15 | ** The dbstat virtual table is used to extract low-level formatting |
| 16 | ** information from an SQLite database in order to implement the |
| 17 | ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script |
| 18 | ** for an example implementation. |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 19 | ** |
| 20 | ** Additional information is available on the "dbstat.html" page of the |
| 21 | ** official SQLite documentation. |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
drh | 8755a5f | 2015-05-12 19:10:18 +0000 | [diff] [blame] | 24 | #include "sqliteInt.h" /* Requires access to internal data structures */ |
drh | 1a4a680 | 2015-05-04 18:31:09 +0000 | [diff] [blame] | 25 | #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \ |
drh | 59ba6e8 | 2015-05-05 10:46:02 +0000 | [diff] [blame] | 26 | && !defined(SQLITE_OMIT_VIRTUALTABLE) |
dan | 6d3eb82 | 2010-07-12 18:12:41 +0000 | [diff] [blame] | 27 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 28 | /* |
| 29 | ** Page paths: |
| 30 | ** |
| 31 | ** The value of the 'path' column describes the path taken from the |
| 32 | ** root-node of the b-tree structure to each page. The value of the |
| 33 | ** root-node path is '/'. |
| 34 | ** |
| 35 | ** The value of the path for the left-most child page of the root of |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 36 | ** a b-tree is '/000/'. (Btrees store content ordered from left to right |
| 37 | ** so the pages to the left have smaller keys than the pages to the right.) |
| 38 | ** The next to left-most child of the root page is |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 39 | ** '/001', and so on, each sibling page identified by a 3-digit hex |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 40 | ** value. The children of the 451st left-most sibling have paths such |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 41 | ** as '/1c2/000/, '/1c2/001/' etc. |
| 42 | ** |
| 43 | ** Overflow pages are specified by appending a '+' character and a |
| 44 | ** six-digit hexadecimal value to the path to the cell they are linked |
| 45 | ** from. For example, the three overflow pages in a chain linked from |
| 46 | ** the left-most cell of the 450th child of the root page are identified |
| 47 | ** by the paths: |
| 48 | ** |
| 49 | ** '/1c2/000+000000' // First page in overflow chain |
| 50 | ** '/1c2/000+000001' // Second page in overflow chain |
| 51 | ** '/1c2/000+000002' // Third page in overflow chain |
| 52 | ** |
| 53 | ** If the paths are sorted using the BINARY collation sequence, then |
| 54 | ** the overflow pages associated with a cell will appear earlier in the |
| 55 | ** sort-order than its child page: |
| 56 | ** |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 57 | ** '/1c2/000/' // Left-most child of 451st child of root |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 58 | */ |
| 59 | #define VTAB_SCHEMA \ |
| 60 | "CREATE TABLE xx( " \ |
drh | 6c0e41b | 2016-06-13 15:59:37 +0000 | [diff] [blame] | 61 | " name TEXT, /* Name of table or index */" \ |
| 62 | " path TEXT, /* Path to page from root */" \ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 63 | " pageno INTEGER, /* Page number */" \ |
drh | 6c0e41b | 2016-06-13 15:59:37 +0000 | [diff] [blame] | 64 | " pagetype TEXT, /* 'internal', 'leaf' or 'overflow' */" \ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 65 | " ncell INTEGER, /* Cells on page (0 for overflow) */" \ |
| 66 | " payload INTEGER, /* Bytes of payload on this page */" \ |
| 67 | " unused INTEGER, /* Bytes of unused space on this page */" \ |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 68 | " mx_payload INTEGER, /* Largest payload size of all cells */" \ |
| 69 | " pgoffset INTEGER, /* Offset of page in file */" \ |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 70 | " pgsize INTEGER, /* Size of the page */" \ |
| 71 | " schema TEXT HIDDEN /* Database schema being analyzed */" \ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 72 | ");" |
| 73 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 74 | |
| 75 | typedef struct StatTable StatTable; |
| 76 | typedef struct StatCursor StatCursor; |
| 77 | typedef struct StatPage StatPage; |
| 78 | typedef struct StatCell StatCell; |
| 79 | |
| 80 | struct StatCell { |
| 81 | int nLocal; /* Bytes of local payload */ |
| 82 | u32 iChildPg; /* Child node (or 0 if this is a leaf) */ |
| 83 | int nOvfl; /* Entries in aOvfl[] */ |
| 84 | u32 *aOvfl; /* Array of overflow page numbers */ |
| 85 | int nLastOvfl; /* Bytes of payload on final overflow page */ |
| 86 | int iOvfl; /* Iterates through aOvfl[] */ |
| 87 | }; |
| 88 | |
| 89 | struct StatPage { |
| 90 | u32 iPgno; |
| 91 | DbPage *pPg; |
| 92 | int iCell; |
| 93 | |
| 94 | char *zPath; /* Path to this page */ |
| 95 | |
| 96 | /* Variables populated by statDecodePage(): */ |
| 97 | u8 flags; /* Copy of flags byte */ |
| 98 | int nCell; /* Number of cells on page */ |
| 99 | int nUnused; /* Number of unused bytes on page */ |
| 100 | StatCell *aCell; /* Array of parsed cells */ |
| 101 | u32 iRightChildPg; /* Right-child page number (or 0) */ |
| 102 | int nMxPayload; /* Largest payload of any cell on this page */ |
| 103 | }; |
| 104 | |
| 105 | struct StatCursor { |
| 106 | sqlite3_vtab_cursor base; |
| 107 | sqlite3_stmt *pStmt; /* Iterates through set of root pages */ |
| 108 | int isEof; /* After pStmt has returned SQLITE_DONE */ |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 109 | int iDb; /* Schema used for this query */ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 110 | |
| 111 | StatPage aPage[32]; |
| 112 | int iPage; /* Current entry in aPage[] */ |
| 113 | |
| 114 | /* Values to return. */ |
| 115 | char *zName; /* Value of 'name' column */ |
| 116 | char *zPath; /* Value of 'path' column */ |
| 117 | u32 iPageno; /* Value of 'pageno' column */ |
| 118 | char *zPagetype; /* Value of 'pagetype' column */ |
| 119 | int nCell; /* Value of 'ncell' column */ |
| 120 | int nPayload; /* Value of 'payload' column */ |
| 121 | int nUnused; /* Value of 'unused' column */ |
| 122 | int nMxPayload; /* Value of 'mx_payload' column */ |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 123 | i64 iOffset; /* Value of 'pgOffset' column */ |
| 124 | int szPage; /* Value of 'pgSize' column */ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | struct StatTable { |
| 128 | sqlite3_vtab base; |
| 129 | sqlite3 *db; |
drh | 857df26 | 2015-05-07 14:41:56 +0000 | [diff] [blame] | 130 | int iDb; /* Index of database to analyze */ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | #ifndef get2byte |
| 134 | # define get2byte(x) ((x)[0]<<8 | (x)[1]) |
| 135 | #endif |
| 136 | |
| 137 | /* |
| 138 | ** Connect to or create a statvfs virtual table. |
| 139 | */ |
| 140 | static int statConnect( |
| 141 | sqlite3 *db, |
| 142 | void *pAux, |
| 143 | int argc, const char *const*argv, |
| 144 | sqlite3_vtab **ppVtab, |
| 145 | char **pzErr |
| 146 | ){ |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 147 | StatTable *pTab = 0; |
| 148 | int rc = SQLITE_OK; |
drh | 857df26 | 2015-05-07 14:41:56 +0000 | [diff] [blame] | 149 | int iDb; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 150 | |
drh | 857df26 | 2015-05-07 14:41:56 +0000 | [diff] [blame] | 151 | if( argc>=4 ){ |
drh | 40aced5 | 2016-01-22 17:48:09 +0000 | [diff] [blame] | 152 | Token nm; |
| 153 | sqlite3TokenInit(&nm, (char*)argv[3]); |
| 154 | iDb = sqlite3FindDb(db, &nm); |
drh | 857df26 | 2015-05-07 14:41:56 +0000 | [diff] [blame] | 155 | if( iDb<0 ){ |
| 156 | *pzErr = sqlite3_mprintf("no such database: %s", argv[3]); |
| 157 | return SQLITE_ERROR; |
| 158 | } |
| 159 | }else{ |
| 160 | iDb = 0; |
| 161 | } |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 162 | rc = sqlite3_declare_vtab(db, VTAB_SCHEMA); |
| 163 | if( rc==SQLITE_OK ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 164 | pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable)); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 165 | if( pTab==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 166 | } |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 167 | |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 168 | assert( rc==SQLITE_OK || pTab==0 ); |
| 169 | if( rc==SQLITE_OK ){ |
| 170 | memset(pTab, 0, sizeof(StatTable)); |
| 171 | pTab->db = db; |
drh | 857df26 | 2015-05-07 14:41:56 +0000 | [diff] [blame] | 172 | pTab->iDb = iDb; |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | *ppVtab = (sqlite3_vtab*)pTab; |
| 176 | return rc; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* |
| 180 | ** Disconnect from or destroy a statvfs virtual table. |
| 181 | */ |
| 182 | static int statDisconnect(sqlite3_vtab *pVtab){ |
| 183 | sqlite3_free(pVtab); |
| 184 | return SQLITE_OK; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | ** There is no "best-index". This virtual table always does a linear |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 189 | ** scan. However, a schema=? constraint should cause this table to |
| 190 | ** operate on a different database schema, so check for it. |
| 191 | ** |
| 192 | ** idxNum is normally 0, but will be 1 if a schema=? constraint exists. |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 193 | */ |
| 194 | static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 195 | int i; |
| 196 | |
| 197 | pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */ |
| 198 | |
| 199 | /* Look for a valid schema=? constraint. If found, change the idxNum to |
| 200 | ** 1 and request the value of that constraint be sent to xFilter. And |
| 201 | ** lower the cost estimate to encourage the constrained version to be |
| 202 | ** used. |
| 203 | */ |
| 204 | for(i=0; i<pIdxInfo->nConstraint; i++){ |
| 205 | if( pIdxInfo->aConstraint[i].usable==0 ) continue; |
| 206 | if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 207 | if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue; |
| 208 | pIdxInfo->idxNum = 1; |
| 209 | pIdxInfo->estimatedCost = 1.0; |
| 210 | pIdxInfo->aConstraintUsage[i].argvIndex = 1; |
| 211 | pIdxInfo->aConstraintUsage[i].omit = 1; |
| 212 | break; |
| 213 | } |
| 214 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 215 | |
| 216 | /* Records are always returned in ascending order of (name, path). |
| 217 | ** If this will satisfy the client, set the orderByConsumed flag so that |
| 218 | ** SQLite does not do an external sort. |
| 219 | */ |
| 220 | if( ( pIdxInfo->nOrderBy==1 |
| 221 | && pIdxInfo->aOrderBy[0].iColumn==0 |
| 222 | && pIdxInfo->aOrderBy[0].desc==0 |
| 223 | ) || |
| 224 | ( pIdxInfo->nOrderBy==2 |
| 225 | && pIdxInfo->aOrderBy[0].iColumn==0 |
| 226 | && pIdxInfo->aOrderBy[0].desc==0 |
| 227 | && pIdxInfo->aOrderBy[1].iColumn==1 |
| 228 | && pIdxInfo->aOrderBy[1].desc==0 |
| 229 | ) |
| 230 | ){ |
| 231 | pIdxInfo->orderByConsumed = 1; |
| 232 | } |
| 233 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 234 | return SQLITE_OK; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | ** Open a new statvfs cursor. |
| 239 | */ |
| 240 | static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 241 | StatTable *pTab = (StatTable *)pVTab; |
| 242 | StatCursor *pCsr; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 243 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 244 | pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor)); |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 245 | if( pCsr==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 246 | return SQLITE_NOMEM_BKPT; |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 247 | }else{ |
| 248 | memset(pCsr, 0, sizeof(StatCursor)); |
| 249 | pCsr->base.pVtab = pVTab; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 250 | pCsr->iDb = pTab->iDb; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 254 | return SQLITE_OK; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 255 | } |
| 256 | |
drh | 5aa2037 | 2018-10-29 18:33:42 +0000 | [diff] [blame] | 257 | static void statClearCells(StatPage *p){ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 258 | int i; |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 259 | if( p->aCell ){ |
| 260 | for(i=0; i<p->nCell; i++){ |
| 261 | sqlite3_free(p->aCell[i].aOvfl); |
| 262 | } |
| 263 | sqlite3_free(p->aCell); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 264 | } |
drh | 5aa2037 | 2018-10-29 18:33:42 +0000 | [diff] [blame] | 265 | p->nCell = 0; |
| 266 | p->aCell = 0; |
| 267 | } |
| 268 | |
| 269 | static void statClearPage(StatPage *p){ |
| 270 | statClearCells(p); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 271 | sqlite3PagerUnref(p->pPg); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 272 | sqlite3_free(p->zPath); |
| 273 | memset(p, 0, sizeof(StatPage)); |
| 274 | } |
| 275 | |
| 276 | static void statResetCsr(StatCursor *pCsr){ |
| 277 | int i; |
| 278 | sqlite3_reset(pCsr->pStmt); |
| 279 | for(i=0; i<ArraySize(pCsr->aPage); i++){ |
| 280 | statClearPage(&pCsr->aPage[i]); |
| 281 | } |
| 282 | pCsr->iPage = 0; |
| 283 | sqlite3_free(pCsr->zPath); |
| 284 | pCsr->zPath = 0; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 285 | pCsr->isEof = 0; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* |
| 289 | ** Close a statvfs cursor. |
| 290 | */ |
| 291 | static int statClose(sqlite3_vtab_cursor *pCursor){ |
| 292 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 293 | statResetCsr(pCsr); |
| 294 | sqlite3_finalize(pCsr->pStmt); |
| 295 | sqlite3_free(pCsr); |
| 296 | return SQLITE_OK; |
| 297 | } |
| 298 | |
| 299 | static void getLocalPayload( |
| 300 | int nUsable, /* Usable bytes per page */ |
| 301 | u8 flags, /* Page flags */ |
| 302 | int nTotal, /* Total record (payload) size */ |
| 303 | int *pnLocal /* OUT: Bytes stored locally */ |
| 304 | ){ |
| 305 | int nLocal; |
| 306 | int nMinLocal; |
| 307 | int nMaxLocal; |
| 308 | |
| 309 | if( flags==0x0D ){ /* Table leaf node */ |
| 310 | nMinLocal = (nUsable - 12) * 32 / 255 - 23; |
| 311 | nMaxLocal = nUsable - 35; |
| 312 | }else{ /* Index interior and leaf nodes */ |
| 313 | nMinLocal = (nUsable - 12) * 32 / 255 - 23; |
| 314 | nMaxLocal = (nUsable - 12) * 64 / 255 - 23; |
| 315 | } |
| 316 | |
| 317 | nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4); |
| 318 | if( nLocal>nMaxLocal ) nLocal = nMinLocal; |
| 319 | *pnLocal = nLocal; |
| 320 | } |
| 321 | |
| 322 | static int statDecodePage(Btree *pBt, StatPage *p){ |
| 323 | int nUnused; |
| 324 | int iOff; |
| 325 | int nHdr; |
| 326 | int isLeaf; |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 327 | int szPage; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 328 | |
| 329 | u8 *aData = sqlite3PagerGetData(p->pPg); |
| 330 | u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0]; |
| 331 | |
| 332 | p->flags = aHdr[0]; |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 333 | if( p->flags==0x0A || p->flags==0x0D ){ |
| 334 | isLeaf = 1; |
| 335 | nHdr = 8; |
| 336 | }else if( p->flags==0x05 || p->flags==0x02 ){ |
| 337 | isLeaf = 0; |
| 338 | nHdr = 12; |
| 339 | }else{ |
| 340 | goto statPageIsCorrupt; |
| 341 | } |
| 342 | if( p->iPgno==1 ) nHdr += 100; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 343 | p->nCell = get2byte(&aHdr[3]); |
| 344 | p->nMxPayload = 0; |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 345 | szPage = sqlite3BtreeGetPageSize(pBt); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 346 | |
| 347 | nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell; |
| 348 | nUnused += (int)aHdr[7]; |
| 349 | iOff = get2byte(&aHdr[1]); |
| 350 | while( iOff ){ |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 351 | int iNext; |
| 352 | if( iOff>=szPage ) goto statPageIsCorrupt; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 353 | nUnused += get2byte(&aData[iOff+2]); |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 354 | iNext = get2byte(&aData[iOff]); |
| 355 | if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt; |
| 356 | iOff = iNext; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 357 | } |
| 358 | p->nUnused = nUnused; |
| 359 | p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]); |
| 360 | |
| 361 | if( p->nCell ){ |
| 362 | int i; /* Used to iterate through cells */ |
drh | ad0961b | 2015-02-21 00:19:25 +0000 | [diff] [blame] | 363 | int nUsable; /* Usable bytes per page */ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 364 | |
drh | ad0961b | 2015-02-21 00:19:25 +0000 | [diff] [blame] | 365 | sqlite3BtreeEnter(pBt); |
| 366 | nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt); |
| 367 | sqlite3BtreeLeave(pBt); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 368 | p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell)); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 369 | if( p->aCell==0 ) return SQLITE_NOMEM_BKPT; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 370 | memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell)); |
| 371 | |
| 372 | for(i=0; i<p->nCell; i++){ |
| 373 | StatCell *pCell = &p->aCell[i]; |
| 374 | |
| 375 | iOff = get2byte(&aData[nHdr+i*2]); |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 376 | if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 377 | if( !isLeaf ){ |
| 378 | pCell->iChildPg = sqlite3Get4byte(&aData[iOff]); |
| 379 | iOff += 4; |
| 380 | } |
| 381 | if( p->flags==0x05 ){ |
| 382 | /* A table interior node. nPayload==0. */ |
| 383 | }else{ |
| 384 | u32 nPayload; /* Bytes of payload total (local+overflow) */ |
| 385 | int nLocal; /* Bytes of payload stored locally */ |
| 386 | iOff += getVarint32(&aData[iOff], nPayload); |
| 387 | if( p->flags==0x0D ){ |
| 388 | u64 dummy; |
| 389 | iOff += sqlite3GetVarint(&aData[iOff], &dummy); |
| 390 | } |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 391 | if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 392 | getLocalPayload(nUsable, p->flags, nPayload, &nLocal); |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 393 | if( nLocal<0 ) goto statPageIsCorrupt; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 394 | pCell->nLocal = nLocal; |
drh | 26d8b0f | 2012-05-11 15:53:18 +0000 | [diff] [blame] | 395 | assert( nPayload>=(u32)nLocal ); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 396 | assert( nLocal<=(nUsable-35) ); |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 397 | if( nPayload>(u32)nLocal ){ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 398 | int j; |
| 399 | int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4); |
| 400 | pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4); |
| 401 | pCell->nOvfl = nOvfl; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 402 | pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 403 | if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 404 | pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]); |
| 405 | for(j=1; j<nOvfl; j++){ |
| 406 | int rc; |
| 407 | u32 iPrev = pCell->aOvfl[j-1]; |
| 408 | DbPage *pPg = 0; |
drh | 9584f58 | 2015-11-04 20:22:37 +0000 | [diff] [blame] | 409 | rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 410 | if( rc!=SQLITE_OK ){ |
| 411 | assert( pPg==0 ); |
| 412 | return rc; |
| 413 | } |
| 414 | pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg)); |
| 415 | sqlite3PagerUnref(pPg); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return SQLITE_OK; |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 423 | |
| 424 | statPageIsCorrupt: |
| 425 | p->flags = 0; |
drh | 5aa2037 | 2018-10-29 18:33:42 +0000 | [diff] [blame] | 426 | statClearCells(p); |
drh | f888158 | 2018-10-29 16:07:10 +0000 | [diff] [blame] | 427 | return SQLITE_OK; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 428 | } |
| 429 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 430 | /* |
dan | 2cf7e0a | 2011-10-05 17:36:27 +0000 | [diff] [blame] | 431 | ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on |
| 432 | ** the current value of pCsr->iPageno. |
| 433 | */ |
| 434 | static void statSizeAndOffset(StatCursor *pCsr){ |
| 435 | StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab; |
drh | 857df26 | 2015-05-07 14:41:56 +0000 | [diff] [blame] | 436 | Btree *pBt = pTab->db->aDb[pTab->iDb].pBt; |
dan | 2cf7e0a | 2011-10-05 17:36:27 +0000 | [diff] [blame] | 437 | Pager *pPager = sqlite3BtreePager(pBt); |
| 438 | sqlite3_file *fd; |
| 439 | sqlite3_int64 x[2]; |
| 440 | |
| 441 | /* The default page size and offset */ |
| 442 | pCsr->szPage = sqlite3BtreeGetPageSize(pBt); |
dan | 7c3210e | 2011-12-21 18:04:41 +0000 | [diff] [blame] | 443 | pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1); |
dan | 2cf7e0a | 2011-10-05 17:36:27 +0000 | [diff] [blame] | 444 | |
| 445 | /* If connected to a ZIPVFS backend, override the page size and |
| 446 | ** offset with actual values obtained from ZIPVFS. |
| 447 | */ |
| 448 | fd = sqlite3PagerFile(pPager); |
| 449 | x[0] = pCsr->iPageno; |
drh | afb39a4 | 2018-03-29 13:47:01 +0000 | [diff] [blame] | 450 | if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ |
dan | 2cf7e0a | 2011-10-05 17:36:27 +0000 | [diff] [blame] | 451 | pCsr->iOffset = x[0]; |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 452 | pCsr->szPage = (int)x[1]; |
dan | 2cf7e0a | 2011-10-05 17:36:27 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | /* |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 457 | ** Move a statvfs cursor to the next entry in the file. |
| 458 | */ |
| 459 | static int statNext(sqlite3_vtab_cursor *pCursor){ |
| 460 | int rc; |
| 461 | int nPayload; |
drh | 5f36a83 | 2015-05-07 18:29:04 +0000 | [diff] [blame] | 462 | char *z; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 463 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 464 | StatTable *pTab = (StatTable *)pCursor->pVtab; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 465 | Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 466 | Pager *pPager = sqlite3BtreePager(pBt); |
| 467 | |
| 468 | sqlite3_free(pCsr->zPath); |
| 469 | pCsr->zPath = 0; |
| 470 | |
drh | a464171 | 2013-11-02 11:34:58 +0000 | [diff] [blame] | 471 | statNextRestart: |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 472 | if( pCsr->aPage[0].pPg==0 ){ |
| 473 | rc = sqlite3_step(pCsr->pStmt); |
| 474 | if( rc==SQLITE_ROW ){ |
dan | 763afe6 | 2010-08-03 06:42:39 +0000 | [diff] [blame] | 475 | int nPage; |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 476 | u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1); |
dan | 763afe6 | 2010-08-03 06:42:39 +0000 | [diff] [blame] | 477 | sqlite3PagerPagecount(pPager, &nPage); |
| 478 | if( nPage==0 ){ |
| 479 | pCsr->isEof = 1; |
| 480 | return sqlite3_reset(pCsr->pStmt); |
| 481 | } |
drh | 9584f58 | 2015-11-04 20:22:37 +0000 | [diff] [blame] | 482 | rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 483 | pCsr->aPage[0].iPgno = iRoot; |
| 484 | pCsr->aPage[0].iCell = 0; |
drh | 5f36a83 | 2015-05-07 18:29:04 +0000 | [diff] [blame] | 485 | pCsr->aPage[0].zPath = z = sqlite3_mprintf("/"); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 486 | pCsr->iPage = 0; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 487 | if( z==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 488 | }else{ |
| 489 | pCsr->isEof = 1; |
| 490 | return sqlite3_reset(pCsr->pStmt); |
| 491 | } |
| 492 | }else{ |
| 493 | |
| 494 | /* Page p itself has already been visited. */ |
| 495 | StatPage *p = &pCsr->aPage[pCsr->iPage]; |
| 496 | |
| 497 | while( p->iCell<p->nCell ){ |
| 498 | StatCell *pCell = &p->aCell[p->iCell]; |
| 499 | if( pCell->iOvfl<pCell->nOvfl ){ |
drh | ad0961b | 2015-02-21 00:19:25 +0000 | [diff] [blame] | 500 | int nUsable; |
| 501 | sqlite3BtreeEnter(pBt); |
| 502 | nUsable = sqlite3BtreeGetPageSize(pBt) - |
| 503 | sqlite3BtreeGetReserveNoMutex(pBt); |
| 504 | sqlite3BtreeLeave(pBt); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 505 | pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); |
| 506 | pCsr->iPageno = pCell->aOvfl[pCell->iOvfl]; |
| 507 | pCsr->zPagetype = "overflow"; |
| 508 | pCsr->nCell = 0; |
| 509 | pCsr->nMxPayload = 0; |
drh | 5f36a83 | 2015-05-07 18:29:04 +0000 | [diff] [blame] | 510 | pCsr->zPath = z = sqlite3_mprintf( |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 511 | "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl |
| 512 | ); |
| 513 | if( pCell->iOvfl<pCell->nOvfl-1 ){ |
| 514 | pCsr->nUnused = 0; |
| 515 | pCsr->nPayload = nUsable - 4; |
| 516 | }else{ |
| 517 | pCsr->nPayload = pCell->nLastOvfl; |
| 518 | pCsr->nUnused = nUsable - 4 - pCsr->nPayload; |
| 519 | } |
| 520 | pCell->iOvfl++; |
dan | 2cf7e0a | 2011-10-05 17:36:27 +0000 | [diff] [blame] | 521 | statSizeAndOffset(pCsr); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 522 | return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 523 | } |
| 524 | if( p->iRightChildPg ) break; |
| 525 | p->iCell++; |
| 526 | } |
| 527 | |
drh | a464171 | 2013-11-02 11:34:58 +0000 | [diff] [blame] | 528 | if( !p->iRightChildPg || p->iCell>p->nCell ){ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 529 | statClearPage(p); |
| 530 | if( pCsr->iPage==0 ) return statNext(pCursor); |
| 531 | pCsr->iPage--; |
drh | a464171 | 2013-11-02 11:34:58 +0000 | [diff] [blame] | 532 | goto statNextRestart; /* Tail recursion */ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 533 | } |
| 534 | pCsr->iPage++; |
| 535 | assert( p==&pCsr->aPage[pCsr->iPage-1] ); |
| 536 | |
| 537 | if( p->iCell==p->nCell ){ |
| 538 | p[1].iPgno = p->iRightChildPg; |
| 539 | }else{ |
| 540 | p[1].iPgno = p->aCell[p->iCell].iChildPg; |
| 541 | } |
drh | 9584f58 | 2015-11-04 20:22:37 +0000 | [diff] [blame] | 542 | rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 543 | p[1].iCell = 0; |
drh | 5f36a83 | 2015-05-07 18:29:04 +0000 | [diff] [blame] | 544 | p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 545 | p->iCell++; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 546 | if( z==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | |
| 550 | /* Populate the StatCursor fields with the values to be returned |
| 551 | ** by the xColumn() and xRowid() methods. |
| 552 | */ |
| 553 | if( rc==SQLITE_OK ){ |
| 554 | int i; |
| 555 | StatPage *p = &pCsr->aPage[pCsr->iPage]; |
| 556 | pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); |
| 557 | pCsr->iPageno = p->iPgno; |
| 558 | |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 559 | rc = statDecodePage(pBt, p); |
| 560 | if( rc==SQLITE_OK ){ |
| 561 | statSizeAndOffset(pCsr); |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 562 | |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 563 | switch( p->flags ){ |
| 564 | case 0x05: /* table internal */ |
| 565 | case 0x02: /* index internal */ |
| 566 | pCsr->zPagetype = "internal"; |
| 567 | break; |
| 568 | case 0x0D: /* table leaf */ |
| 569 | case 0x0A: /* index leaf */ |
| 570 | pCsr->zPagetype = "leaf"; |
| 571 | break; |
| 572 | default: |
| 573 | pCsr->zPagetype = "corrupted"; |
| 574 | break; |
| 575 | } |
| 576 | pCsr->nCell = p->nCell; |
| 577 | pCsr->nUnused = p->nUnused; |
| 578 | pCsr->nMxPayload = p->nMxPayload; |
drh | 5f36a83 | 2015-05-07 18:29:04 +0000 | [diff] [blame] | 579 | pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 580 | if( z==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | 995f8b9 | 2015-04-27 19:53:55 +0000 | [diff] [blame] | 581 | nPayload = 0; |
| 582 | for(i=0; i<p->nCell; i++){ |
| 583 | nPayload += p->aCell[i].nLocal; |
| 584 | } |
| 585 | pCsr->nPayload = nPayload; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 586 | } |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | return rc; |
| 590 | } |
| 591 | |
| 592 | static int statEof(sqlite3_vtab_cursor *pCursor){ |
| 593 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 594 | return pCsr->isEof; |
| 595 | } |
| 596 | |
| 597 | static int statFilter( |
| 598 | sqlite3_vtab_cursor *pCursor, |
| 599 | int idxNum, const char *idxStr, |
| 600 | int argc, sqlite3_value **argv |
| 601 | ){ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 602 | StatCursor *pCsr = (StatCursor *)pCursor; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 603 | StatTable *pTab = (StatTable*)(pCursor->pVtab); |
| 604 | char *zSql; |
| 605 | int rc = SQLITE_OK; |
| 606 | char *zMaster; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 607 | |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 608 | if( idxNum==1 ){ |
| 609 | const char *zDbase = (const char*)sqlite3_value_text(argv[0]); |
| 610 | pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase); |
| 611 | if( pCsr->iDb<0 ){ |
| 612 | sqlite3_free(pCursor->pVtab->zErrMsg); |
| 613 | pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 614 | return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 615 | } |
| 616 | }else{ |
| 617 | pCsr->iDb = pTab->iDb; |
| 618 | } |
drh | e5918c6 | 2010-08-14 12:42:45 +0000 | [diff] [blame] | 619 | statResetCsr(pCsr); |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 620 | sqlite3_finalize(pCsr->pStmt); |
| 621 | pCsr->pStmt = 0; |
| 622 | zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master"; |
| 623 | zSql = sqlite3_mprintf( |
| 624 | "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type" |
| 625 | " UNION ALL " |
| 626 | "SELECT name, rootpage, type" |
| 627 | " FROM \"%w\".%s WHERE rootpage!=0" |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 628 | " ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName, zMaster); |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 629 | if( zSql==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 630 | return SQLITE_NOMEM_BKPT; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 631 | }else{ |
| 632 | rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); |
| 633 | sqlite3_free(zSql); |
| 634 | } |
| 635 | |
| 636 | if( rc==SQLITE_OK ){ |
| 637 | rc = statNext(pCursor); |
| 638 | } |
| 639 | return rc; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static int statColumn( |
| 643 | sqlite3_vtab_cursor *pCursor, |
| 644 | sqlite3_context *ctx, |
| 645 | int i |
| 646 | ){ |
| 647 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 648 | switch( i ){ |
| 649 | case 0: /* name */ |
drh | 5f36a83 | 2015-05-07 18:29:04 +0000 | [diff] [blame] | 650 | sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 651 | break; |
| 652 | case 1: /* path */ |
| 653 | sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); |
| 654 | break; |
| 655 | case 2: /* pageno */ |
| 656 | sqlite3_result_int64(ctx, pCsr->iPageno); |
| 657 | break; |
| 658 | case 3: /* pagetype */ |
| 659 | sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); |
| 660 | break; |
| 661 | case 4: /* ncell */ |
| 662 | sqlite3_result_int(ctx, pCsr->nCell); |
| 663 | break; |
| 664 | case 5: /* payload */ |
| 665 | sqlite3_result_int(ctx, pCsr->nPayload); |
| 666 | break; |
| 667 | case 6: /* unused */ |
| 668 | sqlite3_result_int(ctx, pCsr->nUnused); |
| 669 | break; |
| 670 | case 7: /* mx_payload */ |
| 671 | sqlite3_result_int(ctx, pCsr->nMxPayload); |
| 672 | break; |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 673 | case 8: /* pgoffset */ |
| 674 | sqlite3_result_int64(ctx, pCsr->iOffset); |
| 675 | break; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 676 | case 9: /* pgsize */ |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 677 | sqlite3_result_int(ctx, pCsr->szPage); |
| 678 | break; |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 679 | default: { /* schema */ |
| 680 | sqlite3 *db = sqlite3_context_db_handle(ctx); |
| 681 | int iDb = pCsr->iDb; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 682 | sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC); |
drh | a46a4a6 | 2015-09-08 21:12:53 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 685 | } |
| 686 | return SQLITE_OK; |
| 687 | } |
| 688 | |
| 689 | static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ |
| 690 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 691 | *pRowid = pCsr->iPageno; |
| 692 | return SQLITE_OK; |
| 693 | } |
| 694 | |
drh | 1a4a680 | 2015-05-04 18:31:09 +0000 | [diff] [blame] | 695 | /* |
| 696 | ** Invoke this routine to register the "dbstat" virtual table module |
| 697 | */ |
drh | 3e0327d | 2015-05-11 11:59:15 +0000 | [diff] [blame] | 698 | int sqlite3DbstatRegister(sqlite3 *db){ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 699 | static sqlite3_module dbstat_module = { |
| 700 | 0, /* iVersion */ |
| 701 | statConnect, /* xCreate */ |
| 702 | statConnect, /* xConnect */ |
| 703 | statBestIndex, /* xBestIndex */ |
| 704 | statDisconnect, /* xDisconnect */ |
| 705 | statDisconnect, /* xDestroy */ |
| 706 | statOpen, /* xOpen - open a cursor */ |
| 707 | statClose, /* xClose - close a cursor */ |
| 708 | statFilter, /* xFilter - configure scan constraints */ |
| 709 | statNext, /* xNext - advance a cursor */ |
| 710 | statEof, /* xEof - check for end of scan */ |
| 711 | statColumn, /* xColumn - read data */ |
| 712 | statRowid, /* xRowid - read data */ |
| 713 | 0, /* xUpdate */ |
| 714 | 0, /* xBegin */ |
| 715 | 0, /* xSync */ |
| 716 | 0, /* xCommit */ |
| 717 | 0, /* xRollback */ |
| 718 | 0, /* xFindMethod */ |
| 719 | 0, /* xRename */ |
drh | 7ccf95d | 2017-07-12 18:05:54 +0000 | [diff] [blame] | 720 | 0, /* xSavepoint */ |
| 721 | 0, /* xRelease */ |
| 722 | 0, /* xRollbackTo */ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 723 | }; |
dan | d154a43 | 2015-04-30 20:26:53 +0000 | [diff] [blame] | 724 | return sqlite3_create_module(db, "dbstat", &dbstat_module, 0); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 725 | } |
drh | 1081925 | 2015-05-12 14:22:05 +0000 | [diff] [blame] | 726 | #elif defined(SQLITE_ENABLE_DBSTAT_VTAB) |
drh | 6582ae5 | 2015-05-12 12:24:50 +0000 | [diff] [blame] | 727 | int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; } |
drh | 1a4a680 | 2015-05-04 18:31:09 +0000 | [diff] [blame] | 728 | #endif /* SQLITE_ENABLE_DBSTAT_VTAB */ |