xref: /sqlite-3.40.0/tool/showstat4.c (revision f127814e)
1*f127814eSdrh /*
2*f127814eSdrh ** This utility program decodes and displays the content of the
3*f127814eSdrh ** sqlite_stat4 table in the database file named on the command
4*f127814eSdrh ** line.
5*f127814eSdrh */
6*f127814eSdrh #include <stdio.h>
7*f127814eSdrh #include <string.h>
8*f127814eSdrh #include <stdlib.h>
9*f127814eSdrh #include <ctype.h>
10*f127814eSdrh #include "sqlite3.h"
11*f127814eSdrh 
12*f127814eSdrh typedef sqlite3_int64 i64;   /* 64-bit signed integer type */
13*f127814eSdrh 
14*f127814eSdrh 
15*f127814eSdrh /*
16*f127814eSdrh ** Convert the var-int format into i64.  Return the number of bytes
17*f127814eSdrh ** in the var-int.  Write the var-int value into *pVal.
18*f127814eSdrh */
19*f127814eSdrh static int decodeVarint(const unsigned char *z, i64 *pVal){
20*f127814eSdrh   i64 v = 0;
21*f127814eSdrh   int i;
22*f127814eSdrh   for(i=0; i<8; i++){
23*f127814eSdrh     v = (v<<7) + (z[i]&0x7f);
24*f127814eSdrh     if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
25*f127814eSdrh   }
26*f127814eSdrh   v = (v<<8) + (z[i]&0xff);
27*f127814eSdrh   *pVal = v;
28*f127814eSdrh   return 9;
29*f127814eSdrh }
30*f127814eSdrh 
31*f127814eSdrh 
32*f127814eSdrh 
33*f127814eSdrh int main(int argc, char **argv){
34*f127814eSdrh   sqlite3 *db;
35*f127814eSdrh   sqlite3_stmt *pStmt;
36*f127814eSdrh   char *zIdx = 0;
37*f127814eSdrh   int rc, j, x, y, mxHdr;
38*f127814eSdrh   const unsigned char *aSample;
39*f127814eSdrh   int nSample;
40*f127814eSdrh   i64 iVal;
41*f127814eSdrh   const char *zSep;
42*f127814eSdrh 
43*f127814eSdrh   if( argc!=2 ){
44*f127814eSdrh     fprintf(stderr, "Usage: %s DATABASE-FILE\n", argv[0]);
45*f127814eSdrh     exit(1);
46*f127814eSdrh   }
47*f127814eSdrh   rc = sqlite3_open(argv[1], &db);
48*f127814eSdrh   if( rc!=SQLITE_OK || db==0 ){
49*f127814eSdrh     fprintf(stderr, "Cannot open database file [%s]\n", argv[1]);
50*f127814eSdrh     exit(1);
51*f127814eSdrh   }
52*f127814eSdrh   rc = sqlite3_prepare_v2(db,
53*f127814eSdrh         "SELECT tbl||'.'||idx, nEq, nLT, nDLt, sample "
54*f127814eSdrh         "FROM sqlite_stat4 ORDER BY 1", -1,
55*f127814eSdrh         &pStmt, 0);
56*f127814eSdrh   if( rc!=SQLITE_OK || pStmt==0 ){
57*f127814eSdrh     fprintf(stderr, "%s\n", sqlite3_errmsg(db));
58*f127814eSdrh     sqlite3_close(db);
59*f127814eSdrh     exit(1);
60*f127814eSdrh   }
61*f127814eSdrh   while( SQLITE_ROW==sqlite3_step(pStmt) ){
62*f127814eSdrh     if( zIdx==0 || strcmp(zIdx, (const char*)sqlite3_column_text(pStmt,0))!=0 ){
63*f127814eSdrh       if( zIdx ) printf("\n");
64*f127814eSdrh       sqlite3_free(zIdx);
65*f127814eSdrh       zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0));
66*f127814eSdrh       printf("%s:\n", zIdx);
67*f127814eSdrh     }else{
68*f127814eSdrh       printf("  -----------------------------------------------------------\n");
69*f127814eSdrh     }
70*f127814eSdrh     printf("  nEq    = %s\n", sqlite3_column_text(pStmt,1));
71*f127814eSdrh     printf("  nLt    = %s\n", sqlite3_column_text(pStmt,2));
72*f127814eSdrh     printf("  nDLt   = %s\n", sqlite3_column_text(pStmt,3));
73*f127814eSdrh     printf("  sample = x'");
74*f127814eSdrh     aSample = sqlite3_column_blob(pStmt,4);
75*f127814eSdrh     nSample = sqlite3_column_bytes(pStmt,4);
76*f127814eSdrh     for(j=0; j<nSample; j++) printf("%02x", aSample[j]);
77*f127814eSdrh     printf("'\n          ");
78*f127814eSdrh     zSep = " ";
79*f127814eSdrh     x = decodeVarint(aSample, &iVal);
80*f127814eSdrh     if( iVal<x || iVal>nSample ){
81*f127814eSdrh       printf(" <error>\n");
82*f127814eSdrh       continue;
83*f127814eSdrh     }
84*f127814eSdrh     y = mxHdr = (int)iVal;
85*f127814eSdrh     while( x<mxHdr ){
86*f127814eSdrh       int sz;
87*f127814eSdrh       i64 v;
88*f127814eSdrh       x += decodeVarint(aSample+x, &iVal);
89*f127814eSdrh       if( x>mxHdr ) break;
90*f127814eSdrh       if( iVal<0 ) break;
91*f127814eSdrh       switch( iVal ){
92*f127814eSdrh         case 0:  sz = 0;  break;
93*f127814eSdrh         case 1:  sz = 1;  break;
94*f127814eSdrh         case 2:  sz = 2;  break;
95*f127814eSdrh         case 3:  sz = 3;  break;
96*f127814eSdrh         case 4:  sz = 4;  break;
97*f127814eSdrh         case 5:  sz = 6;  break;
98*f127814eSdrh         case 6:  sz = 8;  break;
99*f127814eSdrh         case 7:  sz = 8;  break;
100*f127814eSdrh         case 8:  sz = 0;  break;
101*f127814eSdrh         case 9:  sz = 0;  break;
102*f127814eSdrh         case 10:
103*f127814eSdrh         case 11: sz = 0;  break;
104*f127814eSdrh         default: sz = (int)(iVal-12)/2;  break;
105*f127814eSdrh       }
106*f127814eSdrh       if( y+sz>nSample ) break;
107*f127814eSdrh       if( iVal==0 ){
108*f127814eSdrh         printf("%sNULL", zSep);
109*f127814eSdrh       }else if( iVal<=7 ){
110*f127814eSdrh         v = (signed char)aSample[y];
111*f127814eSdrh         for(j=1; j<sz; j++){
112*f127814eSdrh           v = (v<<8) + aSample[y+j];
113*f127814eSdrh         }
114*f127814eSdrh         if( iVal==7 ){
115*f127814eSdrh           double r;
116*f127814eSdrh           memcpy(&r, &v, sizeof(r));
117*f127814eSdrh           printf("%s%#g", zSep, r);
118*f127814eSdrh         }else{
119*f127814eSdrh           printf("%s%lld", zSep, v);
120*f127814eSdrh         }
121*f127814eSdrh       }else if( (iVal&1)==0 ){
122*f127814eSdrh         printf("%sx'", zSep);
123*f127814eSdrh         for(j=0; j<sz; j++){
124*f127814eSdrh           printf("%02x", aSample[y+j]);
125*f127814eSdrh         }
126*f127814eSdrh         printf("'");
127*f127814eSdrh       }else{
128*f127814eSdrh         printf("%s\"", zSep);
129*f127814eSdrh         for(j=0; j<sz; j++){
130*f127814eSdrh           char c = (char)aSample[y+j];
131*f127814eSdrh           if( isprint(c) ){
132*f127814eSdrh             if( c=='"' || c=='\\' ) putchar('\\');
133*f127814eSdrh             putchar(c);
134*f127814eSdrh           }else if( c=='\n' ){
135*f127814eSdrh             printf("\\n");
136*f127814eSdrh           }else if( c=='\t' ){
137*f127814eSdrh             printf("\\t");
138*f127814eSdrh           }else if( c=='\r' ){
139*f127814eSdrh             printf("\\r");
140*f127814eSdrh           }else{
141*f127814eSdrh             printf("\\%03o", c);
142*f127814eSdrh           }
143*f127814eSdrh         }
144*f127814eSdrh         printf("\"");
145*f127814eSdrh       }
146*f127814eSdrh       zSep = ",";
147*f127814eSdrh       y += sz;
148*f127814eSdrh     }
149*f127814eSdrh     printf("\n");
150*f127814eSdrh   }
151*f127814eSdrh   sqlite3_free(zIdx);
152*f127814eSdrh   sqlite3_finalize(pStmt);
153*f127814eSdrh   sqlite3_close(db);
154*f127814eSdrh   return 0;
155*f127814eSdrh }
156