local csexp = require"csexp" local sqlite = require"lsqlite3" local function sqlite_ok(errcode) return errcode == sqlite.OK or errcode == sqlite.DONE end local function quote_name(name) return [["]]..string.gsub(name, [["]], [[""]])..[["]] end local function setup(db) db:create_function("parse_node_name", 1, function(ctx, table_name) local obj = csexp.parse(table_name) if type(obj) ~= "table" or obj[1] ~= "node" then ctx:result_null() return end local node_name = obj[2] if not node_name then ctx:result_null() return end ctx:result_text(node_name) end) end local function new_node_type(db, name) local table_name = csexp.serialize{"node", name} local quoted_name = quote_name(table_name) local query = string.format([[ CREATE TABLE %s ( id INTEGER PRIMARY KEY ); ]], quoted_name) -- for _ in db:rows(query) do end db:exec(query) if not sqlite_ok(db:errcode()) then error(db:errmsg()) end end local function get_node_types(db) local query = [[ SELECT parse_node_name(name) FROM sqlite_schema WHERE parse_node_name(name) IS NOT NULL ORDER BY parse_node_name(name) ASC; ]] return db:urows(query) end return { setup = setup, quote_name = quote_name, new_node_type = new_node_type, get_node_types = get_node_types, }