aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_storage_sql2.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2013-09-28 21:58:01 +0200
committerKim Alvefur <zash@zash.se>2013-09-28 21:58:01 +0200
commitc9f9fdd69ca8fa710a9a8467f289e4ca7bb0b94e (patch)
tree6f71a9592027ae95fe1765f94ac7e28ef47c7da1 /plugins/mod_storage_sql2.lua
parent6802ee756301d50ad58f4b10d6612815ef70996e (diff)
downloadprosody-c9f9fdd69ca8fa710a9a8467f289e4ca7bb0b94e.tar.gz
prosody-c9f9fdd69ca8fa710a9a8467f289e4ca7bb0b94e.zip
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Diffstat (limited to 'plugins/mod_storage_sql2.lua')
-rw-r--r--plugins/mod_storage_sql2.lua78
1 files changed, 44 insertions, 34 deletions
diff --git a/plugins/mod_storage_sql2.lua b/plugins/mod_storage_sql2.lua
index 3c5f9d20..1ce8c204 100644
--- a/plugins/mod_storage_sql2.lua
+++ b/plugins/mod_storage_sql2.lua
@@ -256,6 +256,48 @@ function archive_store:append(username, when, with, value)
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;
@@ -265,31 +307,7 @@ function archive_store:find(username, query)
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
@@ -303,15 +321,7 @@ function archive_store:find(username, query)
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;