1 ##########################################################################
4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 proc process_cmdline
{} {
12 cmdline
::process ::A $::argv {
13 {fts5
"use fts5 (this is the default)"}
15 {colsize
"10 10 10" "list of column sizes"}
16 {tblname
"t1" "table name to create"}
17 {detail
"full" "Fts5 detail mode to use"}
18 {repeat
1 "Load each file this many times"}
19 {prefix
"" "Fts prefix= option"}
20 {trans
1 "True to use a transaction"}
24 This script is designed to create fts4
/5 tables with more than one column.
25 The
-colsize option should be
set to a
Tcl list of integer values
, one
for
26 each column in the table. Each value is the number of tokens that will be
27 inserted into the column value
for each row. For example
, setting the
-colsize
28 option to
"5 10" creates an FTS table with
2 columns
, with roughly
5 and
10
29 tokens per row in each
, respectively.
31 Each
"FILE" argument should be a
text file. The contents of these
text files
32 is
split on whitespace characters to form a
list of tokens. The first N1
33 tokens are used
for the first column of the first row
, where N1 is the first
34 element of the
-colsize list. The next N2 are used
for the second column of
35 the first row
, and so on. Rows are added to the table until the entire
list
36 of tokens is exhausted.
40 ###########################################################################
41 ###########################################################################
42 # Command line options processor. This is generic code that can be copied
45 namespace eval cmdline
{
46 proc cmdline_error
{O E
{msg
""}} {
48 puts stderr
"Error: $msg"
54 if {[llength $o]==1} {
55 lappend L
[string toupper
$o]
59 puts stderr
"Usage: $::argv0 ?SWITCHES? $L"
61 puts stderr
"Switches are:"
63 if {[llength $o]==3} {
65 puts stderr
[format " -%-15s %s (default \"%s\")" "$a VAL" $c $b]
66 } elseif
{[llength $o]==2} {
68 puts stderr
[format " -%-15s %s" $a $b]
76 proc process
{avar lArgs O E
} {
78 set zTrailing
"" ;# True if ... is present in $O
81 # Populate A() with default values. Also, for each switch in the command
82 # line spec, set an entry in the idx() array as follows:
84 # {tblname t1 "table name to use"}
85 # -> [set idx(-tblname) {tblname t1 "table name to use"}
87 # For each position parameter, append its name to $lPosargs. If the ...
88 # specifier is present, set $zTrailing to the name of the prefix.
95 if {[string range
$nm end-2 end
]=="..."} {
96 set zTrailing
[string range
$nm 0 end-3
]
106 set A
($nm) [lindex $o 1]
110 error "Error in command line specification"
115 # Set explicitly specified option values
117 set nArg
[llength $lArgs]
118 for {set i
0} {$i < $nArg} {incr i
} {
119 set opt
[lindex $lArgs $i]
120 if {[string range
$opt 0 0]!="-" ||
$opt=="--"} break
121 set c
[array names idx
"${opt}*"]
122 if {[llength $c]==0} { cmdline_error
$O $E "Unrecognized option: $opt"}
123 if {[llength $c]>1} { cmdline_error
$O $E "Ambiguous option: $opt"}
125 if {[llength $idx($c)]==3} {
126 if {$i==[llength $lArgs]-1} {
127 cmdline_error
$O $E "Option requires argument: $c"
130 set A
([lindex $idx($c) 0]) [lindex $lArgs $i]
132 set A
([lindex $idx($c) 0]) 1
136 # Deal with position arguments.
138 set nPosarg
[llength $lPosargs]
139 set nRem
[expr $nArg - $i]
140 if {$nRem < $nPosarg ||
($zTrailing=="" && $nRem > $nPosarg)} {
143 for {set j
0} {$j < $nPosarg} {incr j
} {
144 set A
([lindex $lPosargs $j]) [lindex $lArgs [expr $j+$i]]
146 if {$zTrailing!=""} {
147 set A
($zTrailing) [lrange $lArgs [expr $j+$i] end
]
150 } ;# namespace eval cmdline
151 # End of command line options processor.
152 ###########################################################################
153 ###########################################################################
157 # If -fts4 was specified, use fts4. Otherwise, fts5.
164 sqlite3 db
$A(database
)
166 # Create the FTS table in the db. Return a list of the table columns.
168 proc create_table
{} {
170 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
]
172 set nCol
[llength $A(colsize
)]
173 set cols
[lrange $cols 0 [expr $nCol-1]]
175 set sql
"CREATE VIRTUAL TABLE IF NOT EXISTS $A(tblname) USING $A(fts) ("
176 append sql
[join $cols ,]
177 if {$A(fts
)=="fts5"} { append sql
",detail=$A(detail)" }
178 append sql
", prefix='$A(prefix)');"
184 # Return a list of tokens from the named file.
186 proc readfile
{file} {
195 for {set i
0} {$i < $n} {incr i
} {
196 set res
[concat $res $L]
202 # Load all the data into a big list of tokens.
206 set tokens
[concat $tokens [repeat
[readfile
$f] $A(repeat
)]]
209 set N
[llength $tokens]
211 set cols
[create_table
]
212 set sql
"INSERT INTO $A(tblname) VALUES(\$R([lindex $cols 0])"
213 foreach c
[lrange $cols 1 end
] {
214 append sql
", \$R($c)"
218 if {$A(trans
)} { db
eval BEGIN
}
220 foreach c
$cols s
$A(colsize
) {
221 set R
($c) [lrange $tokens $i [expr $i+$s-1]]
226 if {$A(trans
)} { db
eval COMMIT
}