test_stat.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. ******************************************************************************
  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.
  19. */
  20. #ifndef SQLITE_AMALGAMATION
  21. # include "sqliteInt.h"
  22. #endif
  23. #ifndef SQLITE_OMIT_VIRTUALTABLE
  24. /*
  25. ** Page paths:
  26. **
  27. ** The value of the 'path' column describes the path taken from the
  28. ** root-node of the b-tree structure to each page. The value of the
  29. ** root-node path is '/'.
  30. **
  31. ** The value of the path for the left-most child page of the root of
  32. ** a b-tree is '/000/'. (Btrees store content ordered from left to right
  33. ** so the pages to the left have smaller keys than the pages to the right.)
  34. ** The next to left-most child of the root page is
  35. ** '/001', and so on, each sibling page identified by a 3-digit hex
  36. ** value. The children of the 451st left-most sibling have paths such
  37. ** as '/1c2/000/, '/1c2/001/' etc.
  38. **
  39. ** Overflow pages are specified by appending a '+' character and a
  40. ** six-digit hexadecimal value to the path to the cell they are linked
  41. ** from. For example, the three overflow pages in a chain linked from
  42. ** the left-most cell of the 450th child of the root page are identified
  43. ** by the paths:
  44. **
  45. ** '/1c2/000+000000' // First page in overflow chain
  46. ** '/1c2/000+000001' // Second page in overflow chain
  47. ** '/1c2/000+000002' // Third page in overflow chain
  48. **
  49. ** If the paths are sorted using the BINARY collation sequence, then
  50. ** the overflow pages associated with a cell will appear earlier in the
  51. ** sort-order than its child page:
  52. **
  53. ** '/1c2/000/' // Left-most child of 451st child of root
  54. */
  55. #define VTAB_SCHEMA \
  56. "CREATE TABLE xx( " \
  57. " name STRING, /* Name of table or index */" \
  58. " path INTEGER, /* Path to page from root */" \
  59. " pageno INTEGER, /* Page number */" \
  60. " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" \
  61. " ncell INTEGER, /* Cells on page (0 for overflow) */" \
  62. " payload INTEGER, /* Bytes of payload on this page */" \
  63. " unused INTEGER, /* Bytes of unused space on this page */" \
  64. " mx_payload INTEGER, /* Largest payload size of all cells */" \
  65. " pgoffset INTEGER, /* Offset of page in file */" \
  66. " pgsize INTEGER /* Size of the page */" \
  67. ");"
  68. typedef struct StatTable StatTable;
  69. typedef struct StatCursor StatCursor;
  70. typedef struct StatPage StatPage;
  71. typedef struct StatCell StatCell;
  72. struct StatCell {
  73. int nLocal; /* Bytes of local payload */
  74. u32 iChildPg; /* Child node (or 0 if this is a leaf) */
  75. int nOvfl; /* Entries in aOvfl[] */
  76. u32 *aOvfl; /* Array of overflow page numbers */
  77. int nLastOvfl; /* Bytes of payload on final overflow page */
  78. int iOvfl; /* Iterates through aOvfl[] */
  79. };
  80. struct StatPage {
  81. u32 iPgno;
  82. DbPage *pPg;
  83. int iCell;
  84. char *zPath; /* Path to this page */
  85. /* Variables populated by statDecodePage(): */
  86. u8 flags; /* Copy of flags byte */
  87. int nCell; /* Number of cells on page */
  88. int nUnused; /* Number of unused bytes on page */
  89. StatCell *aCell; /* Array of parsed cells */
  90. u32 iRightChildPg; /* Right-child page number (or 0) */
  91. int nMxPayload; /* Largest payload of any cell on this page */
  92. };
  93. struct StatCursor {
  94. sqlite3_vtab_cursor base;
  95. sqlite3_stmt *pStmt; /* Iterates through set of root pages */
  96. int isEof; /* After pStmt has returned SQLITE_DONE */
  97. StatPage aPage[32];
  98. int iPage; /* Current entry in aPage[] */
  99. /* Values to return. */
  100. char *zName; /* Value of 'name' column */
  101. char *zPath; /* Value of 'path' column */
  102. u32 iPageno; /* Value of 'pageno' column */
  103. char *zPagetype; /* Value of 'pagetype' column */
  104. int nCell; /* Value of 'ncell' column */
  105. int nPayload; /* Value of 'payload' column */
  106. int nUnused; /* Value of 'unused' column */
  107. int nMxPayload; /* Value of 'mx_payload' column */
  108. i64 iOffset; /* Value of 'pgOffset' column */
  109. int szPage; /* Value of 'pgSize' column */
  110. };
  111. struct StatTable {
  112. sqlite3_vtab base;
  113. sqlite3 *db;
  114. };
  115. #ifndef get2byte
  116. # define get2byte(x) ((x)[0]<<8 | (x)[1])
  117. #endif
  118. /*
  119. ** Connect to or create a statvfs virtual table.
  120. */
  121. static int statConnect(
  122. sqlite3 *db,
  123. void *pAux,
  124. int argc, const char *const*argv,
  125. sqlite3_vtab **ppVtab,
  126. char **pzErr
  127. ){
  128. StatTable *pTab;
  129. pTab = (StatTable *)sqlite3_malloc(sizeof(StatTable));
  130. memset(pTab, 0, sizeof(StatTable));
  131. pTab->db = db;
  132. sqlite3_declare_vtab(db, VTAB_SCHEMA);
  133. *ppVtab = &pTab->base;
  134. return SQLITE_OK;
  135. }
  136. /*
  137. ** Disconnect from or destroy a statvfs virtual table.
  138. */
  139. static int statDisconnect(sqlite3_vtab *pVtab){
  140. sqlite3_free(pVtab);
  141. return SQLITE_OK;
  142. }
  143. /*
  144. ** There is no "best-index". This virtual table always does a linear
  145. ** scan of the binary VFS log file.
  146. */
  147. static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  148. /* Records are always returned in ascending order of (name, path).
  149. ** If this will satisfy the client, set the orderByConsumed flag so that
  150. ** SQLite does not do an external sort.
  151. */
  152. if( ( pIdxInfo->nOrderBy==1
  153. && pIdxInfo->aOrderBy[0].iColumn==0
  154. && pIdxInfo->aOrderBy[0].desc==0
  155. ) ||
  156. ( pIdxInfo->nOrderBy==2
  157. && pIdxInfo->aOrderBy[0].iColumn==0
  158. && pIdxInfo->aOrderBy[0].desc==0
  159. && pIdxInfo->aOrderBy[1].iColumn==1
  160. && pIdxInfo->aOrderBy[1].desc==0
  161. )
  162. ){
  163. pIdxInfo->orderByConsumed = 1;
  164. }
  165. pIdxInfo->estimatedCost = 10.0;
  166. return SQLITE_OK;
  167. }
  168. /*
  169. ** Open a new statvfs cursor.
  170. */
  171. static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
  172. StatTable *pTab = (StatTable *)pVTab;
  173. StatCursor *pCsr;
  174. int rc;
  175. pCsr = (StatCursor *)sqlite3_malloc(sizeof(StatCursor));
  176. memset(pCsr, 0, sizeof(StatCursor));
  177. pCsr->base.pVtab = pVTab;
  178. rc = sqlite3_prepare_v2(pTab->db,
  179. "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
  180. " UNION ALL "
  181. "SELECT name, rootpage, type FROM sqlite_master WHERE rootpage!=0"
  182. " ORDER BY name", -1,
  183. &pCsr->pStmt, 0
  184. );
  185. if( rc!=SQLITE_OK ){
  186. sqlite3_free(pCsr);
  187. return rc;
  188. }
  189. *ppCursor = (sqlite3_vtab_cursor *)pCsr;
  190. return SQLITE_OK;
  191. }
  192. static void statClearPage(StatPage *p){
  193. int i;
  194. for(i=0; i<p->nCell; i++){
  195. sqlite3_free(p->aCell[i].aOvfl);
  196. }
  197. sqlite3PagerUnref(p->pPg);
  198. sqlite3_free(p->aCell);
  199. sqlite3_free(p->zPath);
  200. memset(p, 0, sizeof(StatPage));
  201. }
  202. static void statResetCsr(StatCursor *pCsr){
  203. int i;
  204. sqlite3_reset(pCsr->pStmt);
  205. for(i=0; i<ArraySize(pCsr->aPage); i++){
  206. statClearPage(&pCsr->aPage[i]);
  207. }
  208. pCsr->iPage = 0;
  209. sqlite3_free(pCsr->zPath);
  210. pCsr->zPath = 0;
  211. }
  212. /*
  213. ** Close a statvfs cursor.
  214. */
  215. static int statClose(sqlite3_vtab_cursor *pCursor){
  216. StatCursor *pCsr = (StatCursor *)pCursor;
  217. statResetCsr(pCsr);
  218. sqlite3_finalize(pCsr->pStmt);
  219. sqlite3_free(pCsr);
  220. return SQLITE_OK;
  221. }
  222. static void getLocalPayload(
  223. int nUsable, /* Usable bytes per page */
  224. u8 flags, /* Page flags */
  225. int nTotal, /* Total record (payload) size */
  226. int *pnLocal /* OUT: Bytes stored locally */
  227. ){
  228. int nLocal;
  229. int nMinLocal;
  230. int nMaxLocal;
  231. if( flags==0x0D ){ /* Table leaf node */
  232. nMinLocal = (nUsable - 12) * 32 / 255 - 23;
  233. nMaxLocal = nUsable - 35;
  234. }else{ /* Index interior and leaf nodes */
  235. nMinLocal = (nUsable - 12) * 32 / 255 - 23;
  236. nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
  237. }
  238. nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
  239. if( nLocal>nMaxLocal ) nLocal = nMinLocal;
  240. *pnLocal = nLocal;
  241. }
  242. static int statDecodePage(Btree *pBt, StatPage *p){
  243. int nUnused;
  244. int iOff;
  245. int nHdr;
  246. int isLeaf;
  247. int szPage;
  248. u8 *aData = sqlite3PagerGetData(p->pPg);
  249. u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
  250. p->flags = aHdr[0];
  251. p->nCell = get2byte(&aHdr[3]);
  252. p->nMxPayload = 0;
  253. isLeaf = (p->flags==0x0A || p->flags==0x0D);
  254. nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
  255. nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
  256. nUnused += (int)aHdr[7];
  257. iOff = get2byte(&aHdr[1]);
  258. while( iOff ){
  259. nUnused += get2byte(&aData[iOff+2]);
  260. iOff = get2byte(&aData[iOff]);
  261. }
  262. p->nUnused = nUnused;
  263. p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
  264. szPage = sqlite3BtreeGetPageSize(pBt);
  265. if( p->nCell ){
  266. int i; /* Used to iterate through cells */
  267. int nUsable = szPage - sqlite3BtreeGetReserve(pBt);
  268. p->aCell = sqlite3_malloc((p->nCell+1) * sizeof(StatCell));
  269. memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
  270. for(i=0; i<p->nCell; i++){
  271. StatCell *pCell = &p->aCell[i];
  272. iOff = get2byte(&aData[nHdr+i*2]);
  273. if( !isLeaf ){
  274. pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
  275. iOff += 4;
  276. }
  277. if( p->flags==0x05 ){
  278. /* A table interior node. nPayload==0. */
  279. }else{
  280. u32 nPayload; /* Bytes of payload total (local+overflow) */
  281. int nLocal; /* Bytes of payload stored locally */
  282. iOff += getVarint32(&aData[iOff], nPayload);
  283. if( p->flags==0x0D ){
  284. u64 dummy;
  285. iOff += sqlite3GetVarint(&aData[iOff], &dummy);
  286. }
  287. if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
  288. getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
  289. pCell->nLocal = nLocal;
  290. assert( nLocal>=0 );
  291. assert( nPayload>=(u32)nLocal );
  292. assert( nLocal<=(nUsable-35) );
  293. if( nPayload>(u32)nLocal ){
  294. int j;
  295. int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
  296. pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
  297. pCell->nOvfl = nOvfl;
  298. pCell->aOvfl = sqlite3_malloc(sizeof(u32)*nOvfl);
  299. pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
  300. for(j=1; j<nOvfl; j++){
  301. int rc;
  302. u32 iPrev = pCell->aOvfl[j-1];
  303. DbPage *pPg = 0;
  304. rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg);
  305. if( rc!=SQLITE_OK ){
  306. assert( pPg==0 );
  307. return rc;
  308. }
  309. pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
  310. sqlite3PagerUnref(pPg);
  311. }
  312. }
  313. }
  314. }
  315. }
  316. return SQLITE_OK;
  317. }
  318. /*
  319. ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
  320. ** the current value of pCsr->iPageno.
  321. */
  322. static void statSizeAndOffset(StatCursor *pCsr){
  323. StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
  324. Btree *pBt = pTab->db->aDb[0].pBt;
  325. Pager *pPager = sqlite3BtreePager(pBt);
  326. sqlite3_file *fd;
  327. sqlite3_int64 x[2];
  328. /* The default page size and offset */
  329. pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
  330. pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
  331. /* If connected to a ZIPVFS backend, override the page size and
  332. ** offset with actual values obtained from ZIPVFS.
  333. */
  334. fd = sqlite3PagerFile(pPager);
  335. x[0] = pCsr->iPageno;
  336. if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
  337. pCsr->iOffset = x[0];
  338. pCsr->szPage = (int)x[1];
  339. }
  340. }
  341. /*
  342. ** Move a statvfs cursor to the next entry in the file.
  343. */
  344. static int statNext(sqlite3_vtab_cursor *pCursor){
  345. int rc;
  346. int nPayload;
  347. StatCursor *pCsr = (StatCursor *)pCursor;
  348. StatTable *pTab = (StatTable *)pCursor->pVtab;
  349. Btree *pBt = pTab->db->aDb[0].pBt;
  350. Pager *pPager = sqlite3BtreePager(pBt);
  351. sqlite3_free(pCsr->zPath);
  352. pCsr->zPath = 0;
  353. if( pCsr->aPage[0].pPg==0 ){
  354. rc = sqlite3_step(pCsr->pStmt);
  355. if( rc==SQLITE_ROW ){
  356. int nPage;
  357. u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
  358. sqlite3PagerPagecount(pPager, &nPage);
  359. if( nPage==0 ){
  360. pCsr->isEof = 1;
  361. return sqlite3_reset(pCsr->pStmt);
  362. }
  363. rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg);
  364. pCsr->aPage[0].iPgno = iRoot;
  365. pCsr->aPage[0].iCell = 0;
  366. pCsr->aPage[0].zPath = sqlite3_mprintf("/");
  367. pCsr->iPage = 0;
  368. }else{
  369. pCsr->isEof = 1;
  370. return sqlite3_reset(pCsr->pStmt);
  371. }
  372. }else{
  373. /* Page p itself has already been visited. */
  374. StatPage *p = &pCsr->aPage[pCsr->iPage];
  375. while( p->iCell<p->nCell ){
  376. StatCell *pCell = &p->aCell[p->iCell];
  377. if( pCell->iOvfl<pCell->nOvfl ){
  378. int nUsable = sqlite3BtreeGetPageSize(pBt)-sqlite3BtreeGetReserve(pBt);
  379. pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
  380. pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
  381. pCsr->zPagetype = "overflow";
  382. pCsr->nCell = 0;
  383. pCsr->nMxPayload = 0;
  384. pCsr->zPath = sqlite3_mprintf(
  385. "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
  386. );
  387. if( pCell->iOvfl<pCell->nOvfl-1 ){
  388. pCsr->nUnused = 0;
  389. pCsr->nPayload = nUsable - 4;
  390. }else{
  391. pCsr->nPayload = pCell->nLastOvfl;
  392. pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
  393. }
  394. pCell->iOvfl++;
  395. statSizeAndOffset(pCsr);
  396. return SQLITE_OK;
  397. }
  398. if( p->iRightChildPg ) break;
  399. p->iCell++;
  400. }
  401. while( !p->iRightChildPg || p->iCell>p->nCell ){
  402. statClearPage(p);
  403. if( pCsr->iPage==0 ) return statNext(pCursor);
  404. pCsr->iPage--;
  405. p = &pCsr->aPage[pCsr->iPage];
  406. }
  407. pCsr->iPage++;
  408. assert( p==&pCsr->aPage[pCsr->iPage-1] );
  409. if( p->iCell==p->nCell ){
  410. p[1].iPgno = p->iRightChildPg;
  411. }else{
  412. p[1].iPgno = p->aCell[p->iCell].iChildPg;
  413. }
  414. rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg);
  415. p[1].iCell = 0;
  416. p[1].zPath = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
  417. p->iCell++;
  418. }
  419. /* Populate the StatCursor fields with the values to be returned
  420. ** by the xColumn() and xRowid() methods.
  421. */
  422. if( rc==SQLITE_OK ){
  423. int i;
  424. StatPage *p = &pCsr->aPage[pCsr->iPage];
  425. pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
  426. pCsr->iPageno = p->iPgno;
  427. statDecodePage(pBt, p);
  428. statSizeAndOffset(pCsr);
  429. switch( p->flags ){
  430. case 0x05: /* table internal */
  431. case 0x02: /* index internal */
  432. pCsr->zPagetype = "internal";
  433. break;
  434. case 0x0D: /* table leaf */
  435. case 0x0A: /* index leaf */
  436. pCsr->zPagetype = "leaf";
  437. break;
  438. default:
  439. pCsr->zPagetype = "corrupted";
  440. break;
  441. }
  442. pCsr->nCell = p->nCell;
  443. pCsr->nUnused = p->nUnused;
  444. pCsr->nMxPayload = p->nMxPayload;
  445. pCsr->zPath = sqlite3_mprintf("%s", p->zPath);
  446. nPayload = 0;
  447. for(i=0; i<p->nCell; i++){
  448. nPayload += p->aCell[i].nLocal;
  449. }
  450. pCsr->nPayload = nPayload;
  451. }
  452. return rc;
  453. }
  454. static int statEof(sqlite3_vtab_cursor *pCursor){
  455. StatCursor *pCsr = (StatCursor *)pCursor;
  456. return pCsr->isEof;
  457. }
  458. static int statFilter(
  459. sqlite3_vtab_cursor *pCursor,
  460. int idxNum, const char *idxStr,
  461. int argc, sqlite3_value **argv
  462. ){
  463. StatCursor *pCsr = (StatCursor *)pCursor;
  464. statResetCsr(pCsr);
  465. return statNext(pCursor);
  466. }
  467. static int statColumn(
  468. sqlite3_vtab_cursor *pCursor,
  469. sqlite3_context *ctx,
  470. int i
  471. ){
  472. StatCursor *pCsr = (StatCursor *)pCursor;
  473. switch( i ){
  474. case 0: /* name */
  475. sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_STATIC);
  476. break;
  477. case 1: /* path */
  478. sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
  479. break;
  480. case 2: /* pageno */
  481. sqlite3_result_int64(ctx, pCsr->iPageno);
  482. break;
  483. case 3: /* pagetype */
  484. sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
  485. break;
  486. case 4: /* ncell */
  487. sqlite3_result_int(ctx, pCsr->nCell);
  488. break;
  489. case 5: /* payload */
  490. sqlite3_result_int(ctx, pCsr->nPayload);
  491. break;
  492. case 6: /* unused */
  493. sqlite3_result_int(ctx, pCsr->nUnused);
  494. break;
  495. case 7: /* mx_payload */
  496. sqlite3_result_int(ctx, pCsr->nMxPayload);
  497. break;
  498. case 8: /* pgoffset */
  499. sqlite3_result_int64(ctx, pCsr->iOffset);
  500. break;
  501. case 9: /* pgsize */
  502. sqlite3_result_int(ctx, pCsr->szPage);
  503. break;
  504. }
  505. return SQLITE_OK;
  506. }
  507. static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
  508. StatCursor *pCsr = (StatCursor *)pCursor;
  509. *pRowid = pCsr->iPageno;
  510. return SQLITE_OK;
  511. }
  512. int sqlite3_dbstat_register(sqlite3 *db){
  513. static sqlite3_module dbstat_module = {
  514. 0, /* iVersion */
  515. statConnect, /* xCreate */
  516. statConnect, /* xConnect */
  517. statBestIndex, /* xBestIndex */
  518. statDisconnect, /* xDisconnect */
  519. statDisconnect, /* xDestroy */
  520. statOpen, /* xOpen - open a cursor */
  521. statClose, /* xClose - close a cursor */
  522. statFilter, /* xFilter - configure scan constraints */
  523. statNext, /* xNext - advance a cursor */
  524. statEof, /* xEof - check for end of scan */
  525. statColumn, /* xColumn - read data */
  526. statRowid, /* xRowid - read data */
  527. 0, /* xUpdate */
  528. 0, /* xBegin */
  529. 0, /* xSync */
  530. 0, /* xCommit */
  531. 0, /* xRollback */
  532. 0, /* xFindMethod */
  533. 0, /* xRename */
  534. };
  535. sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
  536. return SQLITE_OK;
  537. }
  538. #endif
  539. #if defined(SQLITE_TEST) || TCLSH==2
  540. #include <tcl.h>
  541. static int test_dbstat(
  542. void *clientData,
  543. Tcl_Interp *interp,
  544. int objc,
  545. Tcl_Obj *CONST objv[]
  546. ){
  547. #ifdef SQLITE_OMIT_VIRTUALTABLE
  548. Tcl_AppendResult(interp, "dbstat not available because of "
  549. "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
  550. return TCL_ERROR;
  551. #else
  552. struct SqliteDb { sqlite3 *db; };
  553. char *zDb;
  554. Tcl_CmdInfo cmdInfo;
  555. if( objc!=2 ){
  556. Tcl_WrongNumArgs(interp, 1, objv, "DB");
  557. return TCL_ERROR;
  558. }
  559. zDb = Tcl_GetString(objv[1]);
  560. if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
  561. sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
  562. sqlite3_dbstat_register(db);
  563. }
  564. return TCL_OK;
  565. #endif
  566. }
  567. int SqlitetestStat_Init(Tcl_Interp *interp){
  568. Tcl_CreateObjCommand(interp, "register_dbstat_vtab", test_dbstat, 0, 0);
  569. return TCL_OK;
  570. }
  571. #endif /* if defined(SQLITE_TEST) || TCLSH==2 */