sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / fts5 / tool / showfts5.tcl
| 1 | |
| 2 | |
| 3 | |
| 4 | #------------------------------------------------------------------------- |
| 5 | # Process command line arguments. |
| 6 | # |
| 7 | proc usage {} { |
| 8 | puts stderr "usage: $::argv0 ?OPTIONS? database table" |
| 9 | puts stderr "" |
| 10 | puts stderr " -nterm (count number of terms in each segment)" |
| 11 | puts stderr " -segments (output segment contents)" |
| 12 | puts stderr "" |
| 13 | exit 1 |
| 14 | } |
| 15 | |
| 16 | set O(nterm) 0 |
| 17 | set O(segments) 0 |
| 18 | |
| 19 | if {[llength $argv]<2} usage |
| 20 | foreach a [lrange $argv 0 end-2] { |
| 21 | switch -- $a { |
| 22 | -nterm { |
| 23 | set O(nterm) 1 |
| 24 | } |
| 25 | |
| 26 | -segments { |
| 27 | set O(segments) 1 |
| 28 | } |
| 29 | |
| 30 | default { |
| 31 | usage |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | set database [lindex $argv end-1] |
| 37 | set tbl [lindex $argv end] |
| 38 | |
| 39 | |
| 40 | #------------------------------------------------------------------------- |
| 41 | # Count the number of terms in each segment of fts5 table $tbl. Store the |
| 42 | # counts in the array variable in the parent context named by parameter |
| 43 | # $arrayname, indexed by segment-id. Example: |
| 44 | # |
| 45 | # count_terms fts_tbl A |
| 46 | # foreach {k v} [array get A] { puts "segid=$k nTerm=$v" } |
| 47 | # |
| 48 | proc count_terms {tbl arrayname} { |
| 49 | upvar A $arrayname |
| 50 | array unset A |
| 51 | db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data" { |
| 52 | set desc [lindex $d 0] |
| 53 | if {[regexp {^segid=([0-9]*)} $desc -> id]} { |
| 54 | foreach i [lrange $d 1 end] { |
| 55 | if {[string match {term=*} $i]} { incr A($id) } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | #------------------------------------------------------------------------- |
| 63 | # Start of main program. |
| 64 | # |
| 65 | sqlite3 db $database |
| 66 | catch { load_static_extension db fts5 } |
| 67 | |
| 68 | if {$O(nterm)} { count_terms $tbl A } |
| 69 | |
| 70 | db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data WHERE id=10" { |
| 71 | foreach lvl [lrange $d 1 end] { |
| 72 | puts [lrange $lvl 0 2] |
| 73 | |
| 74 | foreach seg [lrange $lvl 3 end] { |
| 75 | if {$::O(nterm)} { |
| 76 | regexp {^id=([0-9]*)} $seg -> id |
| 77 | set nTerm 0 |
| 78 | catch { set nTerm $A($id) } |
| 79 | puts [format " % -28s nTerm=%d" $seg $nTerm] |
| 80 | } else { |
| 81 | puts [format " % -28s" $seg] |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if {$O(segments)} { |
| 88 | puts "" |
| 89 | db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data WHERE id>10" { |
| 90 | puts $d |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 |