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