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