/ Check-in [497409e1]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Convert the implementation of the ".dbstat" dot-command of the command-line shell to use the sqlite_dbpage table.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dbpage
Files: files | file ages | folders
SHA3-256: 497409e167c7c025fbddc319b4fa9a8b965f70d05ac88c060dee469f70321388
User & Date: drh 2017-10-11 17:51:08
Context
2017-10-11
18:00
Add new extension "checkfreelist", which uses sqlite_dbpage to check that there are no invalid entries on the database free-list. check-in: 21930ef5 user: dan tags: dbpage
17:51
Convert the implementation of the ".dbstat" dot-command of the command-line shell to use the sqlite_dbpage table. check-in: 497409e1 user: drh tags: dbpage
17:13
Enable sqlite_dbpage and dbstat virtual tables in the shell, by default. check-in: 01bf856c user: drh tags: dbpage
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/shell.c.

  4966   4966        { "number of triggers:",
  4967   4967          "SELECT count(*) FROM %s WHERE type='trigger'" },
  4968   4968        { "number of views:",
  4969   4969          "SELECT count(*) FROM %s WHERE type='view'" },
  4970   4970        { "schema size:",
  4971   4971          "SELECT total(length(sql)) FROM %s" },
  4972   4972     };
  4973         -  sqlite3_file *pFile = 0;
  4974   4973     int i;
  4975   4974     char *zSchemaTab;
  4976   4975     char *zDb = nArg>=2 ? azArg[1] : "main";
         4976  +  sqlite3_stmt *pStmt = 0;
  4977   4977     unsigned char aHdr[100];
  4978   4978     open_db(p, 0);
  4979   4979     if( p->db==0 ) return 1;
  4980         -  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile);
  4981         -  if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){
  4982         -    return 1;
  4983         -  }
  4984         -  i = pFile->pMethods->xRead(pFile, aHdr, 100, 0);
  4985         -  if( i!=SQLITE_OK ){
         4980  +  sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
         4981  +                     -1, &pStmt, 0);
         4982  +  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
         4983  +  if( sqlite3_step(pStmt)==SQLITE_ROW
         4984  +   && sqlite3_column_bytes(pStmt,0)>100
         4985  +  ){
         4986  +    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
         4987  +    sqlite3_finalize(pStmt);
         4988  +  }else{
  4986   4989       raw_printf(stderr, "unable to read database header\n");
         4990  +    sqlite3_finalize(pStmt);
  4987   4991       return 1;
  4988   4992     }
  4989   4993     i = get2byteInt(aHdr+16);
  4990   4994     if( i==1 ) i = 65536;
  4991   4995     utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
  4992   4996     utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
  4993   4997     utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);

Changes to src/shell.c.in.

  3606   3606        { "number of triggers:",
  3607   3607          "SELECT count(*) FROM %s WHERE type='trigger'" },
  3608   3608        { "number of views:",
  3609   3609          "SELECT count(*) FROM %s WHERE type='view'" },
  3610   3610        { "schema size:",
  3611   3611          "SELECT total(length(sql)) FROM %s" },
  3612   3612     };
  3613         -  sqlite3_file *pFile = 0;
  3614   3613     int i;
  3615   3614     char *zSchemaTab;
  3616   3615     char *zDb = nArg>=2 ? azArg[1] : "main";
         3616  +  sqlite3_stmt *pStmt = 0;
  3617   3617     unsigned char aHdr[100];
  3618   3618     open_db(p, 0);
  3619   3619     if( p->db==0 ) return 1;
  3620         -  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile);
  3621         -  if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){
  3622         -    return 1;
  3623         -  }
  3624         -  i = pFile->pMethods->xRead(pFile, aHdr, 100, 0);
  3625         -  if( i!=SQLITE_OK ){
         3620  +  sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
         3621  +                     -1, &pStmt, 0);
         3622  +  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
         3623  +  if( sqlite3_step(pStmt)==SQLITE_ROW
         3624  +   && sqlite3_column_bytes(pStmt,0)>100
         3625  +  ){
         3626  +    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
         3627  +    sqlite3_finalize(pStmt);
         3628  +  }else{
  3626   3629       raw_printf(stderr, "unable to read database header\n");
         3630  +    sqlite3_finalize(pStmt);
  3627   3631       return 1;
  3628   3632     }
  3629   3633     i = get2byteInt(aHdr+16);
  3630   3634     if( i==1 ) i = 65536;
  3631   3635     utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
  3632   3636     utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
  3633   3637     utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);