Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 7153:89fa66d4e502
mod_storage_sql: Implement map:set_keys, allowing multiple keys to be set in the same transaction
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 09 Feb 2016 16:56:27 +0100 |
parent | 7149:bb0fd02ae70f |
child | 7156:18b7ffd08d56 |
comparison
equal
deleted
inserted
replaced
7152:ca64255bf7cd | 7153:89fa66d4e502 |
---|---|
125 | 125 |
126 --- Archive store API | 126 --- Archive store API |
127 | 127 |
128 local map_store = {}; | 128 local map_store = {}; |
129 map_store.__index = map_store; | 129 map_store.__index = map_store; |
130 map_store.remove = {}; | |
130 function map_store:get(username, key) | 131 function map_store:get(username, key) |
131 local ok, result = engine:transaction(function() | 132 local ok, result = engine:transaction(function() |
132 if type(key) == "string" and key ~= "" then | 133 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 | 134 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]); | 135 return deserialize(row[1], row[2]); |
142 end); | 143 end); |
143 if not ok then return nil, result; end | 144 if not ok then return nil, result; end |
144 return result; | 145 return result; |
145 end | 146 end |
146 function map_store:set(username, key, data) | 147 function map_store:set(username, key, data) |
148 return self:set_keys(username, { [key] = data or self.remove }); | |
149 end | |
150 function map_store:set_keys(username, keydatas) | |
147 local ok, result = engine:transaction(function() | 151 local ok, result = engine:transaction(function() |
148 if type(key) == "string" and key ~= "" then | 152 for key, data in pairs(keydatas) do |
149 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", | 153 if type(key) == "string" and key ~= "" then |
150 host, username or "", self.store, key); | 154 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", |
151 if data ~= nil then | 155 host, username or "", self.store, key); |
152 local t, value = assert(serialize(data)); | 156 if data ~= self.remove then |
153 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value); | 157 local t, value = assert(serialize(data)); |
154 end | 158 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value); |
155 else | 159 end |
156 local extradata = {}; | 160 else |
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 | 161 local extradata = {}; |
158 extradata = deserialize(row[1], row[2]); | 162 for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, "") do |
159 break; | 163 extradata = deserialize(row[1], row[2]); |
160 end | 164 break; |
161 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", | 165 end |
162 host, username or "", self.store, ""); | 166 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", |
163 extradata[key] = data; | 167 host, username or "", self.store, ""); |
164 local t, value = assert(serialize(extradata)); | 168 extradata[key] = data; |
165 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, "", t, value); | 169 local t, value = assert(serialize(extradata)); |
170 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, "", t, value); | |
171 end | |
166 end | 172 end |
167 return true; | 173 return true; |
168 end); | 174 end); |
169 if not ok then return nil, result; end | 175 if not ok then return nil, result; end |
170 return result; | 176 return result; |