Software /
code /
prosody
Comparison
plugins/storage/mod_storage.lua @ 2678:c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 19 Feb 2010 22:32:28 +0500 |
child | 3277:da0f55661e2b |
comparison
equal
deleted
inserted
replaced
2677:467568f1117d | 2678:c5882e2e12b5 |
---|---|
1 | |
2 module:set_global(); | |
3 | |
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 | |
7 | |
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(); | |
16 dbh:autocommit(true) | |
17 return dbh; | |
18 end)(driver, db, ...)); | |
19 end | |
20 | |
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); | |
28 end | |
29 end | |
30 function unparse_xml(s) | |
31 return tostring(st.deserialize(s)); | |
32 end | |
33 | |
34 local drivers = {}; | |
35 | |
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); | |
42 end | |
43 | |
44 local datamanager = require "util.datamanager"; | |
45 local olddm = {}; | |
46 local dm = {}; | |
47 for key,val in pairs(datamanager) do olddm[key] = val; end | |
48 | |
49 do -- driver based on old datamanager | |
50 local dmd = {}; | |
51 dmd.__index = dmd; | |
52 function dmd:open(host, datastore) | |
53 return setmetatable({ host = host, datastore = datastore }, dmd); | |
54 end | |
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); | |
58 end | |
59 | |
60 local function open(...) | |
61 for _,driver in pairs(drivers) do | |
62 local ds = driver:open(...); | |
63 if ds then return ds; end | |
64 end | |
65 end | |
66 | |
67 local _data_path; | |
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); | |
75 end | |
76 function dm.store(username, host, datastore, data) | |
77 return open(host, datastore):set(username, data); | |
78 end | |
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 | |
82 | |
83 for key,val in pairs(dm) do datamanager[key] = val; end |