Changeset

7179:bfa3b2ee384c

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 22 Feb 2016 15:24:20 +0100
parents 7171:91d36a37a9ff (current diff) 7178:5953f415c815 (diff)
children 7188:e8f20be3e6f8
files
diffstat 6 files changed, 71 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/core/usermanager.lua	Sun Feb 21 16:54:56 2016 +0100
+++ b/core/usermanager.lua	Mon Feb 22 15:24:20 2016 +0100
@@ -81,6 +81,7 @@
 end
 
 local function user_exists(username, host)
+	if hosts[host].sessions[username] then return true; end
 	return hosts[host].users.user_exists(username);
 end
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/mod_debug_sql.lua	Mon Feb 22 15:24:20 2016 +0100
@@ -0,0 +1,25 @@
+-- Enables SQL query logging
+--
+-- luacheck: ignore 213/uri
+
+local engines = module:shared("/*/sql/connections");
+
+for uri, engine in pairs(engines) do
+	engine:debug(true);
+end
+
+setmetatable(engines, {
+	__newindex = function (t, uri, engine)
+		engine:debug(true);
+		rawset(t, uri, engine);
+	end
+});
+
+function module.unload()
+	setmetatable(engines, nil);
+	for uri, engine in pairs(engines) do
+		engine:debug(false);
+	end
+end
+
+
--- a/plugins/mod_storage_sql.lua	Sun Feb 21 16:54:56 2016 +0100
+++ b/plugins/mod_storage_sql.lua	Mon Feb 22 15:24:20 2016 +0100
@@ -235,12 +235,12 @@
 	local args_len = #args
 	-- Before or after specific item, exclusive
 	if query.after then  -- keys better be unique!
-		where[#where+1] = "`sort_id` > (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1)"
+		where[#where+1] = "`sort_id` > COALESCE((SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1), 0)"
 		args[args_len+1], args[args_len+2], args[args_len+3], args[args_len+4] = query.after, args[1], args[2], args[3];
 		args_len = args_len + 4
 	end
 	if query.before then
-		where[#where+1] = "`sort_id` < (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1)"
+		where[#where+1] = "`sort_id` < COALESCE((SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1), (SELECT MAX(`sort_id`)+1 FROM `prosodyarchive`))"
 		args[args_len+1], args[args_len+2], args[args_len+3], args[args_len+4] = query.before, args[1], args[2], args[3];
 	end
 end
@@ -275,7 +275,6 @@
 		end
 
 		sql_query = sql_query:format(t_concat(where, " AND "), query.reverse and "DESC" or "ASC", query.limit and " LIMIT ?" or "");
-		module:log("debug", sql_query);
 		return engine:select(sql_query, unpack(args));
 	end);
 	if not ok then return ok, result end
@@ -301,7 +300,6 @@
 		archive_where(query, args, where);
 		archive_where_id_range(query, args, where);
 		sql_query = sql_query:format(t_concat(where, " AND "));
-		module:log("debug", sql_query);
 		return engine:delete(sql_query, unpack(args));
 	end);
 end
@@ -453,7 +451,7 @@
 	local params = normalize_params(module:get_option("sql", default_params));
 	engine = engines[sql.db2uri(params)];
 	if not engine then
-		module:log("info", "Creating new engine");
+		module:log("debug", "Creating new engine");
 		engine = sql:create_engine(params, function (engine)
 			if module:get_option("sql_manage_tables", true) then
 				-- Automatically create table, ignore failure (table probably already exists)
--- a/tests/test.lua	Sun Feb 21 16:54:56 2016 +0100
+++ b/tests/test.lua	Mon Feb 22 15:24:20 2016 +0100
@@ -23,6 +23,7 @@
 	dotest "util.cache"
 	dotest "util.throttle"
 	dotest "util.uuid"
+	dotest "util.random"
 
 	dosingletest("test_sasl.lua", "latin1toutf8");
 	dosingletest("test_utf8.lua", "valid");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_util_random.lua	Mon Feb 22 15:24:20 2016 +0100
@@ -0,0 +1,10 @@
+-- Makes no attempt at testing how random the bytes are,
+-- just that it returns the number of bytes requested
+
+function bytes(bytes)
+	assert_is(bytes(16));
+
+	for i = 1, 255 do
+		assert_equal(i, #bytes(i));
+	end
+end
--- a/util/sql.lua	Sun Feb 21 16:54:56 2016 +0100
+++ b/util/sql.lua	Mon Feb 22 15:24:20 2016 +0100
@@ -147,6 +147,11 @@
 	rowcount = function(self) return self.__stmt:rowcount(); end;
 } };
 
+local function debugquery(where, sql, ...)
+	local i = 0; local a = {...}
+	log("debug", "[%s] %s", where, sql:gsub("%?", function () i = i + 1; local v = a[i]; if type(v) == "string" then v = ("%q"):format(v); end return tostring(v); end));
+end
+
 function engine:execute_query(sql, ...)
 	if self.params.driver == "PostgreSQL" then
 		sql = sql:gsub("`", "\"");
@@ -172,6 +177,26 @@
 engine.select = engine.execute_query;
 engine.delete = engine.execute_update;
 engine.update = engine.execute_update;
+local function debugwrap(name, f)
+	return function (self, sql, ...)
+		debugquery(name, sql, ...)
+		return f(self, sql, ...)
+	end
+end
+function engine:debug(enable)
+	self._debug = enable;
+	if enable then
+		engine.insert = debugwrap("insert", engine.execute_update);
+		engine.select = debugwrap("select", engine.execute_query);
+		engine.delete = debugwrap("delete", engine.execute_update);
+		engine.update = debugwrap("update", engine.execute_update);
+	else
+		engine.insert = engine.execute_update;
+		engine.select = engine.execute_query;
+		engine.delete = engine.execute_update;
+		engine.update = engine.execute_update;
+	end
+end
 function engine:_transaction(func, ...)
 	if not self.conn then
 		local ok, err = self:connect();
@@ -221,7 +246,9 @@
 	if index.unique then
 		sql = sql:gsub("^CREATE", "CREATE UNIQUE");
 	end
-	--print(sql);
+	if self._debug then
+		debugquery("create", sql);
+	end
 	return self:execute(sql);
 end
 function engine:_create_table(table)
@@ -252,6 +279,9 @@
 	elseif self.params.driver == "MySQL" then
 		sql = sql:gsub(";$", (" CHARACTER SET '%s' COLLATE '%s_bin';"):format(self.charset, self.charset));
 	end
+	if self._debug then
+		debugquery("create", sql);
+	end
 	local success,err = self:execute(sql);
 	if not success then return success,err; end
 	for i,v in ipairs(table.__table__) do