mod_storage_sql: Add map store (backported from trunk)
authorMatthew Wild <mwild1@gmail.com>
Thu, 3 Dec 2015 15:03:24 +0000 (15:03 +0000)
committerMatthew Wild <mwild1@gmail.com>
Thu, 3 Dec 2015 15:03:24 +0000 (15:03 +0000)
plugins/mod_storage_sql.lua

index 172e569e698c17da70f6468f4bc6ab3d9a89f1c5..adf9f9b6a50d5282b6c0d6e717d472419e7827ff 100644 (file)
@@ -125,6 +125,39 @@ end
 
 --- Archive store API
 
+local map_store = {};
+map_store.__index = map_store;
+function map_store:get(username, key)
+       local ok, result = engine:transaction(function()
+               if type(key) == "string" and key ~= "" then
+                       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");
+               end
+       end);
+       if not ok then return nil, result; end
+       return result;
+end
+function map_store:set(username, key, data)
+       local ok, result = engine:transaction(function()
+               if type(key) == "string" and key ~= "" then
+                       engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `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
+               return true;
+       end);
+       if not ok then return nil, result; end
+       return result;
+end
+
 local archive_store = {}
 archive_store.caps = {
        total = true;
@@ -253,6 +286,7 @@ end
 
 local stores = {
        keyval = keyval_store;
+       map = map_store;
        archive = archive_store;
 };