3 local have_DBI, DBI = pcall(require,"DBI");
8 local t_sort = table.sort;
9 local json = require "util.json";
10 local mtools = require "migrator.mtools";
11 local tostring = tostring;
12 local tonumber = tonumber;
15 error("LuaDBI (required for SQL support) was not found, please see https://prosody.im/doc/depends#luadbi", 0);
20 local function create_table(connection, params)
21 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);";
22 if params.driver == "PostgreSQL" then
23 create_sql = create_sql:gsub("`", "\"");
24 elseif params.driver == "MySQL" then
25 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT");
28 local stmt = connection:prepare(create_sql);
30 local ok = stmt:execute();
31 local commit_ok = connection:commit();
32 if ok and commit_ok then
33 local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)";
34 if params.driver == "PostgreSQL" then
35 index_sql = index_sql:gsub("`", "\"");
36 elseif params.driver == "MySQL" then
37 index_sql = index_sql:gsub("`([,)])", "`(20)%1");
39 local stmt, err = connection:prepare(index_sql);
40 local ok, commit_ok, commit_err;
42 ok, err = assert(stmt:execute());
43 commit_ok, commit_err = assert(connection:commit());
45 elseif params.driver == "MySQL" then -- COMPAT: Upgrade tables from 0.8.0
46 -- Failed to create, but check existing MySQL table here
47 local stmt = connection:prepare("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'");
48 local ok = stmt:execute();
49 local commit_ok = connection:commit();
50 if ok and commit_ok then
51 if stmt:rowcount() > 0 then
52 local stmt = connection:prepare("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT");
53 local ok = stmt:execute();
54 local commit_ok = connection:commit();
55 if ok and commit_ok then
56 print("Database table automatically upgraded");
59 repeat until not stmt:fetch();
65 local function serialize(value)
66 local t = type(value);
67 if t == "string" or t == "boolean" or t == "number" then
68 return t, tostring(value);
69 elseif t == "table" then
70 local value,err = json.encode(value);
71 if value then return "json", value; end
74 return nil, "Unhandled value type: "..t;
76 local function deserialize(t, value)
77 if t == "string" then return value;
78 elseif t == "boolean" then
79 if value == "true" then return true;
80 elseif value == "false" then return false; end
81 elseif t == "number" then return tonumber(value);
82 elseif t == "json" then
83 return json.decode(value);
87 local function decode_user(item)
89 user = item[1][1].user;
90 host = item[1][1].host;
93 for i=1,#item do -- loop over stores
95 local store = item[i];
96 for i=1,#store do -- loop over store data
99 local v = deserialize(row.type, row.value);
101 if k ~= "" then result[k] = v; elseif type(v) == "table" then
102 for a,b in pairs(v) do
107 userdata.stores[store[1].store] = result;
113 function reader(input)
114 local dbh = assert(DBI.Connect(
115 assert(input.driver, "no input.driver specified"),
116 assert(input.database, "no input.database specified"),
117 input.username, input.password,
118 input.host, input.port
121 local stmt = assert(dbh:prepare("SELECT * FROM prosody"));
122 assert(stmt:execute());
123 local keys = {"host", "user", "store", "key", "type", "value"};
124 local f,s,val = stmt:rows(true);
125 -- get SQL rows, sorted
126 local iter = mtools.sorted {
127 reader = function() val = f(s, val); return val; end;
130 if not x[keys[i]] then return false; end -- TODO log error, missing field
132 if x.host == "" then x.host = nil; end
133 if x.user == "" then x.user = nil; end
134 if x.store == "" then x.store = nil; end
137 sorter = function(a, b)
138 local a_host, a_user, a_store = a.host or "", a.user or "", a.store or "";
139 local b_host, b_user, b_store = b.host or "", b.user or "", b.store or "";
140 return a_host > b_host or (a_host==b_host and a_user > b_user) or (a_host==b_host and a_user==b_user and a_store > b_store);
143 -- merge rows to get stores
144 iter = mtools.merged(iter, function(a, b)
145 return (a.host == b.host and a.user == b.user and a.store == b.store);
147 -- merge stores to get users
148 iter = mtools.merged(iter, function(a, b)
149 return (a[1].host == b[1].host and a[1].user == b[1].user);
153 return x and decode_user(x);
157 function writer(output, iter)
158 local dbh = assert(DBI.Connect(
159 assert(output.driver, "no output.driver specified"),
160 assert(output.database, "no output.database specified"),
161 output.username, output.password,
162 output.host, output.port
165 create_table(dbh, output);
166 local stmt = assert(dbh:prepare("SELECT * FROM prosody"));
167 assert(stmt:execute());
168 local stmt = assert(dbh:prepare("DELETE FROM prosody"));
169 assert(stmt:execute());
170 local insert_sql = "INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)";
171 if output.driver == "PostgreSQL" then
172 insert_sql = insert_sql:gsub("`", "\"");
174 local insert = assert(dbh:prepare(insert_sql));
176 return function(item)
177 if not item then assert(dbh:commit()) return dbh:close(); end -- end of input
178 local host = item.host or "";
179 local user = item.user or "";
180 for store, data in pairs(item.stores) do
182 local extradata = {};
183 for key, value in pairs(data) do
184 if type(key) == "string" and key ~= "" then
185 local t, value = assert(serialize(value));
186 local ok, err = assert(insert:execute(host, user, store, key, t, value));
188 extradata[key] = value;
191 if next(extradata) ~= nil then
192 local t, extradata = assert(serialize(extradata));
193 local ok, err = assert(insert:execute(host, user, store, "", t, extradata));