xref: /sqlite-3.40.0/tool/showstat4.c (revision a9bc3cf7)
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;
42f127814eSdrh 
43f127814eSdrh   if( argc!=2 ){
44f127814eSdrh     fprintf(stderr, "Usage: %s DATABASE-FILE\n", argv[0]);
45f127814eSdrh     exit(1);
46f127814eSdrh   }
47f127814eSdrh   rc = sqlite3_open(argv[1], &db);
48f127814eSdrh   if( rc!=SQLITE_OK || db==0 ){
49f127814eSdrh     fprintf(stderr, "Cannot open database file [%s]\n", argv[1]);
50f127814eSdrh     exit(1);
51f127814eSdrh   }
52f127814eSdrh   rc = sqlite3_prepare_v2(db,
53f127814eSdrh         "SELECT tbl||'.'||idx, nEq, nLT, nDLt, sample "
54f127814eSdrh         "FROM sqlite_stat4 ORDER BY 1", -1,
55f127814eSdrh         &pStmt, 0);
56f127814eSdrh   if( rc!=SQLITE_OK || pStmt==0 ){
57f127814eSdrh     fprintf(stderr, "%s\n", sqlite3_errmsg(db));
58f127814eSdrh     sqlite3_close(db);
59f127814eSdrh     exit(1);
60f127814eSdrh   }
61f127814eSdrh   while( SQLITE_ROW==sqlite3_step(pStmt) ){
62f127814eSdrh     if( zIdx==0 || strcmp(zIdx, (const char*)sqlite3_column_text(pStmt,0))!=0 ){
63f127814eSdrh       if( zIdx ) printf("\n");
64f127814eSdrh       sqlite3_free(zIdx);
65f127814eSdrh       zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0));
66f127814eSdrh       printf("%s:\n", zIdx);
67f127814eSdrh     }else{
68f127814eSdrh       printf("  -----------------------------------------------------------\n");
69f127814eSdrh     }
70f127814eSdrh     printf("  nEq    = %s\n", sqlite3_column_text(pStmt,1));
71f127814eSdrh     printf("  nLt    = %s\n", sqlite3_column_text(pStmt,2));
72f127814eSdrh     printf("  nDLt   = %s\n", sqlite3_column_text(pStmt,3));
73f127814eSdrh     printf("  sample = x'");
74f127814eSdrh     aSample = sqlite3_column_blob(pStmt,4);
75f127814eSdrh     nSample = sqlite3_column_bytes(pStmt,4);
76f127814eSdrh     for(j=0; j<nSample; j++) printf("%02x", aSample[j]);
77f127814eSdrh     printf("'\n          ");
78f127814eSdrh     zSep = " ";
79f127814eSdrh     x = decodeVarint(aSample, &iVal);
80f127814eSdrh     if( iVal<x || iVal>nSample ){
81f127814eSdrh       printf(" <error>\n");
82f127814eSdrh       continue;
83f127814eSdrh     }
84f127814eSdrh     y = mxHdr = (int)iVal;
85f127814eSdrh     while( x<mxHdr ){
86f127814eSdrh       int sz;
87f127814eSdrh       i64 v;
88f127814eSdrh       x += decodeVarint(aSample+x, &iVal);
89f127814eSdrh       if( x>mxHdr ) break;
90f127814eSdrh       if( iVal<0 ) break;
91f127814eSdrh       switch( iVal ){
92f127814eSdrh         case 0:  sz = 0;  break;
93f127814eSdrh         case 1:  sz = 1;  break;
94f127814eSdrh         case 2:  sz = 2;  break;
95f127814eSdrh         case 3:  sz = 3;  break;
96f127814eSdrh         case 4:  sz = 4;  break;
97f127814eSdrh         case 5:  sz = 6;  break;
98f127814eSdrh         case 6:  sz = 8;  break;
99f127814eSdrh         case 7:  sz = 8;  break;
100f127814eSdrh         case 8:  sz = 0;  break;
101f127814eSdrh         case 9:  sz = 0;  break;
102f127814eSdrh         case 10:
103f127814eSdrh         case 11: sz = 0;  break;
104f127814eSdrh         default: sz = (int)(iVal-12)/2;  break;
105f127814eSdrh       }
106f127814eSdrh       if( y+sz>nSample ) break;
107f127814eSdrh       if( iVal==0 ){
108f127814eSdrh         printf("%sNULL", zSep);
109*a9bc3cf7Sdrh       }else if( iVal==8 || iVal==9 ){
110*a9bc3cf7Sdrh         printf("%s%d", zSep, ((int)iVal)-8);
111f127814eSdrh       }else if( iVal<=7 ){
112f127814eSdrh         v = (signed char)aSample[y];
113f127814eSdrh         for(j=1; j<sz; j++){
114f127814eSdrh           v = (v<<8) + aSample[y+j];
115f127814eSdrh         }
116f127814eSdrh         if( iVal==7 ){
117f127814eSdrh           double r;
118f127814eSdrh           memcpy(&r, &v, sizeof(r));
119f127814eSdrh           printf("%s%#g", zSep, r);
120f127814eSdrh         }else{
121f127814eSdrh           printf("%s%lld", zSep, v);
122f127814eSdrh         }
123f127814eSdrh       }else if( (iVal&1)==0 ){
124f127814eSdrh         printf("%sx'", zSep);
125f127814eSdrh         for(j=0; j<sz; j++){
126f127814eSdrh           printf("%02x", aSample[y+j]);
127f127814eSdrh         }
128f127814eSdrh         printf("'");
129f127814eSdrh       }else{
130f127814eSdrh         printf("%s\"", zSep);
131f127814eSdrh         for(j=0; j<sz; j++){
132f127814eSdrh           char c = (char)aSample[y+j];
133f127814eSdrh           if( isprint(c) ){
134f127814eSdrh             if( c=='"' || c=='\\' ) putchar('\\');
135f127814eSdrh             putchar(c);
136f127814eSdrh           }else if( c=='\n' ){
137f127814eSdrh             printf("\\n");
138f127814eSdrh           }else if( c=='\t' ){
139f127814eSdrh             printf("\\t");
140f127814eSdrh           }else if( c=='\r' ){
141f127814eSdrh             printf("\\r");
142f127814eSdrh           }else{
143f127814eSdrh             printf("\\%03o", c);
144f127814eSdrh           }
145f127814eSdrh         }
146f127814eSdrh         printf("\"");
147f127814eSdrh       }
148f127814eSdrh       zSep = ",";
149f127814eSdrh       y += sz;
150f127814eSdrh     }
151f127814eSdrh     printf("\n");
152f127814eSdrh   }
153f127814eSdrh   sqlite3_free(zIdx);
154f127814eSdrh   sqlite3_finalize(pStmt);
155f127814eSdrh   sqlite3_close(db);
156f127814eSdrh   return 0;
157f127814eSdrh }
158