diff options
-rw-r--r-- | plugins/data/database.lua | 10 | ||||
-rw-r--r-- | plugins/data/load_dat.lua | 13 |
2 files changed, 23 insertions, 0 deletions
diff --git a/plugins/data/database.lua b/plugins/data/database.lua index 5925766dcfe..9e1fd7ae855 100644 --- a/plugins/data/database.lua +++ b/plugins/data/database.lua @@ -1,23 +1,33 @@ local sql = require("lsqlite3") local datfile = {} local db + +function check_db(msg) + if db:errcode() > sqlite3.OK then + io.stderr:write("Error: " .. msg .. " (" .. db:errcode() .. " - " .. db:errmsg() .. ")\n") + end +end + do local dbpath = lfs.env_replace(mame_manager:ui():options().entries.historypath:value():match("([^;]+)")) db = sql.open(dbpath .. "/history.db") if not db then lfs.mkdir(dbpath) db = sql.open(dbpath .. "/history.db") + check_db("opening database") end end if db then local found = false db:exec("select * from sqlite_master where name = 'version'", function() found = true return 0 end) + check_db("checking for 'version' table") if not found then db:exec([[ CREATE TABLE version ( version VARCHAR NOT NULL, datfile VARCHAR UNIQUE NOT NULL)]]) + check_db("creating 'version' table") end end diff --git a/plugins/data/load_dat.lua b/plugins/data/load_dat.lua index fd53b610d4a..9f365921328 100644 --- a/plugins/data/load_dat.lua +++ b/plugins/data/load_dat.lua @@ -9,6 +9,7 @@ function datfile.open(file, vertag, fixupcb) local data local stmt = db:prepare("SELECT f.data FROM \"" .. file .. "_idx\" AS fi, \"" .. file .. [[" AS f WHERE fi.type = ? AND fi.val = ? AND fi.romset = ? AND f.rowid = fi.data]]) + check_db("reading " .. tag1 .. " - " .. tag2 .. " - " .. set) stmt:bind_values(tag1, tag2, set) if stmt:step() == sql.ROW then data = stmt:get_value(0) @@ -32,6 +33,7 @@ function datfile.open(file, vertag, fixupcb) file = file:gsub("[^%w%._]", "") local stmt = db:prepare("SELECT version FROM version WHERE datfile = ?") + check_db("reading version") stmt:bind_values(file) if stmt:step() == sql.ROW then dbver = stmt:get_value(0) @@ -49,8 +51,11 @@ function datfile.open(file, vertag, fixupcb) val VARCHAR NOT NULL, romset VARCHAR NOT NULL, data INTEGER NOT NULL)]]) + check_db("creating index") db:exec("CREATE TABLE \"" .. file .. "\" (data CLOB NOT NULL)") + check_db("creating table") db:exec("CREATE INDEX \"typeval_" .. file .. "\" ON \"" .. file .. "_idx\"(type, val)") + check_db("creating typeval index") end if vertag then @@ -72,10 +77,14 @@ function datfile.open(file, vertag, fixupcb) if dbver then db:exec("DELETE FROM \"" .. file .. "\"") + check_db("deleting") db:exec("DELETE FROM \"" .. file .. "_idx\"") + check_db("deleting index") stmt = db:prepare("UPDATE version SET version = ? WHERE datfile = ?") + check_db("updating version") else stmt = db:prepare("INSERT INTO version VALUES (?, ?)") + check_db("inserting version") end stmt:bind_values(ver, file) stmt:step() @@ -86,6 +95,7 @@ function datfile.open(file, vertag, fixupcb) fh:seek("set") local buffer = fh:read("a") db:exec("BEGIN TRANSACTION") + check_db("beginning transaction") local function gmatchpos() local pos = 1 local function iter() @@ -127,6 +137,7 @@ function datfile.open(file, vertag, fixupcb) data = fixupcb(data) end stmt = db:prepare("INSERT INTO \"" .. file .. "\" VALUES (?)") + check_db("inserting values") stmt:bind_values(data) stmt:step() local row = stmt:last_insert_rowid() @@ -137,6 +148,7 @@ function datfile.open(file, vertag, fixupcb) fixupcb(data) end stmt = db:prepare("INSERT INTO \"" .. file .. "_idx\" VALUES (?, ?, ?, ?)") + check_db("inserting into index") stmt:bind_values(tag1, tag2, set, row) stmt:step() stmt:finalize() @@ -146,6 +158,7 @@ function datfile.open(file, vertag, fixupcb) end end db:exec("END TRANSACTION") + check_db("ending transaction") end fh:close() |