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