Software /
code /
prosody
Comparison
plugins/storage/mod_ejabberd.lua @ 3429:8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 02 Aug 2010 19:04:52 +0500 |
parent | 3415:5ba0e094a5e2 |
comparison
equal
deleted
inserted
replaced
3428:8a12ae696687 | 3429:8cdb0179371a |
---|---|
6 local tostring = tostring; | 6 local tostring = tostring; |
7 local pairs, next = pairs, next; | 7 local pairs, next = pairs, next; |
8 local prosody = prosody; | 8 local prosody = prosody; |
9 local assert = assert; | 9 local assert = assert; |
10 local require = require; | 10 local require = require; |
11 local st = require "util.stanza"; | |
12 local DBI = require "DBI"; | |
11 | 13 |
12 -- connect to db | 14 -- connect to db |
13 local DBI = require "DBI"; | |
14 local option_datastore_params = module:get_option("datastore_params") or error("Missing option: datastore_params"); | 15 local option_datastore_params = module:get_option("datastore_params") or error("Missing option: datastore_params"); |
15 local database; | 16 local database; |
16 do | 17 do |
17 local driver, db = unpack(option_datastore_params); | 18 local driver, db = unpack(option_datastore_params); |
18 module:log("debug", "Opening database: %s", "dbi:"..driver..":"..db); | 19 module:log("debug", "Opening database: %s", "dbi:"..driver..":"..db); |
26 | 27 |
27 -- initialize db | 28 -- initialize db |
28 local ejabberd_init = module:require("ejabberd_init"); | 29 local ejabberd_init = module:require("ejabberd_init"); |
29 ejabberd_init.init(database); | 30 ejabberd_init.init(database); |
30 | 31 |
31 local st = require "util.stanza"; | 32 local sqlcache = {}; |
33 local function prepare(sql) | |
34 module:log("debug", "query: %s", sql); | |
35 local err; | |
36 local r = sqlcache[sql]; | |
37 if not r then | |
38 r, err = database:prepare(sql); | |
39 if not r then error("Unable to prepare SQL statement: "..err); end | |
40 sqlcache[sql] = r; | |
41 end | |
42 return r; | |
43 end | |
44 | |
32 local _parse_xml = module:require("xmlparse"); | 45 local _parse_xml = module:require("xmlparse"); |
33 local parse_xml_real = _parse_xml; | |
34 local function parse_xml(str) | 46 local function parse_xml(str) |
35 local s = _parse_xml(str); | 47 local s = _parse_xml(str); |
36 if s and not s.gsub then | 48 if s and not s.gsub then |
37 return st.preserialize(s); | 49 return st.preserialize(s); |
38 end | 50 end |
180 | 192 |
181 ----------------------------- | 193 ----------------------------- |
182 local driver = {}; | 194 local driver = {}; |
183 driver.__index = driver; | 195 driver.__index = driver; |
184 | 196 |
185 function driver:prepare(sql) | |
186 module:log("debug", "query: %s", sql); | |
187 local err; | |
188 if not self.sqlcache then self.sqlcache = {}; end | |
189 local r = self.sqlcache[sql]; | |
190 if r then return r; end | |
191 r, err = database:prepare(sql); | |
192 if not r then error("Unable to prepare SQL statement: "..err); end | |
193 self.sqlcache[sql] = r; | |
194 return r; | |
195 end | |
196 | |
197 function driver:query(sql, ...) | 197 function driver:query(sql, ...) |
198 local stmt,err = self:prepare(sql); | 198 local stmt,err = prepare(sql); |
199 if not stmt then | 199 if not stmt then |
200 module:log("error", "Failed to prepare SQL [[%s]], error: %s", sql, err); | 200 module:log("error", "Failed to prepare SQL [[%s]], error: %s", sql, err); |
201 return nil, err; | 201 return nil, err; |
202 end | 202 end |
203 local ok, err = stmt:execute(...); | 203 local ok, err = stmt:execute(...); |
212 if stmt and stmt:affected() > 0 then return stmt; end | 212 if stmt and stmt:affected() > 0 then return stmt; end |
213 return nil, err; | 213 return nil, err; |
214 end | 214 end |
215 | 215 |
216 function driver:open(datastore, typ) | 216 function driver:open(datastore, typ) |
217 local instance = setmetatable({}, self); | 217 local instance = setmetatable({ host = module.host, datastore = datastore }, self); |
218 instance.host = module.host; | |
219 instance.datastore = datastore; | |
220 local handler = handlers[datastore]; | 218 local handler = handlers[datastore]; |
221 if not handler then return nil; end | 219 if not handler then return nil; end |
222 for key,val in pairs(handler) do | 220 for key,val in pairs(handler) do |
223 instance[key] = val; | 221 instance[key] = val; |
224 end | 222 end |