0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2025-06-04 21:41:48 +00:00
Files
.cargo
.github
bindings
bottomless
bottomless-cli
docker-compose
docs
ext
libsql
libsql-ffi
libsql-replication
libsql-server
libsql-shell
libsql-sqlite3
art
autoconf
contrib
crates
doc
ext
mptest
src
test
tool
win
GetFile.cs
GetTclKit.bat
Replace.cs
build-all-msvc.bat
build-shell.sh
cg_anno.tcl
checkSpacing.c
cktclsh.sh
custom.txt
dbhash.c
dbtotxt.c
dbtotxt.md
enlargedb.c
extract-sqlite3h.tcl
extract.c
fast_vacuum.c
fragck.tcl
fuzzershell.c
generate-artifacts.sh
genfkey.README
genfkey.test
getlock.c
index_usage.c
kvtest-speed.sh
lemon.c
lempar.c
libvers.c
loadfts.c
logest.c
max-limits.c
merge-test.tcl
mkautoconfamal.sh
mkccode.tcl
mkctimec.tcl
mkkeywordhash.c
mkmsvcmin.tcl
mkopcodec.tcl
mkopcodeh.tcl
mkopts.tcl
mkpragmatab.tcl
mkshellc.tcl
mksourceid.c
mkspeedsql.tcl
mksqlite3c-noext.tcl
mksqlite3c.tcl
mksqlite3h.tcl
mksqlite3internalh.tcl
mktoolzip.tcl
mkvsix.tcl
offsets.c
omittest-msvc.tcl
omittest.tcl
opcodesum.tcl
pagesig.c
pre-release.sh
replace.tcl
restore_jrnl.tcl
rollback-test.c
run-speed-test.sh
showdb.c
showjournal.c
showlocks.c
showshm.c
showstat4.c
showwal.c
soak1.tcl
spaceanal.tcl
speed-check.sh
speedtest.tcl
speedtest16.c
speedtest2.tcl
speedtest8.c
speedtest8inst1.c
spellsift.tcl
split-sqlite3c.tcl
sqldiff.c
sqlite3_analyzer.c.in
sqltclsh.c.in
sqltclsh.tcl
src-verify.c
srcck1.c
srctree-check.tcl
stack_usage.tcl
stripccomments.c
symbols-mingw.sh
symbols.sh
varint.c
vdbe-compress.tcl
vdbe_profile.tcl
version-info.c
warnings-clang.sh
warnings.sh
vsixtest
.gitignore
Dockerfile-wasm-udf
LIBSQL_VERSION
LICENSE.md
Makefile.in
Makefile.linux-gcc
Makefile.msc
README-SQLite.md
VERSION
aclocal.m4
config.guess
config.sub
configure
configure.ac
install-sh
libsql.pc.in
ltmain.sh
magic.txt
main.mk
manifest
manifest.uuid
spec.template
sqlite.pc.in
sqlite3.1
sqlite3.pc.in
sqlite_cfg.h.in
libsql-sys
libsql-sys-tmp
vendored
xtask
.dockerignore
.env
.gitignore
CODE_OF_CONDUCT.md
Cargo.lock
Cargo.toml
Dockerfile
Dockerfile.dev
LICENSE.md
README-libsql.md
README.md
docker-entrypoint.sh
fly.toml
rust-toolchain.toml
2023-10-16 13:58:16 +02:00

164 lines
4.3 KiB
C

