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