drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 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 | |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 12 | #define ISPRINT(X) isprint((unsigned char)(X)) |
| 13 | |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 14 | typedef 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 | */ |
| 21 | static 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 | |
| 35 | int 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; |
drh | 547fb61 | 2014-11-04 21:38:45 +0000 | [diff] [blame] | 44 | int iRow = 0; |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 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 ){ |
drh | 547fb61 | 2014-11-04 21:38:45 +0000 | [diff] [blame] | 66 | if( zIdx ) printf("\n**************************************" |
| 67 | "**************\n\n"); |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 68 | sqlite3_free(zIdx); |
| 69 | zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0)); |
drh | 547fb61 | 2014-11-04 21:38:45 +0000 | [diff] [blame] | 70 | iRow = 0; |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 71 | } |
drh | 547fb61 | 2014-11-04 21:38:45 +0000 | [diff] [blame] | 72 | printf("%s sample %d ------------------------------------\n", zIdx, ++iRow); |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 73 | printf(" nEq = %s\n", sqlite3_column_text(pStmt,1)); |
| 74 | printf(" nLt = %s\n", sqlite3_column_text(pStmt,2)); |
| 75 | printf(" nDLt = %s\n", sqlite3_column_text(pStmt,3)); |
| 76 | printf(" sample = x'"); |
| 77 | aSample = sqlite3_column_blob(pStmt,4); |
| 78 | nSample = sqlite3_column_bytes(pStmt,4); |
| 79 | for(j=0; j<nSample; j++) printf("%02x", aSample[j]); |
| 80 | printf("'\n "); |
| 81 | zSep = " "; |
| 82 | x = decodeVarint(aSample, &iVal); |
| 83 | if( iVal<x || iVal>nSample ){ |
| 84 | printf(" <error>\n"); |
| 85 | continue; |
| 86 | } |
| 87 | y = mxHdr = (int)iVal; |
| 88 | while( x<mxHdr ){ |
| 89 | int sz; |
| 90 | i64 v; |
| 91 | x += decodeVarint(aSample+x, &iVal); |
| 92 | if( x>mxHdr ) break; |
| 93 | if( iVal<0 ) break; |
| 94 | switch( iVal ){ |
| 95 | case 0: sz = 0; break; |
| 96 | case 1: sz = 1; break; |
| 97 | case 2: sz = 2; break; |
| 98 | case 3: sz = 3; break; |
| 99 | case 4: sz = 4; break; |
| 100 | case 5: sz = 6; break; |
| 101 | case 6: sz = 8; break; |
| 102 | case 7: sz = 8; break; |
| 103 | case 8: sz = 0; break; |
| 104 | case 9: sz = 0; break; |
| 105 | case 10: |
| 106 | case 11: sz = 0; break; |
| 107 | default: sz = (int)(iVal-12)/2; break; |
| 108 | } |
| 109 | if( y+sz>nSample ) break; |
| 110 | if( iVal==0 ){ |
| 111 | printf("%sNULL", zSep); |
drh | a9bc3cf | 2014-06-24 20:19:21 +0000 | [diff] [blame] | 112 | }else if( iVal==8 || iVal==9 ){ |
| 113 | printf("%s%d", zSep, ((int)iVal)-8); |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 114 | }else if( iVal<=7 ){ |
| 115 | v = (signed char)aSample[y]; |
| 116 | for(j=1; j<sz; j++){ |
| 117 | v = (v<<8) + aSample[y+j]; |
| 118 | } |
| 119 | if( iVal==7 ){ |
| 120 | double r; |
drh | a4e54e8 | 2017-07-15 17:57:31 +0000 | [diff] [blame] | 121 | char *z; |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 122 | memcpy(&r, &v, sizeof(r)); |
drh | a4e54e8 | 2017-07-15 17:57:31 +0000 | [diff] [blame] | 123 | z = sqlite3_mprintf("%s%!.15g", zSep, r); |
| 124 | printf("%s", z); |
| 125 | sqlite3_free(z); |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 126 | }else{ |
| 127 | printf("%s%lld", zSep, v); |
| 128 | } |
| 129 | }else if( (iVal&1)==0 ){ |
| 130 | printf("%sx'", zSep); |
| 131 | for(j=0; j<sz; j++){ |
| 132 | printf("%02x", aSample[y+j]); |
| 133 | } |
| 134 | printf("'"); |
| 135 | }else{ |
drh | 4c17742 | 2016-12-08 23:52:38 +0000 | [diff] [blame] | 136 | printf("%s'", zSep); |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 137 | for(j=0; j<sz; j++){ |
| 138 | char c = (char)aSample[y+j]; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 139 | if( ISPRINT(c) ){ |
drh | 4c17742 | 2016-12-08 23:52:38 +0000 | [diff] [blame] | 140 | if( c=='\'' || c=='\\' ) putchar('\\'); |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 141 | putchar(c); |
| 142 | }else if( c=='\n' ){ |
| 143 | printf("\\n"); |
| 144 | }else if( c=='\t' ){ |
| 145 | printf("\\t"); |
| 146 | }else if( c=='\r' ){ |
| 147 | printf("\\r"); |
| 148 | }else{ |
| 149 | printf("\\%03o", c); |
| 150 | } |
| 151 | } |
drh | 4c17742 | 2016-12-08 23:52:38 +0000 | [diff] [blame] | 152 | printf("'"); |
drh | f127814 | 2014-06-24 00:59:15 +0000 | [diff] [blame] | 153 | } |
| 154 | zSep = ","; |
| 155 | y += sz; |
| 156 | } |
| 157 | printf("\n"); |
| 158 | } |
| 159 | sqlite3_free(zIdx); |
| 160 | sqlite3_finalize(pStmt); |
| 161 | sqlite3_close(db); |
| 162 | return 0; |
| 163 | } |