plugins/mod_storage_sql.lua

changeset 3980
6b2fac6602b3
parent 3978
13ee740b1f89
equal deleted inserted replaced
3979:bf223e6c2b4c 3980:6b2fac6602b3
23 local tostring = tostring; 23 local tostring = tostring;
24 local tonumber = tonumber; 24 local tonumber = tonumber;
25 local pairs = pairs; 25 local pairs = pairs;
26 local next = next; 26 local next = next;
27 local setmetatable = setmetatable; 27 local setmetatable = setmetatable;
28 local json = { stringify = function(s) return require"util.serialization".serialize(s) end, parse = require"util.serialization".deserialize }; 28 local json = require "util.json";
29 29
30 local connection = ...; 30 local connection = ...;
31 local host,user,store = module.host; 31 local host,user,store = module.host;
32 local params = module:get_option("sql"); 32 local params = module:get_option("sql");
33 33
57 local stmt = assert(connection:prepare("CREATE TABLE `Prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);")); 57 local stmt = assert(connection:prepare("CREATE TABLE `Prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"));
58 assert(stmt:execute()); 58 assert(stmt:execute());
59 module:log("debug", "Initialized new SQLite3 database"); 59 module:log("debug", "Initialized new SQLite3 database");
60 end 60 end
61 assert(connection:commit()); 61 assert(connection:commit());
62 --print("===", json.stringify()) 62 --print("===", json.encode())
63 end 63 end
64 end 64 end
65 65
66 local function serialize(value) 66 local function serialize(value)
67 local t = type(value); 67 local t = type(value);
68 if t == "string" or t == "boolean" or t == "number" then 68 if t == "string" or t == "boolean" or t == "number" then
69 return t, tostring(value); 69 return t, tostring(value);
70 elseif t == "table" then 70 elseif t == "table" then
71 local value,err = json.stringify(value); 71 local value,err = json.encode(value);
72 if value then return "json", value; end 72 if value then return "json", value; end
73 return nil, err; 73 return nil, err;
74 end 74 end
75 return nil, "Unhandled value type: "..t; 75 return nil, "Unhandled value type: "..t;
76 end 76 end
79 elseif t == "boolean" then 79 elseif t == "boolean" then
80 if value == "true" then return true; 80 if value == "true" then return true;
81 elseif value == "false" then return false; end 81 elseif value == "false" then return false; end
82 elseif t == "number" then return tonumber(value); 82 elseif t == "number" then return tonumber(value);
83 elseif t == "json" then 83 elseif t == "json" then
84 return json.parse(value); 84 return json.decode(value);
85 end 85 end
86 end 86 end
87 87
88 local function getsql(sql, ...) 88 local function getsql(sql, ...)
89 if params.driver == "PostgreSQL" then 89 if params.driver == "PostgreSQL" then

mercurial