/*
** This utility program decodes and displays the content of the
** sqlite_stat4 table in the database file named on the command
** line.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "sqlite3.h"
#define ISPRINT(X) isprint((unsigned char)(X))
typedef sqlite3_int64 i64; /* 64-bit signed integer type */
/*
** Convert the var-int format into i64. Return the number of bytes
** in the var-int. Write the var-int value into *pVal.
*/
static int decodeVarint(const unsigned char *z, i64 *pVal){
i64 v = 0;
int i;
for(i=0; i<8; i++){
v = (v<<7) + (z[i]&0x7f);
if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
}
v = (v<<8) + (z[i]&0xff);
*pVal = v;
return 9;
}
int main(int argc, char **argv){
sqlite3 *db;
sqlite3_stmt *pStmt;
char *zIdx = 0;
int rc, j, x, y, mxHdr;
const unsigned char *aSample;
int nSample;
i64 iVal;
const char *zSep;
int iRow = 0;
if( argc!=2 ){
fprintf(stderr, "Usage: %s DATABASE-FILE\n", argv[0]);
exit(1);
}
rc = sqlite3_open(argv[1], &db);
if( rc!=SQLITE_OK || db==0 ){
fprintf(stderr, "Cannot open database file [%s]\n", argv[1]);
exit(1);
}
rc = sqlite3_prepare_v2(db,
"SELECT tbl||'.'||idx, nEq, nLT, nDLt, sample "
"FROM sqlite_stat4 ORDER BY 1", -1,
&pStmt, 0);
if( rc!=SQLITE_OK || pStmt==0 ){
fprintf(stderr, "%s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
while( SQLITE_ROW==sqlite3_step(pStmt) ){
if( zIdx==0 || strcmp(zIdx, (const char*)sqlite3_column_text(pStmt,0))!=0 ){
if( zIdx ) printf("\n**************************************"
"**************\n\n");
sqlite3_free(zIdx);
zIdx = sqlite3_mprintf("%s", sqlite3_column_text(pStmt,0));
iRow = 0;
}
printf("%s sample %d ------------------------------------\n", zIdx, ++iRow);
printf(" nEq = %s\n", sqlite3_column_text(pStmt,1));
printf(" nLt = %s\n", sqlite3_column_text(pStmt,2));
printf(" nDLt = %s\n", sqlite3_column_text(pStmt,3));
printf(" sample = x'");
aSample = sqlite3_column_blob(pStmt,4);
nSample = sqlite3_column_bytes(pStmt,4);
for(j=0; j<nSample; j++) printf("%02x", aSample[j]);
printf("'\n ");
zSep = " ";
x = decodeVarint(aSample, &iVal);
if( iVal<x || iVal>nSample ){
printf(" <error>\n");
continue;
}
y = mxHdr = (int)iVal;
while( x<mxHdr ){
int sz;
i64 v;
x += decodeVarint(aSample+x, &iVal);
if( x>mxHdr ) break;
if( iVal<0 ) break;
switch( iVal ){
case 0: sz = 0; break;
case 1: sz = 1; break;
case 2: sz = 2; break;
case 3: sz = 3; break;
case 4: sz = 4; break;
case 5: sz = 6; break;
case 6: sz = 8; break;
case 7: sz = 8; break;
case 8: sz = 0; break;
case 9: sz = 0; break;
case 10:
case 11: sz = 0; break;
default: sz = (int)(iVal-12)/2; break;
}
if( y+sz>nSample ) break;
if( iVal==0 ){
printf("%sNULL", zSep);
}else if( iVal==8 || iVal==9 ){
printf("%s%d", zSep, ((int)iVal)-8);
}else if( iVal<=7 ){
v = (signed char)aSample[y];
for(j=1; j<sz; j++){
v = (v<<8) + aSample[y+j];
}
if( iVal==7 ){
double r;
char *z;
memcpy(&r, &v, sizeof(r));
z = sqlite3_mprintf("%s%!.15g", zSep, r);
printf("%s", z);
sqlite3_free(z);
}else{
printf("%s%lld", zSep, v);
}
}else if( (iVal&1)==0 ){
printf("%sx'", zSep);
for(j=0; j<sz; j++){
printf("%02x", aSample[y+j]);
}
printf("'");
}else{
printf("%s'", zSep);
for(j=0; j<sz; j++){
char c = (char)aSample[y+j];
if( ISPRINT(c) ){
if( c=='\'' || c=='\\' ) putchar('\\');
putchar(c);
}else if( c=='\n' ){
printf("\\n");
}else if( c=='\t' ){
printf("\\t");
}else if( c=='\r' ){
printf("\\r");
}else{
printf("\\%03o", c);
}
}
printf("'");
}
zSep = ",";
y += sz;
}
printf("\n");
}
sqlite3_free(zIdx);
sqlite3_finalize(pStmt);
sqlite3_close(db);
return 0;
}