local setmetatable = setmetatable;
local xpcall = xpcall;
local json = require "util.json";
+local build_url = require"socket.url".build;
local DBI;
local connection;
local host,user,store = module.host;
local params = module:get_option("sql");
+local dburi;
+local connections = module:shared "/*/sql/connection-cache";
+
+local function db2uri(params)
+ return build_url{
+ scheme = params.driver,
+ user = params.username,
+ password = params.password,
+ host = params.host,
+ port = params.port,
+ path = params.database,
+ };
+end
+
+
local resolve_relative_path = require "core.configmanager".resolve_relative_path;
local function test_connection()
else
module:log("debug", "Database connection closed");
connection = nil;
+ connections[dburi] = nil;
end
end
local function connect()
module:log("debug", "Successfully connected to database");
dbh:autocommit(false); -- don't commit automatically
connection = dbh;
- return connection;
+
+ connections[dburi] = dbh;
end
+ return connection;
end
local function create_table()
end
assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
+
+ dburi = db2uri(params);
+ connection = connections[dburi];
assert(connect());
return ...;
end
local function commit(...)
- if not connection:commit() then return nil, "SQL commit failed"; end
+ local success,err = connection:commit();
+ if not success then return nil, "SQL commit failed: "..tostring(err); end
return ...;
end
end
if success then return ret, err; else return rollback(nil, ret); end
end
+function keyval_store:users()
+ local stmt, err = dosql("SELECT DISTINCT `user` FROM `prosody` WHERE `host`=? AND `store`=?", host, self.store);
+ if not stmt then
+ return rollback(nil, err);
+ end
+ local next = stmt:rows();
+ return commit(function()
+ local row = next();
+ return row and row[1];
+ end);
+end
local function map_store_get(key)
local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
return nil, "not-implemented"
end
-local driver = { name = "sql" };
+local driver = {};
function driver:open(store, typ)
if not typ then -- default key-value store
return nil, "unsupported-store";
end
-function driver:list_stores(username) -- Not to be confused with the list store type
- local sql = (username == true
- and "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`!=?"
- or "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`=?");
+function driver:stores(username)
+ local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" ..
+ (username == true and "!=?" or "=?");
if username == true or not username then
username = "";
end
local stmt, err = dosql(sql, host, username);
if not stmt then
- return nil, err;
- end
- local stores = {};
- for row in stmt:rows() do
- stores[#stores+1] = row[1];
+ return rollback(nil, err);
end
- return stores;
+ local next = stmt:rows();
+ return commit(function()
+ local row = next();
+ return row and row[1];
+ end);
end
function driver:purge(username)
local stmt, err = dosql("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username);
- if not stmt then return stmt, err; end
+ if not stmt then return rollback(stmt, err); end
local changed, err = stmt:affected();
- if not changed then return changed, err; end
- return true, changed;
+ if not changed then return rollback(changed, err); end
+ return commit(true, changed);
end
-module:add_item("data-driver", driver);
+module:provides("storage", driver);