mod_debug_sql: Small plugin that enables raw SQL query logging (for debugging)
[prosody.git] / plugins / mod_storage_sql.lua
index 6efa676c0322a3f673b509021227f40266666f38..af2bcef419d9ca428f869d15efafb041b61daf04 100644 (file)
@@ -275,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
@@ -301,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
@@ -449,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