OLD | NEW |
1 /* | 1 /* |
2 ** This utility program decodes and displays the content of the | 2 ** This utility program decodes and displays the content of the |
3 ** sqlite_stat4 table in the database file named on the command | 3 ** sqlite_stat4 table in the database file named on the command |
4 ** line. | 4 ** line. |
5 */ | 5 */ |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <string.h> | 7 #include <string.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <ctype.h> | 9 #include <ctype.h> |
10 #include "sqlite3.h" | 10 #include "sqlite3.h" |
11 | 11 |
| 12 #define ISPRINT(X) isprint((unsigned char)(X)) |
| 13 |
12 typedef sqlite3_int64 i64; /* 64-bit signed integer type */ | 14 typedef sqlite3_int64 i64; /* 64-bit signed integer type */ |
13 | 15 |
14 | 16 |
15 /* | 17 /* |
16 ** Convert the var-int format into i64. Return the number of bytes | 18 ** Convert the var-int format into i64. Return the number of bytes |
17 ** in the var-int. Write the var-int value into *pVal. | 19 ** in the var-int. Write the var-int value into *pVal. |
18 */ | 20 */ |
19 static int decodeVarint(const unsigned char *z, i64 *pVal){ | 21 static int decodeVarint(const unsigned char *z, i64 *pVal){ |
20 i64 v = 0; | 22 i64 v = 0; |
21 int i; | 23 int i; |
(...skipping 10 matching lines...) Expand all Loading... |
32 | 34 |
33 int main(int argc, char **argv){ | 35 int main(int argc, char **argv){ |
34 sqlite3 *db; | 36 sqlite3 *db; |
35 sqlite3_stmt *pStmt; | 37 sqlite3_stmt *pStmt; |
36 char *zIdx = 0; | 38 char *zIdx = 0; |
37 int rc, j, x, y, mxHdr; | 39 int rc, j, x, y, mxHdr; |
38 const unsigned char *aSample; | 40 const unsigned char *aSample; |
39 int nSample; | 41 int nSample; |
40 i64 iVal; | 42 i64 iVal; |
41 const char *zSep; | 43 const char *zSep; |
| 44 int iRow = 0; |
42 | 45 |
43 if( argc!=2 ){ | 46 if( argc!=2 ){ |
44 fprintf(stderr, "Usage: %s DATABASE-FILE\n", argv[0]); | 47 fprintf(stderr, "Usage: %s DATABASE-FILE\n", argv[0]); |
45 exit(1); | 48 exit(1); |
46 } | 49 } |
47 rc = sqlite3_open(argv[1], &db); | 50 rc = sqlite3_open(argv[1], &db); |
48 if( rc!=SQLITE_OK || db==0 ){ | 51 if( rc!=SQLITE_OK || db==0 ){ |
49 fprintf(stderr, "Cannot open database file [%s]\n", argv[1]); | 52 fprintf(stderr, "Cannot open database file [%s]\n", argv[1]); |
50 exit(1); | 53 exit(1); |
51 } | 54 } |
52 rc = sqlite3_prepare_v2(db, | 55 rc = sqlite3_prepare_v2(db, |
53 "SELECT tbl||'.'||idx, nEq, nLT, nDLt, sample " | 56 "SELECT tbl||'.'||idx, nEq, nLT, nDLt, sample " |
54 "FROM sqlite_stat4 ORDER BY 1", -1, | 57 "FROM sqlite_stat4 ORDER BY 1", -1, |
55 &pStmt, 0); | 58 &pStmt, 0); |
56 if( rc!=SQLITE_OK || pStmt==0 ){ | 59 if( rc!=SQLITE_OK || pStmt==0 ){ |
57 fprintf(stderr, "%s\n", sqlite3_errmsg(db)); | 60 fprintf(stderr, "%s\n", sqlite3_errmsg(db)); |
58 sqlite3_close(db); | 61 sqlite3_close(db); |
59 exit(1); | 62 exit(1); |
60 } | 63 } |
61 while( SQLITE_ROW==sqlite3_step(pStmt) ){ | 64 while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
62 if( zIdx==0 || strcmp(zIdx, (const char*)sqlite3_column_text(pStmt,0))!=0 ){ | 65 if( zIdx==0 || strcmp(zIdx, (const char*)sqlite3_column_text(pStmt,0))!=0 ){ |
63 if( zIdx ) printf("\n"); | 66 if( zIdx ) printf("\n**************************************" |
| 67 "**************\n\n"); |
64 sqlite3_free(zIdx); | 68 sqlite3_free(zIdx); |
65 zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0)); | 69 zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0)); |
66 printf("%s:\n", zIdx); | 70 iRow = 0; |
67 }else{ | |
68 printf(" -----------------------------------------------------------\n"); | |
69 } | 71 } |
| 72 printf("%s sample %d ------------------------------------\n", zIdx, ++iRow); |
70 printf(" nEq = %s\n", sqlite3_column_text(pStmt,1)); | 73 printf(" nEq = %s\n", sqlite3_column_text(pStmt,1)); |
71 printf(" nLt = %s\n", sqlite3_column_text(pStmt,2)); | 74 printf(" nLt = %s\n", sqlite3_column_text(pStmt,2)); |
72 printf(" nDLt = %s\n", sqlite3_column_text(pStmt,3)); | 75 printf(" nDLt = %s\n", sqlite3_column_text(pStmt,3)); |
73 printf(" sample = x'"); | 76 printf(" sample = x'"); |
74 aSample = sqlite3_column_blob(pStmt,4); | 77 aSample = sqlite3_column_blob(pStmt,4); |
75 nSample = sqlite3_column_bytes(pStmt,4); | 78 nSample = sqlite3_column_bytes(pStmt,4); |
76 for(j=0; j<nSample; j++) printf("%02x", aSample[j]); | 79 for(j=0; j<nSample; j++) printf("%02x", aSample[j]); |
77 printf("'\n "); | 80 printf("'\n "); |
78 zSep = " "; | 81 zSep = " "; |
79 x = decodeVarint(aSample, &iVal); | 82 x = decodeVarint(aSample, &iVal); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 }else if( (iVal&1)==0 ){ | 126 }else if( (iVal&1)==0 ){ |
124 printf("%sx'", zSep); | 127 printf("%sx'", zSep); |
125 for(j=0; j<sz; j++){ | 128 for(j=0; j<sz; j++){ |
126 printf("%02x", aSample[y+j]); | 129 printf("%02x", aSample[y+j]); |
127 } | 130 } |
128 printf("'"); | 131 printf("'"); |
129 }else{ | 132 }else{ |
130 printf("%s\"", zSep); | 133 printf("%s\"", zSep); |
131 for(j=0; j<sz; j++){ | 134 for(j=0; j<sz; j++){ |
132 char c = (char)aSample[y+j]; | 135 char c = (char)aSample[y+j]; |
133 if( isprint(c) ){ | 136 if( ISPRINT(c) ){ |
134 if( c=='"' || c=='\\' ) putchar('\\'); | 137 if( c=='"' || c=='\\' ) putchar('\\'); |
135 putchar(c); | 138 putchar(c); |
136 }else if( c=='\n' ){ | 139 }else if( c=='\n' ){ |
137 printf("\\n"); | 140 printf("\\n"); |
138 }else if( c=='\t' ){ | 141 }else if( c=='\t' ){ |
139 printf("\\t"); | 142 printf("\\t"); |
140 }else if( c=='\r' ){ | 143 }else if( c=='\r' ){ |
141 printf("\\r"); | 144 printf("\\r"); |
142 }else{ | 145 }else{ |
143 printf("\\%03o", c); | 146 printf("\\%03o", c); |
144 } | 147 } |
145 } | 148 } |
146 printf("\""); | 149 printf("\""); |
147 } | 150 } |
148 zSep = ","; | 151 zSep = ","; |
149 y += sz; | 152 y += sz; |
150 } | 153 } |
151 printf("\n"); | 154 printf("\n"); |
152 } | 155 } |
153 sqlite3_free(zIdx); | 156 sqlite3_free(zIdx); |
154 sqlite3_finalize(pStmt); | 157 sqlite3_finalize(pStmt); |
155 sqlite3_close(db); | 158 sqlite3_close(db); |
156 return 0; | 159 return 0; |
157 } | 160 } |
OLD | NEW |