mod_debug_sql: Small plugin that enables raw SQL query logging (for debugging)
[prosody.git] / plugins / mod_storage_sql.lua
index 34fcaceed57245b96a625b2a23710ebbba5ab9d9..af2bcef419d9ca428f869d15efafb041b61daf04 100644 (file)
@@ -148,7 +148,8 @@ function map_store:get(username, key)
        return result;
 end
 function map_store:set(username, key, data)
-       return self:set_keys(username, { [key] = data or self.remove });
+       if data == nil then data = self.remove; end
+       return self:set_keys(username, { [key] = data });
 end
 function map_store:set_keys(username, keydatas)
        local ok, result = engine:transaction(function()
@@ -274,7 +275,6 @@ function archive_store:find(username, query)
                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
@@ -300,7 +300,6 @@ function archive_store:delete(username, query)
                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
@@ -448,19 +447,25 @@ end
 
 function module.load()
        if prosody.prosodyctl then return; end
+       local engines = module:shared("/*/sql/connections");
        local params = normalize_params(module:get_option("sql", default_params));
-       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)
-                       -- FIXME: we should check in information_schema, etc.
-                       create_table();
-                       -- Check whether the table needs upgrading
-                       if upgrade_table(params, false) then
-                               module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name);
-                               return false, "database upgrade needed";
+       engine = engines[sql.db2uri(params)];
+       if not engine then
+               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)
+                               -- FIXME: we should check in information_schema, etc.
+                               create_table();
+                               -- Check whether the table needs upgrading
+                               if upgrade_table(params, false) then
+                                       module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name);
+                                       return false, "database upgrade needed";
+                               end
                        end
-               end
-       end);
+               end);
+               engines[sql.db2uri(params)] = engine;
+       end
 
        module:provides("storage", driver);
 end