Comparison

plugins/mod_storage_sql2.lua @ 5735:24f4e73645fe

mod_storage_sql2: Support XML serialization for stanzas.
author Kim Alvefur <zash@zash.se>
date Wed, 10 Jul 2013 13:19:33 +0200
parent 5734:49f1fed6e25e
child 5740:6fce00f61acf
comparison
equal deleted inserted replaced
5734:49f1fed6e25e 5735:24f4e73645fe
1 1
2 local json = require "util.json"; 2 local json = require "util.json";
3 local xml_parse = require "util.xml".parse;
3 local resolve_relative_path = require "core.configmanager".resolve_relative_path; 4 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
5
6 local stanza_mt = require"util.stanza".stanza_mt;
7 local getmetatable = getmetatable;
8 local function is_stanza(x) return getmetatable(x) == stanza_mt; end
4 9
5 local unpack = unpack 10 local unpack = unpack
6 local function iterator(result) 11 local function iterator(result)
7 return function(result) 12 return function(result)
8 local row = result(); 13 local row = result();
132 137
133 local function serialize(value) 138 local function serialize(value)
134 local t = type(value); 139 local t = type(value);
135 if t == "string" or t == "boolean" or t == "number" then 140 if t == "string" or t == "boolean" or t == "number" then
136 return t, tostring(value); 141 return t, tostring(value);
142 elseif is_stanza(value) then
143 return "xml", tostring(value);
137 elseif t == "table" then 144 elseif t == "table" then
138 local value,err = json.encode(value); 145 local value,err = json.encode(value);
139 if value then return "json", value; end 146 if value then return "json", value; end
140 return nil, err; 147 return nil, err;
141 end 148 end
147 if value == "true" then return true; 154 if value == "true" then return true;
148 elseif value == "false" then return false; end 155 elseif value == "false" then return false; end
149 elseif t == "number" then return tonumber(value); 156 elseif t == "number" then return tonumber(value);
150 elseif t == "json" then 157 elseif t == "json" then
151 return json.decode(value); 158 return json.decode(value);
159 elseif t == "xml" then
160 return xml_parse(value);
152 end 161 end
153 end 162 end
154 163
155 local host = module.host; 164 local host = module.host;
156 local user, store; 165 local user, store;