Software /
code /
prosody
Changeset
3429:8cdb0179371a
storage/mod_ejabberd: Reorganized some code.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 02 Aug 2010 19:04:52 +0500 |
parents | 3428:8a12ae696687 |
children | 3430:970690b3cb28 |
files | plugins/storage/mod_ejabberd.lua |
diffstat | 1 files changed, 17 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/plugins/storage/mod_ejabberd.lua Mon Aug 02 18:54:16 2010 +0500 +++ b/plugins/storage/mod_ejabberd.lua Mon Aug 02 19:04:52 2010 +0500 @@ -8,9 +8,10 @@ local prosody = prosody; local assert = assert; local require = require; +local st = require "util.stanza"; +local DBI = require "DBI"; -- connect to db -local DBI = require "DBI"; local option_datastore_params = module:get_option("datastore_params") or error("Missing option: datastore_params"); local database; do @@ -28,9 +29,20 @@ local ejabberd_init = module:require("ejabberd_init"); ejabberd_init.init(database); -local st = require "util.stanza"; +local sqlcache = {}; +local function prepare(sql) + module:log("debug", "query: %s", sql); + local err; + local r = sqlcache[sql]; + if not r then + r, err = database:prepare(sql); + if not r then error("Unable to prepare SQL statement: "..err); end + sqlcache[sql] = r; + end + return r; +end + local _parse_xml = module:require("xmlparse"); -local parse_xml_real = _parse_xml; local function parse_xml(str) local s = _parse_xml(str); if s and not s.gsub then @@ -182,20 +194,8 @@ local driver = {}; driver.__index = driver; -function driver:prepare(sql) - module:log("debug", "query: %s", sql); - local err; - if not self.sqlcache then self.sqlcache = {}; end - local r = self.sqlcache[sql]; - if r then return r; end - r, err = database:prepare(sql); - if not r then error("Unable to prepare SQL statement: "..err); end - self.sqlcache[sql] = r; - return r; -end - function driver:query(sql, ...) - local stmt,err = self:prepare(sql); + local stmt,err = prepare(sql); if not stmt then module:log("error", "Failed to prepare SQL [[%s]], error: %s", sql, err); return nil, err; @@ -214,9 +214,7 @@ end function driver:open(datastore, typ) - local instance = setmetatable({}, self); - instance.host = module.host; - instance.datastore = datastore; + local instance = setmetatable({ host = module.host, datastore = datastore }, self); local handler = handlers[datastore]; if not handler then return nil; end for key,val in pairs(handler) do