showstat4.c 4.3 KB

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