** Implementation of the ".dbinfo" command. ** ** Return 1 on error, 2 to exit, and 0 otherwise. */
| 15551 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
| 15552 | */ |
| 15553 | static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
| 15554 | static const struct { const char *zName; int ofst; } aField[] = { |
| 15555 | { "file change counter:", 24 }, |
| 15556 | { "database page count:", 28 }, |
| 15557 | { "freelist page count:", 36 }, |
| 15558 | { "schema cookie:", 40 }, |
| 15559 | { "schema format:", 44 }, |
| 15560 | { "default cache size:", 48 }, |
| 15561 | { "autovacuum top root:", 52 }, |
| 15562 | { "incremental vacuum:", 64 }, |
| 15563 | { "text encoding:", 56 }, |
| 15564 | { "user version:", 60 }, |
| 15565 | { "application id:", 68 }, |
| 15566 | { "software version:", 96 }, |
| 15567 | }; |
| 15568 | static const struct { const char *zName; const char *zSql; } aQuery[] = { |
| 15569 | { "number of tables:", |
| 15570 | "SELECT count(*) FROM %s WHERE type='table'" }, |
| 15571 | { "number of indexes:", |
| 15572 | "SELECT count(*) FROM %s WHERE type='index'" }, |
| 15573 | { "number of triggers:", |
| 15574 | "SELECT count(*) FROM %s WHERE type='trigger'" }, |
| 15575 | { "number of views:", |
| 15576 | "SELECT count(*) FROM %s WHERE type='view'" }, |
| 15577 | { "schema size:", |
| 15578 | "SELECT total(length(sql)) FROM %s" }, |
| 15579 | }; |
| 15580 | int i, rc; |
| 15581 | unsigned iDataVersion; |
| 15582 | char *zSchemaTab; |
| 15583 | char *zDb = nArg>=2 ? azArg[1] : "main"; |
| 15584 | sqlite3_stmt *pStmt = 0; |
| 15585 | unsigned char aHdr[100]; |
| 15586 | open_db(p, 0); |
| 15587 | if( p->db==0 ) return 1; |
| 15588 | rc = sqlite3_prepare_v2(p->db, |
| 15589 | "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", |
| 15590 | -1, &pStmt, 0); |
| 15591 | if( rc ){ |
| 15592 | utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); |
| 15593 | sqlite3_finalize(pStmt); |
| 15594 | return 1; |
| 15595 | } |
| 15596 | sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); |
| 15597 | if( sqlite3_step(pStmt)==SQLITE_ROW |
| 15598 | && sqlite3_column_bytes(pStmt,0)>100 |
| 15599 | ){ |
| 15600 | memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); |
| 15601 | sqlite3_finalize(pStmt); |
| 15602 | }else{ |
| 15603 | raw_printf(stderr, "unable to read database header\n"); |
| 15604 | sqlite3_finalize(pStmt); |
| 15605 | return 1; |
| 15606 | } |
| 15607 | i = get2byteInt(aHdr+16); |
| 15608 | if( i==1 ) i = 65536; |
| 15609 | utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
| 15610 | utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
no test coverage detected