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
do -- process options to get a db connection
params = params or { driver = "SQLite3" };
-
+
if params.driver == "SQLite3" then
params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
end
-
+
assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
--local dburi = db2uri(params);
engine = mod_sql:create_engine(params);
-
+
-- Encoding mess
set_encoding();
end
local function keyval_store_set(data)
engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?", host, user or "", store);
-
+
if data and next(data) ~= nil then
local extradata = {};
for key, value in pairs(data) do
return key;
end);
end
+
+-- Helpers for building the WHERE clause
+local function archive_where(query, args, where)
+ -- 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
+end
+local function archive_where_id_range(query, args, where)
+ -- 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` = ? 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
+end
+
function archive_store:find(username, query)
query = query or {};
local user,store = username,self.store;
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
+ archive_where(query, args, where);
-- Total matching
if query.total then
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
+ archive_where_id_range(query, args, where);
if query.limit then
args[#args+1] = query.limit;