OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 proc usage {} { |
| 4 puts stderr "$::argv0 ?OPTIONS? DATABASE FILE1..." |
| 5 puts stderr "" |
| 6 puts stderr "Options are" |
| 7 puts stderr " -fts5" |
| 8 puts stderr " -fts4" |
| 9 puts stderr " -colsize <list of column sizes>" |
| 10 puts stderr { |
| 11 This script is designed to create fts4/5 tables with more than one column. |
| 12 The -colsize option should be set to a Tcl list of integer values, one for |
| 13 each column in the table. Each value is the number of tokens that will be |
| 14 inserted into the column value for each row. For example, setting the -colsize |
| 15 option to "5 10" creates an FTS table with 2 columns, with roughly 5 and 10 |
| 16 tokens per row in each, respectively. |
| 17 |
| 18 Each "FILE" argument should be a text file. The contents of these text files is |
| 19 split on whitespace characters to form a list of tokens. The first N1 tokens |
| 20 are used for the first column of the first row, where N1 is the first element |
| 21 of the -colsize list. The next N2 are used for the second column of the first |
| 22 row, and so on. Rows are added to the table until the entire list of tokens |
| 23 is exhausted. |
| 24 } |
| 25 exit -1 |
| 26 } |
| 27 |
| 28 set O(aColSize) [list 10 10 10] |
| 29 set O(tblname) t1 |
| 30 set O(fts) fts5 |
| 31 |
| 32 |
| 33 set options_with_values {-colsize} |
| 34 |
| 35 for {set i 0} {$i < [llength $argv]} {incr i} { |
| 36 set opt [lindex $argv $i] |
| 37 if {[string range $opt 0 0]!="-"} break |
| 38 |
| 39 if {[lsearch $options_with_values $opt]>=0} { |
| 40 incr i |
| 41 if {$i==[llength $argv]} usage |
| 42 set val [lindex $argv $i] |
| 43 } |
| 44 |
| 45 switch -- $opt { |
| 46 -colsize { |
| 47 set O(aColSize) $val |
| 48 } |
| 49 |
| 50 -fts4 { |
| 51 set O(fts) fts4 |
| 52 } |
| 53 |
| 54 -fts5 { |
| 55 set O(fts) fts5 |
| 56 } |
| 57 } |
| 58 } |
| 59 |
| 60 if {$i > [llength $argv]-2} usage |
| 61 set O(db) [lindex $argv $i] |
| 62 set O(files) [lrange $argv [expr $i+1] end] |
| 63 |
| 64 sqlite3 db $O(db) |
| 65 |
| 66 # Create the FTS table in the db. Return a list of the table columns. |
| 67 # |
| 68 proc create_table {} { |
| 69 global O |
| 70 set cols [list a b c d e f g h i j k l m n o p q r s t u v w x y z] |
| 71 |
| 72 set nCol [llength $O(aColSize)] |
| 73 set cols [lrange $cols 0 [expr $nCol-1]] |
| 74 |
| 75 set sql "CREATE VIRTUAL TABLE IF NOT EXISTS $O(tblname) USING $O(fts) (" |
| 76 append sql [join $cols ,] |
| 77 append sql ");" |
| 78 |
| 79 db eval $sql |
| 80 return $cols |
| 81 } |
| 82 |
| 83 # Return a list of tokens from the named file. |
| 84 # |
| 85 proc readfile {file} { |
| 86 set fd [open $file] |
| 87 set data [read $fd] |
| 88 close $fd |
| 89 split $data |
| 90 } |
| 91 |
| 92 |
| 93 # Load all the data into a big list of tokens. |
| 94 # |
| 95 set tokens [list] |
| 96 foreach f $O(files) { |
| 97 set tokens [concat $tokens [readfile $f]] |
| 98 } |
| 99 |
| 100 set N [llength $tokens] |
| 101 set i 0 |
| 102 set cols [create_table] |
| 103 set sql "INSERT INTO $O(tblname) VALUES(\$[lindex $cols 0]" |
| 104 foreach c [lrange $cols 1 end] { |
| 105 append sql ", \$A($c)" |
| 106 } |
| 107 append sql ")" |
| 108 |
| 109 db eval BEGIN |
| 110 while {$i < $N} { |
| 111 foreach c $cols s $O(aColSize) { |
| 112 set A($c) [lrange $tokens $i [expr $i+$s-1]] |
| 113 incr i $s |
| 114 } |
| 115 db eval $sql |
| 116 } |
| 117 db eval COMMIT |
| 118 |
| 119 |
| 120 |
OLD | NEW |