/ Check-in [e87d02d2]
Login

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

Overview
Comment:Add to sqlite3_analyzer command-line options --version and --tclsh, and also the undocumented --debug option.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e87d02d289a2016ea3ee074e914b07a8ac22b21f
User & Date: drh 2016-10-12 18:26:26
Context
2016-10-12
18:55
Avoid reading the -1-th element of an array in the query planner. Fix to a bug introduced by check-in [8e2b25f9b8a7] from earlier today. Curiously, the problem only appeared on 32-bit systems. check-in: 443913d5 user: drh tags: trunk
18:26
Add to sqlite3_analyzer command-line options --version and --tclsh, and also the undocumented --debug option. check-in: e87d02d2 user: drh tags: trunk
15:15
New testcase() macros to ensure coverage of the ORDER BY LIMIT optimization code in where.c. check-in: 61f05269 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to tool/spaceanal.tcl.

    17     17         if {0==[db one { SELECT count(*) FROM sqlite_master WHERE name=$n }]} {
    18     18           return 1
    19     19         }
    20     20       }
    21     21     }
    22     22     return 0
    23     23   }
           24  +
           25  +# Read and run TCL commands from standard input.  Used to implement
           26  +# the --tclsh option.
           27  +#
           28  +proc tclsh {} {
           29  +  set line {}
           30  +  while {![eof stdin]} {
           31  +    if {$line!=""} {
           32  +      puts -nonewline "> "
           33  +    } else {
           34  +      puts -nonewline "% "
           35  +    }
           36  +    flush stdout
           37  +    append line [gets stdin]
           38  +    if {[info complete $line]} {
           39  +      if {[catch {uplevel #0 $line} result]} {
           40  +        puts stderr "Error: $result"
           41  +      } elseif {$result!=""} {
           42  +        puts $result
           43  +      }
           44  +      set line {}
           45  +    } else {
           46  +      append line \n
           47  +    }
           48  +  }
           49  +}
           50  +
    24     51   
    25     52   # Get the name of the database to analyze
    26     53   #
    27     54   proc usage {} {
    28     55     set argv0 [file rootname [file tail [info nameofexecutable]]]
    29     56     puts stderr "Usage: $argv0 ?--pageinfo? ?--stats? database-filename"
    30     57     puts stderr {
    31     58   Analyze the SQLite3 database file specified by the "database-filename"
    32     59   argument and output a report detailing size and storage efficiency
    33     60   information for the database and its constituent tables and indexes.
    34     61   
    35     62   Options:
           63  +
           64  +   --pageinfo   Show how each page of the database-file is used
    36     65   
    37     66      --stats        Output SQL text that creates a new database containing
    38     67                     statistics about the database that was analyzed
    39     68   
    40         -   --pageinfo     Show how each page of the database-file is used
           69  +   --tclsh      Run the built-in TCL interpreter interactively (for debugging)
           70  +
           71  +   --version    Show the version number of SQLite
    41     72   }
    42     73     exit 1
    43     74   }
    44     75   set file_to_analyze {}
    45     76   set flags(-pageinfo) 0
    46     77   set flags(-stats) 0
           78  +set flags(-debug) 0
    47     79   append argv {}
    48     80   foreach arg $argv {
    49     81     if {[regexp {^-+pageinfo$} $arg]} {
    50     82       set flags(-pageinfo) 1
    51     83     } elseif {[regexp {^-+stats$} $arg]} {
    52     84       set flags(-stats) 1
           85  +  } elseif {[regexp {^-+debug$} $arg]} {
           86  +    set flags(-debug) 1
           87  +  } elseif {[regexp {^-+tclsh$} $arg]} {
           88  +    tclsh
           89  +    exit 0
           90  +  } elseif {[regexp {^-+version$} $arg]} {
           91  +    sqlite3 mem :memory:
           92  +    puts [mem one {SELECT sqlite_version()||' '||sqlite_source_id()}]
           93  +    mem close
           94  +    exit 0
    53     95     } elseif {[regexp {^-} $arg]} {
    54     96       puts stderr "Unknown option: $arg"
    55     97       usage
    56     98     } elseif {$file_to_analyze!=""} {
    57     99       usage
    58    100     } else {
    59    101       set file_to_analyze $arg
................................................................................
    96    138   
    97    139   # Open the database
    98    140   #
    99    141   if {[catch {sqlite3 db $file_to_analyze -uri 1} msg]} {
   100    142     puts stderr "error trying to open $file_to_analyze: $msg"
   101    143     exit 1
   102    144   }
          145  +if {$flags(-debug)} {
          146  +  proc dbtrace {txt} {puts $txt; flush stdout;}
          147  +  db trace ::dbtrace
          148  +}
   103    149   
   104    150   db eval {SELECT count(*) FROM sqlite_master}
   105    151   set pageSize [expr {wide([db one {PRAGMA page_size}])}]
   106    152   
   107    153   if {$flags(-pageinfo)} {
   108    154     db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat}
   109    155     db eval {SELECT name, path, pageno FROM temp.stat ORDER BY pageno} {
................................................................................
   137    183                     quote(pgoffset) || ',' ||
   138    184                     quote(pgsize) AS x FROM stat} {
   139    185       puts "INSERT INTO stats VALUES($x);"
   140    186     }
   141    187     puts "COMMIT;"
   142    188     exit 0
   143    189   }
          190  +
   144    191   
   145    192   # In-memory database for collecting statistics. This script loops through
   146    193   # the tables and indices in the database being analyzed, adding a row for each
   147    194   # to an in-memory database (for which the schema is shown below). It then
   148    195   # queries the in-memory db to produce the space-analysis report.
   149    196   #
   150    197   sqlite3 mem :memory:
          198  +if {$flags(-debug)} {
          199  +  proc dbtrace {txt} {puts $txt; flush stdout;}
          200  +  mem trace ::dbtrace
          201  +}
   151    202   set tabledef {CREATE TABLE space_used(
   152    203      name clob,        -- Name of a table or index in the database file
   153    204      tblname clob,     -- Name of associated table
   154    205      is_index boolean, -- TRUE if it is an index, false for a table
   155    206      is_without_rowid boolean, -- TRUE if WITHOUT ROWID table  
   156    207      nentry int,       -- Number of entries in the BTree
   157    208      leaf_entries int, -- Number of leaf entries