aboutsummaryrefslogtreecommitdiffstats
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
commitdad0501c65620aadc5b272c671bb6bb02021c282 (patch)
tree6f71a9592027ae95fe1765f94ac7e28ef47c7da1
parentd3d91c224a5c7309283994536aeac6e4702e2e25 (diff)
downloadprosody-dad0501c65620aadc5b272c671bb6bb02021c282.tar.gz
prosody-dad0501c65620aadc5b272c671bb6bb02021c282.zip
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
-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;