Software /
code /
prosody
Annotate
plugins/storage/mod_storage.lua @ 3244:616a3bb2bad9
MUC: Added room:get_password() and room:set_password().
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 13 Jun 2010 18:13:18 +0500 |
parent | 2678:c5882e2e12b5 |
child | 3277:da0f55661e2b |
rev | line source |
---|---|
2678
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 module:set_global(); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local cache = { data = {} }; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 function cache:get(key) return self.data[key]; end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 function cache:set(key, val) self.data[key] = val; return val; end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local DBI = require "DBI"; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 function get_database(driver, db, ...) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local uri = "dbi:"..driver..":"..db; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 return cache:get(uri) or cache:set(uri, (function(...) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 module:log("debug", "Opening database: %s", uri); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 prosody.unlock_globals(); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local dbh = assert(DBI.Connect(...)); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 prosody.lock_globals(); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 dbh:autocommit(true) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 return dbh; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 end)(driver, db, ...)); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 local st = require "util.stanza"; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local _parse_xml = module:require("xmlparse"); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 parse_xml_real = _parse_xml; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 function parse_xml(str) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local s = _parse_xml(str); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 if s and not s.gsub then |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 return st.preserialize(s); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 function unparse_xml(s) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 return tostring(st.deserialize(s)); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 local drivers = {}; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 --local driver = module:require("sqlbasic").new("SQLite3", "hello.sqlite"); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 local option_datastore = module:get_option("datastore"); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local option_datastore_params = module:get_option("datastore_params") or {}; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 if option_datastore then |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local driver = module:require(option_datastore).new(unpack(option_datastore_params)); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 table.insert(drivers, driver); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 local datamanager = require "util.datamanager"; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 local olddm = {}; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 local dm = {}; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 for key,val in pairs(datamanager) do olddm[key] = val; end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 do -- driver based on old datamanager |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 local dmd = {}; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 dmd.__index = dmd; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 function dmd:open(host, datastore) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 return setmetatable({ host = host, datastore = datastore }, dmd); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 function dmd:get(user) return olddm.load(user, self.host, self.datastore); end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 function dmd:set(user, data) return olddm.store(user, self.host, self.datastore, data); end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 table.insert(drivers, dmd); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 local function open(...) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 for _,driver in pairs(drivers) do |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 local ds = driver:open(...); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 if ds then return ds; end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 local _data_path; |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 --function dm.set_data_path(path) _data_path = path; end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 --function dm.add_callback(...) end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 --function dm.remove_callback(...) end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 --function dm.getpath(...) end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 function dm.load(username, host, datastore) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 local x = open(host, datastore); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 return x:get(username); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 function dm.store(username, host, datastore, data) |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 return open(host, datastore):set(username, data); |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 --function dm.list_append(...) return driver:list_append(...); end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 --function dm.list_store(...) return driver:list_store(...); end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 --function dm.list_load(...) return driver:list_load(...); end |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 |
c5882e2e12b5
mod_storage, plus a bit of SQL and XML.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 for key,val in pairs(dm) do datamanager[key] = val; end |