summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/data/database.lua
blob: da1a2508cad67d7eb53de7d10cb8c4454f3a1bba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
local sql = require("lsqlite3")
local datfile = {}
local db

local function check_db(msg)
	if db:errcode() > sql.OK then
		emu.print_error("Error: " .. msg .. " (" .. db:errcode() .. " - " .. db:errmsg() .. ")\n")
	end
end

do
	local dbpath = emu.subst_env(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")
		if not db then
			emu.print_error("Unable to create history.db\n")
			return false
		end
		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

local dbtable = { prepare = function(...) return db:prepare(...) end,
		  exec = function(...) return db:exec(...) end, ROW = sql.ROW, check = check_db }

return db and dbtable or false