diff plugins/mod_storage_sql2.lua @ 6609:d2faaaca695d

Merge 0.10->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Mar 2015 22:24:57 +0000
parents b89406fa076c
children eb9c842b80fa
line wrap: on
line diff
--- a/plugins/mod_storage_sql2.lua	Fri Mar 27 22:19:44 2015 +0000
+++ b/plugins/mod_storage_sql2.lua	Fri Mar 27 22:24:57 2015 +0000
@@ -214,6 +214,39 @@
 	return iterator(result);
 end
 
+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.__index = archive_store
 function archive_store:append(username, key, when, with, value)
@@ -339,6 +372,7 @@
 
 local stores = {
 	keyval = keyval_store;
+	map = map_store;
 	archive = archive_store;
 };