Comparison

plugins/mod_storage_sql.lua @ 7149:bb0fd02ae70f

mod_storage_sql: Support non-string keys in map store
author Kim Alvefur <zash@zash.se>
date Mon, 08 Feb 2016 20:23:12 +0100
parent 7008:9beba2572e2b
child 7153:89fa66d4e502
comparison
equal deleted inserted replaced
7145:b1a109858502 7149:bb0fd02ae70f
132 if type(key) == "string" and key ~= "" then 132 if type(key) == "string" and key ~= "" then
133 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 133 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
134 return deserialize(row[1], row[2]); 134 return deserialize(row[1], row[2]);
135 end 135 end
136 else 136 else
137 error("TODO: non-string keys"); 137 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
138 local data = deserialize(row[1], row[2]);
139 return data and data[key] or nil;
140 end
138 end 141 end
139 end); 142 end);
140 if not ok then return nil, result; end 143 if not ok then return nil, result; end
141 return result; 144 return result;
142 end 145 end
148 if data ~= nil then 151 if data ~= nil then
149 local t, value = assert(serialize(data)); 152 local t, value = assert(serialize(data));
150 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value); 153 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
151 end 154 end
152 else 155 else
153 error("TODO: non-string keys"); 156 local extradata = {};
157 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do
158 extradata = deserialize(row[1], row[2]);
159 break;
160 end
161 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
162 host, username or "", self.store, "");
163 extradata[key] = data;
164 local t, value = assert(serialize(extradata));
165 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, "", t, value);
154 end 166 end
155 return true; 167 return true;
156 end); 168 end);
157 if not ok then return nil, result; end 169 if not ok then return nil, result; end
158 return result; 170 return result;