2 local json = require "util.json";
3 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
5 local mod_sql = module:require("sql");
6 local params = module:get_option("sql");
8 local engine; -- TODO create engine
10 local function create_table()
11 --[[local Table,Column,Index = mod_sql.Table,mod_sql.Column,mod_sql.Index;
12 local ProsodyTable = Table {
14 Column { name="host", type="TEXT", nullable=false };
15 Column { name="user", type="TEXT", nullable=false };
16 Column { name="store", type="TEXT", nullable=false };
17 Column { name="key", type="TEXT", nullable=false };
18 Column { name="type", type="TEXT", nullable=false };
19 Column { name="value", type="TEXT", nullable=false };
20 Index { name="prosody_index", "host", "user", "store", "key" };
22 engine:transaction(function()
23 ProsodyTable:create(engine);
25 if not module:get_option("sql_manage_tables", true) then
29 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);";
30 if params.driver == "PostgreSQL" then
31 create_sql = create_sql:gsub("`", "\"");
32 elseif params.driver == "MySQL" then
33 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT")
34 :gsub(";$", " CHARACTER SET 'utf8' COLLATE 'utf8_bin';");
37 local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)";
38 if params.driver == "PostgreSQL" then
39 index_sql = index_sql:gsub("`", "\"");
40 elseif params.driver == "MySQL" then
41 index_sql = index_sql:gsub("`([,)])", "`(20)%1");
44 local success,err = engine:transaction(function()
45 engine:execute(create_sql);
46 engine:execute(index_sql);
48 if not success then -- so we failed to create
49 if params.driver == "MySQL" then
50 success,err = engine:transaction(function()
51 local result = engine:execute("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'");
52 if result:rowcount() > 0 then
53 module:log("info", "Upgrading database schema...");
54 engine:execute("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT");
55 module:log("info", "Database table automatically upgraded");
60 module:log("error", "Failed to check/upgrade database schema (%s), please see "
61 .."http://prosody.im/doc/mysql for help",
62 err or "unknown error");
67 local function set_encoding()
68 if params.driver ~= "SQLite3" then
69 local set_names_query = "SET NAMES 'utf8';";
70 if params.driver == "MySQL" then
71 set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';");
73 local success,err = engine:transaction(function() return engine:execute(set_names_query); end);
75 module:log("error", "Failed to set database connection encoding to UTF8: %s", err);
78 if params.driver == "MySQL" then
79 -- COMPAT w/pre-0.9: Upgrade tables to UTF-8 if not already
80 local check_encoding_query = "SELECT `COLUMN_NAME`,`COLUMN_TYPE` FROM `information_schema`.`columns` WHERE `TABLE_NAME`='prosody' AND ( `CHARACTER_SET_NAME`!='utf8' OR `COLLATION_NAME`!='utf8_bin' );";
81 local success,err = engine:transaction(function()
82 local result = engine:execute(check_encoding_query);
83 local n_bad_columns = result:rowcount();
84 if n_bad_columns > 0 then
85 module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns);
86 local fix_column_query1 = "ALTER TABLE `prosody` CHANGE `%s` `%s` BLOB;";
87 local fix_column_query2 = "ALTER TABLE `prosody` CHANGE `%s` `%s` %s CHARACTER SET 'utf8' COLLATE 'utf8_bin';";
88 for row in result:rows() do
89 local column_name, column_type = unpack(row);
90 engine:execute(fix_column_query1:format(column_name, column_name));
91 engine:execute(fix_column_query2:format(column_name, column_name, column_type));
93 module:log("info", "Database encoding upgrade complete!");
96 local success,err = engine:transaction(function() return engine:execute(check_encoding_query); end);
98 module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error");
104 do -- process options to get a db connection
105 params = params or { driver = "SQLite3" };
107 if params.driver == "SQLite3" then
108 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
111 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
113 --local dburi = db2uri(params);
114 engine = mod_sql:create_engine(params);
119 -- Automatically create table, ignore failure (table probably already exists)
123 local function serialize(value)
124 local t = type(value);
125 if t == "string" or t == "boolean" or t == "number" then
126 return t, tostring(value);
127 elseif t == "table" then
128 local value,err = json.encode(value);
129 if value then return "json", value; end
132 return nil, "Unhandled value type: "..t;
134 local function deserialize(t, value)
135 if t == "string" then return value;
136 elseif t == "boolean" then
137 if value == "true" then return true;
138 elseif value == "false" then return false; end
139 elseif t == "number" then return tonumber(value);
140 elseif t == "json" then
141 return json.decode(value);
145 local host = module.host;
148 local function keyval_store_get()
151 for row in engine:select("SELECT `key`,`type`,`value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?", host, user or "", store) do
154 local v = deserialize(row[2], row[3]);
156 if k ~= "" then result[k] = v; elseif type(v) == "table" then
157 for a,b in pairs(v) do
167 local function keyval_store_set(data)
168 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?", host, user or "", store);
170 if data and next(data) ~= nil then
171 local extradata = {};
172 for key, value in pairs(data) do
173 if type(key) == "string" and key ~= "" then
174 local t, value = serialize(value);
176 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, key, t, value);
178 extradata[key] = value;
181 if next(extradata) ~= nil then
182 local t, extradata = serialize(extradata);
183 assert(t, extradata);
184 engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, "", t, extradata);
190 local keyval_store = {};
191 keyval_store.__index = keyval_store;
192 function keyval_store:get(username)
193 user,store = username,self.store;
194 return select(2, engine:transaction(keyval_store_get));
196 function keyval_store:set(username, data)
197 user,store = username,self.store;
198 return engine:transaction(function()
199 return keyval_store_set(data);
202 function keyval_store:users()
203 return engine:transaction(function()
204 return engine:select("SELECT DISTINCT `user` FROM `prosody` WHERE `host`=? AND `store`=?", host, self.store);
210 function driver:open(store, typ)
211 if not typ then -- default key-value store
212 return setmetatable({ store = store }, keyval_store);
214 return nil, "unsupported-store";
217 function driver:stores(username)
218 local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" ..
219 (username == true and "!=?" or "=?");
220 if username == true or not username then
223 return engine:transaction(function()
224 return engine:select(sql, host, username);
228 function driver:purge(username)
229 return engine:transaction(function()
230 local stmt,err = engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username);
235 module:provides("storage", driver);