interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / tool / showstat4.c

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