4 local cache = { data = {} };
5 function cache:get(key) return self.data[key]; end
6 function cache:set(key, val) self.data[key] = val; return val; end
8 local DBI = require "DBI";
9 function get_database(driver, db, ...)
10 local uri = "dbi:"..driver..":"..db;
11 return cache:get(uri) or cache:set(uri, (function(...)
12 module:log("debug", "Opening database: %s", uri);
13 prosody.unlock_globals();
14 local dbh = assert(DBI.Connect(...));
15 prosody.lock_globals();
18 end)(driver, db, ...));
21 local st = require "util.stanza";
22 local _parse_xml = module:require("xmlparse");
23 parse_xml_real = _parse_xml;
24 function parse_xml(str)
25 local s = _parse_xml(str);
26 if s and not s.gsub then
27 return st.preserialize(s);
30 function unparse_xml(s)
31 return tostring(st.deserialize(s));
36 --local driver = module:require("sqlbasic").new("SQLite3", "hello.sqlite");
37 local option_datastore = module:get_option("datastore");
38 local option_datastore_params = module:get_option("datastore_params") or {};
39 if option_datastore then
40 local driver = module:require(option_datastore).new(unpack(option_datastore_params));
41 table.insert(drivers, driver);
44 local datamanager = require "util.datamanager";
47 for key,val in pairs(datamanager) do olddm[key] = val; end
49 do -- driver based on old datamanager
52 function dmd:open(host, datastore)
53 return setmetatable({ host = host, datastore = datastore }, dmd);
55 function dmd:get(user) return olddm.load(user, self.host, self.datastore); end
56 function dmd:set(user, data) return olddm.store(user, self.host, self.datastore, data); end
57 table.insert(drivers, dmd);
60 local function open(...)
61 for _,driver in pairs(drivers) do
62 local ds = driver:open(...);
63 if ds then return ds; end
68 --function dm.set_data_path(path) _data_path = path; end
69 --function dm.add_callback(...) end
70 --function dm.remove_callback(...) end
71 --function dm.getpath(...) end
72 function dm.load(username, host, datastore)
73 local x = open(host, datastore);
74 return x:get(username);
76 function dm.store(username, host, datastore, data)
77 return open(host, datastore):set(username, data);
79 --function dm.list_append(...) return driver:list_append(...); end
80 --function dm.list_store(...) return driver:list_store(...); end
81 --function dm.list_load(...) return driver:list_load(...); end
83 for key,val in pairs(dm) do datamanager[key] = val; end