Posted by
| Nick Gammon
Australia (23,120 posts) Bio
Forum Administrator |
Message
| The help database does not ship with the FTS tables created, to save space. These tables can be quickly created the first time the database is accessed, for example in a plugin's OnPluginInstall function:
-- for fixing up entities
local entities = {
["<"] = "<";
[">"] = ">";
["&"] = "&";
["""] = "\"";
} -- end of entities
local function fix_description (s)
if not s then
return ""
end -- if
-- get rid of tags
s = s:gsub ("</?%a+/?>", "")
-- convert entities ...
s = s:gsub ("&%a-;", entities)
return s
end -- fix_description
function OnPluginInstall ()
-- open database on disk
if not db then
db = assert (sqlite3.open(GetInfo (66) .. "help.db"))
end -- if
local commands = false
-- see if commands table exists
for row in db:nrows("SELECT * FROM sqlite_master WHERE type = 'table' AND name = 'commands'") do
commands = true
end
if not commands then
ColourNote ("red", "", "MUSHclient help database 'help.db' not found.")
db:close ()
db = nil
return
end -- if
local fts4 = false
-- see if fts4 tables exist
for row in db:nrows("SELECT * FROM sqlite_master WHERE type = 'table' AND name = 'commands_lookup'") do
fts4 = true
end
-- if no fts4 tables, make them
if not fts4 then
local start = utils.timer ()
ColourNote ("cyan", "", "Creating help database full-text lookup tables ...")
-- START
assert (db:execute [[
BEGIN TRANSACTION;
DROP TABLE IF EXISTS commands_lookup;
DROP TABLE IF EXISTS dialogs_lookup;
DROP TABLE IF EXISTS functions_lookup;
DROP TABLE IF EXISTS general_doc_lookup;
DROP TABLE IF EXISTS errors_lookup;
DROP TABLE IF EXISTS lua_functions_lookup;
]])
-- COMMANDS
assert (db:execute "CREATE VIRTUAL TABLE commands_lookup USING FTS4(name, summary, description)")
-- fix up HTML stuff
for row in db:nrows("SELECT command_name, short_description, description FROM commands") do
assert (db:execute (string.format ([[
INSERT INTO commands_lookup (name, summary, description)
VALUES (%s, %s, %s)]],
fixsql (row.command_name),
fixsql (row.short_description),
fixsql (fix_description (row.description)))))
end -- for
-- DIALOGS
assert (db:execute "CREATE VIRTUAL TABLE dialogs_lookup USING FTS4(name, summary, description)")
-- fix up HTML stuff
for row in db:nrows("SELECT dialog_name, title, description FROM dialogs") do
assert (db:execute (string.format ([[
INSERT INTO dialogs_lookup (name, summary, description)
VALUES (%s, %s, %s)]],
fixsql (row.dialog_name),
fixsql (row.title),
fixsql (fix_description (row.description)))))
end -- for
-- WORLD FUNCTIONS
assert (db:execute "CREATE VIRTUAL TABLE functions_lookup USING FTS4(name, summary, description, lua_example, lua_notes)")
assert (db:execute [[INSERT INTO functions_lookup (name, summary, description, lua_example, lua_notes)
SELECT name, summary, description, lua_example, lua_notes FROM functions]])
-- GENERAL TOPICS
assert (db:execute "CREATE VIRTUAL TABLE general_doc_lookup USING FTS4(name, summary, description)")
-- fix up HTML stuff
for row in db:nrows("SELECT doc_name, title, description FROM general_doc") do
assert (db:execute (string.format ([[
INSERT INTO general_doc_lookup (name, summary, description)
VALUES (%s, %s, %s)]],
fixsql (row.doc_name),
fixsql (row.title),
fixsql (fix_description (row.description)))))
end -- for
-- ERRORS
assert (db:execute "CREATE VIRTUAL TABLE errors_lookup USING FTS4(name, error_code, description)")
assert (db:execute [[INSERT INTO errors_lookup (name, error_code, description)
SELECT error_name, error_code, meaning FROM errors ]])
-- LUA FUNCTIONS
assert (db:execute "CREATE VIRTUAL TABLE lua_functions_lookup USING FTS4(name, summary, description)")
-- fix up HTML stuff
for row in db:nrows("SELECT name, summary, description FROM lua_functions") do
assert (db:execute (string.format ([[
INSERT INTO lua_functions_lookup (name, summary, description)
VALUES (%s, %s, %s)]],
fixsql (row.name),
fixsql (row.summary),
fixsql (fix_description (row.description)))))
end -- for
-- DONE
assert (db:execute "COMMIT;")
ColourNote ("cyan", "", string.format ("Done. Took %0.3f seconds.", utils.timer () - start))
end -- if
end -- OnPluginInstall
On my PC this took about 0.2 seconds to run.
[EDIT] Modified FTS creation to remove tags like <code> from the lookup database (otherwise you see annoying things like <code> in the snippets). Also convert < to < and so on.
Also modified to check that the database is present (by checking for the "commands" table). SQLite3 tends to create an empty database on opening, so the fact that the open succeeds does not guarantee you have any tables present. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|