From 1b133aa8a0cb4f862ffda75ce24f9fd45e2ddef2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 7 Aug 2014 12:16:16 -0400 Subject: plugins/mod_storage_sql2: Add map store support --- plugins/mod_storage_sql2.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'plugins/mod_storage_sql2.lua') diff --git a/plugins/mod_storage_sql2.lua b/plugins/mod_storage_sql2.lua index 0531c905..ce21d368 100644 --- a/plugins/mod_storage_sql2.lua +++ b/plugins/mod_storage_sql2.lua @@ -216,6 +216,40 @@ function keyval_store:users() return iterator(result); end +local map_store = {}; +map_store.__index = map_store; +function map_store:get(username, key) + return engine:transaction(function() + if type(key) == "string" and key ~= "" then + local iter, state, first = engine:select("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", + host, username, self.store, key or ""); + local row = iter(state, first); + if row then + return deserialize(row.type, row.value); + else + return nil; + end + else + error("TODO: non-string keys"); + end + end); +end +function map_store:set(username, key, data) + return engine:transaction(function() + if data == nil then + engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", + host, username, self.store, key or ""); + elseif type(key) == "string" and key ~= "" then + local t, value = assert(serialize(data)); + engine:update("UPDATE `prosody` SET `type`=?, `value`=? WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", + t, value, host, username, self.store, key); + else + error("TODO: non-string keys"); + end + return true; + end); +end + local archive_store = {} archive_store.__index = archive_store function archive_store:append(username, key, when, with, value) @@ -341,6 +375,7 @@ end local stores = { keyval = keyval_store; + map = map_store; archive = archive_store; }; -- cgit v1.2.3 From 06903bc73909fd1557c326f2cc90404aed4f7dcc Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 7 Aug 2014 18:34:51 -0400 Subject: plugins/mod_storage_sql2: Return correct arguments from map_store operations --- plugins/mod_storage_sql2.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'plugins/mod_storage_sql2.lua') diff --git a/plugins/mod_storage_sql2.lua b/plugins/mod_storage_sql2.lua index ce21d368..c0e2e6cd 100644 --- a/plugins/mod_storage_sql2.lua +++ b/plugins/mod_storage_sql2.lua @@ -219,7 +219,7 @@ end local map_store = {}; map_store.__index = map_store; function map_store:get(username, key) - return engine:transaction(function() + local ok, result = engine:transaction(function() if type(key) == "string" and key ~= "" then local iter, state, first = engine:select("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username, self.store, key or ""); @@ -233,9 +233,11 @@ function map_store:get(username, key) error("TODO: non-string keys"); end end); + if not ok then return nil, result; end + return result; end function map_store:set(username, key, data) - return engine:transaction(function() + local ok, result = engine:transaction(function() if data == nil then engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username, self.store, key or ""); @@ -248,6 +250,8 @@ function map_store:set(username, key, data) end return true; end); + if not ok then return nil, result; end + return result; end local archive_store = {} -- cgit v1.2.3 From 7eb2ec7a56e3ffca8ddbe06ece12ba20c4125fac Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 12 Aug 2014 11:38:12 +0200 Subject: mod_storage_sql2: DELETE then INSERT in map stores --- plugins/mod_storage_sql2.lua | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'plugins/mod_storage_sql2.lua') diff --git a/plugins/mod_storage_sql2.lua b/plugins/mod_storage_sql2.lua index c0e2e6cd..e5357416 100644 --- a/plugins/mod_storage_sql2.lua +++ b/plugins/mod_storage_sql2.lua @@ -221,13 +221,8 @@ map_store.__index = map_store; function map_store:get(username, key) local ok, result = engine:transaction(function() if type(key) == "string" and key ~= "" then - local iter, state, first = engine:select("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", - host, username, self.store, key or ""); - local row = iter(state, first); - if row then - return deserialize(row.type, row.value); - else - return nil; + for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, key) do + return deserialize(row[1], row[2]); end else error("TODO: non-string keys"); @@ -238,13 +233,13 @@ function map_store:get(username, key) end function map_store:set(username, key, data) local ok, result = engine:transaction(function() - if data == nil then + if type(key) == "string" and key ~= "" then engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", - host, username, self.store, key or ""); - elseif type(key) == "string" and key ~= "" then - local t, value = assert(serialize(data)); - engine:update("UPDATE `prosody` SET `type`=?, `value`=? WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", - t, value, host, username, self.store, key); + host, username or "", self.store, key); + if data ~= nil then + local t, value = assert(serialize(data)); + engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value); + end else error("TODO: non-string keys"); end -- cgit v1.2.3