mod_storage_sql2: Do an early return and drop an indentation level
[prosody.git] / plugins / mod_storage_sql2.lua
index 8081c0f9d001992aea7456a184a5afd4d0802df4..8ce5a72242bc8d32098a534ff931aee790e5f698 100644 (file)
@@ -1,12 +1,15 @@
 
 local json = require "util.json";
 local xml_parse = require "util.xml".parse;
+local uuid = require "util.uuid";
 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
 
 local stanza_mt = require"util.stanza".stanza_mt;
 local getmetatable = getmetatable;
+local t_concat = table.concat;
 local function is_stanza(x) return getmetatable(x) == stanza_mt; end
 
+local noop = function() end
 local unpack = unpack
 local function iterator(result)
        return function(result)
@@ -23,7 +26,8 @@ local params = module:get_option("sql");
 local engine; -- TODO create engine
 
 local function create_table()
-       --[[local Table,Column,Index = mod_sql.Table,mod_sql.Column,mod_sql.Index;
+       local Table,Column,Index = mod_sql.Table,mod_sql.Column,mod_sql.Index;
+       --[[
        local ProsodyTable = Table {
                name="prosody";
                Column { name="host", type="TEXT", nullable=false };
@@ -78,40 +82,55 @@ local function create_table()
                        end
                end
        end
+       local ProsodyArchiveTable = Table {
+               name="prosodyarchive";
+               Column { name="sort_id", type="INTEGER PRIMARY KEY AUTOINCREMENT", nullable=false };
+               Column { name="host", type="TEXT", nullable=false };
+               Column { name="user", type="TEXT", nullable=false };
+               Column { name="store", type="TEXT", nullable=false };
+               Column { name="key", type="TEXT", nullable=false }; -- item id
+               Column { name="when", type="INTEGER", nullable=false }; -- timestamp
+               Column { name="with", type="TEXT", nullable=false }; -- related id
+               Column { name="type", type="TEXT", nullable=false };
+               Column { name="value", type=params.driver == "MySQL" and "MEDIUMTEXT" or "TEXT", nullable=false };
+               Index { name="prosodyarchive_index", "host", "user", "store", "key" };
+       };
+       engine:transaction(function()
+               ProsodyArchiveTable:create(engine);
+       end);
 end
 local function set_encoding()
-       if params.driver ~= "SQLite3" then
-               local set_names_query = "SET NAMES 'utf8';";
-               if params.driver == "MySQL" then
-                       set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';");
-               end
-               local success,err = engine:transaction(function() return engine:execute(set_names_query); end);
-               if not success then
-                       module:log("error", "Failed to set database connection encoding to UTF8: %s", err);
-                       return;
-               end
-               if params.driver == "MySQL" then
-                       -- COMPAT w/pre-0.9: Upgrade tables to UTF-8 if not already
-                       local check_encoding_query = "SELECT `COLUMN_NAME`,`COLUMN_TYPE` FROM `information_schema`.`columns` WHERE `TABLE_NAME`='prosody' AND ( `CHARACTER_SET_NAME`!='utf8' OR `COLLATION_NAME`!='utf8_bin' );";
-                       local success,err = engine:transaction(function()
-                               local result = engine:execute(check_encoding_query);
-                               local n_bad_columns = result:rowcount();
-                               if n_bad_columns > 0 then
-                                       module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns);
-                                       local fix_column_query1 = "ALTER TABLE `prosody` CHANGE `%s` `%s` BLOB;";
-                                       local fix_column_query2 = "ALTER TABLE `prosody` CHANGE `%s` `%s` %s CHARACTER SET 'utf8' COLLATE 'utf8_bin';";
-                                       for row in result:rows() do
-                                               local column_name, column_type = unpack(row);
-                                               engine:execute(fix_column_query1:format(column_name, column_name));
-                                               engine:execute(fix_column_query2:format(column_name, column_name, column_type));
-                                       end
-                                       module:log("info", "Database encoding upgrade complete!");
+       if params.driver == "SQLite3" then return end
+       local set_names_query = "SET NAMES 'utf8';";
+       if params.driver == "MySQL" then
+               set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';");
+       end
+       local success,err = engine:transaction(function() return engine:execute(set_names_query); end);
+       if not success then
+               module:log("error", "Failed to set database connection encoding to UTF8: %s", err);
+               return;
+       end
+       if params.driver == "MySQL" then
+               -- COMPAT w/pre-0.9: Upgrade tables to UTF-8 if not already
+               local check_encoding_query = "SELECT `COLUMN_NAME`,`COLUMN_TYPE` FROM `information_schema`.`columns` WHERE `TABLE_NAME`='prosody' AND ( `CHARACTER_SET_NAME`!='utf8' OR `COLLATION_NAME`!='utf8_bin' );";
+               local success,err = engine:transaction(function()
+                       local result = engine:execute(check_encoding_query);
+                       local n_bad_columns = result:rowcount();
+                       if n_bad_columns > 0 then
+                               module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns);
+                               local fix_column_query1 = "ALTER TABLE `prosody` CHANGE `%s` `%s` BLOB;";
+                               local fix_column_query2 = "ALTER TABLE `prosody` CHANGE `%s` `%s` %s CHARACTER SET 'utf8' COLLATE 'utf8_bin';";
+                               for row in result:rows() do
+                                       local column_name, column_type = unpack(row);
+                                       engine:execute(fix_column_query1:format(column_name, column_name));
+                                       engine:execute(fix_column_query2:format(column_name, column_name, column_type));
                                end
-                       end);
-                       local success,err = engine:transaction(function() return engine:execute(check_encoding_query); end);
-                       if not success then
-                               module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error");
+                               module:log("info", "Database encoding upgrade complete!");
                        end
+               end);
+               local success,err = engine:transaction(function() return engine:execute(check_encoding_query); end);
+               if not success then
+                       module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error");
                end
        end
 end
@@ -226,8 +245,94 @@ function keyval_store:users()
        return iterator(result);
 end
 
+local archive_store = {}
+archive_store.__index = archive_store
+function archive_store:append(username, when, with, value)
+       local user,store = username,self.store;
+       return engine:transaction(function()
+               local key = uuid.generate();
+               local t, value = serialize(value);
+               engine:insert("INSERT INTO `prosodyarchive` (`host`, `user`, `store`, `when`, `with`, `key`, `type`, `value`) VALUES (?,?,?,?,?,?,?,?)", host, user or "", store, when, with, key, t, value);
+               return key;
+       end);
+end
+function archive_store:find(username, query)
+       query = query or {};
+       local user,store = username,self.store;
+       local total;
+       local ok, result = engine:transaction(function()
+               local sql_query = "SELECT `key`, `type`, `value`, `when` FROM `prosodyarchive` WHERE %s ORDER BY `sort_id` %s%s;";
+               local args = { host, user or "", store, };
+               local where = { "`host` = ?", "`user` = ?", "`store` = ?", };
+
+               -- Time range, inclusive
+               if query.start then
+                       args[#args+1] = query.start
+                       where[#where+1] = "`when` >= ?"
+               end
+               if query["end"] then
+                       args[#args+1] = query["end"];
+                       if query.start then
+                               where[#where] = "`when` BETWEEN ? AND ?" -- is this inclusive?
+                       else
+                               where[#where+1] = "`when` >= ?"
+                       end
+               end
+
+               -- Related name
+               if query.with then
+                       where[#where+1] = "`with` = ?";
+                       args[#args+1] = query.with
+               end
+
+               -- Unique id
+               if query.key then
+                       where[#where+1] = "`key` = ?";
+                       args[#args+1] = query.key
+               end
+
+               -- Total matching
+               if query.total then
+                       local stats = engine:select(sql_query:gsub("^(SELECT).-(FROM)", "%1 COUNT(*) %2"):format(t_concat(where, " AND "), "DESC", ""), unpack(args));
+                       if stats then
+                               local _total = stats()
+                               total = _total and _total[1];
+                       end
+                       if query.limit == 0 then -- Skip the real query
+                               return noop, total;
+                       end
+               end
+
+               -- Before or after specific item, exclusive
+               if query.after then
+                       where[#where+1] = "`sort_id` > (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? LIMIT 1)"
+                       args[#args+1] = query.after
+               end
+               if query.before then
+                       where[#where+1] = "`sort_id` < (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? LIMIT 1)"
+                       args[#args+1] = query.before
+               end
+
+               if query.limit then
+                       args[#args+1] = query.limit;
+               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
+       return function()
+               local row = result();
+               if row ~= nil then
+                       return row[1], deserialize(row[2], row[3]), row[4];
+               end
+       end, total;
+end
+
 local stores = {
        keyval = keyval_store;
+       archive = archive_store;
 };
 
 local driver = {};