/ Check-in [e82e883b]
Login

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

Overview
Comment:Begin putting functionality into the sqlite3_checker binary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | checkindex
Files: files | file ages | folders
SHA3-256: e82e883b93128e4d1105a82abe8d1860c0a15505b6ca421e187b9bbbc2fdc659
User & Date: drh 2017-11-01 00:10:34
Context
2017-11-01
01:01
Actually perform index checks when running sqlite3_checker check-in: 54530020 user: drh tags: checkindex
00:10
Begin putting functionality into the sqlite3_checker binary. check-in: e82e883b user: drh tags: checkindex
2017-10-31
18:15
Merge all recent enhancements from trunk. check-in: 24adf90f user: drh tags: checkindex
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/repair/sqlite3_checker.tcl.

     1      1   # Read and run TCL commands from standard input.  Used to implement
     2      2   # the --tclsh option.
     3      3   # This TCL script is the main driver script for the sqlite3_checker utility
     4      4   # program.
     5      5   #
     6      6   
            7  +# Special case:
            8  +#
            9  +#      sqlite3_checker --test FILENAME ARGS
           10  +#
           11  +# uses FILENAME in place of this script.
           12  +#
           13  +if {[lindex $argv 0]=="--test" && [llength $argv]>2} {
           14  +  set file [lindex $argv 1]
           15  +  set argv [lrange $argv 2 end]
           16  +  source $file
           17  +  exit 0
           18  +}
           19  +
           20  +# Emulate a TCL shell
           21  +#
     7     22   proc tclsh {} {
     8     23     set line {}
     9     24     while {![eof stdin]} {
    10     25       if {$line!=""} {
    11     26         puts -nonewline "> "
    12     27       } else {
    13     28         puts -nonewline "% "
................................................................................
    23     38         set line {}
    24     39       } else {
    25     40         append line \n
    26     41       }
    27     42     }
    28     43   }
    29     44   
           45  +# Print a usage message on standard error, then quit.
           46  +#
    30     47   proc usage {} {
    31     48     set argv0 [file rootname [file tail [info nameofexecutable]]]
    32     49     puts stderr "Usage: $argv0 OPTIONS database-filename"
    33     50     puts stderr {
    34     51   Do sanity checking on a live SQLite3 database file specified by the
    35     52   "database-filename" argument.
    36     53   
    37     54   Options:
           55  +
           56  +   --freelist   Perform a freelist check
    38     57   
    39     58      --tclsh      Run the built-in TCL interpreter interactively (for debugging)
    40     59   
    41     60      --version    Show the version number of SQLite
    42     61   }
    43     62     exit 1
    44     63   }
    45     64   
    46     65   set file_to_analyze {}
    47     66   append argv {}
           67  +set bFreelistCheck 0
           68  +set bSummary 1
    48     69   foreach arg $argv {
    49     70     if {[regexp {^-+tclsh$} $arg]} {
    50     71       tclsh
    51     72       exit 0
    52     73     }
    53     74     if {[regexp {^-+version$} $arg]} {
    54     75       sqlite3 mem :memory:
    55     76       puts [mem one {SELECT sqlite_version()||' '||sqlite_source_id()}]
    56     77       mem close
    57     78       exit 0
           79  +  }
           80  +  if {[regexp {^-+freelist$} $arg]} {
           81  +    set bFreelistCheck 1
           82  +    set bSummary 0
           83  +    continue
    58     84     }
    59     85     if {[regexp {^-} $arg]} {
    60     86       puts stderr "Unknown option: $arg"
    61     87       usage
    62     88     }
    63     89     if {$file_to_analyze!=""} {
    64     90       usage
................................................................................
    82    108     puts stderr "No such file: $root_filename"
    83    109     exit 1
    84    110   }
    85    111   if {![file readable $root_filename]} {
    86    112     puts stderr "File is not readable: $root_filename"
    87    113     exit 1
    88    114   }
          115  +
          116  +if {[catch {sqlite3 db $file_to_analyze} res]} {
          117  +  puts stderr "Cannot open datababase $root_filename: $res"
          118  +  exit 1
          119  +}
          120  +
          121  +if {$bFreelistCheck} {
          122  +  puts "freelist-check:"
          123  +  flush stdout
          124  +  puts [db one {SELECT checkfreelist('main')}]
          125  +}
          126  +if {$bSummary} {
          127  +  set scale 0
          128  +  set pgsz [db one {PRAGMA page_size}]
          129  +  db eval {SELECT nPage*$pgsz AS sz, name, tbl_name
          130  +             FROM sqlite_btreeinfo
          131  +            WHERE type='index'
          132  +            ORDER BY 1 DESC, name} {
          133  +    if {$scale==0} {
          134  +      if {$sz>10000000} {
          135  +        set scale 1000000.0
          136  +        set unit MB
          137  +      } else {
          138  +        set scale 1000.0
          139  +        set unit KB
          140  +      }
          141  +    }
          142  +    puts [format {%7.1f %s index %s of table %s} \
          143  +            [expr {$sz/$scale}] $unit $name $tbl_name]
          144  +  }
          145  +}