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